bzoj 3809(莫隊+分塊求和)

3809: Gty的二逼妹子序列

Time Limit: 80 Sec  Memory Limit: 28 MB
Submit: 1185  Solved: 330
[Submit][Status][Discuss]

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


解題思路:

首先莫隊,然後剩下就是如何統計顏色了,這裏樹狀數組要超時,可以用分塊(太勝了)


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int n,m,opp,lnow,rnow;
struct ss
 {
    int l,r,a,b,dui;
 }q[1000011];
int pos[100100],sum[100100],shu[100100],s[100100];
int ans[1000010];
 
inline int read()
{
    char y; int x=0,f=1; y=getchar();
    while (y<'0'||y>'9') {if (y=='-') f=-1; y=getchar();}
    while (y>='0' && y<='9') {x=x*10+int(y)-48; y=getchar();}
    return x*f;
}
 
bool cmp(ss x,ss y)
 {
    if ((x.l-1)/opp+1!=(y.l-1)/opp+1)return (x.l-1)/opp+1<(y.l-1)/opp+1;else
     return x.r<y.r;
 }
 
int query(int st,int en)
 {
    int sug=0;
    if (pos[st]==pos[en])
     {
        for (int i=st;i<=en;++i)
         if (shu[i]>=1)++sug;
        return sug;
      }
    for (int i=pos[st]+1;i<=pos[en]-1;++i)
     {
        sug+=sum[i];
      }
    for (int i=st;i<=pos[st]*opp;++i)
     if (shu[i]>=1) ++sug;
    for (int i=(pos[en]-1)*opp+1;i<=en;++i)
     if (shu[i]>=1) ++sug;
    return sug;
 }


void dfs()
 {
 
 }


int main()
{
    n=read(); m=read();
    for (int i=1;i<=n;++i) s[i]=read(),shu[i]=0; opp=sqrt(n);
    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].dui=i;
     }
    sort(q+1,q+m+1,cmp);
    for (int i=1;i<=n;++i)
     {
        pos[i]=(i-1)/opp+1; sum[pos[i]]=0;
     }
    lnow=1; rnow=n;
    for (int i=1;i<=n;++i)
     {
        ++shu[s[i]];
        if (shu[s[i]]==1) ++sum[pos[s[i]]];
     }
    for (int i=1;i<=m;++i)
     {
      if (i==1000000)
      {
      dfs();
 }
        while (lnow<q[i].l)
         {
            --shu[s[lnow]];
            if (shu[s[lnow]]==0) --sum[pos[s[lnow]]];
            lnow++;
         }
        while (lnow>q[i].l)
         {
            ++shu[s[lnow-1]];
            if (shu[s[lnow-1]]==1) ++sum[pos[s[lnow-1]]];
            --lnow;
         }
        while (rnow<q[i].r)
         {
            ++shu[s[rnow+1]];
            if (shu[s[rnow+1]]==1) ++sum[pos[s[rnow+1]]];
            ++rnow;
         }
        while (rnow>q[i].r)
         {
            --shu[s[rnow]];
            if (shu[s[rnow]]==0) --sum[pos[s[rnow]]];
            --rnow;
         }
        ans[q[i].dui]=query(q[i].a,q[i].b);
     }
    for (int i=1;i<=m;++i)
     {
        cout<<ans[i]<<endl;
     }
}

發佈了149 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章