QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#55332#4861. Lexicographic Comparisonzhoukangyang#WA 48ms27228kbC++115.2kb2022-10-13 11:29:292022-10-13 11:29:30

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-13 11:29:30]
  • 评测
  • 测评结果:WA
  • 用时:48ms
  • 内存:27228kb
  • [2022-10-13 11:29:29]
  • 提交

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;
	if(sz(st[w])) mp.erase(*st[w].begin());
	st[w].insert(x); 
	mp[*st[w].begin()] = w;
}
void del(int x) {
//	cout << "del " << x << endl;
	if(!mp.count(x)) return ;
	int u = mp[x];
	mp.erase(u);
	if(sz(st[u])) mp[*st[u].begin()] = u;
}

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: 10ms
memory: 27140kb

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
Wrong Answer
time: 48ms
memory: 27228kb

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:

wrong answer 3rd lines differ - expected: '<', found: '>'