This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<class T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 42, L = 18;
vector<int> g[N];
int a[N], tin[N], tout[N], up[N][L];
set<int> haveone[N], havelca[N];
int timer = 0;
bool anc(int u, int v) {
return tin[u] <= tin[v] && tin[v] < tout[u];
}
void dfs(int v, int p) {
for(int l = 1; l < L; l++) up[v][l] = up[up[v][l - 1]][l - 1];
tin[v] = timer++;
for(auto& x : g[v]) {
if(x == p) continue;
up[x][0] = v;
dfs(x, v);
}
tout[v] = timer;
}
int lca(int v, int u) {
if(anc(v, u)) return v;
if(anc(u, v)) return v;
for(int l = L - 1; l >= 0; l--)
if(!anc(up[v][l], u))
v = up[v][l];
return up[v][0];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n = 0, m = 0, q = 0;
cin >> n >> m >> q;
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[--u].pb(--v);
g[v].pb(u);
}
dfs(0, -1);
vector<int> a(m);
for(int i = 0; i < m; i++) {
cin >> a[i]; a[i]--;
haveone[a[i]].insert(i);
if(i) {
havelca[lca(a[i], a[i - 1])].insert(i - 1);
}
}
while(q--) {
int type; cin >> type;
if(type == 1) {
int pos, v;
cin >> pos >> v; pos--; v--;
haveone[a[pos]].erase(pos);
if(pos) havelca[lca(a[pos], a[pos - 1])].erase(pos - 1);
if(pos + 1 < m) havelca[lca(a[pos], a[pos + 1])].erase(pos);
a[pos] = v;
if(pos) havelca[lca(a[pos], a[pos - 1])].insert(pos - 1);
if(pos + 1 < m) havelca[lca(a[pos], a[pos + 1])].insert(pos);
haveone[a[pos]].insert(pos);
} else {
int l, r, v;
cin >> l >> r >> v;
l--; r--; v--;
auto it1 = haveone[v].lower_bound(l), it2 = havelca[v].lower_bound(l);
if(it1 != haveone[v].end() && *it1 <= r) cout << *it1 + 1 << ' ' << *it1 + 1 << endl;
else if(it2 != havelca[v].end() && *it2 + 1 <= r) cout << *it2 + 1 << ' ' << *it2 + 2 << endl;
else cout << "-1 -1\n";
}
}
return 0;
}
/*
*/
Compilation message (stderr)
treearray.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |