CRB and Queries
Time limit:12000/6000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 1602 Accepted Submission (s): 409
Problem Descriptionthere isNBoys in Codeland.
BoyIHave his coding skillAi .
CRB wants to know who has the suitable coding skill.
So you should treat the following and the types of queries.
Query 1:1l v
The coding skill of boyLHave changed tov.
Query 2:2 LRk
This was a report query which asks thek-th Smallest value of coding skill between boy l and boy R(both inclusive) .
Inputthere is multiple test cases.
The first line contains a single integerN.
Next Line containsNSpace separated integersA1 ,A2 , ...,AN , whereAi Denotes initial coding skill of boyI.
Next line contains a single integerQRepresenting the number of queries.
NextQLines contain queries which can be any of the and the both types.
1≤N,Q≤Ten5
1≤Ai ,v≤Ten9
1≤L≤R≤N
1 ≤ k ≤ r – l + 1
Outputfor each query of type 2, output a single integer corresponding to the answer
Sample Input51 2 3 4 532 2 4 21 3 62 2 4 2
Sample Output34
Authorkut (DPRK)
Source2015 multi-university Training Contest 10
The main topic: give you a series of n number, q times asked. Ask 1 to change a location to value B. Ask 2 then ask L-r what is the number of K-large between. Simply put, it is the dynamic interval K problem with the modification.
Problem-solving idea: Maintain CI's position in the number of numbers in the series after the treap, and maintain CI's positional relationship in the sequence by using the line tree. In fact, I think this is the connotation. Then for the query interval l-r the K-large. Then if the number of the treap tree in the left son of the line tree is greater than the number before the R position minus the Treap tree represented by the left son (L-1), then the position can be found in the left son.
#include <stdio.h> #include <string.h> #include <time.h> #include <algorithm>using namespace STD; #define MID (L+r)/2#define lson rt*2,l,mid#define rson rt*2+1,mid+1,rconst int maxn=1e6;struct treap{int rk; int coun; int sz; int V; Treap *ch[2]; Treap () {coun=0; sz=0; RK=-MAXN; v=0; } int cmp (int x) const {if (x==v) {return-1; } return x<v? 0:1; }}; Treap *seg[maxn*8]; Treap *null;int oper[maxn][4];int a[maxn],b[maxn*3];int discr (int l,int r,int key) {while (l<=r) {int md= (L+R) /2; if (Key<b[md]) {r=md-1; }else if (key > B[md]) {l=md+1; }else{return MD; }} return-1;} void Update (TREAP * &o) {O->sz = O->ch[0]->sz + O->coun + o->ch[1]->sz;} void Rotate (TREAP * &o,int d) {treap *k=o->ch[d^1]; o->ch[d^1]= k->ch[d]; k->ch[d]=o; Update (o); Update (k); O=k;} void Insert_tp (TREAP * &o,int x) {if (o==null) {o = new treap (); o->ch[0]=o->ch[1]=null; O->v = x; O->coun = 1; O->rk = rand (); }else{int d=o->cmp (x); INSERT_TP (O->CH[D],X); if (O->ch[d]->rk > O->rk) rotate (o,d^1); } update (o);} void insert_seg (int rt,int l,int r,int pos,int x) {INSERT_TP (seg[rt],x); if (l==r) return; if (pos<=mid) {insert_seg (lson,pos,x); }else{insert_seg (rson,pos,x); }}void free_tp (treap *&o) {if (o->ch[0]==null&&o->ch[1]==null) {free (o); return; } if (O->ch[0]!=null) {FREE_TP (o->ch[0]); } if (O->ch[1]!=null) {FREE_TP (o->ch[1]); } free (o);} void Clean (int rt,int l,int R) {if (l==r) {FREE_TP (seg[rt]); return; } clean (Lson); Clean (Rson); FREE_TP (Seg[rt]);} void Del_tp (TREAP * &o,int x) {int d=o->cmp (x); if (d==-1) {if (O->ch[0]==null&&o->ch[1]==null) {//Treap *pt=o; o = null; Free (PT); } else if (o->ch[0]==null) {treap *pt=o; o=o->ch[1]; Free (PT); }else if (o->ch[1]==null) {treap *pt=o; o= o->ch[0]; Free (PT); }else{int d2= (o->ch[0]->rk >o->ch[1]->rk? 1:0); Rotate (O,D2); DEL_TP (O->CH[D2],X); }}else{DEL_TP (o->ch[d],x); } update (o);} void del_seg (int rt,int l,int r,int pos,int x) {DEL_TP (seg[rt],x); if (l==r) return; if (pos<=mid) {del_seg (lson,pos,x); }else{del_seg (rson,pos,x); }}int Select (treap *o,int x) {if (o==null) {return 0; } if (O->v > x) return Select (O->ch[0],x); return O->ch[0]->sz + O->coun +select (o->ch[1],x);} int query (int rt,int l,int r,int x,int y,int k) {if (l==r) return L; int Ans=select (seg[rt*2],y)-select (seg[rt*2],x); if (ans>=k) return query (LSON,X,Y,K); else return query (Rson,x,y,k-ans);} void init () {null =new treap (); null->ch[0]=null->ch[1]=null; Null->sz = null->coun=0; for (int i=0;i<=maxn*4-1;i++) {seg[i] = null; }}int Main () {//Freopen ("1007.in", "R", stdin);//Freopen ("OUT.txt", "w", stdout); int n,q,typ,x,y,z,nn,mm; while (scanf ("%d", &n)!=eof) {init (); mm=0; for (int i=0;i<n;i++) {scanf ("%d", &a[i]); B[mm++]=a[i]; } scanf ("%d", &q); for (int i=0;i<q;i++) {scanf ("%d", &typ); Oper[i][0]=typ; if (typ==2) {scanf ("%d%d%d", &x,&y,&z); Oper[i][1]=x; Oper[i][2]=y; Oper[i][3]=z; }else{scanf ("%d%d", &x,&z); Oper[i][1]=x; Oper[i][2]=z; B[mm++]=z; }} sort (b,b+mm); Nn=1; for (int i=1;i<mm;i++) {if (b[i]!=b[i-1]) {b[nn++]=b[i]; }} for (int i=0;i<n;i++) {a[i]= discr (0,nn-1,a[i]) +1; Insert_seg (1,1,nn,a[i],i+1); } for (int i=0;i<q;i++) {if (oper[i][0]==1) {int kk=0; Del_seg (1,1,nn,a[oper[i][1]-1],oper[i][1]); KK=DISCR (0,nn-1,oper[i][2]); a[oper[i][1]-1]=kk+1; Insert_seg (1,1,nn,a[oper[i][1]-1],oper[i][1]); }else{int tmp=query (1,1,nn,oper[i][1]-1,oper[i][2],oper[i][3])-1; printf ("%d\n", b[tmp]); }} clean (1,1,NN); } return 0;} /*51 1 1 1 1101 2 32 1 4 31 5 61 4 21 5 32 1 3 32 1 4 42 2 5 22 2 5 32 2 5 4*/
HDU 5412--CRB and Queries —————— "segment tree set Treap (no AC)"