http://acm.hdu.edu.cn/showproblem.php?pid=4614
HDU 4614 Vases and Flowers (2013多校第二场线段树)
1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 50010; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 void fre() { 27 freopen("in.txt","r",stdin); 28 } 29 // inline int r() { 30 // int x=0,f=1;char ch=getchar(); 31 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 32 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 33 // } 34 struct Edge { 35 int l,r; 36 int lazy,sum; 37 } e[N<<2]; 38 39 void pushdown(int rt) { 40 e[rt].sum=e[rt<<1].sum+e[rt<<1|1].sum; 41 } 42 void pushup(int rt) { 43 if(e[rt].lazy!=-1) { 44 e[rt<<1].lazy=e[rt<<1|1].lazy=e[rt].lazy; 45 e[rt<<1].sum=(e[rt<<1].r-e[rt<<1].l+1)*e[rt].lazy; 46 e[rt<<1|1].sum=(e[rt<<1|1].r-e[rt<<1|1].l+1)*e[rt].lazy; 47 e[rt].lazy=-1; 48 } 49 } 50 51 void build(int l,int r,int rt) { 52 e[rt].l=l; 53 e[rt].r=r; 54 e[rt].lazy=-1; 55 if(l==r) { 56 e[rt].sum=0; 57 return; 58 } 59 int mid=mi(); 60 build(lson); 61 build(rson); 62 pushdown(rt); 63 } 64 65 int query(int l,int r,int rt) { 66 if(e[rt].l==l&&e[rt].r==r) { 67 return e[rt].sum; 68 } 69 pushup(rt); 70 int mid=(e[rt].l+e[rt].r)>>1; 71 if(r<=mid) return query(l,r,rt<<1); 72 else if(l>mid) return query(l,r,rt<<1|1); 73 else return query(l,mid,rt<<1)+query(mid+1,r,rt<<1|1); 74 } 75 76 void update(int l,int r,int rt,int c){ 77 if(e[rt].l==l&&e[rt].r==r){ 78 e[rt].lazy=c; 79 e[rt].sum=(e[rt].r-e[rt].l+1)*c; 80 return; 81 } 82 pushup(rt); 83 int mid=(e[rt].l+e[rt].r)>>1; 84 if(r<=mid) update(l,r,rt<<1,c); 85 else if(l>mid) update(l,r,rt<<1|1,c); 86 else { 87 update(l,mid,rt<<1,c); 88 update(mid+1,r,rt<<1|1,c); 89 } 90 pushdown(rt); 91 } 92 93 int main() { 94 // fre(); 95 int T; 96 scanf("%d",&T); 97 while(T--) { 98 int n,q; 99 cin>>n>>q; 100 build(1,n,1); 101 while(q--) { 102 int op,a,b; 103 cin>>op>>a>>b; 104 if(op==1) { 105 int L=a+1,R=n; 106 int st,ed; 107 if((n-L+1-query(L,n,1))==0) { 108 printf("Can not put any one. "); 109 continue; 110 } 111 st=inf; 112 while(L<=R) { 113 int mid=(L+R)>>1; 114 if((mid-(a+1)+1-query(a+1,mid,1))>=1) { 115 st=min(st,mid); 116 R=mid-1; 117 } else { 118 L=mid+1; 119 } 120 } 121 int tem=n-st+1-query(st,n,1); 122 if(tem<b) b=tem; 123 ed=inf; 124 L=st,R=n; 125 while(L<=R) { 126 int mid=(L+R)>>1; 127 tem=mid-st+1-query(st,mid,1); 128 if(tem==b) { 129 ed=min(ed,mid); 130 R=mid-1; 131 } else if(tem>b) { 132 R=mid-1; 133 } else { 134 L=mid+1; 135 } 136 } 137 printf("%d %d ",st-1,ed-1); 138 update(st,ed,1,1); 139 } else { 140 printf("%d ",query(a+1,b+1,1)); 141 update(a+1,b+1,1,0); 142 } 143 } 144 cout<<endl; 145 } 146 return 0; 147 }