# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
54196 | ulna | Birthday gift (IZhO18_treearray) | C++11 | 1893 ms | 98516 KiB |
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 ("O3")
#include <bits/stdc++.h>
using namespace std;
// why am I so weak
const int maxn = 2e5;
int n, m, q;
int a[maxn];
vector<int> g[maxn];
#define MAX_LOG 18
int depth[maxn];
int par[MAX_LOG][maxn];
set<int> s[maxn];
set<int> ss[maxn];
void dfs(int v, int par = -1, int d = 0) {
::par[0][v] = par;
depth[v] = d;
for (auto u : g[v]) if (u != par) {
dfs(u, v, d + 1);
}
}
inline int lca(int v, int u) {
if (depth[u] > depth[v]) swap(u, v);
for (int i = 0; i < MAX_LOG; i++) if (((depth[v] - depth[u]) >> i) & 1) {
v = par[i][v];
}
if (u == v) return u;
for (int i = MAX_LOG - 1; i >= 0; i--) if (par[i][v] != par[i][u]) {
v = par[i][v], u = par[i][u];
}
return par[0][v];
}
inline void upd(int id, int mode) {
if (id < 0 || id + 1 >= m) return;
if (mode == -1) ss[lca(a[id], a[id + 1])].erase(id);
else ss[lca(a[id], a[id + 1])].insert(id);
}
int main() {
cin >> n >> m >> q;
for (int i = 0; i + 1 < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
x--, y--;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(0);
for (int i = 0; i + 1 < MAX_LOG; i++) for (int j = 0; j < n; j++) {
if (par[i][j] < 0) par[i + 1][j] = -1;
else par[i + 1][j] = par[i][par[i][j]];
}
for (int i = 0; i < m; i++) {
scanf("%d", &a[i]);
a[i]--;
s[a[i]].insert(i);
}
for (int i = 0; i + 1 < m; i++) {
ss[lca(a[i], a[i + 1])].insert(i);
}
while (q--) {
int ope;
scanf("%d", &ope);
if (ope == 1) {
int x, y;
scanf("%d %d", &x, &y);
x--, y--;
s[a[x]].erase(x);
upd(x, -1);
upd(x - 1, -1);
a[x] = y;
s[a[x]].insert(x);
upd(x, 1);
upd(x - 1, 1);
} else {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
x--, y--, z--;
auto lb = s[z].lower_bound(x);
if (lb != s[z].end() && *lb <= y) {
printf("%d %d\n", *lb + 1, *lb + 1);
continue;
}
lb = ss[z].lower_bound(x);
if (lb != ss[z].end() && *lb < y) {
printf("%d %d\n", *lb + 1, *lb + 2);
continue;
}
puts("-1 -1");
}
}
return 0;
}
Compilation message (stderr)
# | 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... |