QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#55326 | #4861. Lexicographic Comparison | zhoukangyang# | RE | 2ms | 27104kb | C++11 | 4.6kb | 2022-10-13 10:47:00 | 2022-10-13 10:47:01 |
Judging History
answer
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define sz(a) ((int) (a).size())
#define vi vector < int >
#define me(a, x) memset(a, x, sizeof(a))
#define ull unsigned long long
#define ld __float128
using namespace std;
const int N = 5e5 + 7, M = 1e5 + 7;
int ch[N][2], fa[N], siz[N], mn[N], vs[N], tag[N], rev[N];
bool get(int x) {
return ch[fa[x]][0] == x || ch[fa[x]][1] == x;
}
void upd(int x) {
if(x)
siz[x] = 1 + siz[ch[x][0]] + siz[ch[x][1]],
mn[x] = min(x, min(mn[ch[x][0]], mn[ch[x][1]]));
}
void flip(int x) {
if(x) rev[x] ^= 1, swap(ch[x][0], ch[x][1]);
}
void mark(int x, int y) {
if(x) tag[x] = vs[x] = y;
}
void push(int x) {
if(rev[x]) flip(ch[x][0]), flip(ch[x][1]), rev[x] = 0;
if(tag[x]) mark(ch[x][0], tag[x]), mark(ch[x][1], tag[x]), tag[x] = 0;
}
void rotate(int x) {
int y = fa[x], z = fa[y], fson = ch[y][1] == x, ano = ch[x][fson ^ 1];
if(get(y)) ch[z][ch[z][1] == y] = x;
if(ano) fa[ano] = y;
fa[x] = z, fa[y] = x, ch[x][fson ^ 1] = y, ch[y][fson] = ano, upd(y), upd(x);
}
int F[N], ftot, fx;
void splay(int x) {
F[ftot = 1] = fx = x;
while(get(fx)) F[++ftot] = fx = fa[fx];
while(ftot) push(F[ftot--]);
while(get(x)) {
int y = fa[x], z = fa[y];
if(get(y)) rotate(((ch[y][1] == x) ^ (ch[z][1] == y)) ? x : y);
rotate(x);
}
}
void access(int x) {
for(int y = 0; x; y = x, x = fa[x])
splay(x), ch[x][1] = y, upd(x);
}
void makeroot(int x) {
access(x), splay(x), flip(x);
}
void split(int x, int y) {
makeroot(x), access(y), splay(y);
}
int findroot(int x) {
access(x), splay(x);
while(ch[x][0]) push(x), x = ch[x][0];
return splay(x), x;
}
void link(int x, int y) {
makeroot(x);
if(findroot(y) != x) fa[x] = y;
}
void cut(int x, int y) {
makeroot(x);
if(findroot(y) == x && ch[x][1] == y && !ch[y][0])
fa[y] = ch[x][1] = 0, upd(x);
}
int kth(int x, int k) {
while(x) {
push(x);
if(k <= siz[ch[x][0]]) x = ch[x][0];
else {
k -= siz[ch[x][0]] + 1;
if(!k) return x;
x = ch[x][1];
}
}
return -1;
}
int n, m, a[N], p[N], MP[N];
set < int > st[N];
map < int, int > mp;
void broke(int x) { // break x in the p's cyc.
// cout << "x = " << x << ' ' << MP[x] << endl;
split(MP[x], x);
if(siz[x] == 1) return ;
int vp = vs[x];
// cout << "vp = " << vp << endl;
if(vp != x) {
cut(x, p[x]);
link(vp, p[vp]);
access(x), splay(x), mark(x, x);
}
}
void insert(int x, int w) {
// cout << x << " :: " << w << endl;
mp[x] = w;
}
void del(int x) {
assert(mp.count(x));
mp.erase(x);
}
void Main() {
cin >> n >> m, mn[0] = 1e9;
L(i, 1, n) p[i] = i, MP[i] = i, a[i] = i;
mp.clear();
L(i, 1, n)
fa[i] = ch[i][0] = ch[i][1] = tag[i] = vs[i] = rev[i] = 0,
siz[i] = 1, mn[i] = i, mp[i] = 1, insert(i, 1), mark(i, i);
while(m--) {
string op;
ll x, y;
cin >> op >> x >> y;
if(op == "swap_p") {
if(x == y) continue;
if(findroot(x) == findroot(y)) {
broke(x), split(MP[x], x), del(mn[x]);
cut(y, p[y]);
swap(MP[p[x]], MP[p[y]]), swap(p[x], p[y]);
split(MP[x], x), mark(x, x), insert(mn[x], siz[x]);
split(MP[y], y), mark(y, y), insert(mn[y], siz[y]);
} else {
broke(x), broke(y);
split(MP[x], x), del(mn[x]);
split(MP[y], y), del(mn[y]);
link(y, p[x]);
swap(MP[p[x]], MP[p[y]]), swap(p[x], p[y]);
split(MP[x], x), mark(x, x), insert(mn[x], siz[x]);
}
} else if(op == "swap_a") {
swap(a[x], a[y]);
} else {
--x, --y;
// mp.clear();
// L(i, 1, n) {
// if(findroot(i) != findroot(p[i])) {
// link(i, p[i]);
// } else {
// makeroot(p[i]), access(i), splay(i), mark(i, i);
// mp[mn[i]] = siz[i];
// }
// }
ll d = abs(x - y);
int mnp = 0, ds = 0;
for(auto u : mp) {
// cout << "sec = " << u.second << endl;
if(d % u.second) {
mnp = u.first;
ds = u.second;
break ;
}
}
if(!mnp) {
cout << "=\n";
continue;
}
// cout << mnp << " and " << ds << endl;
int dx = x % ds, dy = y % ds;
// cout << "SIZ = " << MP[mnp] << endl;
broke(MP[mnp]), makeroot(mnp), access(MP[mnp]), splay(MP[mnp]);
int px = kth(MP[mnp], dx + 1), py = kth(MP[mnp], dy + 1);
// cout << px << " and " << py << endl;
if(a[px] < a[py]) {
cout << "<\n";
} else {
cout << ">\n";
}
}
}
}
int main() {
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--) Main();
return 0;
}
/*
1 5 5
swap_p 1 2
swap_p 2 3
swap_p 3 4
*/
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 27104kb
input:
2 5 5 cmp 1 2 swap_p 1 2 cmp 1 2 swap_a 1 2 cmp 1 2 1 1 swap_a 1 1
output:
= < >
result:
ok 3 lines
Test #2:
score: -100
Dangerous Syscalls
input:
10000 5 3 swap_p 2 2 cmp 8 9 swap_a 4 2 19 1 swap_p 7 1 6 12 cmp 5 10 swap_p 3 4 cmp 3 4 swap_p 2 2 cmp 9 3 swap_p 5 2 swap_a 3 3 cmp 5 7 swap_a 1 2 cmp 7 2 swap_a 5 4 cmp 9 2 2 8 swap_p 2 1 swap_p 1 1 cmp 4 7 cmp 3 6 swap_p 2 1 swap_p 1 1 swap_a 2 2 cmp 7 1 70 6 cmp 9 5 swap_p 53 63 swap_p 53 31 sw...