Submission #378912

#TimeUsernameProblemLanguageResultExecution timeMemory
378912casperwangBirthday gift (IZhO18_treearray)C++14
100 / 100
1151 ms79596 KiB
#include <bits/stdc++.h>
#define All(x) x.begin(), x.end()
#define pb emplace_back
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;
#define debug(args...) kout("[ " + string(#args) + " ]", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ',kout(b...); }
template <class T> void pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }

const int MAXN = 200000;
const int L = 20;
int N, M, Q;
int a, b, c;
vector <int> path[MAXN+1];
int anc[MAXN+1][L];
pii dfs[MAXN+1];
int cnt;
int v[MAXN*2+1];
set <int> pos[MAXN+1];
int typ;

void DFS(int now, int par) {
	dfs[now].ff = ++cnt;
	for (int i : path[now]) {
		if (i == par) continue;
		anc[i][0] = now;
		DFS(i, now);
	}
	dfs[now].ss = ++cnt;
}

void build() {
	for (int i = 1; i < L; i++) {
		for (int j = 1; j <= N; j++) {
			anc[j][i] = anc[anc[j][i-1]][i-1];
		}
	}
}

bool ancestor(int a, int b) {
	return dfs[a].ff <= dfs[b].ff && dfs[b].ss <= dfs[a].ss;
}

int LCA(int a, int b) {
	if (ancestor(a, b)) return a;
	for (int i = L-1; i >= 0; i--) {
		if (!ancestor(anc[a][i], b))
			a = anc[a][i];
	}
	return anc[a][0];
}

void del(int x, int v) {
	pos[x].erase(v);
}

void add(int x, int v) {
	pos[x].insert(v);
}

signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> N >> M >> Q;
	for (int i = 0; i < N-1; i++) {
		cin >> a >> b;
		path[a].pb(b);
		path[b].pb(a);
	}
	anc[1][0] = 1;
	DFS(1, 0);
	build();
	for (int i = 1; i <= M; i++) {
		cin >> v[i*2];
		if (i > 1)
			v[i*2-1] = LCA(v[i*2-2], v[i*2]);
	}
	for (int i = 2; i <= 2*M; i++)
		pos[v[i]].insert(i);
	for (int i = 0; i < Q; i++) {
		cin >> typ;
		if (typ == 1) {
			cin >> a >> c;
			del(v[a*2], a*2);
			v[a*2] = c;
			add(v[a*2], a*2);
			if (a > 1) {
				del(v[a*2-1], a*2-1);
				v[a*2-1] = LCA(v[a*2], v[a*2-2]);
				add(v[a*2-1], a*2-1);
			}
			if (a < M) {
				del(v[a*2+1], a*2+1);
				v[a*2+1] = LCA(v[a*2], v[a*2+2]);
				add(v[a*2+1], a*2+1);
			}
		} else {
			cin >> a >> b >> c;
			auto k = pos[c].lower_bound(a*2);
			if (k != pos[c].end() && *k <= b*2) {
				cout << *k/2 << ' ' << (*k+1)/2 << '\n';
			} else {
				cout << -1 << ' ' << -1 << '\n';
			}
		}
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...