$ Luogu $ $ P1972 $ $ [SDOI2009] $ $ HH $ necklace

link

background

\ (CCF \) \ (NOI \) \ (2009 \) , Shandong Province, on behalf of Team Selection \ (Day2 \) \ (T1 \) , \ (Luogu \) \ (P1972 / BZOJ1878 \)

The meaning of problems

Given \ (n-\) a \ (int \) integer in the range, inquiry \ (Q \) The number of kinds of the number of times a given interval.

solution

Gugu Gu

Code

$View$ $Code$
  
  
   
   
//省略头文件
using namespace std;
inline int read()
{
    int ret=0,f=1;
    char ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        ret=(ret<<1)+(ret<<3)+ch-'0';
        ch=getchar();
    }
    return ret*f;
}
int n,q,nxt=1,nl,nr,no,num,a[1000005],bit[1000005],pos[1000005],ans[1000005];
struct queries
{
    int l,r,id;
}qry[1000005];
inline bool cmp(queries x,queries y)
{
    return y.r>x.r;
}
inline void modify(int x,int y)
{
    for(;x<=n;x+=x&-x)
        bit[x]+=y;
}
inline int query(int x)
{
    int ans=0;
    for(;x;x-=x&-x)
        ans+=bit[x];
    return ans;
}
int main()
{
    n=read();
    for(register int i=1;i<=n;i++)
        a[i]=read();
    q=read();
    for(register int i=1;i<=q;i++)
    {
        qry[i].l=read();
        qry[i].r=read();
        qry[i].id=i;
    }
    sort(qry+1,qry+q+1,cmp);
    for(register int i=1;i<=q;i++)
    {
        nl=qry[i].l;
        nr=qry[i].r;
        for(register int j=nxt;j<=nr;j++)
        {
            if(pos[a[j]])
                modify(pos[a[j]],-1);
            modify(j,1);
            pos[a[j]]=j;
        }
        nxt=nr+1;
        ans[qry[i].id]=query(nr)-query(nl-1);
    }
    for(register int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
    return 0;
}

  
  

Guess you like

Origin www.cnblogs.com/Peter0701/p/11442949.html