标签:
题目链接:
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 628 Accepted Submission(s): 160
#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;
}
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5933529.html