P2617 Dynamic Rankings
P2617 Dynamic Rankings
分析:
整体二分!
代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 5 inline int read() { 6 int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; 7 for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; 8 } 9 10 const int N = 10010; 11 struct OPT{ 12 int l,r,k,ty,id; 13 }A[N*3],B1[N*3],B2[N*3]; 14 int ans[N],last[N],n,m; 15 16 struct BIT{ 17 int sum[N]; 18 inline void update(int p,int v) { 19 for (; p<=n; p+=p&(-p)) sum[p] += v; 20 } 21 inline int query(int p) { 22 int ans = 0; 23 for (; p; p-=p&(-p)) ans += sum[p]; 24 return ans; 25 } 26 }bit; 27 28 void solve(int Head,int Tail,int L,int R) { 29 if (Head > Tail) return ; 30 if (L == R) { 31 for (int i=Head; i<=Tail; ++i) 32 if (A[i].ty == 2) ans[A[i].id] = L; 33 return; 34 } 35 int M = (L + R) >> 1,p1 = 0,p2 = 0; 36 for (int i=Head; i<=Tail; ++i) { 37 if (A[i].ty <= 1) { 38 if (A[i].k <= M) {B1[++p1] = A[i];bit.update(A[i].l,A[i].ty?-1:1);} 39 else B2[++p2] = A[i]; 40 } 41 else { 42 int tmp = bit.query(A[i].r)-bit.query(A[i].l-1); 43 if (tmp >= A[i].k) B1[++p1] = A[i]; 44 else A[i].k -= tmp,B2[++p2] = A[i]; 45 } 46 } 47 for (int i=Head; i<=Tail; ++i) 48 if (A[i].ty <= 1 && A[i].k <= M) bit.update(A[i].l,A[i].ty?1:-1); 49 for (int i=1; i<=p1; ++i) A[Head+i-1] = B1[i]; 50 for (int i=1; i<=p2; ++i) A[Head+p1+i-1] = B2[i]; 51 solve(Head,Head+p1-1,L,M); 52 solve(Head+p1,Tail,M+1,R); 53 } 54 55 int main() { 56 n = read(),m = read(); 57 int QueIndex = 0,Idx = 0,Mx = 0,Mn = 1e9; 58 for (int i=1; i<=n; ++i) { 59 int x = read(); 60 A[++Idx] = (OPT){i,i,x,0,0}; 61 last[i] = x; 62 Mx = max(Mx,x);Mn = min(Mn,x); 63 } 64 char opt[5]; 65 for (int i=1; i<=m; ++i) { 66 scanf("%s",opt); 67 if (opt[0] == 'Q') { 68 int a = read(),b = read(),c = read(); 69 A[++Idx] = (OPT){a,b,c,2,++QueIndex}; 70 } 71 else { 72 int p = read(),x = read(); 73 A[++Idx] = (OPT){p,p,last[p],1,0}; 74 A[++Idx] = (OPT){p,p,last[p]=x,0,0}; 75 Mx = max(Mx,x);Mn = min(Mn,x); 76 } 77 } 78 solve(1,Idx,Mn,Mx); 79 for (int i=1; i<=QueIndex; ++i) printf("%d\n",ans[i]); 80 return 0; 81 }