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], n, m, q; unordered_map<int, int> M[N / SQ + 4], M2[N / SQ + 4];
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]]++;
}
}
void timee() {
//while (1) {}
}
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]);
if (i / SQ > N / SQ + 3) timee();
if (A[i] >= N) timee();
M2[i / SQ][A[i]]++;
if (i) {
B[i - 1] = LCA(A[i - 1], A[i]);
if (B[i - 1] < 0 || B[i - 1] >= N) timee();
if ((i - 1) / SQ < 0 || (i - 1) / SQ > N / SQ) timee();
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:57:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d%d", &n, &m, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:59:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | int u, v; scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d", &A[i]);
| ~~~~~^~~~~~~~~~~~~
treearray.cpp:76:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | int t; scanf("%d", &t);
| ~~~~~^~~~~~~~~~
treearray.cpp:78:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | int p, v; scanf("%d%d", &p, &v);
| ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:81:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | 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... |