0%

2022牛客多校八 G Lexicographic Comparison

平衡树维护置换环

的数据结构真的锻炼能力,平衡树能力++

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
const int N = 100005;
int n, m, k;

namespace Splay {
int c[N], son[N][2], f[N], sz[N], minid[N];
#define ls son[x][0]
#define rs son[x][1]

inline int cmp(int x, int y)
{
if (!x || !y)return x | y;
else return x < y ? x : y;
}

inline void pushup(int x)
{
sz[x] = sz[ls] + sz[rs] + 1;
minid[x] = cmp(x, cmp(minid[ls], minid[rs]));
}

inline int get(int x) { return x == son[f[x]][1]; }

inline void connect(int x, int y, int d)
{
if (x)son[x][d] = y;
if (y)f[y] = x;
}

inline void rotate(int x)
{
int fa = f[x], ffa = f[fa], p = get(x), q = get(fa);
connect(fa, son[x][p ^ 1], p);
connect(x, fa, p ^ 1);
connect(ffa, x, q);
pushup(fa), pushup(x);
}

void splay(int x, int ed)
{
for (int fa; fa = f[x], fa != ed; rotate(x))
if (f[fa] != ed)rotate(get(fa) == get(x) ? fa : x);
}

inline int get_minid(int x)
{
splay(x, 0);
return minid[x];
}

int getlast(int x)
{
splay(x, 0);
while (son[x][1])
x = son[x][1];
splay(x, 0);
return x;
}

inline int kth(int x, int rk)
{
splay(x, 0);
while (1)
{
if (sz[ls] >= rk)
x = ls;
else
{
rk -= sz[ls] + 1;
if (!rk)return x;
x = rs;
}
}
}

void movefront(int x)
{
splay(x, 0);
if (!ls)return;
int r = getlast(x);
int y = ls;
ls = 0;
pushup(x);
son[r][1] = y;
f[y] = r;
pushup(r);
splay(x, 0);
}

#undef ls
#undef rs
}
using namespace Splay;
int p[N], circle_sz[N];
set<int> S1[N], S2;
int SZ;

void print(int x)
{
if (son[x][0])print(son[x][0]);
printf("%d ", x);
if (son[x][1])print(son[x][1]);
}

inline void add(int x)
{
splay(x, 0);
circle_sz[minid[x]] = sz[x];
if (sz[x] <= SZ)S1[sz[x]].insert(minid[x]);
else S2.insert(minid[x]);
}

inline void del(int x)
{
splay(x, 0);
if (sz[x] <= SZ)S1[sz[x]].erase(minid[x]);
else S2.erase(minid[x]);
}

void merge(int x, int y)
{
if (get_minid(x) > get_minid(y))swap(x, y);
del(x), del(y);
movefront(x), movefront(p[y]);
if (x != p[x])
{
splay(x, 0), splay(p[x], x);
son[p[x]][0] = p[y];
f[p[y]] = p[x];
pushup(p[x]);
splay(p[x], 0);
}
else
{
son[x][1] = p[y];
f[p[y]] = x;
pushup(x);
splay(x, 0);
}
add(x);
}

void divide(int x, int y)
{
del(x);
movefront(x);
if (sz[x] == 2)
{
splay(x, 0);
son[x][1] = 0;
f[y] = 0;
pushup(x);
}
else
{
if (x == p[y])swap(x, y);

assert(x != p[y]);

splay(x, 0), splay(p[y], x);
int z = son[p[y]][0];
son[p[y]][0] = 0;
f[z] = 0;
pushup(p[y]);
splay(p[y], 0);

}
add(x), add(y);
}


int main()
{
int q, u, v, w, x, y, z, T;
cin >> T;
while (T--)
{
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
son[i][0] = son[i][1] = 0, S1[i].clear();
S2.clear();
fill(f, f + n + 1, 0);
fill(sz + 1, sz + n + 1, 1);
iota(minid + 1, minid + n + 1, 1);
iota(c + 1, c + n + 1, 1);
iota(p + 1, p + n + 1, 1);
SZ = sqrt(n);
char s[10];
while (q--)
{
ll x, y;
scanf("%s%lld%lld", s, &x, &y);
if (s[0] == 's' && s[5] == 'a')
swap(c[x], c[y]);
else if (s[0] == 's' && s[5] == 'p')
{
if (x == y)continue;
if (get_minid(x) != get_minid(y))
merge(x, y);
else
divide(x, y);
swap(p[x], p[y]);
}
else
{
ll dif = abs(x - y);
int id = n + 1;
for (int i = 1; i <= SZ; i++)
if (!S1[i].empty() && dif % i)
id = min(id, *S1[i].begin());
for (auto x:S2)
if (dif % circle_sz[x])
id = min(id, x);
if (id == n + 1)puts("=");
else
{
x = (x - 1) % circle_sz[id] + 1;
y = (y - 1) % circle_sz[id] + 1;
movefront(id);
x = kth(id, x);
y = kth(id, y);
if (c[x] < c[y])puts("<");
else puts(">");
}
}
}
}
return 0;
}