Submission #1034473

# Submission time Handle Problem Language Result Execution time Memory
1034473 2024-07-25T14:11:35 Z pera Birthday gift (IZhO18_treearray) C++17
100 / 100
664 ms 75740 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1 , LOG = 18;
int n , m , q , a[N] , d[N] , LCA[N] , up[N][LOG];
vector<int> g[N];
set<int> E[N] , O[N];
void dfs(int u , int p = 1){
   up[u][0] = p;
   d[u] = d[p] + 1;
   for(int x = 0;x < (int)g[u].size();x ++){
      if(g[u][x] != p){
         dfs(g[u][x] , u);
      }
   }
}
int lca(int u , int v){
   if(d[u] < d[v]){
      swap(u , v);
   }
   int s = d[u] - d[v];
   for(int bit = 0;bit < LOG;bit ++){
      if(s >> bit & 1){
         u = up[u][bit];
      }
   }
   if(u == v){
      return u;
   }
   for(int bit = LOG - 1;bit >= 0;bit --){
      if(up[u][bit] != up[v][bit]){
         u = up[u][bit];
         v = up[v][bit];
      }
   }
   return up[u][0];
}
int main(){
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   cin >> n >> m >> q;
   for(int x = 1;x < n;x ++){
      int u , v;
      cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
   }
   dfs(1);
   for(int bit = 1;bit < LOG;bit ++){
      for(int x = 1;x <= n;x ++){
         up[x][bit] = up[up[x][bit - 1]][bit - 1];
      }
   }
   for(int x = 1;x <= m;x ++){
      cin >> a[x];
      E[a[x]].insert(x);
      if(x > 1){
         LCA[x - 1] = lca(a[x] , a[x - 1]);
         O[LCA[x - 1]].insert(x - 1);
      }
   }
   for(int i = 1;i <= q;i ++){
      int type;
      cin >> type;
      if(type == 1){
         int pos , v;
         cin >> pos >> v;
         E[a[pos]].erase(pos);
         if(pos < m){
            O[LCA[pos]].erase(pos);
         }
         if(pos > 1){
            O[LCA[pos - 1]].erase(pos - 1);
         }
         a[pos] = v;
         E[a[pos]].insert(pos);
         if(pos < m){
            LCA[pos] = lca(a[pos] , a[pos + 1]);
            O[LCA[pos]].insert(pos);
         }
         if(pos > 1){
            LCA[pos - 1] = lca(a[pos - 1] , a[pos]);
            O[LCA[pos - 1]].insert(pos - 1);
         }
      }else{
         int l , r , v;
         cin >> l >> r >> v;
         auto it = E[v].lower_bound(l);
         if(it != E[v].end() && *it <= r){
            cout << *it << " " << *it << endl;
            continue;
         }
         it = O[v].lower_bound(l);
         if(it != O[v].end() && *it < r){
            cout << *it << " " << *it + 1 << endl;
            continue;
         }
         cout << -1 << " " << -1 << endl;
      }
   }
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB n=5
2 Correct 9 ms 23896 KB n=100
3 Correct 10 ms 23900 KB n=100
4 Correct 9 ms 23900 KB n=100
5 Correct 9 ms 23912 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 10 ms 23900 KB n=100
8 Correct 9 ms 23900 KB n=100
9 Correct 10 ms 23900 KB n=100
10 Correct 9 ms 23780 KB n=100
11 Correct 10 ms 23896 KB n=100
12 Correct 9 ms 23896 KB n=100
13 Correct 11 ms 23900 KB n=100
14 Correct 9 ms 23900 KB n=100
15 Correct 9 ms 23900 KB n=100
16 Correct 9 ms 23900 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 10 ms 23960 KB n=100
19 Correct 9 ms 23900 KB n=100
20 Correct 10 ms 23900 KB n=100
21 Correct 10 ms 23816 KB n=100
22 Correct 9 ms 23900 KB n=100
23 Correct 9 ms 23900 KB n=100
24 Correct 12 ms 24156 KB n=100
25 Correct 9 ms 23896 KB n=100
26 Correct 9 ms 23896 KB n=12
27 Correct 9 ms 23900 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB n=5
2 Correct 9 ms 23896 KB n=100
3 Correct 10 ms 23900 KB n=100
4 Correct 9 ms 23900 KB n=100
5 Correct 9 ms 23912 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 10 ms 23900 KB n=100
8 Correct 9 ms 23900 KB n=100
9 Correct 10 ms 23900 KB n=100
10 Correct 9 ms 23780 KB n=100
11 Correct 10 ms 23896 KB n=100
12 Correct 9 ms 23896 KB n=100
13 Correct 11 ms 23900 KB n=100
14 Correct 9 ms 23900 KB n=100
15 Correct 9 ms 23900 KB n=100
16 Correct 9 ms 23900 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 10 ms 23960 KB n=100
19 Correct 9 ms 23900 KB n=100
20 Correct 10 ms 23900 KB n=100
21 Correct 10 ms 23816 KB n=100
22 Correct 9 ms 23900 KB n=100
23 Correct 9 ms 23900 KB n=100
24 Correct 12 ms 24156 KB n=100
25 Correct 9 ms 23896 KB n=100
26 Correct 9 ms 23896 KB n=12
27 Correct 9 ms 23900 KB n=100
28 Correct 10 ms 23900 KB n=500
29 Correct 10 ms 23900 KB n=500
30 Correct 10 ms 23896 KB n=500
31 Correct 11 ms 23900 KB n=500
32 Correct 10 ms 23900 KB n=500
33 Correct 10 ms 23896 KB n=500
34 Correct 10 ms 23900 KB n=500
35 Correct 10 ms 23900 KB n=500
36 Correct 11 ms 23896 KB n=500
37 Correct 10 ms 23896 KB n=500
38 Correct 11 ms 23900 KB n=500
39 Correct 10 ms 23896 KB n=500
40 Correct 10 ms 23900 KB n=500
41 Correct 10 ms 23900 KB n=500
42 Correct 10 ms 23900 KB n=500
43 Correct 10 ms 23900 KB n=500
44 Correct 10 ms 23900 KB n=500
45 Correct 10 ms 23896 KB n=500
46 Correct 10 ms 23896 KB n=500
47 Correct 10 ms 23896 KB n=500
48 Correct 10 ms 23900 KB n=500
49 Correct 10 ms 23900 KB n=500
50 Correct 11 ms 23900 KB n=500
51 Correct 13 ms 24032 KB n=500
52 Correct 10 ms 23900 KB n=500
53 Correct 10 ms 23900 KB n=500
54 Correct 10 ms 23896 KB n=500
55 Correct 9 ms 23900 KB n=278
56 Correct 10 ms 23900 KB n=500
57 Correct 10 ms 23900 KB n=500
58 Correct 11 ms 24068 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB n=5
2 Correct 9 ms 23896 KB n=100
3 Correct 10 ms 23900 KB n=100
4 Correct 9 ms 23900 KB n=100
5 Correct 9 ms 23912 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 10 ms 23900 KB n=100
8 Correct 9 ms 23900 KB n=100
9 Correct 10 ms 23900 KB n=100
10 Correct 9 ms 23780 KB n=100
11 Correct 10 ms 23896 KB n=100
12 Correct 9 ms 23896 KB n=100
13 Correct 11 ms 23900 KB n=100
14 Correct 9 ms 23900 KB n=100
15 Correct 9 ms 23900 KB n=100
16 Correct 9 ms 23900 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 10 ms 23960 KB n=100
19 Correct 9 ms 23900 KB n=100
20 Correct 10 ms 23900 KB n=100
21 Correct 10 ms 23816 KB n=100
22 Correct 9 ms 23900 KB n=100
23 Correct 9 ms 23900 KB n=100
24 Correct 12 ms 24156 KB n=100
25 Correct 9 ms 23896 KB n=100
26 Correct 9 ms 23896 KB n=12
27 Correct 9 ms 23900 KB n=100
28 Correct 10 ms 23900 KB n=500
29 Correct 10 ms 23900 KB n=500
30 Correct 10 ms 23896 KB n=500
31 Correct 11 ms 23900 KB n=500
32 Correct 10 ms 23900 KB n=500
33 Correct 10 ms 23896 KB n=500
34 Correct 10 ms 23900 KB n=500
35 Correct 10 ms 23900 KB n=500
36 Correct 11 ms 23896 KB n=500
37 Correct 10 ms 23896 KB n=500
38 Correct 11 ms 23900 KB n=500
39 Correct 10 ms 23896 KB n=500
40 Correct 10 ms 23900 KB n=500
41 Correct 10 ms 23900 KB n=500
42 Correct 10 ms 23900 KB n=500
43 Correct 10 ms 23900 KB n=500
44 Correct 10 ms 23900 KB n=500
45 Correct 10 ms 23896 KB n=500
46 Correct 10 ms 23896 KB n=500
47 Correct 10 ms 23896 KB n=500
48 Correct 10 ms 23900 KB n=500
49 Correct 10 ms 23900 KB n=500
50 Correct 11 ms 23900 KB n=500
51 Correct 13 ms 24032 KB n=500
52 Correct 10 ms 23900 KB n=500
53 Correct 10 ms 23900 KB n=500
54 Correct 10 ms 23896 KB n=500
55 Correct 9 ms 23900 KB n=278
56 Correct 10 ms 23900 KB n=500
57 Correct 10 ms 23900 KB n=500
58 Correct 11 ms 24068 KB n=500
59 Correct 12 ms 24152 KB n=2000
60 Correct 12 ms 24412 KB n=2000
61 Correct 12 ms 24412 KB n=2000
62 Correct 12 ms 24152 KB n=2000
63 Correct 12 ms 24156 KB n=2000
64 Correct 13 ms 24412 KB n=2000
65 Correct 16 ms 24156 KB n=2000
66 Correct 12 ms 24408 KB n=2000
67 Correct 12 ms 24156 KB n=2000
68 Correct 12 ms 24412 KB n=2000
69 Correct 13 ms 24156 KB n=2000
70 Correct 12 ms 24156 KB n=2000
71 Correct 12 ms 24156 KB n=2000
72 Correct 14 ms 24240 KB n=2000
73 Correct 15 ms 24156 KB n=2000
74 Correct 13 ms 24156 KB n=1844
75 Correct 14 ms 24156 KB n=2000
76 Correct 13 ms 24156 KB n=2000
77 Correct 13 ms 24156 KB n=2000
78 Correct 13 ms 24156 KB n=2000
79 Correct 13 ms 24152 KB n=2000
80 Correct 12 ms 24412 KB n=2000
81 Correct 13 ms 24412 KB n=2000
82 Correct 12 ms 24380 KB n=2000
83 Correct 13 ms 24408 KB n=2000
84 Correct 12 ms 24152 KB n=2000
85 Correct 12 ms 24408 KB n=2000
86 Correct 13 ms 24156 KB n=2000
87 Correct 17 ms 24156 KB n=2000
88 Correct 12 ms 24412 KB n=2000
89 Correct 12 ms 24408 KB n=2000
90 Correct 13 ms 24412 KB n=2000
91 Correct 13 ms 24388 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB n=5
2 Correct 9 ms 23896 KB n=100
3 Correct 10 ms 23900 KB n=100
4 Correct 9 ms 23900 KB n=100
5 Correct 9 ms 23912 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 10 ms 23900 KB n=100
8 Correct 9 ms 23900 KB n=100
9 Correct 10 ms 23900 KB n=100
10 Correct 9 ms 23780 KB n=100
11 Correct 10 ms 23896 KB n=100
12 Correct 9 ms 23896 KB n=100
13 Correct 11 ms 23900 KB n=100
14 Correct 9 ms 23900 KB n=100
15 Correct 9 ms 23900 KB n=100
16 Correct 9 ms 23900 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 10 ms 23960 KB n=100
19 Correct 9 ms 23900 KB n=100
20 Correct 10 ms 23900 KB n=100
21 Correct 10 ms 23816 KB n=100
22 Correct 9 ms 23900 KB n=100
23 Correct 9 ms 23900 KB n=100
24 Correct 12 ms 24156 KB n=100
25 Correct 9 ms 23896 KB n=100
26 Correct 9 ms 23896 KB n=12
27 Correct 9 ms 23900 KB n=100
28 Correct 10 ms 23900 KB n=500
29 Correct 10 ms 23900 KB n=500
30 Correct 10 ms 23896 KB n=500
31 Correct 11 ms 23900 KB n=500
32 Correct 10 ms 23900 KB n=500
33 Correct 10 ms 23896 KB n=500
34 Correct 10 ms 23900 KB n=500
35 Correct 10 ms 23900 KB n=500
36 Correct 11 ms 23896 KB n=500
37 Correct 10 ms 23896 KB n=500
38 Correct 11 ms 23900 KB n=500
39 Correct 10 ms 23896 KB n=500
40 Correct 10 ms 23900 KB n=500
41 Correct 10 ms 23900 KB n=500
42 Correct 10 ms 23900 KB n=500
43 Correct 10 ms 23900 KB n=500
44 Correct 10 ms 23900 KB n=500
45 Correct 10 ms 23896 KB n=500
46 Correct 10 ms 23896 KB n=500
47 Correct 10 ms 23896 KB n=500
48 Correct 10 ms 23900 KB n=500
49 Correct 10 ms 23900 KB n=500
50 Correct 11 ms 23900 KB n=500
51 Correct 13 ms 24032 KB n=500
52 Correct 10 ms 23900 KB n=500
53 Correct 10 ms 23900 KB n=500
54 Correct 10 ms 23896 KB n=500
55 Correct 9 ms 23900 KB n=278
56 Correct 10 ms 23900 KB n=500
57 Correct 10 ms 23900 KB n=500
58 Correct 11 ms 24068 KB n=500
59 Correct 12 ms 24152 KB n=2000
60 Correct 12 ms 24412 KB n=2000
61 Correct 12 ms 24412 KB n=2000
62 Correct 12 ms 24152 KB n=2000
63 Correct 12 ms 24156 KB n=2000
64 Correct 13 ms 24412 KB n=2000
65 Correct 16 ms 24156 KB n=2000
66 Correct 12 ms 24408 KB n=2000
67 Correct 12 ms 24156 KB n=2000
68 Correct 12 ms 24412 KB n=2000
69 Correct 13 ms 24156 KB n=2000
70 Correct 12 ms 24156 KB n=2000
71 Correct 12 ms 24156 KB n=2000
72 Correct 14 ms 24240 KB n=2000
73 Correct 15 ms 24156 KB n=2000
74 Correct 13 ms 24156 KB n=1844
75 Correct 14 ms 24156 KB n=2000
76 Correct 13 ms 24156 KB n=2000
77 Correct 13 ms 24156 KB n=2000
78 Correct 13 ms 24156 KB n=2000
79 Correct 13 ms 24152 KB n=2000
80 Correct 12 ms 24412 KB n=2000
81 Correct 13 ms 24412 KB n=2000
82 Correct 12 ms 24380 KB n=2000
83 Correct 13 ms 24408 KB n=2000
84 Correct 12 ms 24152 KB n=2000
85 Correct 12 ms 24408 KB n=2000
86 Correct 13 ms 24156 KB n=2000
87 Correct 17 ms 24156 KB n=2000
88 Correct 12 ms 24412 KB n=2000
89 Correct 12 ms 24408 KB n=2000
90 Correct 13 ms 24412 KB n=2000
91 Correct 13 ms 24388 KB n=2000
92 Correct 535 ms 67168 KB n=200000
93 Correct 557 ms 71344 KB n=200000
94 Correct 562 ms 74312 KB n=200000
95 Correct 511 ms 67008 KB n=200000
96 Correct 470 ms 67268 KB n=200000
97 Correct 664 ms 69972 KB n=200000
98 Correct 522 ms 67268 KB n=200000
99 Correct 582 ms 66500 KB n=200000
100 Correct 515 ms 67272 KB n=200000
101 Correct 554 ms 75604 KB n=200000
102 Correct 440 ms 68180 KB n=200000
103 Correct 515 ms 68152 KB n=200000
104 Correct 429 ms 68004 KB n=200000
105 Correct 442 ms 67764 KB n=200000
106 Correct 436 ms 67816 KB n=200000
107 Correct 425 ms 67920 KB n=200000
108 Correct 508 ms 66896 KB n=200000
109 Correct 528 ms 66816 KB n=200000
110 Correct 525 ms 66896 KB n=200000
111 Correct 528 ms 67144 KB n=200000
112 Correct 560 ms 74616 KB n=200000
113 Correct 604 ms 69972 KB n=200000
114 Correct 518 ms 67012 KB n=200000
115 Correct 613 ms 67924 KB n=200000
116 Correct 483 ms 66968 KB n=200000
117 Correct 538 ms 74984 KB n=200000
118 Correct 640 ms 68948 KB n=200000
119 Correct 469 ms 67008 KB n=200000
120 Correct 524 ms 75344 KB n=200000
121 Correct 536 ms 75328 KB n=200000
122 Correct 495 ms 75740 KB n=200000
123 Correct 441 ms 67800 KB n=200000
124 Correct 130 ms 36872 KB n=25264