This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* I do it for the glory */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 2e5 + 10, MOD = 1e9 + 7, SQ = 500;
int H[N], P[19][N], A[N], B[N], M[N / SQ + 1][N], M2[N / SQ + 1][N], n, m, q;
vector<int> adj[N];
void preDFS(int v, int p = -1) {
if (!~p) P[0][v] = v;
for (int i = 1; i < 19; i++)
P[i][v] = P[i - 1][P[i - 1][v]];
for (int &u : adj[v])
if (u != p)
H[u] = H[v] + 1, P[0][u] = v, preDFS(u, v);
}
int LCA(int u, int v) {
if (H[u] < H[v]) swap(u, v);
for (int i = H[u] - H[v]; i; i -= i & -i)
u = P[__builtin_ctz(i)][u];
for (int i = 18; ~i; i--)
if (P[i][u] != P[i][v])
u = P[i][u], v = P[i][v];
return u == v ? v : P[0][v];
}
void upd(int id, int v) {
M2[id / SQ][A[id]]--;
A[id] = v;
M2[id / SQ][A[id]]++;
if (id) {
M[(id - 1) / SQ][B[id - 1]]--;
B[id - 1] = LCA(A[id], A[id - 1]);
M[(id - 1) / SQ][B[id - 1]]++;
}
if (id + 1 < m) {
M[id / SQ][B[id]]--;
B[id] = LCA(A[id], A[id + 1]);
M[id / SQ][B[id]]++;
}
}
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
adj[u].push_back(v), adj[v].push_back(u);
}
preDFS(1);
for (int i = 0; i < m; i++) {
scanf("%d", &A[i]); M2[i / SQ][A[i]]++;
if (i) B[i - 1] = LCA(A[i - 1], A[i]), M[(i - 1) / SQ][B[i - 1]]++;
}
for (; q; q--) {
int t; scanf("%d", &t);
if (t < 2) {
int p, v; scanf("%d%d", &p, &v);
upd(p - 1, v);
} else {
int l, r, v; scanf("%d%d%d", &l, &r, &v);
pii ret = {-1, -1}; int f = 0; l--, r--; r--;
for (int i = l; i <= r && !f; ) {
if (i % SQ == 0 && i + SQ - 1 <= r) {
if (M[i / SQ][v]) {
for (int j = i; j < i + SQ && !f; j++)
if (B[j] == v) { f = 1, ret = {j + 1, j + 2}; }
} else i += SQ;
} else {
if (B[i] == v) { f = 1, ret = {i + 1, i + 2}; }
else i++;
}
}
for (int i = l; i <= r + 1 && !f; ) {
if (i % SQ == 0 && i + SQ <= r) {
if (M2[i / SQ][v]) {
for (int j = i; j < i + SQ && !f; j++) {
if (A[j] == v) { f = 1; ret = {j + 1, j + 1}; }
}
} else i += SQ;
} else {
if (A[i] == v) { f = 1; ret = {i + 1, i + 1}; }
else i++;
}
}
printf("%d %d\n", ret.F, ret.S);
}
}
return 0;
}
Compilation message (stderr)
treearray.cpp: In function 'int main()':
treearray.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | scanf("%d%d%d", &n, &m, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:55:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | int u, v; scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d", &A[i]); M2[i / SQ][A[i]]++;
| ~~~~~^~~~~~~~~~~~~
treearray.cpp:64:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | int t; scanf("%d", &t);
| ~~~~~^~~~~~~~~~
treearray.cpp:66:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | int p, v; scanf("%d%d", &p, &v);
| ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:69:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | int l, r, v; scanf("%d%d%d", &l, &r, &v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |