1. 程式人生 > >HDU 5919 - Sequence II (2016CCPC長春) 主席樹 (區間第K小+區間不同值個數)

HDU 5919 - Sequence II (2016CCPC長春) 主席樹 (區間第K小+區間不同值個數)

HDU 5919

題意:

  動態處理一個序列的區間問題,對於一個給定序列,每次輸入區間的左端點和右端點,輸出這個區間中:每個數字第一次出現的位子留下, 輸出這些位子中最中間的那個,就是(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; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 256; const double esp = 1e-8; const double PI=acos(-1.0); template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 2e5+9; struct node { int l,r; int sum; } t[maxn*40]; 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) >> 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 l,int r,int x,int L, int R){ if(l >= L && r<=R){ return t[x].sum; } int mid = (l + r) >> 1; int res = 0; if(mid >= L) res += query(l,mid,t[x].l,L,R); if(mid < R) res += query(mid+1,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)>>1; if(cnt >= k) return getk(l,mid,t[x].l,k); else return getk(mid+1, r,t[x].r,k-cnt); } int main(){ int T,n,m; scanf("%d", &T); for(int tt=1; tt<=T; tt++){ tot = 0; scanf("%d%d", &n, &m); for(int i=1; i<=n; i++)scanf("%d", &a[i]); memset(vis, -1, sizeof(vis)); memset(rt, 0, sizeof(rt)); for(int i=n; i>=1; i--){ if(vis[a[i]] == -1){ update(1,n,rt[i],rt[i+1],i,1); } else{ int tmp; update(1,n,tmp,rt[i+1], vis[a[i]], -1); update(1,n,rt[i],tmp,i,1); } vis[a[i]] = i; } printf("Case #%d:", tt); int la = 0; for(int i=1; i<=m; i++){ int l,r,L,R; scanf("%d%d", &L, &R); l = min((L+la)%n + 1, (R+la)%n + 1); r = max((L+la)%n + 1, (R+la)%n + 1); int k = query(1,n,rt[l],l,r); k = (k + 1) / 2; la = getk(1,n,rt[l],k); printf(" %d", la); } puts(""); } return 0; }
HDU - 5919