Submission #84474

#TimeUsernameProblemLanguageResultExecution timeMemory
84474inomBirthday gift (IZhO18_treearray)C++14
56 / 100
1610 ms108896 KiB
#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)

treearray.cpp: In function 'int main()':
treearray.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d", &n, &k, &q);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &u, &v);
         ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &a[i]);
         ~~~~~^~~~~~~~~~~~~
treearray.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &t);
         ~~~~~^~~~~~~~~~
treearray.cpp:83:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &pos, &v);
             ~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:101:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d %d", &l, &r, &v);
             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...