原题链接:http://www.tyvj.cn/p/1730
树套树。。。
如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define lc root<<1
#define rc root<<1|1
#define INF 0x3f3f3f3f
#define _max(a,b) ((a)>(b)?(a):(b))
#define _min(a,b) ((a)>(b)?(b):(a))
const int Max_N = ;
struct SBT *null;
struct SBT{
int v, s, c;
SBT *ch[];
SBT(int _v = ) : v(_v){
c = s = ;
ch[] = ch[] = null;
}
inline void push_up(){
s = ch[]->s + ch[]->s + c;
}
inline int cmp(int x) const{
return v == x ? - : x > v;
}
}stack[Max_N << ], *ptr[Max_N << ];
int sz = , sum = , arr[Max_N];
void init(){
null = &stack[sz++];
null->s = null->c = ;
}
inline void rotate(SBT* &x, int d){
SBT *k = x->ch[!d];
x->ch[!d] = k->ch[d];
k->ch[d] = x;
k->s = x->s;;
x->push_up();
x = k;
}
void Maintain(SBT* &x, int d){
if (x->ch[d] == null) return;
if (x->ch[d]->ch[d]->s > x->ch[!d]->s){
rotate(x, !d);
} else if (x->ch[d]->ch[!d]->s > x->ch[d]->s){
rotate(x->ch[d], d), rotate(x, !d);
} else {
return;
}
Maintain(x, ), Maintain(x, );
}
void insert(SBT* &x, int v){
if (x == null){
x = &stack[sz++];
x->v = v;
x->ch[] = x->ch[] = null;
} else {
x->s++;
int d = x->cmp(v);
if (- == d){
x->c++;
return;
}
insert(x->ch[d], v);
x->push_up();
Maintain(x, d);
}
}
void del(SBT* &x, int v){
if (x == null) return;
int d = x->cmp(v);
x->s--;
if (- == d){
if (x->c > ){
x->c--;
} else if (x->ch[] == null || x->ch[] == null){
x = x->ch[] != null ? x->ch[] : x->ch[];
} else {
SBT *ret = x->ch[];
for (; ret->ch[] != null; ret = ret->ch[]);
del(x->ch[], x->v = ret->v);
}
} else {
del(x->ch[d], v);
}
if (x != null) x->push_up();
}
int sbt_rank(SBT *x, int v){
int t = , cur = ;
for (; x != null;){
t = x->ch[]->s;
if (v == x->v) return cur + t;
else if (v < x->v) x = x->ch[];
else cur += t + x->c, x = x->ch[];
}
return cur;
}
int sbt_pred(SBT *x, int v){
int t = -;
for (; x != null;){
if (x->v < v){
t = x->v;
x = x->ch[];
}
else x = x->ch[];
}
return t == - ? -INF : t;
}
int sbt_succ(SBT *x, int v){
int t = -;
for (; x != null;){
if (x->v > v){
t = x->v;
x = x->ch[];
}
else x = x->ch[];
}
return t == - ? INF : t;
}
void seg_built(int root, int l, int r){
ptr[root] = null;
for (int i = l; i <= r; i++) insert(ptr[root], arr[i]);
if (l == r) return;
int mid = (l + r) >> ;
seg_built(lc, l, mid);
seg_built(rc, mid + , r);
}
void seg_rank(int root, int l, int r, int x, int y, int v){
if (x > r || y < l) return;
if (x <= l && y >= r){
sum += sbt_rank(ptr[root], v);
return;
}
int mid = (l + r) >> ;
seg_rank(lc, l, mid, x, y, v);
seg_rank(rc, mid + , r, x, y, v);
}
void seg_modify(int root, int l, int r, int pos, int v){
if (pos > r || pos < l) return;
del(ptr[root], arr[pos]);
insert(ptr[root], v);
if (l == r) return;
int mid = (l + r) >> ;
seg_modify(lc, l, mid, pos, v);
seg_modify(rc, mid + , r, pos, v);
}
int seg_pred(int root, int l, int r, int x, int y, int v){
if (x > r || y < l) return -INF;
if (x <= l && y >= r) return sbt_pred(ptr[root], v);
int mid = (l + r) >> ;
int v1 = seg_pred(lc, l, mid, x, y, v);
int v2 = seg_pred(rc, mid + , r, x, y, v);
return _max(v1, v2);
}
int seg_succ(int root, int l, int r, int x, int y, int v){
if (x > r || y < l) return INF;
if (x <= l && y >= r) return sbt_succ(ptr[root], v);
int mid = (l + r) >> ;
int v1 = seg_succ(lc, l, mid, x, y, v);
int v2 = seg_succ(rc, mid + , r, x, y, v);
return _min(v1, v2);
}
void gogo(int n, int a, int b, int k){
int mid, l = , r = ;
while (l < r){
sum = ;
mid = (l + r) >> ;
seg_rank(, , n, a, b, mid);
if (sum < k) l = mid + ;
else r = mid;
}
printf("%d\n", l - );
}
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
init();
int i, n, m, a, b, c, d;
scanf("%d %d", &n, &m);
for (i = ; i <= n; i++) scanf("%d", &arr[i]);
seg_built(, , n);
while (m--){
scanf("%d", &a);
switch (a){
case :
sum = ;
scanf("%d %d %d", &b, &c, &d);
seg_rank(, , n, b, c, d);
printf("%d\n",sum);
break;
case :
scanf("%d %d %d", &b, &c, &d);
gogo(n, b, c, d);
break;
case :
scanf("%d %d", &b, &c);
seg_modify(, , n, b, c), arr[b] = c;
break;
case :
scanf("%d %d %d", &b, &c, &d);
printf("%d\n", seg_pred(, , n, b, c, d));
break;
case :
scanf("%d %d %d", &b, &c, &d);
printf("%d\n", seg_succ(, , n, b, c, d));
break;
}
}
return ;
}