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
| #include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cctype>
using namespace std;
namespace fast_io {
...//隐去快读模版
}using namespace fast_io;
namespace normal_io{
inline char read(){
return getchar();
}
inline void read(char *c){
scanf("%s",c);
}
inline void read(int &x){
scanf("%d",&x);
}
inline void print(int x){
printf("%d",x);
}
inline void print(char x){
putchar(x);
}
inline void flush(){
return;
}
}using namespace normal_io;
struct treap{
struct node{
int val,p,cnt;
node* son[2];
};
const static int MAXN = 1000000;
int treapcnt;
node pool[MAXN],*null,*root;
treap(){
treapcnt = 0;
newnode(null);
srand(19260817);
null->val = -0x3f3f3f3f;
null->p = 2147483647;
null->cnt = 0;
root = null;
}
void rotate(node *&r,int tmp){
node *t = r->son[1-tmp];
r->son[1-tmp] = t->son[tmp];
t->son[tmp] = r;
r = t;
}
void newnode(node *&r){
r = &pool[treapcnt++];
r->son[0] = r->son[1] = null;
}
void __insert(node *&r,int v){
if(r == null){
newnode(r);
r->val = v;r->p = rand();r->cnt = 1;
}
else{
if(r->val == v)
r->cnt++;
else{
int tmp = v > r->val;
__insert(r->son[tmp],v);
if(r->son[tmp]->p < r->p)
rotate(r,1-tmp);
}
}
}
node *find(node *r,int t){
while(r->son[t]!=null)
r = r->son[t];
return r;
}
void __erase(node *&r,int v){
if(r->val == v){
if(r->cnt > 1)
r->cnt--;
else{
if(r->son[0] == null&&r->son[1] == null){
r = null;return;
}
else{
int tt = r->son[0]->p > r->son[1]->p;
rotate(r,1-tt);
__erase(r,v);
}
}
}
else{
int tmp = v > r->val;
__erase(r->son[tmp],v);
}
}
node *nei(int v,int t){
node* nown = root,*last = null;
while(nown!=null&&nown->val!=v){
//printf("2\n");
int tmp = v > nown->val;
if(tmp!=t) last = nown;
nown = nown->son[tmp];
}
if(nown->son[t]!=null){
last = find(nown->son[t],1-t);
}
return last;
}
bool find(int v){
node *r = root;
while(r!=null&&r->val!=v){
int tmp = v > r->val;
r = r->son[tmp];
}
return r != null;
}
inline void __print(node *r,int depth = 0){
if(r == null) return;
else{
__print(r->son[0],depth+1);
for(int i = 0;i<depth;i++) putchar(' ');
printf("val:%d cnt:%d P:%d son?:%d %d\n",r->val,r->cnt,r->p,r->son[0]!=null,r->son[1]!=null);
__print(r->son[1],depth+1);
}
}
void insert(int v){
__insert(root,v);
}
void erase(int v){
__erase(root,v);
}
void print(){
__print(root);
}
};
//以上treap常规模版
treap a,b;
const int MAXN = 1000000;
int head[MAXN],tail[MAXN],minn,n,m;
//a是所有数,b是所有相邻数差值
//head记录此块最前数,tail记录最后数。
void init(){
minn = 0x3f3f3f3f;
read(n),read(m);
static int tmp[MAXN];
for(int i = 1;i<=n;i++){
int t;read(t);
a.insert(t);
head[i] = tail[i] = tmp[i] = t;
}
sort(tmp+1,tmp+n+1);
for(int i = 2;i<=n;i++){
//更新初始的两个查询答案
b.insert(abs(head[i]-head[i-1]));
minn = min(minn,tmp[i]-tmp[i-1]);
}
}
void solve(){
char op[20];int x,y;
for(int i = 1;i<=m;i++){
read(op);
if(op[4] == 'G'){
print(b.find(b.root,0)->val),print('\n');
//寻找最小相邻元素差值
}
else if(op[4] == 'S'){
print(minn),print('\n');
//寻找排序元素差值
}
else if(op[4] == 'R'){
read(x),read(y);
if(x != n){
b.erase(abs(head[x+1]-tail[x]));
b.insert(abs(head[x+1]-y));
}
b.insert(abs(tail[x]-y));
tail[x] = y;
//更新查询2答案
if(a.find(y)) minn = 0;
else{
int low = a.nei(y,0)->val,up = a.nei(y,1)->val;
minn = min(minn,min(abs(y-low),abs(up-y)));
}
a.insert(y);
//更新查询3答案
}
else if(op[4] =='P'){
a.print();
printf("------------------------\n");
b.print();
//调试用
}
//printf("Finish\n");
}
}
int main(){
init();
solve();
flush();
return 0;
}
|