BZOJ 3809: Gty的二逼妹子序列 莫队+分块

3809: Gty的二逼妹子序列

题目连接:

http://www.lydsy.com/JudgeOnline/problem.php?id=3809

Description

Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题。
对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数。
为了方便,我们规定妹子们的美丽度全都在[1,n]中。
给定一个长度为n(1<=n<=100000)的正整数序列s(1<=si<=n),对于m(1<=m<=1000000)次询问“l,r,a,b”,每次输出sl...sr中,权值∈[a,b]的权值的种类数。

Input

第一行包括两个整数n,m(1<=n<=100000,1<=m<=1000000),表示数列s中的元素数和询问数。
第二行包括n个整数s1...sn(1<=si<=n)。
接下来m行,每行包括4个整数l,r,a,b(1<=l<=r<=n,1<=a<=b<=n),意义见题目描述。
保证涉及的所有数在C++的int内。
保证输入合法。

Output

对每个询问,单独输出一行,表示sl...sr中权值∈[a,b]的权值的种类数。

Sample Input

10 10

4 4 5 1 4 1 5 1 2 1

5 9 1 2

3 4 7 9

4 4 2 5

2 3 4 7

5 10 4 4

3 9 1 1

1 4 5 9

8 9 3 3

2 2 1 6

8 9 1 4

Sample Output

2

0

0

2

1

1

1

0

1

2

Hint

题意

题解

我感觉树套树是可以做的,但是太烦了,不想去写

还是莫队+分块直接卡过去吧,莫队+分块的写法就比较简单了,全是大暴力……

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int a[maxn],pos[maxn];
int ans,n,m,num;
int Ans[maxn*10],belong[maxn],block,l[maxn],r[maxn],c[maxn],d[maxn];
void build()
{
    block=sqrt(n/2);
    num=n/block;if(n%block)num++;
    for(int i=1;i<=num;i++)
        l[i]=(i-1)*block+1,r[i]=i*block;
    r[num]=n;
    for(int i=1;i<=n;i++)
        belong[i]=(i-1)/block+1;
}
struct query
{
    int l,r,id;
    int a,b;
}Q[maxn*10];
bool cmp(query a,query b)
{
    if(pos[a.l]==pos[b.l])
        return a.r<b.r;
    return pos[a.l]<pos[b.l];
}
void Updata(int x)
{
    c[x]++;
    if(c[x]==1)d[belong[x]]++;
}
void Delete(int x)
{
    c[x]--;
    if(c[x]==0)d[belong[x]]--;
}
int query(int L,int R){
    int tmp = 0;
    for(int i=belong[L]+1;i<=belong[R]-1;i++)
        tmp+=d[i];
    if(belong[L]==belong[R]){
        for(int i=L;i<=R;i++)
            if(c[i])tmp++;
        return tmp;
    }
    else{
        for(int i=L;i<=r[belong[L]];i++)
            if(c[i])tmp++;
        for(int i=l[belong[R]];i<=R;i++)
            if(c[i])tmp++;
    }
    return tmp;
}
int main()
{
    n=read(),m=read();
    int sz =ceil(sqrt(1.0*n));
    for(int i=1;i<=n;i++)
    {
        a[i]=read();
        pos[i]=(i-1)/sz;
    }
    for(int i=1;i<=m;i++)
    {
        Q[i].l=read();
        Q[i].r=read();
        Q[i].a=read();
        Q[i].b=read();
        Q[i].id = i;
    }
    sort(Q+1,Q+1+m,cmp);
    int L=1,R=0;
    build();
    for(int i=1;i<=m;i++)
    {
        int id = Q[i].id;
        while(R<Q[i].r)
        {
            R++;
            Updata(a[R]);
        }
        while(L>Q[i].l)
        {
            L--;
            Updata(a[L]);
        }
        while(R>Q[i].r)
        {
            Delete(a[R]);
            R--;
        }
        while(L<Q[i].l)
        {
            Delete(a[L]);
            L++;
        }
        Ans[id]=query(Q[i].a,Q[i].b);
    }
    for(int i=1;i<=m;i++)
        printf("%d\n",Ans[i]);
}
posted @ 2016-07-08 11:37  qscqesze  阅读(473)  评论(0编辑  收藏  举报