【线段树】HDU 5818 Joint Stacks
2021-09-07 17:05:00 # ACM

题链

题目解析

由于合并的方式是按照 $push$ 的顺序构成的,所以用线段树维护一个序列,每次 $push$ 操作新增一个值并标记这个值是 $A$ 还是 $B$ 栈的,$pop$ 操作就在序列上找到对应栈的最新的一个值并删除,可以通过二分解决,$merge$ 操作就是将现有的所有值都标记为 $A$ 或者 $B$ 栈的,是区间覆盖操作;

线段树维护的节点信息有:{
  区间是 $A$ 栈和 $B$ 栈的值的个数;
  叶节点的值;
  懒惰标记;
}

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define LL long long
#define ULL unsigned long long
#define Pair pair<LL ,LL >
#define ls rt<<1
#define rs rt<<1|1
#define PI acos(-1.0)
#define eps 1e-8
#define mod 998244353
#define MAXN 9e18
#define MS 1000005

// f[n] = 276601605(691504013^n - 308495997^n) (mod 1e9+9)

LL n,m,k;
LL ca;
struct node{
LL cnt[2];
LL val;
LL la;
}p[MS<<2];
LL tot;

void build(int l,int r,int rt){
p[rt].cnt[0] = p[rt].cnt[1] = 0;
p[rt].val = 0; p[rt].la = -1;
if(l == r) return;
int m = l+r>>1;
build(l,m,ls); build(m+1,r,rs);
}

void push_up(int rt){
for(int i=0;i<2;i++)
p[rt].cnt[i] = p[ls].cnt[i] + p[rs].cnt[i];
}

void push_down(int rt){
if(p[rt].la != -1){
LL t = p[rt].la;
LL sum;
sum = p[ls].cnt[0] + p[ls].cnt[1];
p[ls].cnt[0] = p[ls].cnt[1] = 0;
p[ls].cnt[t] = sum;

sum = p[rs].cnt[0] + p[rs].cnt[1];
p[rs].cnt[0] = p[rs].cnt[1] = 0;
p[rs].cnt[t] = sum;

p[ls].la = p[rs].la = t;
p[rt].la = -1;
}
}

void modify(int pos,int l,int r,int rt,LL val,int t){
if(l == r){
p[rt].val = val, p[rt].cnt[t] = 1;
return;
}
int m = l+r>>1;
push_down(rt);
if(m >= pos) modify(pos,l,m,ls,val,t);
else modify(pos,m+1,r,rs,val,t);
push_up(rt);
}

LL query(int L,int R,int l,int r,int rt,int t){
if(L <= l && r <= R) return p[rt].cnt[t];
int m = l+r>>1;
push_down(rt);
LL ans = 0;
if(m >= L) ans += query(L,R,l,m,ls,t);
if(m < R) ans += query(L,R,m+1,r,rs,t);
return ans;
}

LL remove(int pos,int l,int r,int rt){
if(l == r){
p[rt].cnt[0] = p[rt].cnt[1] = 0;
return p[rt].val;
}
int m = l+r>>1;
push_down(rt);
LL ans;
if(m >= pos) ans = remove(pos,l,m,ls);
else ans = remove(pos,m+1,r,rs);
push_up(rt);
return ans;
}

void cover(int L,int R,int l,int r,int rt,int t){
if(L <= l && r <= R){
LL sum = p[rt].cnt[0] + p[rt].cnt[1];
p[rt].cnt[0] = p[rt].cnt[1] = 0;
p[rt].cnt[t] = sum;
p[rt].la = t;
return;
}
int m = l+r>>1;
push_down(rt);
if(m >= L) cover(L,R,l,m,ls,t);
if(m < R) cover(L,R,m+1,r,rs,t);
push_up(rt);
}

void solve() {
cout << "Case #" << ++ca << ":\n";
build(1,n,1); tot = 0;
for(int i=1;i<=n;i++){
char op[9], c, d; cin >> op+1 >> c;
int t = c=='A'?0:1;

if(op[2] == 'u'){
LL val; cin >> val;
tot++;
modify(tot,1,n,1,val,t);
}
else if(op[2] == 'o'){
int l = 1, r = tot;
int ans = l;
while(l <= r){
int mid = l+r>>1;
if(query(mid,tot,1,n,1,t) > 0){
ans = mid;
l = mid+1;
}
else r = mid-1;
}
cout << remove(ans,1,n,1) << "\n";
}
else if(op[2] == 'e'){
cin >> d;
if(tot >= 1) cover(1,tot,1,n,1,t);
}
}
}

int main() {
ios::sync_with_stdio(false);
// srand(time(0));
LL ce = 1;
// cin >> ce;
// scanf("%lld",&ce);
while(cin >> n && n) {
solve();
}

return 0;
}
/*


*/
Prev
2021-09-07 17:05:00 # ACM
Next