hdu 5919 Sequence II (主席树,求区间不同数的个数)

题意:
  给定一个序列 nn,有 mm次查询,每次查询一个区间 [l,r][l,r],求区间中每一种数在区间中第一次出现的位置的中位数,强制在线。
题解:
  强制在线
  利用主席树求区间不同数的个数
  这里有个技巧
  倒着插入主席树

  在寻找位置的中位数上就可以一个log解决了

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 1000100
#define MOD 1000000007

const int MAXN = 200100;
const int M = MAXN * 50;
int n,m,tot;
int a[MAXN];
int T[MAXN],lson[M],rson[M],c[M];
int build(int l,int r)
{
    int root = tot++;
    c[root] = 0;
    if(l != r)
    {
        int mid = (l+r)>>1;
        lson[root] = build(l,mid);
        rson[root] = build(mid+1,r);
    }
    return root;
}
int update(int root,int pos,int val)
{
    int newroot = tot++,tmp = newroot;
    c[newroot] = c[root] + val;
    int l = 1,r = n;
    while(l < r)
    {
        int mid = (l+r)>>1;
        if(pos <= mid)
        {
            lson[newroot] = tot++;
            rson[newroot] = rson[root];
            newroot = lson[newroot];
            root = lson[root];
            r = mid;
        }
        else
        {
            rson[newroot] = tot++;
            lson[newroot] = lson[root];
            newroot = rson[newroot];
            root = rson[root];
            l = mid + 1;
        }
        c[newroot] = c[root] + val;
    }
    return tmp;
}
int query(int root,int pos)
{
    int ret = 0;
    int l = 1,r = n;
    while(pos < r)
    {
        int mid = (l+r)>>1;
        if(pos <= mid)
        {
            r = mid;
            root = lson[root];
        }
        else
        {
            ret += c[lson[root]];
            root = rson[root];
            l = mid + 1;
        }
    }
    return ret + c[root];
}
int solve(int x,int l,int r,int k)
{
    if(l == r)
        return l;
    int mid = (l+r)>>1;
    if(c[lson[x]] >= k)
        return solve(lson[x],l,mid,k);
    return solve(rson[x],mid + 1,r,k-c[lson[x]]);
}
int last[MAXN];
int main()
{
    int t,C = 1;
    scanf("%d",&t);
    while(t--)
    {
        tot = 0;
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++)
            scanf("%d",&a[i]);
        memset(c,0,sizeof(c));
        memset(lson,0,sizeof(lson));
        memset(rson,0,sizeof(rson));
        T[n+1] = build(1,n);
        memset(last,-1,sizeof(last));
        for(int i = n; i >= 1; i--)
        {
            if(last[a[i]] == -1)
                T[i] = update(T[i+1],i,1);
            else
            {
                int tmp = update(T[i+1],last[a[i]],-1);
                T[i] = update(tmp,i,1);
            }
            last[a[i]] = i;
        }
        int pre = 0;
        printf("Case #%d:",C++);
        for(int i = 0; i < m; i++)
        {
            int l,r,L,R;
            scanf("%d%d",&L,&R);
            l = min((L+pre)%n+1,(R+pre)%n+1);
            r = max((L+pre)%n+1,(R+pre)%n+1);
            int k = (query(T[l],r) + 1) >> 1;
            pre = solve(T[l],1,n,k);
            printf(" %d",pre);
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(数据结构—主席树)