Problem 3. intkth
Input file: intkth.in
Output file: intkth.out
Time limit: 3 seconds
Memory limit: 512 MB
我看好你哟。
给你一个长度为n 的序列,有m 个操作:
• modify u x 将第u 个数修改为x
• query l r k 询问区间[l; r] 中第k 小的数1
Input
第1 行2 个整数:n m,表示序列长度和操作数。
第2 行n 个整数:a1 a2 a3 : : : an,表示给定序列。
接下来m 行,每行表示上面的某个操作。
Output
对于每个询问操作,输出其结果。
Sample
intkth.in
5 5
5 2 1 3 4
query 1 4 3
modify 4 5
query 1 4 3
modify 1 3
query 1 4 3
intkth.out
3
5
3
Note
• 对于30% 的数据,1 nm 103
• 对于100% 的数据,1 nm 105,1 aiux n,1 k r �� l + 1,1 l r n
树状数组套可持久化值域线段树
/*#include<cstdio>
using namespace std;
const int N = 100000 + 5;
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;
}
struct Node{
int cnt;
Node *ls, *rs;
}pool[N*200],*roots[N],*tail = pool, *null, *va[10000], *vb[10000];
int n,q;
int aa[N];
int ca, cb;
void init() {
null = ++tail;
null->ls = null;
null->rs = null;
null->cnt = 0;
}
Node *newnode() {
Node *nd = ++tail;
nd->ls = null;
nd->rs = null;
nd->cnt = 0;
return nd;
}
void modify( int lf, int rg, int pos, int delta ) {
for( int t = 0; t < ca; t++ ) va[t]->cnt += delta;
if( lf == rg ) return ;
int mid = (lf + rg)>>1;
if( pos <= mid ) {
for( int t = 0; t < ca; t++ ) {
if( va[t]->ls == null ) va[t]->ls = newnode();
va[t] = va[t]->ls;
}
modify( lf, mid, pos, delta );
} else {
for( int t = 0; t < ca; t++ ) {
if( va[t]->rs == null ) va[t]->rs = newnode();
va[t] = va[t]->rs;
}
modify( mid+1, rg, pos, delta );
}
}
int query_seg( int lf, int rg, int k ) {
if( lf == rg ) return lf;
int mid = ( lf + rg ) >> 1;
int lz = 0;
for( int t = 0; t < ca; t++ ) lz += va[t]->ls->cnt;
for( int t = 0; t < cb; t++ ) lz -= vb[t]->ls->cnt;
if( k <= lz ) {
for( int t = 0; t < ca; t++ ) va[t] = va[t]->ls;
for( int t = 0; t < cb; t++ ) vb[t] = vb[t]->ls;
return query_seg( lf, mid, k );
} else {
for( int t = 0; t < ca; t++ ) va[t] = va[t]->rs;
for( int t = 0; t < cb; t++ ) vb[t] = vb[t]->rs;
k -= lz;
return query_seg( mid+1, rg, k );
}
}
void modify( int u, int x, int delta ) {
ca = 0;
for( int i = u; i <= n; i += i & -i )
va[ca++] = roots[i];
modify( 1, n, x, delta );
}
void modify( int u, int x ) {
modify( u, aa[u], -1 );
modify( u, x, +1 );
aa[u] = x;
}
int query( int lf, int rg, int k ){
ca = cb = 0;
for( int i = rg; i; i -= i&-i ) va[ca++] = roots[i];
for( int i = lf-1; i; i -= i&-i ) vb[cb++] = roots[i];
return query_seg(1,n,k);
}
int main(){
freopen("intkth.in","r",stdin);
freopen("intkth.out","w",stdout);
n = read(); q = read();
for( int i = 1; i <= n; i++ ) aa[i] = read();
init();
for( int i = 1; i <= n; i++ ) roots[i] = newnode();
for( int i = 1; i <= n; i++ ) modify( i, aa[i], +1 );
while( q-- ){
char s[15];
scanf("%s", s);
if( s[0] == 'q'){
int l, r, k;
l = read();r = read();k = read();
printf("%d\n",query( l, r, k ));
}else{
int u,x;
u = read();x = read();
modify( u, x );
}
}
return 0;
}*/
//又打了一遍
#include<cstdio>
using namespace std;
const int N = 100000 + 5;
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;
}
struct Node{
int cnt;
Node *ls, *rs;
}pool[N*200],*roots[N],*tail = pool, *null, *va[10000], *vb[10000];
int n,q;
int aa[N];
int ca, cb;
void init() {
null = ++tail;
null->ls = null;
null->rs = null;
null->cnt = 0;
}
Node *newnode() {
Node *nd = ++tail;
nd->ls = null;
nd->rs = null;
nd->cnt = 0;
return nd;
}
void modify( int lf, int rg, int pos, int delta ) {
for( int i = 0; i < ca; i++ ) va[i]->cnt += delta;
if( lf == rg ) return ;
int mid = (lf + rg)>>1;
if( pos <= mid ) {
for( int i = 0; i < ca; i++ ) {
if( va[i]->ls == null ) va[i]->ls = newnode();
va[i] = va[i]->ls;
}
modify( lf, mid, pos, delta );
} else {
for( int i = 0; i < ca; i++ ) {
if( va[i]->rs == null ) va[i]->rs = newnode();
va[i] = va[i]->rs;
}
modify( mid+1, rg, pos, delta );
}
}
int query_seg( int lf, int rg, int k ) {
if( lf == rg ) return lf;
int mid = ( lf + rg ) >> 1;
int lz = 0;
for( int i = 0; i < ca; i++ ) lz += va[i]->ls->cnt;
for( int i = 0; i < cb; i++ ) lz -= vb[i]->ls->cnt;
if( k <= lz ){
for( int i = 0; i < ca; i++ ) va[i] = va[i]->ls;
for( int i = 0; i < cb; i++ ) vb[i] = vb[i]->ls;
return query_seg( lf, mid, k );
} else{
for( int i = 0; i < ca; i++ ) va[i] = va[i]->rs;
for( int i = 0; i < cb; i++ ) vb[i] = vb[i]->rs;
k -= lz;
return query_seg( mid+1, rg, k );
}
}
void modify( int u, int x, int delta ) {
ca = 0;
for( int i = u; i <= n; i += i&-i)
va[ca++] = roots[i];
modify( 1, n, x, delta);
}
void modify( int u, int x ) {
modify( u, aa[u], -1 );
modify( u, x, +1 );
aa[u] = x;
}
int query( int lf, int rg, int k ){
ca = cb = 0;
for( int i = rg; i; i -= i&-i ) va[ca++] = roots[i];
for( int i = lf-1; i; i -= i&-i ) vb[cb++] = roots[i];
return query_seg(1,n,k);
}
int main(){
freopen("intkth.in","r",stdin);
freopen("intkth.out","w",stdout);
n = read(); q = read();
for( int i = 1; i <= n; i++ ) aa[i] = read();
init();
for( int i = 1; i <= n; i++ ) roots[i] = newnode();
for( int i = 1; i <= n; i++ ) modify( i, aa[i], +1 );
while( q-- ){
char s[15];
scanf("%s", s);
if( s[0] == 'q'){
int l, r, k;
l = read();r = read();k = read();
printf("%d\n",query( l, r, k ));
}else{
int u,x;
u = read();x = read();
modify( u, x );
}
}
return 0;
}