题目链接:传送门

Description

给你一个序列,支持一下操作:

  1. add(x, y, D): 区间[x, y] + D
  2. reverse(x, y): 区间[x,y]翻转
  3. revolve(x, y, T): 将区间[x,y]的最右一个数提到最左,进行T次
  4. insert(x, P): 在序列第x个数后添加P这个数
  5. delete(x): 删除第x个数
  6. min(x, y): 查询区间[x, y]最小值

Solution

Splay裸题 12456都是常规操作, 第3个操作相当于在T %= (y-x+1)后将区间[y-z+1, y]剪切到区间[x, y-z]的前面,也是一个比较容易的操作 不过这道题又调了我一个下午,最后发现是insert中在while找x的后继时忘记push_down了

Code

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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <limits.h>

using namespace std;

const int Maxn = 1e6 + 10;

int N, M, A[Maxn];
int root;

struct tree
{
int val, minn, rev, add, size, ch[2], fa;
};

namespace Splay
{
tree Tree[Maxn];
int cnt = 0;
#define lson Tree[x].ch[0]
#define rson Tree[x].ch[1]

inline void print (int x)
{
if (lson) print(lson);
cout<<Tree[x].val<<" ";
if (rson) print(rson);
}

inline void newnode (int &x, int val, int fa)
{
x = ++cnt;
Tree[x].minn = Tree[x].val = val;
Tree[x].fa = fa;
Tree[x].size = 1;
Tree[x].rev = Tree[x].add = Tree[x].ch[0] = Tree[x].ch[1] = 0;
}

inline void delnode (int x)
{
Tree[x].val = Tree[x].minn = Tree[x].rev = Tree[x].add = Tree[x].size = 0;
Tree[x].ch[0] = Tree[x].ch[1] = Tree[x].fa = 0;
}

inline void push_up (int x)
{
Tree[x].minn = Tree[x].val;
Tree[x].size = 1;
if (lson) Tree[x].minn = min (Tree[x].minn, Tree[lson].minn), Tree[x].size += Tree[lson].size;
if (rson) Tree[x].minn = min (Tree[x].minn, Tree[rson].minn), Tree[x].size += Tree[rson].size;
}

inline void push_down (int x)
{
if (Tree[x].add)
{
Tree[lson].val += Tree[x].add; Tree[rson].val += Tree[x].add;
Tree[lson].add += Tree[x].add; Tree[rson].add += Tree[x].add;
Tree[lson].minn += Tree[x].add; Tree[rson].minn += Tree[x].add;
Tree[x].add = 0;
}
if (Tree[x].rev)
{
//swap (lson, rson);
if (lson) swap (Tree[lson].ch[0], Tree[lson].ch[1]), Tree[lson].rev ^= 1;
if (rson) swap (Tree[rson].ch[0], Tree[rson].ch[1]), Tree[rson].rev ^= 1;
// Tree[Tree[lson].ch[0]].rev ^= 1; Tree[Tree[lson].ch[1]].rev ^= 1;
// Tree[Tree[rson].ch[0]].rev ^= 1; Tree[Tree[rson].ch[1]].rev ^= 1;
Tree[x].rev = 0;
}
}

inline int judge_dir (int x)
{
return Tree[Tree[x].fa].ch[1] == x;
}

inline void connect (int x, int fa, int dir)
{
Tree[x].fa = fa;
Tree[fa].ch[dir] = x;
}

inline void rotate (int x)
{
int f = Tree[x].fa;
int dirx = judge_dir(x);
int anc = Tree[f].fa;
int dirf = judge_dir(f);
push_down(f), push_down(x);
connect (Tree[x].ch[dirx ^ 1], f, dirx);
connect (f, x, (dirx ^ 1));
connect (x, anc, dirf);
push_up(f), push_up(x);
}

inline void splay (int x, int y)
{
while (Tree[x].fa != y)
{
int f = Tree[x].fa;
int dirx = judge_dir(x), dirf = judge_dir(f);
if (Tree[f].fa == y) rotate(x);
else if (dirx == dirf) rotate(f), rotate(x);
else rotate(x), rotate(x);
}
if (!y) root = x;
}

inline void build (int &x, int l, int r, int fa)
{
if (l > r) return ;
int mid = (l + r) >> 1;
newnode (x, A[mid], fa);
build (lson, l, mid - 1, x);
build (rson, mid + 1, r, x);
push_up(x);
}

inline int kth (int k)
{
int x = root;
while (1)
{
push_down (x);
if (k == Tree[lson].size + 1) return x;
if (k <= Tree[lson].size) x = lson;
else k -= (Tree[lson].size + 1), x = rson;
}
}

inline void add (int x, int y, int z)
{
x = kth(x - 1), y = kth(y + 1);
splay (x, 0); splay (y, x);
x = Tree[y].ch[0];
Tree[x].val += z; Tree[x].minn += z; Tree[x].add += z;
splay (x, 0);
}

inline void reverse (int x, int y)
{
x = kth(x - 1), y = kth(y + 1);
splay (x, 0); splay (y, x);
x = Tree[y].ch[0];
// cout<<x<<endl;
// cout<<lson<<" "<<rson<<endl;
swap(lson, rson);
Tree[x].rev ^= 1;
splay (x, 0);
//Tree[lson].rev ^= 1;
//Tree[rson].rev ^= 1;
}

inline void revolve (int l1, int r1, int l2, int r2)
{
int x = kth(l2 - 1), y = kth(r2 + 1);
splay (x, 0), splay (y, x);
int a = Tree[y].ch[0];
Tree[y].ch[0] = 0;
x = kth(l1 - 1), y = kth(l1);
splay (x, 0), splay (y, x);
connect(a, y, 0);
}

inline void insert (int x, int val)
{
int tmp = x;
x = kth(x);
splay (x, 0);
int now = rson;
while (Tree[now].ch[0]) push_down (now),/*attention!*/ now = Tree[now].ch[0];
push_down(now);//attention!
//the above 3 lines you can also write : int now = kth(tmp + 1);
newnode (x, val, now);
connect (x, now, 0);
splay (x, 0);
}

inline void del (int x)
{
splay (kth(x - 1), 0);
splay (kth(x + 1), root);
x = root;
delnode (Tree[rson].ch[0]);
Tree[rson].ch[0] = 0;
push_up (rson);
push_up (x);
}

inline int query (int x, int y)
{
x = kth(x - 1), y = kth(y + 1);
splay (x, 0), splay (y, x);
//print(root);
//cout<<endl;
return Tree[Tree[y].ch[0]].minn;
}
}

