제출 #1332628

#제출 시각아이디문제언어결과실행 시간메모리
1332628blackscreen1Birthday gift (IZhO18_treearray)C++20
100 / 100
1806 ms93788 KiB
#include <bits//stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;
#define ll long long
#define iloop(m, h) for (auto i = m; i != h; i += (m < h ? 1 : -1))
#define jloop(m, h) for (auto j = m; j != h; j += (m < h ? 1 : -1))
#define kloop(m, h) for (auto k = m; k != h; k += (m < h ? 1 : -1))
#define lloop(m, h) for (auto l = m; l != h; l += (m < h ? 1 : -1))
#define pll pair<ll, ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
ll n, m, q, a[200005];
set<ll> st[200005], st2[200005];
vector<ll> adj[200005];
ll cmd, t1, t2, t3, twok[20][200005], dpt[200005];
void dfs(ll nd, ll pr, ll dp) {
	dpt[nd] = dp;
	twok[0][nd] = pr;
	iloop(1, 20) {
		if (twok[i-1][nd] == -1) break;
		twok[i][nd] = twok[i-1][twok[i-1][nd]];
	}
	for (auto it : adj[nd]) if (it != pr) dfs(it, nd, dp+1);
}
ll lca(ll x, ll y) {
	if (dpt[x] < dpt[y]) swap(x, y);
	iloop(19, -1) if ((dpt[x] - dpt[y])&(1<<i)) x = twok[i][x];
	if (x == y) return x;
	iloop(19, -1) if (twok[i][x] != twok[i][y]) x = twok[i][x], y = twok[i][y];
	return twok[0][x];
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	memset(twok, -1, sizeof(twok));
	cin >> n >> m >> q;
	iloop(1, n) {
		cin >> t1 >> t2;
		t1--, t2--;
		adj[t1].push_back(t2);
		adj[t2].push_back(t1);
	}
	dfs(0, -1, 0);
	iloop(0, m) {cin >> a[i]; a[i]--;}
	iloop(0, m) st[a[i]].insert(i);
	iloop(0, m-1) st2[lca(a[i], a[i+1])].insert(i);
	while (q--) {
		cin >> cmd;
		if (cmd == 1) {
			cin >> t1 >> t2;
			t1--, t2--;
			st[a[t1]].erase(t1);
			if (t1) st2[lca(a[t1-1], a[t1])].erase(t1-1);
			if (t1 < m-1) st2[lca(a[t1], a[t1+1])].erase(t1);
			a[t1] = t2;
			st[a[t1]].insert(t1);
			if (t1) st2[lca(a[t1-1], a[t1])].insert(t1-1);
			if (t1 < m-1) st2[lca(a[t1], a[t1+1])].insert(t1);
		}
		else {
			cin >> t1 >> t2 >> t3;
			t1--, t2--, t3--;
			auto it = st[t3].lower_bound(t1);
			if (it != st[t3].end() && *it <= t2) cout << *it + 1 << " " << *it + 1 << "\n";
			else {
				auto it2 = st2[t3].lower_bound(t1);
				if (it2 != st2[t3].end() && *it2 < t2) cout << *it2 + 1 << " " << *it2 + 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...