Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=2871

线段树的各种综合

单点更新,区间合并

 

View Code
  1 #include <cstdio> 
2 using namespace std;
3
4 #define lch (rt<<1)
5 #define rch (rt<<1|1)
6 const int N=50010;
7 int u[N],v[N],se;
8 struct node
9 {
10 int l,r;
11 int len() {return r-l+1;}
12 int s,ls,rs,cnt,c;
13 }st[N*4];
14 int max(int x,int y,int z)
15 {
16 int t=x>y?x:y;
17 return t>z?t:z;
18 }
19 void build(int l,int r,int rt)
20 {
21 st[rt].l=l; st[rt].r=r;
22 st[rt].s=st[rt].ls=st[rt].rs=r-l+1;
23 st[rt].cnt=0; st[rt].c=-1;
24 if(l==r) return;
25 int m=(l+r)/2;
26 build(l,m,lch);
27 build(m+1,r,rch);
28 }
29 void pushdown(int rt)
30 {
31 if(st[rt].c==-1) return;
32 st[lch].c=st[rch].c=st[rt].c;
33 if(st[rt].c==0)
34 {
35 st[lch].s=st[lch].ls=st[lch].rs=st[lch].len();
36 st[rch].s=st[rch].ls=st[rch].rs=st[rch].len();
37 }
38 else st[lch].s=st[lch].ls=st[lch].rs=st[rch].s=st[rch].ls=st[rch].rs=0;
39 st[rt].c=-1;
40 }
41 void pushup(int rt)
42 {
43 st[rt].s=max(st[lch].s,st[rch].s,st[lch].rs+st[rch].ls);
44 st[rt].ls=st[lch].ls;
45 if(st[lch].ls==st[lch].len()) st[rt].ls+=st[rch].ls;
46 st[rt].rs=st[rch].rs;
47 if(st[rch].rs==st[rch].len()) st[rt].rs+=st[lch].rs;
48 }
49 void update(int a,int b,int x,int rt)
50 {
51 int l=st[rt].l, r=st[rt].r;
52 if(a<=l && r<=b)
53 {
54 st[rt].c=x;
55 if(x==0) st[rt].s=st[rt].ls=st[rt].rs=st[rt].len();
56 else st[rt].s=st[rt].ls=st[rt].rs=0;
57 return;
58 }
59 pushdown(rt);
60 int m=(l+r)/2;
61 if(a<=m) update(a,b,x,lch);
62 if(b>m) update(a,b,x,rch);
63 pushup(rt);
64 }
65 void update2(int a,int x,int rt)
66 {
67 int l=st[rt].l, r=st[rt].r;
68 if(l==r)
69 {
70 st[rt].cnt=x;
71 return;
72 }
73 int m=(l+r)/2;
74 if(a<=m) update2(a,x,lch);
75 else update2(a,x,rch);
76 st[rt].cnt=st[lch].cnt+st[rch].cnt;
77 }
78 int query1(int x,int rt)
79 {
80 int l=st[rt].l, r=st[rt].r;
81 if(l==r) return l;
82 pushdown(rt);
83 if(st[lch].s>=x) return query1(x,lch);
84 if(st[lch].rs+st[rch].ls>=x) return st[lch].r-st[lch].rs+1;
85 return query1(x,rch);
86 }
87 int query2(int x,int rt)
88 {
89 int l=st[rt].l, r=st[rt].r;
90 if(l==r) return st[rt].c;
91 pushdown(rt);
92 int m=(l+r)/2;
93 if(x<=m) return query2(x,lch);
94 else return query2(x,rch);
95 }
96 int query3(int x,int rt)
97 {
98 int l=st[rt].l, r=st[rt].r;
99 if(l==r) return l;
100 if(x>st[lch].cnt) return query3(x-st[lch].cnt,rch);
101 else return query3(x,lch);
102 }
103 int main()
104 {
105 int n,m;
106 while(~scanf("%d%d",&n,&m))
107 {
108 se=0;
109 build(1,n,1);
110 st[1].c=0;
111 while(m--)
112 {
113 char op[10];
114 scanf("%s",op);
115 if(op[0]=='R')
116 {
117 printf("Reset Now\n");
118 update(1,n,0,1);
119 for(int i=1;i<=se;i++) update2(u[i],0,1);
120 se=0;
121 }
122 else if(op[0]=='N')
123 {
124 int x;
125 scanf("%d",&x);
126 if(x<=st[1].s)
127 {
128 int k=query1(x,1);
129 printf("New at %d\n",k);
130 se++;
131 u[se]=k; v[se]=k+x-1;
132 update(u[se],v[se],se,1);
133 update2(u[se],1,1);
134 }
135 else printf("Reject New\n");
136 }
137 else if(op[0]=='F')
138 {
139 int x;
140 scanf("%d",&x);
141 int k=query2(x,1);
142 if(k!=0)
143 {
144 printf("Free from %d to %d\n",u[k],v[k]);
145 update(u[k],v[k],0,1);
146 update2(u[k],0,1);
147 }
148 else printf("Reject Free\n");
149 }
150 else if(op[0]=='G')
151 {
152 int x;
153 scanf("%d",&x);
154 if(st[1].cnt>=x)
155 {
156 int k=query3(x,1);
157 printf("Get at %d\n",k);
158 }
159 else printf("Reject Get\n");
160 }
161 }
162 printf("\n");
163 }
164 return 0;
165 }

 

posted on 2012-04-08 09:27  Qiuqiqiu  阅读(188)  评论(0编辑  收藏  举报