Submission #40443

#TimeUsernameProblemLanguageResultExecution timeMemory
40443szawinisBirthday gift (IZhO18_treearray)C++14
100 / 100
2045 ms105188 KiB
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <set> #include <unordered_map> #include <unordered_set> using namespace std; const int N = 1 << 18; int n, m, q, a[N]; vector<int> g[N]; set<int> pos[N], lca_pos[N]; int d[N]; vector<int> anc[N]; void dfs(int u, int p) { for(int v: g[u]) if(v != p) { d[v] = d[u] + 1; anc[v].push_back(u); for(int j = 0; 1 << j+1 <= d[v]; j++) anc[v].push_back(anc[anc[v][j]][j]); dfs(v, u); } } int lca(int u, int v) { if(d[v] > d[u]) swap(u, v); if(d[u] > d[v]) { for(int j = 0; 1 << j <= d[u] - d[v]; j++) if(d[u] - d[v] >> j & 1) u = anc[u][j]; } if(u == v) return u; for(int j = 18; j >= 0; j--) if(j < anc[u].size() && anc[u][j] != anc[v][j]) u = anc[u][j], v = anc[v][j]; return anc[u][0]; } int main() { scanf("%d %d %d", &n, &m, &q); for(int i = 1, u, v; i < n; i++) { scanf("%d %d", &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs(1, 0); for(int i = 1; i <= m; i++) { scanf("%d", a+i); pos[a[i]].insert(i); if(i > 1) lca_pos[lca(a[i-1], a[i])].insert(i-1); } for(int z = 0, tp, l, r, v, i; z < q; z++) { scanf("%d", &tp); if(tp == 1) { scanf("%d %d", &i, &v); if(i < m) lca_pos[lca(a[i], a[i+1])].erase(i); if(i > 1) lca_pos[lca(a[i-1], a[i])].erase(i-1); pos[a[i]].erase(i); a[i] = v; if(i < m) lca_pos[lca(a[i], a[i+1])].insert(i); if(i > 1) lca_pos[lca(a[i-1], a[i])].insert(i-1); pos[a[i]].insert(i); } else { scanf("%d %d %d", &l, &r, &v); auto it = lca_pos[v].lower_bound(l); if(it != lca_pos[v].end() && *it < r) printf("%d %d\n", *it, *it+1); else { auto it = pos[v].lower_bound(l); if(it == pos[v].end() || *it > r) printf("-1 -1\n"); else printf("%d %d\n", *it, *it); } } } }

Compilation message (stderr)

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:21:24: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   for(int j = 0; 1 << j+1 <= d[v]; j++)
                        ^
treearray.cpp: In function 'int lca(int, int)':
treearray.cpp:30:12: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
    if(d[u] - d[v] >> j & 1) u = anc[u][j];
            ^
treearray.cpp:34:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(j < anc[u].size() && anc[u][j] != anc[v][j])
        ^
treearray.cpp: In function 'int main()':
treearray.cpp:40:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &m, &q);
                               ^
treearray.cpp:42:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
                         ^
treearray.cpp:48:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", a+i);
                   ^
treearray.cpp:53:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &tp);
                   ^
treearray.cpp:55:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &i, &v);
                          ^
treearray.cpp:64:33: 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...