Submission #167577

#TimeUsernameProblemLanguageResultExecution timeMemory
167577DrumpfTheGodEmperorBirthday gift (IZhO18_treearray)C++14
100 / 100
1144 ms82912 KiB
#include <bits/stdc++.h>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
		freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
typedef pair<int, int> ii;
const int N = 2e5 + 5;
vector<int> g[N];
int n, m, q, a[N], dep[N], par[N][20], b[N];
void dfs(int u, int p) {
	par[u][0] = p;
	fa (v, g[u]) if (v != p) {
		dep[v] = dep[u] + 1;
		dfs(v, u);
	}
}
void sparse() {
	fw (j, 1, 20) fw (i, 0, n) if (par[i][j - 1] != -1) {
		par[i][j] = par[par[i][j - 1]][j - 1];
	}
}
int getLCA(int u, int v) {
	if (dep[u] < dep[v]) swap(u, v);
	int diff = dep[u] - dep[v];
	fw (i, 0, 20) if (diff & (1 << i)) u = par[u][i];
	if (u == v) return u;
	bw (i, 20, 0) if (par[u][i] != par[v][i]) {
		u = par[u][i], v = par[v][i];
	}
	return par[u][0];
}
set<int> app1[N], app2[N];
signed main() {
	#ifdef BLU
	in("blu.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m >> q;
	fw (i, 1, n) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		g[u].pb(v), g[v].pb(u);
	}
	fw (i, 0, m) cin >> a[i], a[i]--;
	dfs(0, -1);
	sparse();
	//b[i] = lca(a[i], a[i + 1])
	fw (i, 0, m - 1) b[i] = getLCA(a[i], a[i + 1]);
	fw (i, 0, m) app1[a[i]].insert(i);
	fw (i, 0, m - 1) app2[b[i]].insert(i);
	while (q--) {
		int type;
		cin >> type;
		if (type == 1) {
			int pos, v;
			cin >> pos >> v;
			pos--, v--;
			app1[a[pos]].erase(pos);
			if (pos) app2[b[pos - 1]].erase(pos - 1);
			if (pos < m - 1) app2[b[pos]].erase(pos);
			a[pos] = v;
			app1[a[pos]].insert(pos);
			if (pos) {
				b[pos - 1] = getLCA(a[pos - 1], a[pos]);
				app2[b[pos - 1]].insert(pos - 1);
			}
			if (pos < m - 1) {
				b[pos] = getLCA(a[pos], a[pos + 1]);
//				st2.ins(pos, b[pos]);
				app2[b[pos]].insert(pos);
			}
		} else {
			int l, r, v;
			cin >> l >> r >> v;
			l--, r--, v--;
			auto lol = app1[v].upper_bound(r);
			int x = -1;
			if (lol != app1[v].begin()) {
				--lol;
				x = *lol;
			}
			if (x >= l && x <= r) {
//				cout << "what's cookin'\n";
				cout << x + 1 << " " << x + 1 << "\n";
			} else {
//				x = st2.check(l, r - 1, v);
				lol = app2[v].upper_bound(r - 1);
				x = -1;
				if (lol != app2[v].begin()) {
					--lol;
					x = *lol;
				}
				if (x >= l && x < r) cout << x + 1 << " " << x + 2 << "\n";
				else cout << "-1 -1\n";
			}
		}
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...