Submission #948090

# Submission time Handle Problem Language Result Execution time Memory
948090 2024-03-17T15:04:56 Z nguyennh Birthday gift (IZhO18_treearray) C++14
100 / 100
743 ms 87124 KB
#include<bits/stdc++.h>
#define el '\n'
using namespace std ;

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

const int MN = 2e5 + 5;

vector<int> adj[MN];
int a[MN] , depth[MN] , up[MN][20];
set<int> pos[MN] , satis[MN];

void dfs(int u){
  for ( auto x : adj[u] ){
    if (x == up[u][0]) continue;
    depth[x] = depth[u] + 1;
    up[x][0] = u;
    for ( int i = 1 ; i < 19 ; i++ ) up[x][i] = up[up[x][i - 1]][i - 1];
    dfs(x);
  }
}

int lca(int u , int v){
  if (depth[u] != depth[v]){
    if (depth[u] < depth[v]) swap(u , v);
    int k = depth[u] - depth[v];
    for ( int i = 0 ; (1 << i) <= k ; i++ ){
      if (k >> i & 1) u = up[u][i];
    }
  }
  if (u == v) return u;
  int k = __lg(depth[u]);
  for ( int i = k ; i >= 0 ; i-- ){
    if (up[u][i] ^ up[v][i]){
      u = up[u][i];
      v = up[v][i];
    }
  }
  return up[u][0];
}

