[bzoj] 1878 HH????????? || ??????

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[bzoj] 1878 HH????????? || ??????相关的知识,希望对你有一定的参考价值。

??????????????????   ?????????   std   poi   line   zoj   href   pre   www   

??????

???????????? n ???????????????,????????? m ?????????,?????????????????? [ l , r ] ??????????????????????????????


?????????
??????\(O(n\log(n))\)???
??????????????????
???????????????????????????????????????????????????????????????????????????sort?????????two-points???????????????????????????

#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 50010
#define M 200010
using namespace std;
int n,m,ans[M],tot,a[N],cnt[1000010],s,l,r;

int bel(int x) { return ((x-1)/s+1); }

struct hhh
{
    int x,y,id;
    bool operator < (const hhh &b) const
    {
        if (bel(x)==bel(b.x)) return y<b.y;
        return bel(x)<bel(b.x);
    }
}q[M];

int read()
{
    int ans=0,fu=1;
    char j=getchar();
    for (;j<'0' || j>'9';j=getchar()) if (j=='-') fu=-1;
    for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
    return ans*fu;
}

int main()
{
    n=read();s=sqrt(n);
    for (int i=1;i<=n;i++)
    a[i]=read();
    m=read();
    for (int i=1;i<=m;i++) q[i].x=read(),q[i].y=read(),q[i].id=i;
    sort(q+1,q+m+1);
    for (int i=1;i<=m;i++)
    {
    while (l<q[i].x) if (!--cnt[a[l++]]) tot--;
    while (l>q[i].x) if (!cnt[a[--l]]++) ++tot;
    while (r<q[i].y) if (!cnt[a[++r]]++) ++tot;
    while (r>q[i].y) if (!--cnt[a[r--]]) tot--;
    ans[q[i].id]=tot;
    }
    for (int i=1;i<=m;i++) printf("%d\n",ans[i]);
    return 0;
}

以上是关于[bzoj] 1878 HH????????? || ??????的主要内容,如果未能解决你的问题,请参考以下文章

[BZOJ1878][SDOI2009]HH的项链

bzoj1878 [SDOI2009]HH的项链

[bzoj] 1878 HH????????? || ??????

BZOJ [1878[SDOI2009]HH的项链

[bzoj1878] [SDOI2009]HH的项链(树状数组+离线)

BZOJ1878: [SDOI2009]HH的项链[树状数组 离线]