1901:zju2112 Dynamic Rankings time limit:10 Sec Memory limit:128 MB
submit:7292 solved:3038
[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.
SourceSolution
I've never written this question. Just leave a board. The previous AC was Han da A's ...
Code
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < Cmath> #include <vector>using namespace Std;inline int read () {int x=0,f=1; char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') f=-1; Ch=getchar ();} while (ch>= ' 0 ' && ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} #define MAXN 30010 int n,m,a[maxn],ls[maxn],cnt,mp[maxn];struct qnode{int o,x,y,z;} Q[maxn];vector <int> add,sub;namespace prtree{int Sum[maxn*80],lson[maxn*80],rson[maxn*80],root[maxn],sz; inline void Insert (int l,int r,int &x,int y,int pos,int val) {x=++sz; Sum[x]=sum[y]+val;if (l==r) return;lson[x]=lson[ Y]; Rson[x]=rson[y];int mid= (l+r) >>1;if (pos<=mid) Insert (l,mid,lson[x],lson[y],pos,val), else insert (mid+1,r, Rson[x],rson[y],pos,val);} inline int Query (int l,int r,int kth) {if (l==r) return l;int mid= (l+r) >>1,sum=0;for (int i=0; i<add.size (); i++) sum+=sum[lson[add[i]]];for (int i=0; i<sub.size (); i++) sum-=sUm[lson[sub[i]]];if (sum<kth) {for (int i=0; i<add.size (); i++) add[i]=rson[add[i]];for (int i=0; i<sub.size (); i++) Sub[i]=rson[sub[i]];return Query (mid+1,r,kth-sum);} else{for (int i=0; i<add.size (); i++) add[i]=lson[add[i]];for (int i=0; i<sub.size (); i++) Sub[i]=lson[sub[i]]; Return Query (L,MID,KTH);}} inline void Build () {for (int i=1; i<=n; i++) Insert (1,cnt,root[i],root[i-1],a[i],1);}} Using namespace prtree;namespace bit{int tree[maxn];inline int lowbit (int x) {return x&-x;} inline void Modify (int x,int pos,int val) {for (int i=x; i<=n; i+=lowbit (i)) Prtree::insert (1,cnt,tree[i],tree[i],pos, val);} inline void Add (int x) {add.push_back (root[x]), for (int i=x; i; i-=lowbit (i)) Add.push_back (Tree[i]);} inline void Sub (int x) {sub.push_back (root[x]), for (int i=x; i; i-=lowbit (i)) Sub.push_back (Tree[i]);} inline void Clear () {add.clear (); Sub.clear ();}} Using namespace Bit;int Main () {N=read (), M=read (); for (int. i=1; i<=n; i++) Ls[++cnt]=a[i]=read (); for (int i=1; i<=m; I+ +) {char opt[2]; scanf ("%s", opt+1), switch (opt[1]) {case ' Q ': Q[i].o=1,q[i].x=read (), Q[i].y=read (), Q[i].z=read (); Break;case ' C ': Q[i].o=2,q[i].x=read (), Q[i].y=read (), ls[++cnt]=q[i].y; Break;}} Sort (ls+1,ls+cnt+1); Cnt=unique (ls+1,ls+cnt+1)-ls-1;for (int i=1; i<=n; i++) A[i]=lower_bound (ls+1,ls+cnt+1,a[i])-ls; Prtree::build (); for (int i=1, i<=m; i++) {int O=q[i].o,x=q[i].x,y=q[i].y,z=q[i].z;switch (o) {case 1:bit::clear (); Bit::add (y); Bit::sub (x-1);p rintf ("%d\n", Ls[prtree::query (1,cnt,z)]), Break;case 2:y=lower_bound (ls+1,ls+cnt+1,y)-ls; Bit::modify (x,a[x],-1); A[x]=y; Bit::modify (x,a[x],1); Break;}} return 0;} /*5 2 1 4 7Q 1 4 3C 2 6Q 2 5 3*/
"BZOJ-1901" Dynamic rankings with the Chairman tree