hdu-5919 Sequence II(主席树)

题目链接:

Sequence II

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 628    Accepted Submission(s): 160


Problem Description
Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,,an There are m queries.

In the i-th query, you are given two integers li and ri. Consider the subsequence ali,ali+1,ali+2,,ari.

We can denote the positions(the positions according to the original sequence) where an integer appears first in this subsequence as p(i)1,p(i)2,,p(i)ki (in ascending order, i.e.,p(i)1<p(i)2<<p(i)ki).

Note that ki is the number of different integers in this subsequence. You should output p(i)ki2for the i-th query.
 


Input
In the first line of input, there is an integer T (T2) denoting the number of test cases.

Each test case starts with two integers n (n2×105) and m (m2×105). There are n integers in the next line, which indicate the integers in the sequence(i.e., a1,a2,,an,0ai2×105).

There are two integers li and ri in the following m lines.

However, Mr. Frog thought that this problem was too young too simple so he became angry. He modified each query to li,ri(1lin,1rin). As a result, the problem became more exciting.

We can denote the answers as ans1,ans2,,ansm. Note that for each test case ans0=0.

You can get the correct input li,ri from what you read (we denote them as li,ri)by the following formula:
li=min{(li+ansi1) mod n+1,(ri+ansi1) mod n+1}

ri=max{(li+ansi1) mod n+1,(ri+ansi1) mod n+1}
 


Output
You should output one single line for each test case.

For each test case, output one line “Case #x: p1,p2,,pm”, where x is the case number (starting from 1) and p1,p2,,pm is the answer.
 


Sample Input
2 5 2 3 3 1 5 4 2 2 4 4 5 2 2 5 2 1 2 2 3 2 4
 


Sample Output
Case #1: 3 3
Case #2: 3 1
 
题意:
 
求给定一个区间,在对前边一个答案进行运算后得到一个新的区间,然后问这个区间里面有k个不同的数,把它们第一次出现的位置从小到大排序,问第k/2个位置是什么;
 
思路:
 
主席树的题目,主席树可以用来求区间有多少个不同的数,和询问区间的第k大,这个题目两个都考察了;
主席树求区间不同数的数目的时候,从后往前插入,同时在插入前删除之前插入的那个,然后求一个区间的个数就是在左端点的那个树上求询问区间的数的个数;
求完数目后然后再求位置的中位数,就是求区间k大了;也是在左端点那棵树上求了,这就是线段树的操作了,
我的主席树的写法跟网上找到的都不一样;不过是对的就行;
 
AC代码:
 
#pragma comment(linker, "/STACK:102400000,102400000")  
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;
typedef unsigned long long ULL;
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e4+120;
const int maxn=2e5+220;
const double eps=1e-12;

int n,m,root[maxn],a[maxn],pre[maxn],cnt;
struct node
{
    int l,r,sum;
}T[maxn*40];
void update(int l,int r,int &x,int y,int pos,int val)
{
    T[++cnt]=T[y],T[cnt].sum+=val;x=cnt;
    if(l==r)return ;
    int mid=(l+r)>>1;
    if(pos<=mid)update(l,mid,T[x].l,T[y].l,pos,val);
    else update(mid+1,r,T[x].r,T[y].r,pos,val);   
}
int query(int o,int l,int r,int L,int R)
{
    if(L<=l&&R>=r)return T[o].sum;
    int mid=(l+r)>>1;
    int ans=0;
    if(L<=mid)ans+=query(T[o].l,l,mid,L,R);
    if(R>mid)ans+=query(T[o].r,mid+1,r,L,R);
    return ans;
}
int getans(int o,int l,int r,int k)
{
    if(l==r)return l;
    int ans=T[T[o].l].sum;
    int mid=(l+r)>>1;
    if(ans>=k)return getans(T[o].l,l,mid,k);
    else return getans(T[o].r,mid+1,r,k-ans);
}
int main()
{
    int t,Case=0;
    read(t);
    while(t--)
    {
        int u,v,ans=0;
        read(n);read(m);
        for(int i=1;i<=n;i++)read(a[i]);
        mst(pre,-1);
        mst(root,0);
        cnt=0;
        T[n+1].l=T[n+1].r=T[n+1].sum=0;
        for(int i=n;i>=1;i--)
        {
            if(pre[a[i]]==-1)update(1,n,root[i],root[i+1],i,1);
            else 
            {
                int temp;
                update(1,n,temp,root[i+1],pre[a[i]],-1);
                update(1,n,root[i],temp,i,1);
            }
            pre[a[i]]=i;
        } 
        printf("Case #%d:",++Case);
        for(int i=1;i<=m;i++)
        {
            read(u);read(v);
            u=(u+ans)%n+1;
            v=(v+ans)%n+1;
            if(u>v)swap(u,v);
            int k=(query(root[u],1,n,u,v)+1)>>1;
            ans=getans(root[u],1,n,k);
            printf(" %d",ans);
        }
        printf("\n");
    }
    return 0;
}

  

 

 
posted @ 2016-10-06 10:38  LittlePointer  阅读(611)  评论(0编辑  收藏  举报