In fact, this problem has been done before, line tree has not been familiar, so also has not understood
The key point is that the segment tree primitive interval represents the number of each container (size different)
For example, the first is unrelated, so 1 of the containers have N 2 3 4 ... for 0 x
The additional information for each node in the segment tree is the interval and
The main problem is to find the code is the key, such as the left and right sub-tree is Sum 27 25, the 26th largest container must be on the left subtree, continue to recursion, then in the Zuozi to find (26-25) Large container ...
The common point is to change the points change the sum of changes (additional information) The situation is only a large transformation of K
Line tree is still too little to do, the recent need to strengthen specialized training
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<stack>#include<string>#include<queue>#include<vector>#include<algorithm>#include<ctime>using namespacestd;//#define Edsonlin#ifdef Edsonlin#defineDebug (...) fprintf (stderr,__va_args__)#else#defineDebug (...)#endif //EdsonlintypedefLong LongLl;typedefDoubledb;Const intINF =0x3f3f3f;Const intMAXN = 2e5+Ten;Const intMaxnn = 2e6+ -;//const int MAXM = 1E6;//const int MAXM = 3e4+100;Const intMOD =1000000007;ConstDB EPS = 1e-3;#definePB push_backintA[MAXN],P[MAXN];intn,m,k;intReadint () {intX;SCANF ("%d", &x);returnx;}intFind (intx) {return(X==p[x]?X:find (p[x]));}voidUnion (intUintV) {P[u] = find (u);p [v] = find (v);p [p[v]] =p[u];}voidinit () { for(intI=1; i<=n;i++) {A[i]=1; P[i]=i; }}structst{intsum[maxn*3]; intN; voidPopupinto) { intLC = o*2; intrc = o*2|1; Sum[o]= SUM[LC] +SUM[RC]; } voidBuildintOintLcintRC) { if(lc==1) Sum[o] =N; ElseSum[o] =0; if(LC==RC)return; if(lc<RC) { intMC = LC + (RC-LC)/2; Build (o*2, LC,MC); Build (o*2|1, mc+1, RC); } } voidUpdateintOintLcintRcintValintc) { intMC = LC + (RC-LC)/2; if(Lc==rc&&lc==val) {Sum[o] + = C;return;} if(LC==RC)return; if(mc<val) Update (o*2|1, mc+1, rc,val,c);//This step is prone to error changing all and Val information about the node ElseUpdate (o*2, Lc,mc,val,c); Popup (o); //recursion up, change parent node additional information } voidQueryintOintLcintRcintk) { if(lc==RC) {printf ("%d\n", LC); return; } intMC = lc+ (RC-LC)/2; if(k<=sum[o*2|1]) query (o*2|1, mc+1, rc,k); ElseQuery (o*2, lc,mc,k-sum[o*2|1]); } voidinit () {memset (sum,0,sizeof(sum)); This->n =N; };} Solver;intMain () { while(cin>>n>>m) {init (); Solver.init (); Solver.build (1,1, N); for(intI=0; i<m;i++){ intSG; scanf ("%d",&SG); if(!SG) { intu,v; scanf ("%d%d",&u,&v); U=Find (U); V=Find (v); if(U==V)Continue; /*Union (u,v);*/P[v]=u; Solver.update (1,1, n,a[u],-1); Solver.update (1,1, n,a[v],-1); Solver.update (1,1, N,a[u]+a[v],1); A[u]+=A[v]; }Else{scanf ("%d",&k); Solver.query (1,1, n,k); } } } //cout << "Hello world!" << Endl; return 0;}
View Code
POJ2985 and search set + line tree to find the number of k large