Bzoj1901 Zju2112 Dynamic Rankings

#include<bits/stdc++.h>
const int N = 10050;
const int M = 10050;
const int Node = 8000050;
int tra(int x){
int l = 1, r = cnt;
while (l < r){
int mid = (l + r + 1) >> 1;
if (b[mid] <= x)
l = mid;
else
r = mid - 1;
}
return c[l];
}
void modify(int rt, int l, int r, int x, int f){
tr[rt]+=f;
if (l == r) return;
if (x <= (l+r)/2){
if (!lc[rt]) lc[rt] = ++s;
modify(lc[rt],l,(l+r)/2,x,f);
}else{
if (!rc[rt]) rc[rt] = ++s;
modify(rc[rt],(l+r)/2+1,r,x,f);
}
}
void modify(int pos, int x, int f){
for (int i=pos; i<=n; i+=i&-i)
modify(i,1,c[cnt],x,f);
}
void init(){
std::sort(b+1,b+cnt+1);
c[1] = 1; ref[c[1]] = b[1];
for (int i=2; i<=cnt; i++)
c[i] = c[i - 1] + (b[i] > b[i - 1]),
ref[c[i]] = b[i];
s = n;
for (int i=1; i<=n; i++)
modify(i,tra(a[i]),1);
}
int qry(int x, int y, int k){
int tot = 0;
for (int i=x; i>0; i-=i&-i) rec[++tot][0] = -1, rec[tot][1] = i;//mistake because more '++'
for (int i=y; i>0; i-=i&-i) rec[++tot][0] =  1, rec[tot][1] = i;
int l=1, r=c[cnt];
while (l < r){
int sum = 0;
for (int i=1; i<=tot; i++)
sum += tr[lc[rec[i][1]]] * rec[i][0];
if (k <= sum){
r = (l+r)/2;
for (int i=1; i<=tot; i++)
rec[i][1] = lc[rec[i][1]];
}else{
l = (l+r)/2+1;
k -= sum;
for (int i=1; i<=tot; i++)
rec[i][1] = rc[rec[i][1]];
}
}
return ref[l];
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1; i<=n; i++)
scanf("%d",&a[i]),
b[++cnt] = a[i];
for (int i=1; i<=m; i++){
char ch[10];
scanf("%s",ch + 1);
}
init();
for (int i=1; i<=m; i++)
}