POJ2104 整体二分
1 #include<cstdio> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<cctype> 7 #include<climits> 8 using namespace std; 9 const int maxn=2e5+5; 10 template<typename T>T read(){ 11 T x=0,f=1;char c=getchar(); 12 for(;!isdigit(c);c=getchar())if(c=='-')f=-1; 13 for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48); 14 return x*f; 15 } 16 struct TAS{ 17 int a[maxn]; 18 void add(int x,int y){for(int i=x;i<maxn;i+=(i&-i))a[i]+=y;} 19 int sum(int x,int y=0){for(int i=x;i>0;i-=(i&-i))y+=a[i];return y;} 20 }T; 21 struct op{ 22 int x,y,k,s,tp,cur; 23 op(){}; 24 op(int tp,int x,int y,int s,int k=0,int cur=0): 25 tp(tp),x(x),y(y),s(s),k(k),cur(cur){}; 26 }q[maxn],q1[maxn],q2[maxn]; 27 int num,tmp[maxn],ans[maxn]; 28 void Bin_Search(int ql,int qr,int l,int r){ 29 if(ql>qr)return; 30 if(l==r){ 31 for(int i=ql;i<=qr;++i) 32 if(q[i].tp)ans[q[i].s]=l; 33 return; 34 } 35 int mid=l+r>>1; 36 for(int i=ql;i<=qr;++i){ 37 if(!q[i].tp&&q[i].y<=mid)T.add(q[i].x,1); 38 else if(q[i].tp)tmp[i]=T.sum(q[i].y)-T.sum(q[i].x-1); 39 } 40 for(int i=ql;i<=qr;++i) 41 if(!q[i].tp&&q[i].y<=mid)T.add(q[i].x,-1); 42 int l1=0,l2=0; 43 for(int i=ql;i<=qr;++i){ 44 if(q[i].tp){ 45 if(q[i].cur+tmp[i]>q[i].k-1)q1[++l1]=q[i]; 46 else{q[i].cur+=tmp[i];q2[++l2]=q[i];} 47 } 48 else{ 49 if(q[i].y<=mid)q1[++l1]=q[i]; 50 else q2[++l2]=q[i]; 51 } 52 } 53 for(int i=1;i<=l1;++i)q[ql+i-1]=q1[i]; 54 for(int i=1;i<=l2;++i)q[ql+l1+i-1]=q2[i]; 55 Bin_Search(ql,ql+l1-1,l,mid); 56 Bin_Search(ql+l1,qr,mid+1,r); 57 } 58 int n,m; 59 int main(){ 60 scanf("%d%d",&n,&m); 61 memset(q,0,sizeof q);num=0; 62 for(int i=1;i<=n;++i)q[++num]=op(0,i,read<int>(),0); 63 for(int i=1;i<=m;++i){ 64 int x=read<int>(),y=read<int>(),z=read<int>(); 65 q[++num]=op(1,x,y,i,z); 66 } 67 Bin_Search(1,num,-1e9,1e9); 68 for(int i=1;i<=m;++i)printf("%d\n",ans[i]); 69 return 0; 70 }
板子题就是好用