hdu4614 (二分线段树)
#include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #include<cmath> #include<string> #include<cstring> #include<set> #include<map> #include<list> #include<queue> #include<vector> #define tree int o,int l,int r #define lson o<<1,l,mid #define rson o<<1|1,mid+1,r #define lo o<<1 #define ro o<<1|1 #define ULL unsigned long long #define LL long long #define UI unsigned int #define inf 0x7fffffff #define eps 1e-7 #define N 50009 using namespace std; int T,n,m,t,maxv,x,y,ql,qr,k,b,a,f; int sum[N<<2]; int setv[N<<2]; void maintain(int o) { sum[o]=sum[o<<1]+sum[o<<1|1]; } void pushdown(tree) { if(setv[o]!=-1) { int mid=(l+r)>>1; setv[o<<1]=setv[o<<1|1]=setv[o]; sum[o<<1]=setv[o]*(mid-l+1); sum[o<<1|1]=setv[o]*(r-mid); setv[o]=-1; } } void update(tree,int val) { if(ql<=l&&qr>=r) { setv[o]=val; sum[o]=val*(r-l+1); } else { pushdown(o,l,r); int mid=(l+r)>>1; if(ql<=mid) update(lson,val); if(qr>mid) update(rson,val); maintain(o); } } int query(tree) { if(setv[o]!=-1) return (min(r,qr)-max(l,ql)+1)*setv[o]; if(ql<=l&&qr>=r) return sum[o]; else { pushdown(o,l,r); int mid=(l+r)>>1; int ans=0; if(ql<=mid) ans+=query(lson); if(qr>mid) ans+=query(rson); // maintain(o); return ans; } } int bsearch(int l,int r,int x) { ql=l; while(l<r) { int mid=(l+r)>>1; qr=mid; int s=query(1,0,n); s=mid-ql+1-s; if(s>x) r=mid-1; else if(s<x) l=mid+1; else r=mid; } return l; } int main() { #ifndef ONLINE_JUDGE freopen("ex.in","r",stdin); #endif scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); n--; memset(sum,0,sizeof(sum)); memset(setv,-1,sizeof(setv)); while(m--) { scanf("%d%d%d",&k,&a,&b); f=b; if(k==1) { ql=a,qr=n; int s=query(1,0,n); if(qr-ql+1-s==0) printf("Can not put any one.\n"); else { s=min(qr-ql+1-s,f); int ll=ql,rr=qr;//ql会不断变化 int xl=bsearch(ll,rr,1); int xr=bsearch(ll,rr,s); ql=xl,qr=xr; printf("%d %d\n",ql,qr); update(1,0,n,1); } } else { ql=a,qr=b; printf("%d\n",query(1,0,n)); update(1,0,n,0); } } puts(""); } return 0; }