Submission #854360

# Submission time Handle Problem Language Result Execution time Memory
854360 2023-09-27T03:58:47 Z NeroZein Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 68208 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int LOG = 20; 
const int N = 2e5 + 5; 

int a[N]; 
int lc[N]; 
vector<int> g[N]; 
int dep[N], up[LOG][N]; 
set<int> lco[N], no[N]; 

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); 
  }
}

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][u];
}

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);
  }
  dfs(1, 1); 
  for (int i = 0; i < m; ++i) {
    cin >> a[i]; 
    no[a[i]].insert(i); 
  }
  for (int i = 0; i < m - 1; ++i) {
    lc[i] = lca(a[i], a[i + 1]); 
    lco[lc[i]].insert(i); 
  }
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int pos, v;
      cin >> pos >> v;
      pos--; 
      no[a[pos]].erase(pos);
      a[pos] = v; 
      no[a[pos]].insert(pos);
      if (pos < m) {
        lco[lc[pos]].erase(pos);
        lc[pos] = lca(a[pos], a[pos + 1]); 
        lco[lc[pos]].insert(pos);         
      }
      if (pos > 0) {
        lco[lc[pos - 1]].erase(pos - 1);
        lc[pos - 1] = lca(a[pos - 1], a[pos]); 
        lco[lc[pos - 1]].insert(pos - 1); 
      }
    } 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 39512 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 6 ms 39516 KB n=100
