naked with modified Chairman tree: Before using bit set splay ( http://www.cnblogs.com/JSZX11556/p/4625552.html ) a over. But the line tree is good to write ... And fast (the constant is smaller than the balance tree). Spacetime complexity is O (Nlog (n) +mlog^2 (n ))
-------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>#include <cctype>using namespace std; #define H (v) (Lower_bound (hash, hash + Hn, v)-hash + 1)const int MAXN = 10009;int N, Q;int SEQ[MAXN];int HASH[MAXN << 1], Hn;int Val, Pos;inline int Read () {char C = getchar ();For (;!isdigit (c); c = GetChar ());int ret = 0;For (; IsDigit (c); c = GetChar ())ret = ret * + C-' 0 ';return ret;}struct Event {int L, R, V;Event (int _l = 0, int _r = 0, int _v = 0):L (_l), R (_r), V (_v) {}} Q[MAXN];void Init () {Hn = 0;N = Read (), Q = Read ();for (int i = 1; I <= N; i++)hash[hn++] = (Seq[i] = read ());for (int i = 0; i < Q; i++) {char C = getchar ();for (; C! = ' C ' && c! = ' Q '; c = GetChar ());if (c = = ' C ') {int p = read (), V = Read ();Q[i] = Event ( -1, P, v);hash[hn++] = v;} else {int L = Read ()-1, R = Read ();Q[i] = Event (l, R, read ());}}sort (hash, hash + Hn);Hn = unique (hash, hash + Hn)-hash;}struct Node {Node *LC, *RC;int v;} pool[5000000], *pt, *null, *ROOT[MAXN], *V[MAXN];void Init_sgt () {pt = pool;Null = pt++;null->v = 0;NULL->LC = NULL->RC = Null;}node* Modify (node* t, int l, int r) {node* o = pt++;o->v = t->v + Val;if (l! = r) {int m = (L + r) >> 1;if (Pos <= m) {O->LC = Modify (T->LC, L, m);O->RC = t->rc;} else {O->LC = t->lc;O->RC = Modify (T->RC, M + 1, R);}}return o;}void Build () {root[0] = Null;Val = 1;for (int i = 1; I <= N; i++) {Pos = seq[i] = H (Seq[i]);Root[i] = Modify (Root[i-1], 1, Hn);}}Node *l[20], *r[20];int Ln, Rn;void Work () {for (int i = 1; I <= Hn; i++)V[i] = Null;for (int i = 0; i < Q; i++)if (~Q[I].L) {Ln = Rn = 0;for (int p = Q[I].L; p; p-= P & P)l[ln++] = v[p];for (int p = Q[I].R; p; p-= P & P)r[rn++] = v[p];Node *_l = root[q[i].l], *_r = ROOT[Q[I].R];int L = 1, r = Hn;While (L < r) {int cnt = _r->lc->v-_l->lc->v, M = (L + R) >> 1;For (int j = 0; J < Ln; J + +)cnt-= l[j]->lc->v;For (int j = 0; J < Rn; J + +)cnt + = r[j]->lc->v;if (CNT >= q[i].v) {_l = _l->lc;_r = _r->lc;For (int j = 0; J < Ln; J + +)L[j] = l[j]->lc;For (int j = 0; J < Rn; J + +)R[j] = r[j]->lc;r = m;} else {Q[I].V-= cnt;_l = _l->rc;_r = _r->rc;For (int j = 0; J < Ln; J + +)L[j] = l[j]->rc;For (int j = 0; J < Rn; J + +)R[j] = r[j]->rc;L = m + 1;}}printf ("%d\n", Hash[l-1]);} else {Pos = SEQ[Q[I].R], Val =-1;for (int p = Q[I].R; p <= Hn; p + = P & P)V[p] = Modify (V[p], 1, Hn);Pos = SEQ[Q[I].R] = H (Q[I].V), Val = 1;for (int p = Q[I].R; p <= Hn; p + = P & P)V[p] = Modify (V[p], 1, Hn);}}int main () {Init ();Init_sgt ();Build ();Work ();return 0;}
------------------------------------------------------------------------
1901:zju2112 Dynamic Rankings Time limit: ten Sec Memory Limit: MB
Submit: 5988 Solved: 2489
[Submit] [Status] [Discuss] Description
Given a sequence of n A[1],a[2],a[3]......a[n], the program must answer this question: For a given i,j,k, the small number of K in A[i],a[i+1],a[i+2]......a[j] is (1≤k≤j-i+1), and, You can change the value of some a[i], and after the change, the program can continue to answer the above question for the changed a. You need to make a program that reads the sequence a from the input file and then reads in a series of instructions, including the Ask and modify instructions. For each inquiry instruction, you must output the correct answer. The first line has two positive integers n (1≤n≤10000), M (1≤m≤10000). Indicates the length of the sequence and the number of instructions, respectively. The second line has n number, which means a[1],a[2]......a[n], these numbers are smaller than 10^9. The next M-line describes each instruction, and the format of each line is one of the following two formats. Q I j K or C i t Q i j K (I,j,k is a number, 1≤i≤j≤n, 1≤k≤j-i+1) indicates an inquiry instruction asking for a small number of k in the A[i],a[i+1]......a[j]. C i T (1≤i≤n,0≤t≤10^9) means to change a[i] into T.
Input
For each query, you need to output his answer, with each output occupying a separate line.
Outputsample Input5 3
3 2 1) 4 7
Q 1 4 3
C 2 6
Q 2 5 3
Sample Output3
6
HINT
20% of the data, m,n≤100; 40% of the data, m,n≤1000; 100% of the data, m,n≤10000.
Source
Bzoj 1901:zju2112 Dynamic Rankings (tree array set Chairman tree)