이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* 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 = 400;
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;
}
컴파일 시 표준 에러 (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... |