4 Correct 7 ms 39512 KB n=100
5 Correct 7 ms 39516 KB n=100
6 Correct 7 ms 39516 KB n=100
7 Correct 7 ms 39572 KB n=100
8 Correct 7 ms 39644 KB n=100
9 Correct 7 ms 39512 KB n=100
10 Correct 7 ms 39516 KB n=100
11 Correct 8 ms 39644 KB n=100
12 Correct 7 ms 39516 KB n=100
13 Correct 8 ms 39516 KB n=100
14 Correct 8 ms 39516 KB n=100
15 Correct 7 ms 39640 KB n=100
16 Correct 7 ms 39516 KB n=100
17 Correct 8 ms 39516 KB n=100
18 Correct 7 ms 39616 KB n=100
19 Correct 7 ms 39616 KB n=100
20 Correct 7 ms 39516 KB n=100
21 Correct 7 ms 39516 KB n=100
22 Correct 7 ms 39516 KB n=100
23 Correct 7 ms 39616 KB n=100
24 Correct 8 ms 39516 KB n=100
25 Correct 7 ms 39516 KB n=100
26 Correct 7 ms 39712 KB n=12
27 Correct 7 ms 39516 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 7 ms 39512 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 6 ms 39516 KB n=100
4 Correct 7 ms 39512 KB n=100
5 Correct 7 ms 39516 KB n=100
6 Correct 7 ms 39516 KB n=100
7 Correct 7 ms 39572 KB n=100
8 Correct 7 ms 39644 KB n=100
9 Correct 7 ms 39512 KB n=100
10 Correct 7 ms 39516 KB n=100
11 Correct 8 ms 39644 KB n=100
12 Correct 7 ms 39516 KB n=100
13 Correct 8 ms 39516 KB n=100
14 Correct 8 ms 39516 KB n=100
15 Correct 7 ms 39640 KB n=100
16 Correct 7 ms 39516 KB n=100
17 Correct 8 ms 39516 KB n=100
18 Correct 7 ms 39616 KB n=100
19 Correct 7 ms 39616 KB n=100
20 Correct 7 ms 39516 KB n=100
21 Correct 7 ms 39516 KB n=100
22 Correct 7 ms 39516 KB n=100
23 Correct 7 ms 39616 KB n=100
24 Correct 8 ms 39516 KB n=100
25 Correct 7 ms 39516 KB n=100
26 Correct 7 ms 39712 KB n=12
27 Correct 7 ms 39516 KB n=100
28 Correct 8 ms 39516 KB n=500
29 Correct 8 ms 39512 KB n=500
30 Correct 7 ms 39512 KB n=500
31 Correct 7 ms 39516 KB n=500
32 Correct 8 ms 39516 KB n=500
33 Correct 8 ms 39516 KB n=500
34 Correct 9 ms 39516 KB n=500
35 Correct 7 ms 39516 KB n=500
36 Correct 7 ms 39516 KB n=500
37 Correct 7 ms 39512 KB n=500
38 Correct 7 ms 39512 KB n=500
39 Correct 7 ms 39512 KB n=500
40 Correct 7 ms 39516 KB n=500
41 Correct 7 ms 39516 KB n=500
42 Correct 7 ms 39692 KB n=500
43 Correct 8 ms 39516 KB n=500
44 Correct 8 ms 39512 KB n=500
45 Correct 10 ms 39512 KB n=500
46 Correct 7 ms 39516 KB n=500
47 Correct 7 ms 39516 KB n=500
48 Correct 8 ms 39516 KB n=500
49 Correct 7 ms 39516 KB n=500
50 Correct 7 ms 39644 KB n=500
51 Correct 7 ms 39768 KB n=500
52 Correct 8 ms 39768 KB n=500
53 Correct 8 ms 39516 KB n=500
54 Correct 7 ms 39512 KB n=500
55 Correct 7 ms 39516 KB n=278
56 Correct 7 ms 39512 KB n=500
57 Correct 7 ms 39516 KB n=500
58 Correct 7 ms 39516 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 7 ms 39512 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 6 ms 39516 KB n=100
4 Correct 7 ms 39512 KB n=100
5 Correct 7 ms 39516 KB n=100
6 Correct 7 ms 39516 KB n=100
7 Correct 7 ms 39572 KB n=100
8 Correct 7 ms 39644 KB n=100
9 Correct 7 ms 39512 KB n=100
10 Correct 7 ms 39516 KB n=100
11 Correct 8 ms 39644 KB n=100
12 Correct 7 ms 39516 KB n=100
13 Correct 8 ms 39516 KB n=100
14 Correct 8 ms 39516 KB n=100
15 Correct 7 ms 39640 KB n=100
16 Correct 7 ms 39516 KB n=100
17 Correct 8 ms 39516 KB n=100
18 Correct 7 ms 39616 KB n=100
19 Correct 7 ms 39616 KB n=100
20 Correct 7 ms 39516 KB n=100
21 Correct 7 ms 39516 KB n=100
22 Correct 7 ms 39516 KB n=100
23 Correct 7 ms 39616 KB n=100
24 Correct 8 ms 39516 KB n=100
25 Correct 7 ms 39516 KB n=100
26 Correct 7 ms 39712 KB n=12
27 Correct 7 ms 39516 KB n=100
28 Correct 8 ms 39516 KB n=500
29 Correct 8 ms 39512 KB n=500
30 Correct 7 ms 39512 KB n=500
31 Correct 7 ms 39516 KB n=500
32 Correct 8 ms 39516 KB n=500
33 Correct 8 ms 39516 KB n=500
34 Correct 9 ms 39516 KB n=500
35 Correct 7 ms 39516 KB n=500
36 Correct 7 ms 39516 KB n=500
37 Correct 7 ms 39512 KB n=500
38 Correct 7 ms 39512 KB n=500
39 Correct 7 ms 39512 KB n=500
40 Correct 7 ms 39516 KB n=500
41 Correct 7 ms 39516 KB n=500
42 Correct 7 ms 39692 KB n=500
43 Correct 8 ms 39516 KB n=500
44 Correct 8 ms 39512 KB n=500
45 Correct 10 ms 39512 KB n=500
46 Correct 7 ms 39516 KB n=500
47 Correct 7 ms 39516 KB n=500
48 Correct 8 ms 39516 KB n=500
49 Correct 7 ms 39516 KB n=500
50 Correct 7 ms 39644 KB n=500
51 Correct 7 ms 39768 KB n=500
52 Correct 8 ms 39768 KB n=500
53 Correct 8 ms 39516 KB n=500
54 Correct 7 ms 39512 KB n=500
55 Correct 7 ms 39516 KB n=278
56 Correct 7 ms 39512 KB n=500
57 Correct 7 ms 39516 KB n=500
58 Correct 7 ms 39516 KB n=500
59 Correct 18 ms 39772 KB n=2000
60 Correct 9 ms 40028 KB n=2000
61 Correct 9 ms 39768 KB n=2000
62 Correct 9 ms 39772 KB n=2000
63 Correct 23 ms 39772 KB n=2000
64 Correct 9 ms 39772 KB n=2000
65 Correct 24 ms 39908 KB n=2000
66 Correct 9 ms 39772 KB n=2000
67 Correct 15 ms 39768 KB n=2000
68 Correct 9 ms 39772 KB n=2000
69 Correct 9 ms 40024 KB n=2000
70 Correct 9 ms 39772 KB n=2000
71 Correct 8 ms 39772 KB n=2000
72 Correct 10 ms 39772 KB n=2000
73 Correct 9 ms 39772 KB n=2000
74 Correct 17 ms 39768 KB n=1844
75 Correct 10 ms 39772 KB n=2000
76 Correct 9 ms 39908 KB n=2000
77 Correct 9 ms 39772 KB n=2000
78 Correct 9 ms 39768 KB n=2000
79 Correct 15 ms 39768 KB n=2000
80 Correct 9 ms 40028 KB n=2000
81 Correct 9 ms 39768 KB n=2000
82 Correct 16 ms 39772 KB n=2000
83 Correct 9 ms 40000 KB n=2000
84 Correct 10 ms 39772 KB n=2000
85 Correct 9 ms 39768 KB n=2000
86 Correct 9 ms 39768 KB n=2000
87 Correct 9 ms 39772 KB n=2000
88 Correct 8 ms 39932 KB n=2000
89 Correct 9 ms 40056 KB n=2000
90 Correct 8 ms 40028 KB n=2000
91 Correct 8 ms 39880 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 7 ms 39512 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 6 ms 39516 KB n=100
4 Correct 7 ms 39512 KB n=100
5 Correct 7 ms 39516 KB n=100
6 Correct 7 ms 39516 KB n=100
7 Correct 7 ms 39572 KB n=100
8 Correct 7 ms 39644 KB n=100
9 Correct 7 ms 39512 KB n=100
10 Correct 7 ms 39516 KB n=100
11 Correct 8 ms 39644 KB n=100
12 Correct 7 ms 39516 KB n=100
13 Correct 8 ms 39516 KB n=100
14 Correct 8 ms 39516 KB n=100
15 Correct 7 ms 39640 KB n=100
16 Correct 7 ms 39516 KB n=100
17 Correct 8 ms 39516 KB n=100
18 Correct 7 ms 39616 KB n=100
19 Correct 7 ms 39616 KB n=100
20 Correct 7 ms 39516 KB n=100
21 Correct 7 ms 39516 KB n=100
22 Correct 7 ms 39516 KB n=100
23 Correct 7 ms 39616 KB n=100
24 Correct 8 ms 39516 KB n=100
25 Correct 7 ms 39516 KB n=100
26 Correct 7 ms 39712 KB n=12
27 Correct 7 ms 39516 KB n=100
28 Correct 8 ms 39516 KB n=500
29 Correct 8 ms 39512 KB n=500
30 Correct 7 ms 39512 KB n=500
31 Correct 7 ms 39516 KB n=500
32 Correct 8 ms 39516 KB n=500
33 Correct 8 ms 39516 KB n=500
34 Correct 9 ms 39516 KB n=500
35 Correct 7 ms 39516 KB n=500
36 Correct 7 ms 39516 KB n=500
37 Correct 7 ms 39512 KB n=500
38 Correct 7 ms 39512 KB n=500
39 Correct 7 ms 39512 KB n=500
40 Correct 7 ms 39516 KB n=500
41 Correct 7 ms 39516 KB n=500
42 Correct 7 ms 39692 KB n=500
43 Correct 8 ms 39516 KB n=500
44 Correct 8 ms 39512 KB n=500
45 Correct 10 ms 39512 KB n=500
46 Correct 7 ms 39516 KB n=500
47 Correct 7 ms 39516 KB n=500
48 Correct 8 ms 39516 KB n=500
49 Correct 7 ms 39516 KB n=500
50 Correct 7 ms 39644 KB n=500
51 Correct 7 ms 39768 KB n=500
52 Correct 8 ms 39768 KB n=500
53 Correct 8 ms 39516 KB n=500
54 Correct 7 ms 39512 KB n=500
55 Correct 7 ms 39516 KB n=278
56 Correct 7 ms 39512 KB n=500
57 Correct 7 ms 39516 KB n=500
58 Correct 7 ms 39516 KB n=500
59 Correct 18 ms 39772 KB n=2000
60 Correct 9 ms 40028 KB n=2000
61 Correct 9 ms 39768 KB n=2000
62 Correct 9 ms 39772 KB n=2000
63 Correct 23 ms 39772 KB n=2000
64 Correct 9 ms 39772 KB n=2000
65 Correct 24 ms 39908 KB n=2000
66 Correct 9 ms 39772 KB n=2000
67 Correct 15 ms 39768 KB n=2000
68 Correct 9 ms 39772 KB n=2000
69 Correct 9 ms 40024 KB n=2000
70 Correct 9 ms 39772 KB n=2000
71 Correct 8 ms 39772 KB n=2000
72 Correct 10 ms 39772 KB n=2000
73 Correct 9 ms 39772 KB n=2000
74 Correct 17 ms 39768 KB n=1844
75 Correct 10 ms 39772 KB n=2000
76 Correct 9 ms 39908 KB n=2000
77 Correct 9 ms 39772 KB n=2000
78 Correct 9 ms 39768 KB n=2000
79 Correct 15 ms 39768 KB n=2000
80 Correct 9 ms 40028 KB n=2000
81 Correct 9 ms 39768 KB n=2000
82 Correct 16 ms 39772 KB n=2000
83 Correct 9 ms 40000 KB n=2000
84 Correct 10 ms 39772 KB n=2000
85 Correct 9 ms 39768 KB n=2000
86 Correct 9 ms 39768 KB n=2000
87 Correct 9 ms 39772 KB n=2000
88 Correct 8 ms 39932 KB n=2000
89 Correct 9 ms 40056 KB n=2000
90 Correct 8 ms 40028 KB n=2000
91 Correct 8 ms 39880 KB n=2000
92 Execution timed out 4100 ms 68208 KB Time limit exceeded
93 Halted 0 ms 0 KB -