the
Given n points, each point is entitled to a value, the operation will be connected to a certain two points of the Unicom block, or to inquire about the K-large point in a unicom block.
Maintain a balanced tree for each point, for unicom operation Heuristic merging, the size of small trees each point of violence apart, inserted into the large size of the tree.
#include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> #define N 200010 US
ing namespace std;
int n,m,x,y,q;
Char op;
struct lef{lef *fa,*ch[2];
int sz,r,w,g;
}p[n],*rt,*u,*v,*city[n];
inline void Updat (Lef *x) {if (!x) return; x->sz=1;
if (x->ch[0]) x->sz+=x->ch[0]->sz;
if (x->ch[1]) x->sz+=x->ch[1]->sz;
} inline void *reborn (Lef *x) {x->fa=x->ch[0]=x->ch[1]=null;
x->sz=1;
} inline void rot (lef *x) {lef *y=x->fa,*z=y->fa; int lor=x==y->ch[1]; if (z) z->ch[z->ch[1]==y]=x;
x->fa=z;
if (y->ch[lor]=x->ch[lor^1]) y->ch[lor]->fa=y; (x->ch[lor^1]=y)->fa=x; Updat (y);
Updat (x);
} inline void InserT (lef *&x,lef *y) {if (!x) {X=y;return;}
x->sz++;
if (x->w>y->w) {InserT (x->ch[0],y);
x->ch[0]->fa=x;
if (x->r>x->ch[0]->r) rot (x->ch[0]);
} else{InserT (x->ch[1],y); X->ch[1]->Fa=x;
if (x->r>x->ch[1]->r) rot (x->ch[1]);
} inline char C () {static Char buf[100000],*p1=buf,*p2=buf;
if (P1==P2) {p2= (p1=buf) +fread (Buf,1,100000,stdin);
if (P1==P2) return EOF;
return *p1++;
} inline void ReaD (int &x) {char ch=c (); x=0; for (; Ch> ' 9 ' | | ch< ' 0 ';
Ch=c ()); for (;
ch>= ' 0 ' &&ch<= ' 9 '; x=x*10+ch-' 0 ', ch=c ());
} inline Lef *getrt (lef *x) {if (!x) return NULL;
while (X->FA) x=x->fa;
return x;
} void Merge (Lef *x,lef *y) {if (x->ch[0)) merge (X->ch[0],y);
if (x->ch[1]) merge (x->ch[1],y);
Reborn (x);
InserT (Y,X);
int check (lef *x,int y) {int k=1;
if (x->ch[0]) k+=x->ch[0]->sz;
if (k==y) return x->g;
if (k<y) return check (X->CH[1],Y-K);
else return check (x->ch[0],y);
int main () {freopen ("2733.in", "R", stdin);
Freopen ("2733.out", "w", stdout);
Read (n); read (m);
for (int i=1;i<=n;i++) {city[i]=&p[i];
ReaD (CITY[I]->W); City[i]->r=rand ();
city[i]->g=i;
city[i]->sz=1;
for (int i=1;i<=m;i++) {read (x); Read (y);
U=GETRT (City[x]);
V=GETRT (City[y]);
if (u==v) continue;
if (U->SZ>V->SZ) swap (U,V);
Merge (U,V);
ReaD (q);
for (int i=1;i<=q;i++) {while ((Op=c ())!= ' Q ' &&op!= ' B ');
if (op== ' B ') {read (x); Read (y);
U=GETRT (City[x]);
V=GETRT (City[y]);
if (u==v) continue;
if (U->SZ>V->SZ) swap (U,V);
Merge (U,V);
else{read (x); Read (y);
U=GETRT (City[x]); if (!u| |
Y>u->sz) puts ("-1");
else printf ("%d\n", check (u,y));
}
}
}