This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <unordered_set>
#include <cstdlib>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <bitset>
#include <queue>
#include <unordered_map>
#include <fstream>
#include <random>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
vector <vector<int>> G;
const int N = 2e5 + 10;
int a[N];
int dp[N][20];
int depth[N];
set <int> f1[N];
set <int> f2[N];
void dfs(int v, int p) {
depth[v] = depth[p] + 1;
dp[v][0] = p;
for (int j = 1; j < 20; j++) {
dp[v][j] = dp[dp[v][j - 1]][j - 1];
}
for (auto it : G[v]) {
if (it == p) continue;
dfs(it, v);
}
}
int find_lca(int a, int b) {
if (depth[a] < depth[b]) swap(a, b);
int k = depth[a] - depth[b];
for (int i = 0; i < 20; i++) {
if (k & (1 << i)) {
a = dp[a][i];
}
}
if (a == b) return a;
for (int i = 19; i >= 0; i--) {
if (dp[a][i] != dp[b][i]) {
a = dp[a][i];
b = dp[b][i];
}
}
return dp[a][0];
}
int main() {
int n, m, q;
cin >> n >> m >> q;
G.resize(n + 1);
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(1, 1);
for (int i = 1; i <= m; i++) {
cin >> a[i];
f1[a[i]].insert(i);
}
for (int i = 1; i <= m - 1; i++) {
f2[find_lca(a[i], a[i + 1])].insert(i);
}
while (q--) {
int type;
cin >> type;
if (type == 1) {
int pos, val;
cin >> pos >> val;
f1[a[pos]].erase(pos);
f1[val].insert(pos);
if (pos != 1) {
f2[find_lca(a[pos - 1], a[pos])].erase(pos - 1);
f2[find_lca(a[pos - 1], val)].insert(pos - 1);
}
if (pos != n) {
f2[find_lca(a[pos], a[pos + 1])].erase(pos);
f2[find_lca(val, a[pos + 1])].insert(pos);
}
a[pos] = val;
}
else {
int l, r, v;
cin >> l >> r >> v;
auto it = f1[v].lower_bound(l);
if (it != f1[v].end() && *it <= r) {
cout << *it << " " << *it << "\n";
continue;
}
auto w = f2[v].lower_bound(l);
if (w != f2[v].end() && *w < r) {
cout << *w << " " << *w + 1 << "\n";
continue;
}
cout << -1 << " " << -1 << "\n";
}
}
}
# | 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... |