A - Sequence II

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=5919

Meaning of the questions: give a range, after the operator will get a range. Then ask this interval there are several different numbers, assuming that there are n different numbers,

            This interval is then the number of different sorts, the output of the n / 2 in the first time interval can be a subscript.

Thinking: this is the topic I was chairman of a tree, be sure to go to the Chairman of the tree think, is entitled to the position of the first occurrence, so you can move forward from the achievements.

          Then maintained in accordance with the index for each position, again to avoid duplication of the array can be, and remember to open a large array of points

#include<iostream>
#include<cmath>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
int a[8000010];
int root[2000010],lson[8000010],rson[8000010];
int sum[8000010],top,fa[2000010];
void init()
{
    top = 0;
    memset(sum,0,sizeof(sum));
    memset(root,0,sizeof(root));
    memset(lson,0,sizeof(lson));
    memset(rson,0,sizeof(rson));
    for(int i=0;i<2000010;i++)
        four [c] = - 1 ;
}
void Update(int l,int r,int &now,int last,int pos,int x)
{
    now=++top;
    lson[now]=lson[last];
    rson[now]=rson[last];
    sum[now]=sum[last]+x;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    if(pos<=mid)
        Update(l,mid,lson[now],lson[last],pos,x);
    else
        Update(mid+1,r,rson[now],rson[last],pos,x);
}

int Query(int rt,int L,int R,int l,int r)
{
    if(l<=L&&r>=R)
        return sum[rt];
    int mid=(L+R)>>1;
    int ans=0;
    if(l<=mid)
        ans+=Query(lson[rt],L,mid,l,r);
    if(r>mid)
        years + = Query (rson [rt], mid + 1 , R, l, r);
    return years;
}
int getpos(int rt,int l,int r,int k)
{
    if(l==r)
        return l;
    int ans=sum[lson[rt]];
    int mid=(l+r)>>1;
    if(ans>=k)
        return getpos(lson[rt],l,mid,k);
    else
        return getpos(rson[rt],mid+1,r,k-ans);
}
int main ()
{
    int T,n,m;
    scanf("%d",&T);
    int u=0;
    while(T--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=n;i>=1;i--)
        {
            if(fa[a[i]]==-1)
                Update(1,n,root[i],root[i+1],i,1);
            else
            {
                Update(1,n,root[i],root[i+1],fa[a[i]],-1);
                Update(1,n,root[i],root[i],i,1);
            }
            makes [a [i]] = i;
        }
        int years = 0 ;
        printf("Case #%d:",++u);
        while(m--)
        {
            you have to, to;
            scanf("%d%d",&l,&r);
            l = (l + years)% n + 1 ;
            r = (r + ans) % n + 1;
            if(l > r)
                swap(l,r);
            int k=(Query(root[l],1,n,l,r)+1)>>1;
            printf(" %d",ans=getpos(root[l],1,n,k));
        }
        printf("\n");
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zcb123456789/p/11939436.html