Submission #1030130

# Submission time Handle Problem Language Result Execution time Memory
1030130 2024-07-21T21:02:31 Z pera Tourism (JOI23_tourism) C++17
100 / 100
617 ms 23120 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
int n , m , q , T = 0;
vector<int> t(N) , C(N) , id(N) , ans(N) , sz(N) , d(N) , up(N) , top(N);
vector<vector<int>> g(N);
vector<vector<pair<int , int>>> query(N);
set<tuple<int , int , int>> S;
void update(int u , int x){
   while(u < N){
      t[u] += x;
      u += u&-u;
   }
}
int get(int u){
   int sum = 0;
   while(u > 0){
      sum += t[u];
      u -= u&-u;
   }
   return sum;
}
void dfs(int u , int p = 1){
   if(u != 1){
      g[u].erase(find(g[u].begin() , g[u].end() , p));
   }
   up[u] = p;
   d[u] = d[p] + 1;
   for(int x = 0;x < (int)g[u].size();x ++){
      dfs(g[u][x] , u);
      sz[u] += sz[g[u][x]];
      if(sz[g[u][0]] < sz[g[u][x]]){
         swap(g[u][0] , g[u][x]);
      }
   }
   ++sz[u];
}
void hld(int u){
   id[u] = ++T;
   for(int x = 0;x < (int)g[u].size();x ++){
      top[g[u][x]] = (x == 0 ? top[u] : g[u][x]);
      hld(g[u][x]);
   }
}
void upd(int l , int r , int x){
   //cout << "make " << l << " " << r << " " << x << endl;
   while(true){
      auto it = S.lower_bound({l , -1 , -1});
      if(it == S.end() || get<0>(*it) < l || get<1>(*it) > r){
         break;
      }
      int L = get<1>(*it) , R = get<0>(*it) , v = get<2>(*it);
      //cout << "xx " << L << " " << R << " " << v << endl;
      S.erase(it);
      update(v , -(min(r , R) - max(L , l) + 1));
      if(L < l){
         S.insert({l - 1 , L , v});
      }
      if(r < R){
         S.insert({R , r + 1 , v});
      }
   }
   S.insert({r , l , x});
   update(x , r - l + 1);
}
void UPD(int u , int v , int x){
   while(top[u] != top[v]){
      //cout << u << " da " << v << endl;
      if(d[top[u]] < d[top[v]]){
         swap(u , v);
      }
      upd(id[top[u]] , id[u] , x);
      u = up[top[u]];
   }
  //cout << u << " das " << v << endl;
   if(d[u] > d[v]){
      swap(u , v);
   }
   upd(id[u] , id[v] , x);
}
int main(){
   cin >> n >> m >> q;
   for(int i = 1;i < n;i ++){
      int u , v;
      cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
   }
   dfs(1);
   top[1] = 1;
   hld(1);
   for(int i = 1;i <= m;i ++){
      cin >> C[i];
   }
   for(int i = 1;i <= q;i ++){
      int l , r;
      cin >> l >> r;
      query[r].push_back({l , i});
   }
   for(int i = 1;i <= m;i ++){
      if(i > 1){
         //cout << C[i - 1] << " -> " << C[i] << endl;
         UPD(C[i - 1] , C[i] , i - 1);
      }
      upd(id[C[i]] , id[C[i]] , i);
      for(auto [l , id] : query[i]){
         ans[id] = get(N - 1) - get(l - 1);
      }
   }
   for(int i = 1;i <= q;i ++){
      cout << ans[i] << " ";
   }
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8280 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 3 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 3 ms 8296 KB Output is correct
6 Correct 3 ms 8284 KB Output is correct
7 Correct 3 ms 8288 KB Output is correct
8 Correct 5 ms 8288 KB Output is correct
9 Correct 4 ms 8280 KB Output is correct
10 Correct 4 ms 8280 KB Output is correct
11 Correct 4 ms 8536 KB Output is correct
12 Correct 4 ms 8284 KB Output is correct
13 Correct 3 ms 8284 KB Output is correct
14 Correct 4 ms 8284 KB Output is correct
15 Correct 3 ms 8288 KB Output is correct
16 Correct 3 ms 8284 KB Output is correct
17 Correct 4 ms 8288 KB Output is correct
18 Correct 3 ms 8296 KB Output is correct
19 Correct 4 ms 8264 KB Output is correct
20 Correct 4 ms 8284 KB Output is correct
21 Correct 4 ms 8284 KB Output is correct
22 Correct 3 ms 8284 KB Output is correct
23 Correct 4 ms 8092 KB Output is correct
24 Correct 4 ms 8284 KB Output is correct
25 Correct 3 ms 8284 KB Output is correct
26 Correct 4 ms 8284 KB Output is correct
27 Correct 3 ms 8284 KB Output is correct
28 Correct 3 ms 8284 KB Output is correct
29 Correct 4 ms 8280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8280 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 3 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 3 ms 8296 KB Output is correct
6 Correct 3 ms 8284 KB Output is correct
7 Correct 3 ms 8288 KB Output is correct
8 Correct 5 ms 8288 KB Output is correct
9 Correct 4 ms 8280 KB Output is correct
10 Correct 4 ms 8280 KB Output is correct
11 Correct 4 ms 8536 KB Output is correct
12 Correct 4 ms 8284 KB Output is correct
13 Correct 3 ms 8284 KB Output is correct
14 Correct 4 ms 8284 KB Output is correct
15 Correct 3 ms 8288 KB Output is correct
16 Correct 3 ms 8284 KB Output is correct
17 Correct 4 ms 8288 KB Output is correct
18 Correct 3 ms 8296 KB Output is correct
19 Correct 4 ms 8264 KB Output is correct
20 Correct 4 ms 8284 KB Output is correct
21 Correct 4 ms 8284 KB Output is correct
22 Correct 3 ms 8284 KB Output is correct
23 Correct 4 ms 8092 KB Output is correct
24 Correct 4 ms 8284 KB Output is correct
25 Correct 3 ms 8284 KB Output is correct
26 Correct 4 ms 8284 KB Output is correct
27 Correct 3 ms 8284 KB Output is correct
28 Correct 3 ms 8284 KB Output is correct
29 Correct 4 ms 8280 KB Output is correct
30 Correct 7 ms 8284 KB Output is correct
31 Correct 7 ms 8268 KB Output is correct
32 Correct 8 ms 8284 KB Output is correct
33 Correct 8 ms 8500 KB Output is correct
34 Correct 8 ms 8276 KB Output is correct
35 Correct 8 ms 8284 KB Output is correct
36 Correct 8 ms 8284 KB Output is correct
37 Correct 8 ms 8284 KB Output is correct
38 Correct 6 ms 8284 KB Output is correct
39 Correct 6 ms 8284 KB Output is correct
40 Correct 7 ms 8284 KB Output is correct
41 Correct 6 ms 8288 KB Output is correct
42 Correct 6 ms 8284 KB Output is correct
43 Correct 6 ms 8284 KB Output is correct
44 Correct 7 ms 8292 KB Output is correct
45 Correct 7 ms 8284 KB Output is correct
46 Correct 7 ms 8300 KB Output is correct
47 Correct 7 ms 8284 KB Output is correct
48 Correct 7 ms 8284 KB Output is correct
49 Correct 7 ms 8284 KB Output is correct
50 Correct 6 ms 8284 KB Output is correct
51 Correct 6 ms 8408 KB Output is correct
52 Correct 6 ms 8280 KB Output is correct
53 Correct 6 ms 8292 KB Output is correct
54 Correct 8 ms 8540 KB Output is correct
55 Correct 6 ms 8284 KB Output is correct
56 Correct 5 ms 8284 KB Output is correct
57 Correct 4 ms 8368 KB Output is correct
58 Correct 8 ms 8280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 4 ms 8192 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 110 ms 17280 KB Output is correct
5 Correct 98 ms 19280 KB Output is correct
6 Correct 119 ms 20708 KB Output is correct
7 Correct 147 ms 22864 KB Output is correct
8 Correct 148 ms 23020 KB Output is correct
9 Correct 150 ms 22864 KB Output is correct
10 Correct 146 ms 23120 KB Output is correct
11 Correct 146 ms 22868 KB Output is correct
12 Correct 147 ms 22356 KB Output is correct
13 Correct 144 ms 22436 KB Output is correct
14 Correct 148 ms 22352 KB Output is correct
15 Correct 65 ms 20180 KB Output is correct
16 Correct 141 ms 22620 KB Output is correct
17 Correct 65 ms 11604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8284 KB Output is correct
2 Correct 204 ms 12836 KB Output is correct
3 Correct 293 ms 13596 KB Output is correct
4 Correct 257 ms 13860 KB Output is correct
5 Correct 390 ms 16472 KB Output is correct
6 Correct 395 ms 16468 KB Output is correct
7 Correct 377 ms 16508 KB Output is correct
8 Correct 374 ms 16468 KB Output is correct
9 Correct 399 ms 16464 KB Output is correct
10 Correct 402 ms 16724 KB Output is correct
11 Correct 360 ms 16348 KB Output is correct
12 Correct 400 ms 16620 KB Output is correct
13 Correct 380 ms 16976 KB Output is correct
14 Correct 419 ms 17828 KB Output is correct
15 Correct 402 ms 20820 KB Output is correct
16 Correct 387 ms 16980 KB Output is correct
17 Correct 380 ms 16980 KB Output is correct
18 Correct 365 ms 16944 KB Output is correct
19 Correct 253 ms 13392 KB Output is correct
20 Correct 278 ms 13652 KB Output is correct
21 Correct 260 ms 13396 KB Output is correct
22 Correct 247 ms 13648 KB Output is correct
23 Correct 250 ms 13444 KB Output is correct
24 Correct 245 ms 13388 KB Output is correct
25 Correct 249 ms 13392 KB Output is correct
26 Correct 282 ms 13460 KB Output is correct
27 Correct 253 ms 13660 KB Output is correct
28 Correct 262 ms 13396 KB Output is correct
29 Correct 246 ms 13388 KB Output is correct
30 Correct 261 ms 13648 KB Output is correct
31 Correct 256 ms 13904 KB Output is correct
32 Correct 266 ms 14416 KB Output is correct
33 Correct 279 ms 15696 KB Output is correct
34 Correct 299 ms 18012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8280 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 484 ms 17056 KB Output is correct
5 Correct 506 ms 16980 KB Output is correct
6 Correct 600 ms 19284 KB Output is correct
7 Correct 585 ms 20560 KB Output is correct
8 Correct 587 ms 20564 KB Output is correct
9 Correct 617 ms 20580 KB Output is correct
10 Correct 609 ms 20656 KB Output is correct
11 Correct 597 ms 20440 KB Output is correct
12 Correct 580 ms 20560 KB Output is correct
13 Correct 611 ms 20532 KB Output is correct
14 Correct 67 ms 11584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8280 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 3 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 3 ms 8296 KB Output is correct
6 Correct 3 ms 8284 KB Output is correct
7 Correct 3 ms 8288 KB Output is correct
8 Correct 5 ms 8288 KB Output is correct
9 Correct 4 ms 8280 KB Output is correct
10 Correct 4 ms 8280 KB Output is correct
11 Correct 4 ms 8536 KB Output is correct
12 Correct 4 ms 8284 KB Output is correct
13 Correct 3 ms 8284 KB Output is correct
14 Correct 4 ms 8284 KB Output is correct
15 Correct 3 ms 8288 KB Output is correct
16 Correct 3 ms 8284 KB Output is correct
17 Correct 4 ms 8288 KB Output is correct
18 Correct 3 ms 8296 KB Output is correct
19 Correct 4 ms 8264 KB Output is correct
20 Correct 4 ms 8284 KB Output is correct
21 Correct 4 ms 8284 KB Output is correct
22 Correct 3 ms 8284 KB Output is correct
23 Correct 4 ms 8092 KB Output is correct
24 Correct 4 ms 8284 KB Output is correct
25 Correct 3 ms 8284 KB Output is correct
26 Correct 4 ms 8284 KB Output is correct
27 Correct 3 ms 8284 KB Output is correct
28 Correct 3 ms 8284 KB Output is correct
29 Correct 4 ms 8280 KB Output is correct
30 Correct 7 ms 8284 KB Output is correct
31 Correct 7 ms 8268 KB Output is correct
32 Correct 8 ms 8284 KB Output is correct
33 Correct 8 ms 8500 KB Output is correct
34 Correct 8 ms 8276 KB Output is correct
35 Correct 8 ms 8284 KB Output is correct
36 Correct 8 ms 8284 KB Output is correct
37 Correct 8 ms 8284 KB Output is correct
38 Correct 6 ms 8284 KB Output is correct
39 Correct 6 ms 8284 KB Output is correct
40 Correct 7 ms 8284 KB Output is correct
41 Correct 6 ms 8288 KB Output is correct
42 Correct 6 ms 8284 KB Output is correct
43 Correct 6 ms 8284 KB Output is correct
44 Correct 7 ms 8292 KB Output is correct
45 Correct 7 ms 8284 KB Output is correct
46 Correct 7 ms 8300 KB Output is correct
47 Correct 7 ms 8284 KB Output is correct
48 Correct 7 ms 8284 KB Output is correct
49 Correct 7 ms 8284 KB Output is correct
50 Correct 6 ms 8284 KB Output is correct
51 Correct 6 ms 8408 KB Output is correct
52 Correct 6 ms 8280 KB Output is correct
53 Correct 6 ms 8292 KB Output is correct
54 Correct 8 ms 8540 KB Output is correct
55 Correct 6 ms 8284 KB Output is correct
56 Correct 5 ms 8284 KB Output is correct
57 Correct 4 ms 8368 KB Output is correct
58 Correct 8 ms 8280 KB Output is correct
59 Correct 3 ms 8284 KB Output is correct
60 Correct 4 ms 8192 KB Output is correct
61 Correct 4 ms 8284 KB Output is correct
62 Correct 110 ms 17280 KB Output is correct
63 Correct 98 ms 19280 KB Output is correct
64 Correct 119 ms 20708 KB Output is correct
65 Correct 147 ms 22864 KB Output is correct
66 Correct 148 ms 23020 KB Output is correct
67 Correct 150 ms 22864 KB Output is correct
68 Correct 146 ms 23120 KB Output is correct
69 Correct 146 ms 22868 KB Output is correct
70 Correct 147 ms 22356 KB Output is correct
71 Correct 144 ms 22436 KB Output is correct
72 Correct 148 ms 22352 KB Output is correct
73 Correct 65 ms 20180 KB Output is correct
74 Correct 141 ms 22620 KB Output is correct
75 Correct 65 ms 11604 KB Output is correct
76 Correct 3 ms 8284 KB Output is correct
77 Correct 204 ms 12836 KB Output is correct
78 Correct 293 ms 13596 KB Output is correct
79 Correct 257 ms 13860 KB Output is correct
80 Correct 390 ms 16472 KB Output is correct
81 Correct 395 ms 16468 KB Output is correct
82 Correct 377 ms 16508 KB Output is correct
83 Correct 374 ms 16468 KB Output is correct
84 Correct 399 ms 16464 KB Output is correct
85 Correct 402 ms 16724 KB Output is correct
86 Correct 360 ms 16348 KB Output is correct
87 Correct 400 ms 16620 KB Output is correct
88 Correct 380 ms 16976 KB Output is correct
89 Correct 419 ms 17828 KB Output is correct
90 Correct 402 ms 20820 KB Output is correct
91 Correct 387 ms 16980 KB Output is correct
92 Correct 380 ms 16980 KB Output is correct
93 Correct 365 ms 16944 KB Output is correct
94 Correct 253 ms 13392 KB Output is correct
95 Correct 278 ms 13652 KB Output is correct
96 Correct 260 ms 13396 KB Output is correct
97 Correct 247 ms 13648 KB Output is correct
98 Correct 250 ms 13444 KB Output is correct
99 Correct 245 ms 13388 KB Output is correct
100 Correct 249 ms 13392 KB Output is correct
101 Correct 282 ms 13460 KB Output is correct
102 Correct 253 ms 13660 KB Output is correct
103 Correct 262 ms 13396 KB Output is correct
104 Correct 246 ms 13388 KB Output is correct
105 Correct 261 ms 13648 KB Output is correct
106 Correct 256 ms 13904 KB Output is correct
107 Correct 266 ms 14416 KB Output is correct
108 Correct 279 ms 15696 KB Output is correct
109 Correct 299 ms 18012 KB Output is correct
110 Correct 3 ms 8280 KB Output is correct
111 Correct 3 ms 8284 KB Output is correct
112 Correct 4 ms 8284 KB Output is correct
113 Correct 484 ms 17056 KB Output is correct
114 Correct 506 ms 16980 KB Output is correct
115 Correct 600 ms 19284 KB Output is correct
116 Correct 585 ms 20560 KB Output is correct
117 Correct 587 ms 20564 KB Output is correct
118 Correct 617 ms 20580 KB Output is correct
119 Correct 609 ms 20656 KB Output is correct
120 Correct 597 ms 20440 KB Output is correct
121 Correct 580 ms 20560 KB Output is correct
122 Correct 611 ms 20532 KB Output is correct
123 Correct 67 ms 11584 KB Output is correct
124 Correct 378 ms 19708 KB Output is correct
125 Correct 287 ms 17548 KB Output is correct
126 Correct 448 ms 20232 KB Output is correct
127 Correct 469 ms 20056 KB Output is correct
128 Correct 407 ms 20048 KB Output is correct
129 Correct 461 ms 20196 KB Output is correct
130 Correct 411 ms 20048 KB Output is correct
131 Correct 164 ms 22220 KB Output is correct
132 Correct 151 ms 22868 KB Output is correct
133 Correct 150 ms 19792 KB Output is correct
134 Correct 316 ms 17240 KB Output is correct
135 Correct 310 ms 17236 KB Output is correct
136 Correct 319 ms 17048 KB Output is correct
137 Correct 199 ms 21192 KB Output is correct
138 Correct 200 ms 21136 KB Output is correct
139 Correct 197 ms 21196 KB Output is correct
140 Correct 199 ms 21028 KB Output is correct
141 Correct 199 ms 21044 KB Output is correct
142 Correct 203 ms 20940 KB Output is correct
143 Correct 70 ms 14036 KB Output is correct
144 Correct 421 ms 19796 KB Output is correct