int main()
{
#ifdef hk_cnyali
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif
scanf("%d", &N);
A[1] = A[N + 2] = INT_MAX;
for (int i = 2; i <= N + 1; ++i) scanf("%d", &A[i]);
Splay :: build (root, 1, N + 2, 0);
scanf("%d", &M);
while (M --)
{
char s[10];
scanf("%s", s);
int x, y, z;
if (s[0] == 'A')
{
scanf("%d%d%d", &x, &y, &z);
x++ , y++;
Splay :: add (x, y, z);
}
else if (s[0] == 'R' && s[3] == 'E')
{
scanf("%d%d", &x, &y);
x++, y++;
Splay :: reverse (x, y);
}
else if (s[0] == 'R' && s[3] == 'O')
{
scanf("%d%d%d", &x, &y, &z);
x++, y++;
z %= (y - x + 1);
Splay :: revolve (x, y - z, y - z + 1, y);
}
else if (s[0] == 'I')
{
scanf("%d%d", &x, &y);
x++;
Splay :: insert (x, y);
}
else if (s[0] == 'D')
{
scanf("%d", &x);
x++;
Splay :: del (x);
}
else
{
scanf("%d%d", &x, &y);
x++, y++;
printf("%d\n", Splay :: query (x, y));
}
}
return 0;
}