题意:
动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2那个。
思路:
主席树操作,这里的思路是从n到1开始建树。其他就是主席树查询区间第K小,计算区间不同值个数。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
#include <unordered_map>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue
#define max3(a,b,c) max(max(a,b),c) typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = 2e5+;
struct node {
int l,r;
int sum;
} t[maxn*];
int tot;
int vis[maxn],rt[maxn],a[maxn]; void update(int l,int r,int &x, int y,int pos,int val){
tot++; x = tot;
t[x] = t[y];
t[x].sum += val;
if(l == r)return;
int mid = (l + r) >> ;
if(pos <= mid) update(l,mid,t[x].l,t[y].l,pos,val);
else update(mid+,r,t[x].r,t[y].r,pos,val);
} int query(int l,int r,int x,int L, int R){
if(l >= L && r<=R){
return t[x].sum;
}
int mid = (l + r) >> ;
int res = ;
if(mid >= L) res += query(l,mid,t[x].l,L,R);
if(mid < R) res += query(mid+,r,t[x].r,L,R);
return res;
} int getk(int l,int r,int x,int k){
if(l == r)return l;
int cnt = t[t[x].l].sum;
int mid = (l+r)>>;
if(cnt >= k) return getk(l,mid,t[x].l,k);
else return getk(mid+, r,t[x].r,k-cnt);
}
int main(){
int T,n,m; scanf("%d", &T);
for(int tt=; tt<=T; tt++){
tot = ;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++)scanf("%d", &a[i]);
memset(vis, -, sizeof(vis));
memset(rt, , sizeof(rt));
for(int i=n; i>=; i--){
if(vis[a[i]] == -){
update(,n,rt[i],rt[i+],i,);
}
else{
int tmp;
update(,n,tmp,rt[i+], vis[a[i]], -);
update(,n,rt[i],tmp,i,);
}
vis[a[i]] = i;
}
printf("Case #%d:", tt);
int la = ;
for(int i=; i<=m; i++){
int l,r,L,R; scanf("%d%d", &L, &R);
l = min((L+la)%n + , (R+la)%n + );
r = max((L+la)%n + , (R+la)%n + );
int k = query(,n,rt[l],l,r);
k = (k + ) / ;
la = getk(,n,rt[l],k);
printf(" %d", la);
}
puts("");
}
return ;
}
HDU - 5919