int32_t main (){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n , m , q;
  cin >> n >> m >> q;
  for ( int i = 1 ; i < n ; i++ ){
    int u , v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1);
  for ( int i = 1 ; i <= m ; i++ ) cin >> a[i];
  for ( int i = 1 ; i <= m ; i++ ){
    pos[a[i]].insert(i);
    if (i < m) satis[lca(a[i] , a[i + 1])].insert(i);
  }
  while(q--){
    int type;
    cin >> type;
    if (type == 2){
      int l , r , v;
      cin >> l >> r >> v;
      auto it = pos[v].lower_bound(l);
      if (it != pos[v].end() && *it <= r){
        cout << *it << " " << *it << el;
        continue;
      }
      auto it2 = satis[v].lower_bound(l);
      if (it2 != satis[v].end() && *it2 < r){
        cout << *it2 << " " << *it2 + 1 << el;
        continue;
      }
      cout << -1 << " " << -1 << el;
    }
    else {
      int p , v;
      cin >> p >> v;
      auto it = pos[a[p]].lower_bound(p);
      if (it != pos[a[p]].end() && *it == p) pos[a[p]].erase(it);
      pos[v].insert(p);
      if (p > 1){
        int x = lca(v , a[p - 1]) , y = lca(a[p] , a[p - 1]);
        if (x != y){
          auto it1 = satis[y].lower_bound(p - 1);
          if (it1 != satis[y].end() && *it1 == p - 1) satis[y].erase(it1);
          satis[x].insert(p - 1);
        }
      }
      if (p < m){
        int x = lca(v , a[p + 1]) , y = lca(a[p] , a[p + 1]);
        if (x != y){
          auto it1 = satis[y].lower_bound(p);
          if (it1 != satis[y].end() && *it1 == p) satis[y].erase(it1);
          satis[x].insert(p);
        }
      }
      a[p] = v;
    }
  }
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 26712 KB n=5
2 Correct 6 ms 26712 KB n=100
3 Correct 6 ms 26716 KB n=100
4 Correct 6 ms 26716 KB n=100
5 Correct 6 ms 26716 KB n=100
6 Correct 6 ms 26712 KB n=100
7 Correct 6 ms 26716 KB n=100
8 Correct 6 ms 26716 KB n=100
9 Correct 7 ms 26868 KB n=100
10 Correct 6 ms 26716 KB n=100
11 Correct 6 ms 26716 KB n=100
12 Correct 6 ms 26716 KB n=100
13 Correct 6 ms 26808 KB n=100
14 Correct 6 ms 26716 KB n=100
15 Correct 6 ms 26716 KB n=100
16 Correct 6 ms 26712 KB n=100
17 Correct 6 ms 26712 KB n=100
18 Correct 6 ms 26716 KB n=100
19 Correct 6 ms 26712 KB n=100
20 Correct 6 ms 26716 KB n=100
21 Correct 6 ms 26712 KB n=100
22 Correct 8 ms 26712 KB n=100
23 Correct 6 ms 26716 KB n=100
24 Correct 6 ms 26712 KB n=100
25 Correct 6 ms 26824 KB n=100
26 Correct 6 ms 26716 KB n=12
27 Correct 6 ms 26820 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 7 ms 26712 KB n=5
2 Correct 6 ms 26712 KB n=100
3 Correct 6 ms 26716 KB n=100
4 Correct 6 ms 26716 KB n=100
5 Correct 6 ms 26716 KB n=100
6 Correct 6 ms 26712 KB n=100
7 Correct 6 ms 26716 KB n=100
8 Correct 6 ms 26716 KB n=100
9 Correct 7 ms 26868 KB n=100
10 Correct 6 ms 26716 KB n=100
11 Correct 6 ms 26716 KB n=100
12 Correct 6 ms 26716 KB n=100
13 Correct 6 ms 26808 KB n=100
14 Correct 6 ms 26716 KB n=100
15 Correct 6 ms 26716 KB n=100
16 Correct 6 ms 26712 KB n=100
17 Correct 6 ms 26712 KB n=100
18 Correct 6 ms 26716 KB n=100
19 Correct 6 ms 26712 KB n=100
20 Correct 6 ms 26716 KB n=100
21 Correct 6 ms 26712 KB n=100
22 Correct 8 ms 26712 KB n=100
23 Correct 6 ms 26716 KB n=100
24 Correct 6 ms 26712 KB n=100
25 Correct 6 ms 26824 KB n=100
26 Correct 6 ms 26716 KB n=12
27 Correct 6 ms 26820 KB n=100
28 Correct 6 ms 26716 KB n=500
29 Correct 7 ms 26716 KB n=500
30 Correct 7 ms 26712 KB n=500
31 Correct 6 ms 26712 KB n=500
32 Correct 6 ms 26712 KB n=500
33 Correct 7 ms 26716 KB n=500
34 Correct 6 ms 26716 KB n=500
35 Correct 8 ms 26716 KB n=500
36 Correct 6 ms 26716 KB n=500
37 Correct 6 ms 26712 KB n=500
38 Correct 7 ms 26716 KB n=500
39 Correct 6 ms 26716 KB n=500
40 Correct 7 ms 26716 KB n=500
41 Correct 6 ms 26716 KB n=500
42 Correct 6 ms 26716 KB n=500
43 Correct 6 ms 26716 KB n=500
44 Correct 7 ms 26716 KB n=500
45 Correct 7 ms 26716 KB n=500
46 Correct 7 ms 26716 KB n=500
47 Correct 8 ms 26716 KB n=500
48 Correct 6 ms 26716 KB n=500
49 Correct 6 ms 26716 KB n=500
50 Correct 7 ms 26716 KB n=500
51 Correct 7 ms 26832 KB n=500
52 Correct 6 ms 26716 KB n=500
53 Correct 6 ms 26816 KB n=500
54 Correct 6 ms 26716 KB n=500
55 Correct 6 ms 26716 KB n=278
56 Correct 7 ms 26716 KB n=500
57 Correct 7 ms 26772 KB n=500
58 Correct 7 ms 26968 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 7 ms 26712 KB n=5
2 Correct 6 ms 26712 KB n=100
3 Correct 6 ms 26716 KB n=100
4 Correct 6 ms 26716 KB n=100
5 Correct 6 ms 26716 KB n=100
6 Correct 6 ms 26712 KB n=100
7 Correct 6 ms 26716 KB n=100
8 Correct 6 ms 26716 KB n=100
9 Correct 7 ms 26868 KB n=100
10 Correct 6 ms 26716 KB n=100
11 Correct 6 ms 26716 KB n=100
12 Correct 6 ms 26716 KB n=100
13 Correct 6 ms 26808 KB n=100
14 Correct 6 ms 26716 KB n=100
15 Correct 6 ms 26716 KB n=100
16 Correct 6 ms 26712 KB n=100
17 Correct 6 ms 26712 KB n=100
18 Correct 6 ms 26716 KB n=100
19 Correct 6 ms 26712 KB n=100
20 Correct 6 ms 26716 KB n=100
21 Correct 6 ms 26712 KB n=100
22 Correct 8 ms 26712 KB n=100
23 Correct 6 ms 26716 KB n=100
24 Correct 6 ms 26712 KB n=100
25 Correct 6 ms 26824 KB n=100
26 Correct 6 ms 26716 KB n=12
27 Correct 6 ms 26820 KB n=100
28 Correct 6 ms 26716 KB n=500
29 Correct 7 ms 26716 KB n=500
30 Correct 7 ms 26712 KB n=500
31 Correct 6 ms 26712 KB n=500
32 Correct 6 ms 26712 KB n=500
33 Correct 7 ms 26716 KB n=500
34 Correct 6 ms 26716 KB n=500
35 Correct 8 ms 26716 KB n=500
36 Correct 6 ms 26716 KB n=500
37 Correct 6 ms 26712 KB n=500
38 Correct 7 ms 26716 KB n=500
39 Correct 6 ms 26716 KB n=500
40 Correct 7 ms 26716 KB n=500
41 Correct 6 ms 26716 KB n=500
42 Correct 6 ms 26716 KB n=500
43 Correct 6 ms 26716 KB n=500
44 Correct 7 ms 26716 KB n=500
45 Correct 7 ms 26716 KB n=500
46 Correct 7 ms 26716 KB n=500
47 Correct 8 ms 26716 KB n=500
48 Correct 6 ms 26716 KB n=500
49 Correct 6 ms 26716 KB n=500
50 Correct 7 ms 26716 KB n=500
51 Correct 7 ms 26832 KB n=500
52 Correct 6 ms 26716 KB n=500
53 Correct 6 ms 26816 KB n=500
54 Correct 6 ms 26716 KB n=500
55 Correct 6 ms 26716 KB n=278
56 Correct 7 ms 26716 KB n=500
57 Correct 7 ms 26772 KB n=500
58 Correct 7 ms 26968 KB n=500
59 Correct 8 ms 26968 KB n=2000
60 Correct 8 ms 27224 KB n=2000
61 Correct 10 ms 26972 KB n=2000
62 Correct 8 ms 26972 KB n=2000
63 Correct 7 ms 26972 KB n=2000
64 Correct 9 ms 26972 KB n=2000
65 Correct 8 ms 26968 KB n=2000
66 Correct 8 ms 26972 KB n=2000
67 Correct 9 ms 26968 KB n=2000
68 Correct 8 ms 27176 KB n=2000
69 Correct 7 ms 26968 KB n=2000
70 Correct 8 ms 26972 KB n=2000
71 Correct 8 ms 26972 KB n=2000
72 Correct 8 ms 26972 KB n=2000
73 Correct 8 ms 26968 KB n=2000
74 Correct 9 ms 26972 KB n=1844
75 Correct 9 ms 26972 KB n=2000
76 Correct 8 ms 26968 KB n=2000
77 Correct 9 ms 26968 KB n=2000
78 Correct 8 ms 27068 KB n=2000
79 Correct 8 ms 26968 KB n=2000
80 Correct 8 ms 27096 KB n=2000
81 Correct 8 ms 26968 KB n=2000
82 Correct 8 ms 26972 KB n=2000
83 Correct 9 ms 27224 KB n=2000
84 Correct 8 ms 26972 KB n=2000
85 Correct 8 ms 26972 KB n=2000
86 Correct 9 ms 27160 KB n=2000
87 Correct 7 ms 26968 KB n=2000
88 Correct 8 ms 27228 KB n=2000
89 Correct 8 ms 27228 KB n=2000
90 Correct 8 ms 27228 KB n=2000
91 Correct 8 ms 26972 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 7 ms 26712 KB n=5
2 Correct 6 ms 26712 KB n=100
3 Correct 6 ms 26716 KB n=100
4 Correct 6 ms 26716 KB n=100
5 Correct 6 ms 26716 KB n=100
6 Correct 6 ms 26712 KB n=100
7 Correct 6 ms 26716 KB n=100
8 Correct 6 ms 26716 KB n=100
9 Correct 7 ms 26868 KB n=100
10 Correct 6 ms 26716 KB n=100
11 Correct 6 ms 26716 KB n=100
12 Correct 6 ms 26716 KB n=100
13 Correct 6 ms 26808 KB n=100
14 Correct 6 ms 26716 KB n=100
15 Correct 6 ms 26716 KB n=100
16 Correct 6 ms 26712 KB n=100
17 Correct 6 ms 26712 KB n=100
18 Correct 6 ms 26716 KB n=100
19 Correct 6 ms 26712 KB n=100
20 Correct 6 ms 26716 KB n=100
21 Correct 6 ms 26712 KB n=100
22 Correct 8 ms 26712 KB n=100
23 Correct 6 ms 26716 KB n=100
24 Correct 6 ms 26712 KB n=100
25 Correct 6 ms 26824 KB n=100
26 Correct 6 ms 26716 KB n=12
27 Correct 6 ms 26820 KB n=100
28 Correct 6 ms 26716 KB n=500
29 Correct 7 ms 26716 KB n=500
30 Correct 7 ms 26712 KB n=500
31 Correct 6 ms 26712 KB n=500
32 Correct 6 ms 26712 KB n=500
33 Correct 7 ms 26716 KB n=500
34 Correct 6 ms 26716 KB n=500
35 Correct 8 ms 26716 KB n=500
36 Correct 6 ms 26716 KB n=500
37 Correct 6 ms 26712 KB n=500
38 Correct 7 ms 26716 KB n=500
39 Correct 6 ms 26716 KB n=500
40 Correct 7 ms 26716 KB n=500
41 Correct 6 ms 26716 KB n=500
42 Correct 6 ms 26716 KB n=500
43 Correct 6 ms 26716 KB n=500
44 Correct 7 ms 26716 KB n=500
45 Correct 7 ms 26716 KB n=500
46 Correct 7 ms 26716 KB n=500
47 Correct 8 ms 26716 KB n=500
48 Correct 6 ms 26716 KB n=500
49 Correct 6 ms 26716 KB n=500
50 Correct 7 ms 26716 KB n=500
51 Correct 7 ms 26832 KB n=500
52 Correct 6 ms 26716 KB n=500
53 Correct 6 ms 26816 KB n=500
54 Correct 6 ms 26716 KB n=500
55 Correct 6 ms 26716 KB n=278
56 Correct 7 ms 26716 KB n=500
57 Correct 7 ms 26772 KB n=500
58 Correct 7 ms 26968 KB n=500
59 Correct 8 ms 26968 KB n=2000
60 Correct 8 ms 27224 KB n=2000
61 Correct 10 ms 26972 KB n=2000
62 Correct 8 ms 26972 KB n=2000
63 Correct 7 ms 26972 KB n=2000
64 Correct 9 ms 26972 KB n=2000
65 Correct 8 ms 26968 KB n=2000
66 Correct 8 ms 26972 KB n=2000
67 Correct 9 ms 26968 KB n=2000
68 Correct 8 ms 27176 KB n=2000
69 Correct 7 ms 26968 KB n=2000
70 Correct 8 ms 26972 KB n=2000
71 Correct 8 ms 26972 KB n=2000
72 Correct 8 ms 26972 KB n=2000
73 Correct 8 ms 26968 KB n=2000
74 Correct 9 ms 26972 KB n=1844
75 Correct 9 ms 26972 KB n=2000
76 Correct 8 ms 26968 KB n=2000
77 Correct 9 ms 26968 KB n=2000
78 Correct 8 ms 27068 KB n=2000
79 Correct 8 ms 26968 KB n=2000
80 Correct 8 ms 27096 KB n=2000
81 Correct 8 ms 26968 KB n=2000
82 Correct 8 ms 26972 KB n=2000
83 Correct 9 ms 27224 KB n=2000
84 Correct 8 ms 26972 KB n=2000
85 Correct 8 ms 26972 KB n=2000
86 Correct 9 ms 27160 KB n=2000
87 Correct 7 ms 26968 KB n=2000
88 Correct 8 ms 27228 KB n=2000
89 Correct 8 ms 27228 KB n=2000
90 Correct 8 ms 27228 KB n=2000
91 Correct 8 ms 26972 KB n=2000
92 Correct 429 ms 74572 KB n=200000
93 Correct 707 ms 80968 KB n=200000
94 Correct 674 ms 85348 KB n=200000
95 Correct 405 ms 74440 KB n=200000
96 Correct 358 ms 74424 KB n=200000
97 Correct 743 ms 79856 KB n=200000
98 Correct 416 ms 74484 KB n=200000
99 Correct 596 ms 74848 KB n=200000
100 Correct 447 ms 74436 KB n=200000
101 Correct 605 ms 87124 KB n=200000
102 Correct 321 ms 75672 KB n=200000
103 Correct 291 ms 75556 KB n=200000
104 Correct 291 ms 75604 KB n=200000
105 Correct 303 ms 75860 KB n=200000
106 Correct 313 ms 75896 KB n=200000
107 Correct 331 ms 76016 KB n=200000
108 Correct 502 ms 74380 KB n=200000
109 Correct 546 ms 74556 KB n=200000
110 Correct 522 ms 74516 KB n=200000
111 Correct 439 ms 74060 KB n=200000
112 Correct 683 ms 86028 KB n=200000
113 Correct 674 ms 80120 KB n=200000
114 Correct 394 ms 73928 KB n=200000
115 Correct 716 ms 76916 KB n=200000
116 Correct 366 ms 74436 KB n=200000
117 Correct 595 ms 86352 KB n=200000
118 Correct 657 ms 78416 KB n=200000
119 Correct 369 ms 74612 KB n=200000
120 Correct 618 ms 86220 KB n=200000
121 Correct 584 ms 86556 KB n=200000
122 Correct 596 ms 86612 KB n=200000
123 Correct 320 ms 75804 KB n=200000
124 Correct 115 ms 41748 KB n=25264