POJ3580 SuperMemo

http://poj.org/problem?id=3580
這題是序列操作題,總共有6種操作:

  • 區間加值
  • 區間反轉
  • 區間旋轉
  • 插入
  • 刪除
  • 查詢區間最小值

看到反轉和旋轉,就不能用Segment Tree解決了,這題我是用Treap來解,不知道還有沒有其他種資料結構可以處理,這題穩穩作就行了,比較要注意的是,翻轉有可能超過長度,所以要取餘數。是題適合練習的Treap的題目。

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include <cstdlib>
using namespace std;
struct Treap{
int val, pri, sz;
int add, mn;
bool rev;
Treap *lc, *rc;
Treap(){}
Treap(int _val)
{
val = mn = _val;
pri = rand();
sz = 1;
add = 0;
rev = false;
lc = rc = NULL;
}
void push();
void pull();
};

void dfs(Treap *&t)
{
cout << t->val;
cout << '(';
if(t->lc)dfs(t->lc);
cout << '|';
if(t->rc)dfs(t->rc);
cout << ')';
}

int getSize(Treap *a){
return (a == NULL ? 0 : a->sz);
}

void Treap::push()
{
if(rev == true)
{
if(lc != NULL)
{
swap(lc->lc, lc->rc);
lc->rev = !lc->rev;
}
if(rc != NULL)
{
swap(rc->lc, rc->rc);
rc->rev = !rc->rev;
}
rev = false;
}
if(add != 0)
{
if(lc != NULL)
{
lc->val += add;
lc->add += add;
lc->mn += add;
}
if(rc != NULL)
{
rc->val += add;
rc->add += add;
rc->mn += add;
}
add = 0;
}
}

void Treap::pull()
{
sz = getSize(lc) + getSize(rc) + 1;
mn = val;
if(lc != NULL)
{
mn = min(mn, lc->mn);
}
if(rc != NULL)
{
mn = min(mn, rc->mn);
}
}

void split(Treap *t, Treap *&a, Treap *&b, int k)
{
if(t == NULL)
{
a = b = NULL;
return;
}
t->push();
if(getSize(t->lc) < k)
{
a = t;
split(t->rc, a->rc, b, k - getSize(t->lc) - 1);
a->pull();
}
else
{
b = t;
split(t->lc, a, b->lc, k);
b->pull();
}
}

Treap* merge(Treap *a, Treap *b)
{
if(!a || !b)
{
return (a ? a : b);
}
if(a->pri > b->pri)
{
a->push();
a->rc = merge(a->rc, b);
a->pull();
return a;
}
else
{
b->push();
b->lc = merge(a, b->lc);
b->pull();
return b;
}
}

void addVal(Treap *&t, int x, int y, int d)
{
Treap *a, *b, *c;
split(t, b, c, y);
split(b, a, b, x - 1);
b->val += d;
b->add += d;
b->mn += d;
t = merge(a, merge(b, c));
}

void revolve(Treap *&t, int x, int y, int T)
{
int len = y - x + 1;
T %= len;
Treap *a, *b, *c, *d, *e;
split(t, b, c, y);
split(b, a, b, x - 1);
split(b, d, e, len - T);
t = merge(a, merge(e, merge(d, c)));
}

void Reverse(Treap *&t, int x, int y)
{
Treap *a, *b, *c;
split(t, b, c, y);
split(b, a, b, x - 1);
b->rev = !b->rev;
swap(b->lc, b->rc);
t = merge(a, merge(b, c));
}

void Insert(Treap *&t, int x, int p)
{
Treap *a, *b;
split(t, a, b, x);
t = merge(a, merge(new Treap(p), b));
}

void Delete(Treap *&t, int x)
{
Treap *a, *b, *c;
split(t, b, c, x);
split(b, a, b, x - 1);
t = merge(a, c);
}

int getMin(Treap *&t, int x, int y)
{
Treap *a, *b, *c;
split(t, b, c, y);
split(b, a, b, x - 1);
int res = b->mn;
t = merge(a, merge(b, c));
return res;
}

int main()
{
cin.tie(NULL); ios_base::sync_with_stdio(false);

int n, q;
string s;
Treap *root = NULL;

cin >> n;
for(int i = 0, val; i != n; ++i)
{
cin >> val;
root = merge(root, new Treap(val));
}

cin >> q;
for(int i = 0, x, y, z; i != q; ++i)
{
// cout << '*' << i << '\n';
// dfs(root); cout << '\n';
cin >> s;
if(s == "ADD")
{
cin >> x >> y >> z;
addVal(root, x, y, z);
}
else if(s == "REVERSE")
{
cin >> x >> y;
Reverse(root, x, y);
}
else if(s == "REVOLVE")
{
cin >> x >> y >> z;
revolve(root, x, y, z);
}
else if(s == "INSERT")
{
cin >> x >> y;
Insert(root, x, y);
}
else if(s == "DELETE")
{
cin >> x;
Delete(root, x);
}
else if(s == "MIN")
{
cin >> x >> y;
cout << getMin(root, x, y) << '\n';
}
}
}

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Recommended Posts