1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #define M 305 using namespace std; int A[M*M],sz[M],K; struct node { int id,num; void set(int _id,int _num) { id=_id,num=_num; } } C[M][M]; bool cmp(node a,node b) { return a.num<b.num; } int Find(int id,int R,int x) { int L=0,res=-1; while(L<=R) { int mid=(L+R)>>1; if(C[id][mid].num<=x) { res=mid; L=mid+1; } else R=mid-1; } return res; } int find_less(int x,int a,int b) { int id_a=a/K,id_b=b/K; int res=0,i; if(id_a==id_b) { for(i=a; i<=b; i++) res+=A[i]<=x; } else { for(i=a; i<id_a*K+sz[id_a]; i++) res+=A[i]<=x; for(i=id_a+1; i<id_b; i++) res+=Find(i,sz[i]-1,x)+1; for(i=id_b*K; i<=b; i++) res+=A[i]<=x; } return res; } int query(int a,int b,int k) { int L=0,R=1000000000; int res=0; while(L<=R) { int mid=(L+R)>>1; if(find_less(mid,a,b)>=k) { res=mid; R=mid-1; } else L=mid+1; } return res; } void update(int a,int x) { int i,id_a=a/K; A[a]=x; for(i=0; i<sz[id_a]; i++) if(C[id_a][i].id==a) { C[id_a][i].num=x; break; } sort(C[id_a],C[id_a]+sz[id_a],cmp); } int main() { int cas,n,m,i,j,k,a,b; scanf("%d",&cas); while(cas--) { scanf("%d %d",&n,&m); K=int(sqrt(n)); memset(sz,0,sizeof(sz)); for(i=0; i<n; i++) { scanf("%d",&A[i]); C[i/K][i%K].set(i,A[i]); sz[i/K]++; } for(i=0; i<=(n-1)/K; i++) { sort(C[i],C[i]+sz[i],cmp); } char op[2]; while(m--&&scanf("%s",op)) { if(op[0]=='Q') { scanf("%d %d %d",&a,&b,&k); printf("%d\n",query(--a,--b,k)); } else { scanf("%d %d",&a,&b); update(--a,b); } } } return 0; }
|