# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
84474 | inom | Birthday gift (IZhO18_treearray) | C++14 | 1610 ms | 108896 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.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MX = 200 * 1000 + 7;
const int LG = 18;
vector<int> g[MX];
int tin[MX], tout[MX];
int timer = 1;
int p[MX][LG];
int a[MX];
set<int> x[MX], y[MX];
void dfs(int v = 1, int par = 1) {
tin[v] = timer;
timer++;
p[v][0] = par;
for (int i = 1; i < LG; i++) {
p[v][i] = p[p[v][i - 1]][i - 1];
}
for (int to : g[v]) {
if (to != par) {
dfs(to, v);
}
}
tout[v] = timer;
timer++;
}
bool isAncestor(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int getLCA(int u, int v) {
if (isAncestor(u, v)) {
return u;
} else if (isAncestor(v, u)) {
return v;
} else {
for (int i = LG - 1; i >= 0; i--) {
if (!isAncestor(p[u][i], v)) {
u = p[u][i];
}
}
return p[u][0];
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, k, q;
scanf("%d %d %d", &n, &k, &q);
for (int i = 1; i <= n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
x[i].insert(k + 1);
y[i].insert(k + 1);
}
dfs();
for (int i = 1; i <= k; i++) {
scanf("%d", &a[i]);
x[a[i]].insert(i);
if (i > 1) {
y[getLCA(a[i - 1], a[i])].insert(i - 1);
}
}
for (int i = 0; i < q; i++) {
int t;
scanf("%d", &t);
if (t == 1) {
int pos, v;
scanf("%d %d", &pos, &v);
x[a[pos]].erase(pos);
if (pos > 1) {
y[getLCA(a[pos - 1], a[pos])].erase(pos - 1);
}
if (pos + 1 <= n) {
y[getLCA(a[pos], a[pos + 1])].erase(pos);
}
a[pos] = v;
x[a[pos]].insert(pos);
if (pos > 1) {
y[getLCA(a[pos - 1], a[pos])].insert(pos - 1);
}
if (pos + 1 <= n) {
y[getLCA(a[pos], a[pos + 1])].insert(pos);
}
} else {
int l, r, v;
scanf("%d %d %d", &l, &r, &v);
int p1 = *x[v].lower_bound(l), p2 = *y[v].lower_bound(l);
if (p1 <= r) {
printf("%d %d\n", p1, p1);
} else if (p2 + 1 <= r) {
printf("%d %d\n", p2, p2 + 1);
} else {
printf("-1 -1\n");
}
}
}
}
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... |