Submission #854558

# Submission time Handle Problem Language Result Execution time Memory
854558 2023-09-28T04:09:25 Z NeroZein Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 67068 KB
#include "bits/stdc++.h"
using namespace std;
 
const int LOG = 20; 
const int N = 2e5 + 5; 
 
int a[N]; 
vector<int> g[N]; 
int dep[N], up[LOG][N]; 
set<int> lco[N], no[N]; 
 
inline void dfs(int v, int p) {
  for (int u : g[v]) {
    if (u == p) {
      continue;
    }
    dep[u] = dep[v] + 1; 
    up[0][u] = v;
    for (int j = 1; j < LOG; ++j) {
      up[j][u] = up[j - 1][up[j - 1][u]]; 
    }
    dfs(u, v); 
  }
}
 
inline int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int j = 0; j < LOG; ++j) {
    if ((dep[u] - dep[v]) >> j & 1) {
      u = up[j][u];
    }
  }
  if (u == v) return v;
  for (int j = LOG - 1; j >= 0; --j) {
    if (up[j][u] != up[j][v]) {
      u = up[j][u], v = up[j][v]; 
    }
  }
  return up[0][v];
}
 
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, q;
  cin >> n >> m >> q;
  for(int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < m; ++i) {
    cin >> a[i]; 
  }
  for (int i = 0; i < n; ++i) {
    no[a[i]].insert(i); 
  }
  dfs(1, 1); 
  for (int i = 0; i < m - 1; ++i) {
    lco[lca(a[i], a[i + 1])].insert(i); 
  }
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int pos, v;
      cin >> pos >> v;
      pos--; 
      no[a[pos]].erase(pos);
      if (pos > 0) {
        lco[lca(a[pos], a[pos - 1])].erase(pos - 1);
      }
      if (pos < m) {
        lco[lca(a[pos], a[pos + 1])].erase(pos);
      }
      a[pos] = v; 
      no[a[pos]].insert(pos);
      if (pos > 0) {
        lco[lca(a[pos], a[pos - 1])].insert(pos - 1); 
      }
      if (pos < m) {
        lco[lca(a[pos], a[pos + 1])].insert(pos); 
      }
    } else {
      int l, r, v;
      cin >> l >> r >> v;
      --l, --r;
      auto s1 = lower_bound(no[v].begin(), no[v].end(), l); 
      if (s1 != no[v].end() && *s1 <= r) {
        cout << *s1 + 1 << ' ' << *s1 + 1 << '\n';
        continue;
      }
      s1 = lower_bound(lco[v].begin(), lco[v].end(), l); 
      if (s1 != lco[v].end() && *s1 < r) {
        cout << *s1 + 1 << ' ' << *s1 + 2 << '\n';
      } else {
        cout << -1 << ' ' << -1 << '\n';
      }
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 38232 KB n=5
2 Correct 7 ms 38336 KB n=100
3 Correct 7 ms 38236 KB n=100
4 Correct 8 ms 38488 KB n=100
5 Correct 7 ms 38236 KB n=100
6 Correct 7 ms 38232 KB n=100
7 Correct 8 ms 38232 KB n=100
8 Correct 7 ms 38236 KB n=100
9 Correct 7 ms 38236 KB n=100
10 Correct 7 ms 38236 KB n=100
11 Correct 7 ms 38232 KB n=100
12 Correct 8 ms 38504 KB n=100
13 Correct 7 ms 38232 KB n=100
14 Correct 7 ms 38232 KB n=100
15 Correct 7 ms 38236 KB n=100
16 Correct 8 ms 38236 KB n=100
17 Correct 7 ms 38232 KB n=100
18 Correct 7 ms 38236 KB n=100
19 Correct 7 ms 38236 KB n=100
20 Correct 7 ms 38236 KB n=100
21 Correct 8 ms 38492 KB n=100
22 Correct 7 ms 38356 KB n=100
23 Correct 8 ms 38352 KB n=100
24 Correct 7 ms 38236 KB n=100
25 Correct 7 ms 38232 KB n=100
26 Correct 7 ms 38236 KB n=12
27 Correct 7 ms 38232 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 7 ms 38232 KB n=5
2 Correct 7 ms 38336 KB n=100
3 Correct 7 ms 38236 KB n=100
4 Correct 8 ms 38488 KB n=100
5 Correct 7 ms 38236 KB n=100
6 Correct 7 ms 38232 KB n=100
7 Correct 8 ms 38232 KB n=100
8 Correct 7 ms 38236 KB n=100
9 Correct 7 ms 38236 KB n=100
10 Correct 7 ms 38236 KB n=100
11 Correct 7 ms 38232 KB n=100
12 Correct 8 ms 38504 KB n=100
13 Correct 7 ms 38232 KB n=100
14 Correct 7 ms 38232 KB n=100
15 Correct 7 ms 38236 KB n=100
16 Correct 8 ms 38236 KB n=100
17 Correct 7 ms 38232 KB n=100
18 Correct 7 ms 38236 KB n=100
19 Correct 7 ms 38236 KB n=100
20 Correct 7 ms 38236 KB n=100
21 Correct 8 ms 38492 KB n=100
22 Correct 7 ms 38356 KB n=100
23 Correct 8 ms 38352 KB n=100
24 Correct 7 ms 38236 KB n=100
25 Correct 7 ms 38232 KB n=100
26 Correct 7 ms 38236 KB n=12
27 Correct 7 ms 38232 KB n=100
28 Correct 8 ms 38236 KB n=500
29 Correct 8 ms 38236 KB n=500
30 Correct 7 ms 38236 KB n=500
31 Correct 7 ms 38236 KB n=500
32 Correct 8 ms 38236 KB n=500
33 Correct 7 ms 38236 KB n=500
34 Correct 8 ms 38236 KB n=500
35 Correct 8 ms 38236 KB n=500
36 Correct 7 ms 38232 KB n=500
37 Correct 7 ms 38236 KB n=500
38 Correct 7 ms 38232 KB n=500
39 Correct 7 ms 38236 KB n=500
40 Correct 7 ms 38208 KB n=500
41 Correct 8 ms 38236 KB n=500
42 Correct 7 ms 38232 KB n=500
43 Correct 7 ms 38232 KB n=500
44 Correct 8 ms 38236 KB n=500
45 Correct 7 ms 38236 KB n=500
46 Correct 7 ms 38236 KB n=500
47 Correct 7 ms 38236 KB n=500
48 Correct 9 ms 38236 KB n=500
49 Correct 7 ms 38236 KB n=500
50 Correct 7 ms 38232 KB n=500
51 Correct 8 ms 38236 KB n=500
52 Correct 8 ms 38236 KB n=500
53 Correct 7 ms 38236 KB n=500
54 Correct 7 ms 38236 KB n=500
55 Correct 7 ms 38236 KB n=278
56 Correct 7 ms 38232 KB n=500
57 Correct 8 ms 38236 KB n=500
58 Correct 7 ms 38324 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 7 ms 38232 KB n=5
2 Correct 7 ms 38336 KB n=100
3 Correct 7 ms 38236 KB n=100
4 Correct 8 ms 38488 KB n=100
5 Correct 7 ms 38236 KB n=100
6 Correct 7 ms 38232 KB n=100
7 Correct 8 ms 38232 KB n=100
8 Correct 7 ms 38236 KB n=100
9 Correct 7 ms 38236 KB n=100
10 Correct 7 ms 38236 KB n=100
11 Correct 7 ms 38232 KB n=100
12 Correct 8 ms 38504 KB n=100
13 Correct 7 ms 38232 KB n=100
14 Correct 7 ms 38232 KB n=100
15 Correct 7 ms 38236 KB n=100
16 Correct 8 ms 38236 KB n=100
17 Correct 7 ms 38232 KB n=100
18 Correct 7 ms 38236 KB n=100
19 Correct 7 ms 38236 KB n=100
20 Correct 7 ms 38236 KB n=100
21 Correct 8 ms 38492 KB n=100
22 Correct 7 ms 38356 KB n=100
23 Correct 8 ms 38352 KB n=100
24 Correct 7 ms 38236 KB n=100
25 Correct 7 ms 38232 KB n=100
26 Correct 7 ms 38236 KB n=12
27 Correct 7 ms 38232 KB n=100
28 Correct 8 ms 38236 KB n=500
29 Correct 8 ms 38236 KB n=500
30 Correct 7 ms 38236 KB n=500
31 Correct 7 ms 38236 KB n=500
32 Correct 8 ms 38236 KB n=500
33 Correct 7 ms 38236 KB n=500
34 Correct 8 ms 38236 KB n=500
35 Correct 8 ms 38236 KB n=500
36 Correct 7 ms 38232 KB n=500
37 Correct 7 ms 38236 KB n=500
38 Correct 7 ms 38232 KB n=500
39 Correct 7 ms 38236 KB n=500
40 Correct 7 ms 38208 KB n=500
41 Correct 8 ms 38236 KB n=500
42 Correct 7 ms 38232 KB n=500
43 Correct 7 ms 38232 KB n=500
44 Correct 8 ms 38236 KB n=500
45 Correct 7 ms 38236 KB n=500
46 Correct 7 ms 38236 KB n=500
47 Correct 7 ms 38236 KB n=500
48 Correct 9 ms 38236 KB n=500
49 Correct 7 ms 38236 KB n=500
50 Correct 7 ms 38232 KB n=500
51 Correct 8 ms 38236 KB n=500
52 Correct 8 ms 38236 KB n=500
53 Correct 7 ms 38236 KB n=500
54 Correct 7 ms 38236 KB n=500
55 Correct 7 ms 38236 KB n=278
56 Correct 7 ms 38232 KB n=500
57 Correct 8 ms 38236 KB n=500
58 Correct 7 ms 38324 KB n=500
59 Correct 19 ms 38492 KB n=2000
60 Correct 11 ms 38748 KB n=2000
61 Correct 11 ms 38488 KB n=2000
62 Correct 10 ms 38408 KB n=2000
63 Correct 23 ms 38496 KB n=2000
64 Correct 10 ms 38496 KB n=2000
65 Correct 24 ms 38744 KB n=2000
66 Correct 10 ms 38492 KB n=2000
67 Correct 16 ms 38488 KB n=2000
68 Correct 9 ms 38492 KB n=2000
69 Correct 9 ms 38492 KB n=2000
70 Correct 10 ms 38640 KB n=2000
71 Correct 9 ms 38492 KB n=2000
72 Correct 8 ms 38492 KB n=2000
73 Correct 9 ms 38488 KB n=2000
74 Correct 18 ms 38492 KB n=1844
75 Correct 9 ms 38752 KB n=2000
76 Correct 9 ms 38488 KB n=2000
77 Correct 9 ms 38488 KB n=2000
78 Correct 9 ms 38492 KB n=2000
79 Correct 15 ms 38492 KB n=2000
80 Correct 10 ms 38492 KB n=2000
81 Correct 9 ms 38492 KB n=2000
82 Correct 15 ms 38492 KB n=2000
83 Correct 9 ms 38492 KB n=2000
84 Correct 9 ms 38492 KB n=2000
85 Correct 10 ms 38492 KB n=2000
86 Correct 11 ms 38492 KB n=2000
87 Correct 9 ms 38508 KB n=2000
88 Correct 9 ms 38488 KB n=2000
89 Correct 9 ms 38492 KB n=2000
90 Correct 9 ms 38504 KB n=2000
91 Correct 9 ms 38492 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 7 ms 38232 KB n=5
2 Correct 7 ms 38336 KB n=100
3 Correct 7 ms 38236 KB n=100
4 Correct 8 ms 38488 KB n=100
5 Correct 7 ms 38236 KB n=100
6 Correct 7 ms 38232 KB n=100
7 Correct 8 ms 38232 KB n=100
8 Correct 7 ms 38236 KB n=100
9 Correct 7 ms 38236 KB n=100
10 Correct 7 ms 38236 KB n=100
11 Correct 7 ms 38232 KB n=100
12 Correct 8 ms 38504 KB n=100
13 Correct 7 ms 38232 KB n=100
14 Correct 7 ms 38232 KB n=100
15 Correct 7 ms 38236 KB n=100
16 Correct 8 ms 38236 KB n=100
17 Correct 7 ms 38232 KB n=100
18 Correct 7 ms 38236 KB n=100
19 Correct 7 ms 38236 KB n=100
20 Correct 7 ms 38236 KB n=100
21 Correct 8 ms 38492 KB n=100
22 Correct 7 ms 38356 KB n=100
23 Correct 8 ms 38352 KB n=100
24 Correct 7 ms 38236 KB n=100
25 Correct 7 ms 38232 KB n=100
26 Correct 7 ms 38236 KB n=12
27 Correct 7 ms 38232 KB n=100
28 Correct 8 ms 38236 KB n=500
29 Correct 8 ms 38236 KB n=500
30 Correct 7 ms 38236 KB n=500
31 Correct 7 ms 38236 KB n=500
32 Correct 8 ms 38236 KB n=500
33 Correct 7 ms 38236 KB n=500
34 Correct 8 ms 38236 KB n=500
35 Correct 8 ms 38236 KB n=500
36 Correct 7 ms 38232 KB n=500
37 Correct 7 ms 38236 KB n=500
38 Correct 7 ms 38232 KB n=500
39 Correct 7 ms 38236 KB n=500
40 Correct 7 ms 38208 KB n=500
41 Correct 8 ms 38236 KB n=500
42 Correct 7 ms 38232 KB n=500
43 Correct 7 ms 38232 KB n=500
44 Correct 8 ms 38236 KB n=500
45 Correct 7 ms 38236 KB n=500
46 Correct 7 ms 38236 KB n=500
47 Correct 7 ms 38236 KB n=500
48 Correct 9 ms 38236 KB n=500
49 Correct 7 ms 38236 KB n=500
50 Correct 7 ms 38232 KB n=500
51 Correct 8 ms 38236 KB n=500
52 Correct 8 ms 38236 KB n=500
53 Correct 7 ms 38236 KB n=500
54 Correct 7 ms 38236 KB n=500
55 Correct 7 ms 38236 KB n=278
56 Correct 7 ms 38232 KB n=500
57 Correct 8 ms 38236 KB n=500
58 Correct 7 ms 38324 KB n=500
59 Correct 19 ms 38492 KB n=2000
60 Correct 11 ms 38748 KB n=2000
61 Correct 11 ms 38488 KB n=2000
62 Correct 10 ms 38408 KB n=2000
63 Correct 23 ms 38496 KB n=2000
64 Correct 10 ms 38496 KB n=2000
65 Correct 24 ms 38744 KB n=2000
66 Correct 10 ms 38492 KB n=2000
67 Correct 16 ms 38488 KB n=2000
68 Correct 9 ms 38492 KB n=2000
69 Correct 9 ms 38492 KB n=2000
70 Correct 10 ms 38640 KB n=2000
71 Correct 9 ms 38492 KB n=2000
72 Correct 8 ms 38492 KB n=2000
73 Correct 9 ms 38488 KB n=2000
74 Correct 18 ms 38492 KB n=1844
75 Correct 9 ms 38752 KB n=2000
76 Correct 9 ms 38488 KB n=2000
77 Correct 9 ms 38488 KB n=2000
78 Correct 9 ms 38492 KB n=2000
79 Correct 15 ms 38492 KB n=2000
80 Correct 10 ms 38492 KB n=2000
81 Correct 9 ms 38492 KB n=2000
82 Correct 15 ms 38492 KB n=2000
83 Correct 9 ms 38492 KB n=2000
84 Correct 9 ms 38492 KB n=2000
85 Correct 10 ms 38492 KB n=2000
86 Correct 11 ms 38492 KB n=2000
87 Correct 9 ms 38508 KB n=2000
88 Correct 9 ms 38488 KB n=2000
89 Correct 9 ms 38492 KB n=2000
90 Correct 9 ms 38504 KB n=2000
91 Correct 9 ms 38492 KB n=2000
92 Execution timed out 4077 ms 67068 KB Time limit exceeded
93 Halted 0 ms 0 KB -