1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #define ls(i) T[i].ls 9 #define rs(i) T[i].rs 10 #define w(i) T[i].w 11 #define Find(i) (lower_bound(LX.begin(),LX.begin()+n1,i)-LX.begin())+1 12 13 using namespace std; 14 const int N=60000+10; 15 struct node{ 16 int ls,rs,w; 17 node(){ls=rs=w=0;} 18 }T[2000000]; 19 struct ope{ 20 int i,l,r,k; 21 }op[11000]; 22 vector LX,Q1,Q2; 23 int n,n1,m,cnt; 24 int a[61000],root[61000*2]; 25 inline int lowbit(int x){ 26 return x&-x; 27 } 28 void build(int &i,int l,int r,int x){ 29 T[++cnt]=T[i]; i=cnt; 30 w(i)++; 31 if (l==r) return; 32 int m=(l+r)>>1; 33 if (x<=m) build(ls(i),l,m,x); 34 else build(rs(i),m+1,r,x); 35 } 36 void ins(int &i,int l,int r,int x,int v){ 37 if (i==0){ T[++cnt]=T[i]; i=cnt; } 38 w(i)+=v; 39 if (l==r) return; 40 int m=(l+r)>>1; 41 if (x<=m) ins(ls(i),l,m,x,v); 42 else ins(rs(i),m+1,r,x,v); 43 } 44 void my_ins(int pos,int x,int v){ 45 int t=Find(x); 46 for (int i=pos;i<=n;i+=lowbit(i)){ 47 ins(root[i],1,n1,t,v); 48 } 49 } 50 int Qy(vector Q1,vector Q2,int l,int r,int k){ 51 if (l==r) return l; 52 int c=0; 53 int m=(l+r)>>1; 54 for (int i=0;i =k?ls(Q1[i]):rs(Q1[i])); 57 for (int i=0;i =k?ls(Q2[i]):rs(Q2[i])); 58 59 if (c>=k) return Qy(Q1,Q2,l,m,k); 60 else return Qy(Q1,Q2,m+1,r,k-c); 61 } 62 void query(int l,int r,int k){ 63 Q1.clear();Q2.clear(); 64 Q1.push_back(root[l!=1?l-1+n:0]); 65 Q2.push_back(root[r+n]); 66 for (int i=l-1;i>0;i-=lowbit(i)) Q1.push_back(root[i]); 67 for (int i=r;i>0;i-=lowbit(i)) Q2.push_back(root[i]); 68 69 int t=Qy(Q1,Q2,1,n1,k); 70 printf("%d\n",LX[t-1]); 71 } 72 void work(){ 73 cnt=0; 74 //for (int i=0;i
转自:http://www.cnblogs.com/Rlemon/archive/2013/05/24/3096264.html