跳到正文

剪贴板

剪贴板 ni6kf34e

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;

const int N = 100010; char o; int n, m, q, a[N], rt[N], x, y;
__gnu_pbds::gp_hash_table<int, int> c[N];
int lbt(int x) {return x & (-x);}
int find(int x) {return rt[x] == x ? x : rt[x] = find(rt[x]);}
int get(int x, int y) {return c[x].find(y) != c[x].end() ? c[x][y] : 0;}
void merge(int x, int y) {
	int a = find(x), b = find(y); if(a == b) return ;
	if(c[a].size() > c[b].size()) swap(a, b); rt[a] = b;
	for(auto it: c[a]) {
		x = it.first, y = it.second;
		c[b][x] += y;
	}
	c[a].clear();
}
int main() {
	cin >> n >> m; a[n + 1] = -1;
	for(int i = 1; i <= n; i++) {
		scanf("%d", &x); a[x] = i; rt[i] = i;
		for(int j = x; j <= n; j += lbt(j)) c[i][j]++;
	}
	while(m--) {int x, y; scanf("%d%d", &x, &y); merge(x, y);} cin >> m;
	while(m--) {
		cin >> o; scanf("%d%d", &x, &y);
		if(o == 'Q') {
			x = find(x); int ans = 0, t = 0;
			for(int j = 19, a, b; j >= 0; j--) if((a = ans + (1 << j)) <= n && (b = t + get(x, a)) < y) ans = a, t = b;
			printf("%d\n", a[ans + 1]);
		}
		else merge(x, y);
	}
	return 0;
}