QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#55330 | #4861. Lexicographic Comparison | zhoukangyang# | TL | 910ms | 36132kb | C++11 | 5.1kb | 2022-10-13 11:27:04 | 2022-10-13 11:27:05 |
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) {
// cout << "link" << x << ' ' << y << endl;
makeroot(x);
if(findroot(y) != x) fa[x] = y;
else cout << "/jy" << endl;
}
void cut(int x, int y) {
// cout << "del " << x << ' ' << y << endl;
makeroot(x);
if(findroot(y) == x && ch[x][1] == y && !ch[y][0])
fa[y] = ch[x][1] = 0, upd(x);
else
cout << "JY" << endl;
}
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;
access(x), splay(x);
int vp = vs[x];
if(vp == x) return ;
// cout << "vp = " << vp << endl;
// cout << "equal " << x << ' ' << vp << ' ' << p[vp] << endl;
// cout << "cut " << x << ' ' << p[x] << endl;
cut(x, p[x]);
link(vp, p[vp]);
split(p[x], x), mark(x, x);
}
void insert(int x, int w) {
// cout << x << " :: " << w << endl;
mp[x] = w;
}
void del(int x) {
// cout << "del " << x << endl;
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,
insert(i, 1), mark(i, i), upd(1);
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(p[x], x), del(mn[x]);
cut(y, p[y]);
swap(MP[p[x]], MP[p[y]]), swap(p[x], p[y]);
split(p[x], x), mark(x, x), insert(mn[x], siz[x]);
split(p[y], y), mark(y, y), insert(mn[y], siz[y]);
} else {
broke(x), broke(y);
split(p[x], x), del(mn[x]);
split(p[y], y), del(mn[y]);
link(y, p[x]);
swap(MP[p[x]], MP[p[y]]), swap(p[x], p[y]);
split(p[x], x), mark(x, x), insert(mn[x], siz[x]);
}
// cout << "QAQ" << endl;
} 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";
}
}
// L(i, 1, n) cout << p[i] << ' ';
// cout << endl;
//// L(i, 1, n) cout << MP[i] << ' ';
//// cout << endl;
// L(i, 1, n) cout << findroot(i) << ' ';
// cout << endl;
}
}
int main() {
// freopen("data.in", "r", stdin);
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--) Main();
return 0;
}
/*
1
3 4
swap_p 3 1
swap_p 2 1
cmp 1 3
swap_p 1 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 27196kb
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: 0
Accepted
time: 56ms
memory: 27144kb
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...
output:
= = < = = < < > < = = > = = = = = = = = = = > < = < = > = = = > = > > = = = = = = = = > < = = = > < = < = = = < = < > = = = = > > > > = = = = = = = = = < = > > > = = = = = = = = = < = < = < = = = > > < = = = = > < = = = = = = = = = < > < > = = = = = > < < < > = < = > = < = = = = = = < < > > < = = = ...
result:
ok 33282 lines
Test #3:
score: 0
Accepted
time: 64ms
memory: 27040kb
input:
10000 2 15 swap_a 2 2 swap_p 2 2 cmp 326694868363016462 925456129016738104 swap_p 1 2 swap_a 2 2 swap_p 2 1 swap_p 2 1 swap_a 2 2 cmp 194403870824900634 290296733170168261 cmp 685622533532390613 845098181059391203 swap_p 2 2 swap_p 2 2 swap_p 2 2 swap_p 1 1 swap_a 2 1 31 3 swap_a 5 30 swap_a 15 1 cm...
output:
= > = = = = < = = < < < > = > = > = = = = = < < > < = = < < < > = = = = = = = < = = = < = > = = = = < = = = = = = > < = = = = = = = < = < = < = > > = = = = = = = < < > > = = = < = = = < > < = = = = > > = > = = = = > = = > < < = = = = = < = = = > > > > = > = = > = = = = = = = = = < < > < > = = > = < ...
result:
ok 33428 lines
Test #4:
score: 0
Accepted
time: 86ms
memory: 27076kb
input:
1000 65 43 swap_a 42 31 swap_a 56 25 cmp 13794376267158472 46277656098109759 cmp 444036536304978262 755847352552132493 cmp 908256097321476449 595621612325961509 cmp 635480759573261863 559355393434597718 swap_a 27 57 cmp 585086370256076627 733151313507052357 swap_a 43 62 swap_p 64 50 swap_p 40 16 swa...
output:
= = = = = < < < < > > < = < > > < < > > > = < = < > > < < < > > < > > > < < < > < < < > < > < < > > > < > > > > < < < < > > < = = = = = = = = > = = = > = = < > < < < > < < = = = = < = = = > > > > > > = = = > = = < > = < < > < = < > > < > < > > < > < < < < > < < < > > > > > > > > < < > = = = = > < > ...
result:
ok 33319 lines
Test #5:
score: 0
Accepted
time: 123ms
memory: 27504kb
input:
100 879 867 swap_p 111 344 cmp 361083772874837023 709151040161323902 swap_a 99 180 cmp 641913877862669667 450742900066998679 cmp 39483451443775826 957145697637778063 swap_a 237 178 swap_a 31 326 swap_p 291 662 swap_a 84 703 swap_a 541 37 swap_a 57 615 swap_a 332 609 swap_p 74 439 swap_a 281 350 swap...
output:
< = > < = > = < > = = < = = > < = = = > > > < < > < = > < > > > > > < > > < < < < < < > = < < > > < > < > > < > < < > > > = < = > < < = < < < > < > > < > > < > < < > > < > > > > < > > < < < < > < > < < < < < > > > > > > > > < > < < < > > < < > > > > > > < < > > > < > < < > < > > > < < < > > < > > < ...
result:
ok 33308 lines
Test #6:
score: 0
Accepted
time: 264ms
memory: 29872kb
input:
10 2397 3950 swap_a 643 401 cmp 515978739075589733 566064461406691441 swap_p 108 1740 cmp 645610656342389227 935405231824412954 swap_p 1949 972 swap_a 350 395 cmp 584097429036799031 466605450371139077 cmp 188110274758711055 52954946610635907 cmp 898925291698145288 529496510940857315 cmp 369035636621...
output:
= < = = > = > < < < > = < > = = < < < < = < > < < < > > < < < < = > < > < > > < > < > < < < = > < > = < > < < < = > > > > > < > > < < < = < < < > > < > = < < < < = = = > < > < = < > > > = > < < < = > = > > > < > > = < > > < > = < > = > > < > > = < > > < < = > > < > > < > > > < = > < < < = < > < < > ...
result:
ok 33547 lines
Test #7:
score: 0
Accepted
time: 910ms
memory: 36036kb
input:
1 100000 100000 cmp 275296188064989117 800031045635430494 swap_p 387 71136 cmp 795078469543095339 86592103476177255 swap_p 49521 71789 swap_p 79546 16901 swap_a 92586 88319 cmp 165520908415920510 83593495696513240 cmp 397367134248646770 481876428149248833 swap_a 13927 77270 swap_a 47748 40395 cmp 23...
output:
= = = > = > = > = < < = = = = = > = = < = < > = = = < = = > = = < = = < < = < < = < > > = = = = < = = = < > = < < > < = < > < > = = < < = = = = = = < = > > < = = = = = > < > < < < > = = < = = < > < > > < = < < > > > < > < = > = > < < > < = > < < < < > > > < < < > < > > > = > < = < < > > < > > > > > ...
result:
ok 33339 lines
Test #8:
score: 0
Accepted
time: 823ms
memory: 36028kb
input:
1 100000 100000 cmp 711525147933505257 539965757374435240 cmp 772631397936820308 681918367983678100 swap_p 32225 37695 cmp 908390687737274785 813678038049709833 cmp 937904202854552410 12308320153007147 swap_a 4841 25610 cmp 917660754232600606 924722230106760619 swap_a 43334 13111 swap_a 14653 89397 ...
output:
= = = > > = = = = > = = < = = > = < < > < = > > > = = < = < = < = > = < = > < < = > = > < = = < = = > > > = = > > > = = = < < = = > = > = < < > = = = = > = < < < = > = > = = = = = > < = = > < < = = > = = = < < > = < < > > = < > < < < < > < < < > < > < > < > = < < > < < < > = > > < > = < > < > > = < ...
result:
ok 33018 lines
Test #9:
score: 0
Accepted
time: 709ms
memory: 36108kb
input:
1 100000 100000 swap_a 47080 69562 cmp 762845072453568546 81871312173171816 swap_p 50683 36267 swap_a 1106 8448 swap_p 33023 74990 swap_p 20793 57647 swap_a 76519 58086 swap_p 4716 55099 swap_a 80469 59619 swap_a 90650 70560 cmp 349858792087292474 769398732484415241 swap_a 7191 80796 swap_a 73779 68...
output:
= > = > = < = = > < = < = > = > > > = > = < < = < > > < = > = = < = < = = < > < < < = = > = < = = > > < > < < < > = > = > > > > = = = = = > = > < = = < = = = = = < < > < < = < = < > < > > > > > < < < < < > > = < < > > = < > = < < < = < < < < = < < = < = < = < > = > > = < > < < < < < = > > = > > > < ...
result:
ok 33392 lines
Test #10:
score: 0
Accepted
time: 621ms
memory: 36096kb
input:
1 100000 100000 cmp 583983059080602946 243207209117285947 cmp 943450775895518815 492498714512827142 cmp 496703023811323100 155542152882112147 swap_p 83477 5804 cmp 292296110154672437 213693414719502592 swap_a 65042 53869 cmp 791788720971316669 53728981001645229 swap_p 15063 80147 cmp 827461442511953...
output:
= = = < = = < = = < < < = = = = > = < = < = = < < > > = = < > < = = > > = = = < < < = < = > < < = = = = = = = = = = < = = > = > > = > < = = > > < = < > = < = > = = < < = < > < < = = < < < < = > < = = = < = > = = = = = > < = > < > = < = = = = = = > > = < = < > = > > = = = < = > < < = > < = > > = < > ...
result:
ok 33481 lines
Test #11:
score: 0
Accepted
time: 709ms
memory: 36132kb
input:
1 100000 100000 swap_a 30227 43877 swap_p 46302 44772 swap_a 64941 79197 cmp 91013920853007211 964613016882996222 cmp 258377755420086237 61743627329152983 swap_a 91790 41931 cmp 297004985803970515 262531377925362079 cmp 560237748477773595 786407574131206764 swap_a 19973 42446 swap_a 63085 6414 swap_...
output:
< = = < < > < < = = = = < > > = < < < = = = = = = = < = = < < < < > = = > > < = > > < < > = = = = > > > = = = < < < = = = = = > = > < < = = = > = > > < = > = = > = > = = > = > = < = > = < = = = > > = > = = = = < = = < > < = = > = < = = < = < = = = > = > > < = > = > > < < > = = > < < > = < < < = < > ...
result:
ok 33642 lines
Test #12:
score: -100
Time Limit Exceeded
input:
1 100000 100000 swap_a 88322 88321 swap_p 59728 59727 cmp 970709134002514987 378535920005436396 swap_a 16103 16104 swap_p 26234 26233 swap_a 7304 7303 cmp 199519809604421701 453382810567022576 swap_a 76013 76014 swap_a 51979 51980 swap_p 39573 39574 swap_a 92697 92698 cmp 731583826302382596 60589963...