Submission #1030777

# Submission time Handle Problem Language Result Execution time Memory
1030777 2024-07-22T09:53:24 Z NeroZein Tourism (JOI23_tourism) C++17
100 / 100
873 ms 24420 KB
#include "bits/stdc++.h"
using namespace std;

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

const int N = 1e5 + 5; 

int c[N]; 
int bit[N];
int ans[N];
vector<int> g[N];
set<array<int, 3>> se; 
int idd, id[N], top[N];
int sz[N], pr[N], dep[N];
vector<pair<int, int>> qs[N];

void dfs(int v, int p) {
  sz[v] = 1; 
  for (int u : g[v]) {
    if (u == p) {
      continue; 
    }
    pr[u] = v;
    dep[u] = dep[v] + 1; 
    dfs(u, v);
    sz[v] += sz[u]; 
  }
}

void dfs_hld(int v, int p, int tp) {
  top[v] = tp;
  id[v] = ++idd; 
  int heavy = 0;
  for (int u : g[v]) {
    if (u != p && sz[u] > sz[heavy]) {
      heavy = u;
    }
  }
  if (heavy) {
    dfs_hld(heavy, v, tp);
  }
  for (int u : g[v]) {
    if (u != p && u != heavy) {
      dfs_hld(u, v, u); 
    }
  }
}

void upd(int i, int v) {
  while (i < N) {
    bit[i] += v;
    i += i & -i;
  }
}

int qry(int i) {
  int ret = 0;
  while (i) {
    ret += bit[i];
    i -= i & -i;
  }
  return ret;
}

int qry(int l, int r) {
  return qry(r) - qry(l - 1); 
}

void ins(int l, int r, int val) {
  vector<array<int, 3>> pp, psh;
  psh.push_back({l, r, val});
  auto itr = prev(se.upper_bound({l, INT_MAX, INT_MAX}));
  if ((*itr) [1] >= l) {
    if ((*itr)[0] < l) {
      psh.push_back({(*itr)[0], l - 1, (*itr)[2]});
    }
  }
  while (itr != se.end() && (*itr)[0] <= r) {
    pp.push_back(*itr);
    if ((*itr)[1] > r) {
      psh.push_back({r + 1, (*itr)[1], (*itr)[2]});
    }
    itr++;
  }
  for (auto [xl, xr, xv] : pp) {
    if (xv > 0) {
      upd(xv, -(xr - xl + 1));      
    }
    se.erase({xl, xr, xv});
  }
  for (auto [xl, xr, xv] : psh) {
    if (xv > 0) {
      upd(xv, xr - xl + 1);       
    }
    se.insert({xl, xr, xv});
  }
}

void upd(int u, int v, int val) {
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) {
      swap(u, v); 
    }
    ins(id[top[u]], id[u], val);
    u = pr[top[u]];
  }
  if (dep[u] < dep[v]) {
    swap(u, v);
  }
  ins(id[v], id[u], val);
}

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);
  dfs_hld(1, 1, 1);
  se.insert({1, n, 0});
  for (int i = 1; i <= m; ++i) {
    cin >> c[i]; 
  }
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    qs[r].push_back({l, i});
    if (r == 1) {
      ans[i] = 1;
    }
  }
  for (int r = 2; r <= m; ++r) {
    upd(c[r - 1], c[r], r - 1);
    upd(c[r], c[r], r); 
    for (auto [ql, qi] : qs[r]) {
      ans[qi] = qry(ql, r); 
    }
  }
  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5172 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 3 ms 5172 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5036 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5164 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5260 KB Output is correct
13 Correct 3 ms 5464 KB Output is correct
14 Correct 3 ms 5252 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5256 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5212 KB Output is correct
25 Correct 3 ms 5180 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5172 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 3 ms 5172 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5036 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5164 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5260 KB Output is correct
13 Correct 3 ms 5464 KB Output is correct
14 Correct 3 ms 5252 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5256 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5212 KB Output is correct
25 Correct 3 ms 5180 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 6 ms 5212 KB Output is correct
31 Correct 7 ms 5436 KB Output is correct
32 Correct 8 ms 5468 KB Output is correct
33 Correct 8 ms 5468 KB Output is correct
34 Correct 8 ms 5508 KB Output is correct
35 Correct 8 ms 5468 KB Output is correct
36 Correct 8 ms 5720 KB Output is correct
37 Correct 8 ms 5464 KB Output is correct
38 Correct 7 ms 5724 KB Output is correct
39 Correct 6 ms 5436 KB Output is correct
40 Correct 5 ms 5468 KB Output is correct
41 Correct 5 ms 5468 KB Output is correct
42 Correct 7 ms 5468 KB Output is correct
43 Correct 5 ms 5468 KB Output is correct
44 Correct 7 ms 5468 KB Output is correct
45 Correct 6 ms 5468 KB Output is correct
46 Correct 6 ms 5468 KB Output is correct
47 Correct 6 ms 5448 KB Output is correct
48 Correct 6 ms 5212 KB Output is correct
49 Correct 5 ms 5468 KB Output is correct
50 Correct 5 ms 5468 KB Output is correct
51 Correct 5 ms 5468 KB Output is correct
52 Correct 5 ms 5468 KB Output is correct
53 Correct 6 ms 5468 KB Output is correct
54 Correct 5 ms 5468 KB Output is correct
55 Correct 7 ms 5468 KB Output is correct
56 Correct 3 ms 5212 KB Output is correct
57 Correct 3 ms 5212 KB Output is correct
58 Correct 8 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 3 ms 5212 KB Output is correct
4 Correct 107 ms 16872 KB Output is correct
5 Correct 86 ms 19540 KB Output is correct
6 Correct 108 ms 21168 KB Output is correct
7 Correct 161 ms 24420 KB Output is correct
8 Correct 133 ms 24404 KB Output is correct
9 Correct 126 ms 24248 KB Output is correct
10 Correct 155 ms 24348 KB Output is correct
11 Correct 124 ms 24400 KB Output is correct
12 Correct 135 ms 23892 KB Output is correct
13 Correct 142 ms 23888 KB Output is correct
14 Correct 144 ms 23888 KB Output is correct
15 Correct 35 ms 20948 KB Output is correct
16 Correct 128 ms 23892 KB Output is correct
17 Correct 45 ms 9808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 259 ms 11604 KB Output is correct
3 Correct 482 ms 12712 KB Output is correct
4 Correct 330 ms 13140 KB Output is correct
5 Correct 498 ms 17044 KB Output is correct
6 Correct 518 ms 16976 KB Output is correct
7 Correct 578 ms 16980 KB Output is correct
8 Correct 506 ms 16976 KB Output is correct
9 Correct 481 ms 16720 KB Output is correct
10 Correct 545 ms 16840 KB Output is correct
11 Correct 496 ms 16916 KB Output is correct
12 Correct 514 ms 16980 KB Output is correct
13 Correct 527 ms 17484 KB Output is correct
14 Correct 521 ms 18328 KB Output is correct
15 Correct 535 ms 21852 KB Output is correct
16 Correct 481 ms 17340 KB Output is correct
17 Correct 495 ms 17488 KB Output is correct
18 Correct 516 ms 17492 KB Output is correct
19 Correct 335 ms 13176 KB Output is correct
20 Correct 318 ms 13136 KB Output is correct
21 Correct 351 ms 13136 KB Output is correct
22 Correct 340 ms 13136 KB Output is correct
23 Correct 329 ms 13140 KB Output is correct
24 Correct 342 ms 13088 KB Output is correct
25 Correct 328 ms 12944 KB Output is correct
26 Correct 366 ms 13036 KB Output is correct
27 Correct 343 ms 13136 KB Output is correct
28 Correct 355 ms 13152 KB Output is correct
29 Correct 357 ms 13136 KB Output is correct
30 Correct 331 ms 13224 KB Output is correct
31 Correct 304 ms 13644 KB Output is correct
32 Correct 303 ms 14164 KB Output is correct
33 Correct 316 ms 15440 KB Output is correct
34 Correct 310 ms 18000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 3 ms 5176 KB Output is correct
4 Correct 694 ms 16280 KB Output is correct
5 Correct 647 ms 16532 KB Output is correct
6 Correct 751 ms 19796 KB Output is correct
7 Correct 832 ms 21584 KB Output is correct
8 Correct 808 ms 21584 KB Output is correct
9 Correct 811 ms 21360 KB Output is correct
10 Correct 779 ms 21588 KB Output is correct
11 Correct 817 ms 21592 KB Output is correct
12 Correct 782 ms 21356 KB Output is correct
13 Correct 873 ms 21588 KB Output is correct
14 Correct 46 ms 9848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5172 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 3 ms 5172 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5036 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5164 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5260 KB Output is correct
13 Correct 3 ms 5464 KB Output is correct
14 Correct 3 ms 5252 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5256 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5212 KB Output is correct
25 Correct 3 ms 5180 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 6 ms 5212 KB Output is correct
31 Correct 7 ms 5436 KB Output is correct
32 Correct 8 ms 5468 KB Output is correct
33 Correct 8 ms 5468 KB Output is correct
34 Correct 8 ms 5508 KB Output is correct
35 Correct 8 ms 5468 KB Output is correct
36 Correct 8 ms 5720 KB Output is correct
37 Correct 8 ms 5464 KB Output is correct
38 Correct 7 ms 5724 KB Output is correct
39 Correct 6 ms 5436 KB Output is correct
40 Correct 5 ms 5468 KB Output is correct
41 Correct 5 ms 5468 KB Output is correct
42 Correct 7 ms 5468 KB Output is correct
43 Correct 5 ms 5468 KB Output is correct
44 Correct 7 ms 5468 KB Output is correct
45 Correct 6 ms 5468 KB Output is correct
46 Correct 6 ms 5468 KB Output is correct
47 Correct 6 ms 5448 KB Output is correct
48 Correct 6 ms 5212 KB Output is correct
49 Correct 5 ms 5468 KB Output is correct
50 Correct 5 ms 5468 KB Output is correct
51 Correct 5 ms 5468 KB Output is correct
52 Correct 5 ms 5468 KB Output is correct
53 Correct 6 ms 5468 KB Output is correct
54 Correct 5 ms 5468 KB Output is correct
55 Correct 7 ms 5468 KB Output is correct
56 Correct 3 ms 5212 KB Output is correct
57 Correct 3 ms 5212 KB Output is correct
58 Correct 8 ms 5504 KB Output is correct
59 Correct 2 ms 5212 KB Output is correct
60 Correct 2 ms 5212 KB Output is correct
61 Correct 3 ms 5212 KB Output is correct
62 Correct 107 ms 16872 KB Output is correct
63 Correct 86 ms 19540 KB Output is correct
64 Correct 108 ms 21168 KB Output is correct
65 Correct 161 ms 24420 KB Output is correct
66 Correct 133 ms 24404 KB Output is correct
67 Correct 126 ms 24248 KB Output is correct
68 Correct 155 ms 24348 KB Output is correct
69 Correct 124 ms 24400 KB Output is correct
70 Correct 135 ms 23892 KB Output is correct
71 Correct 142 ms 23888 KB Output is correct
72 Correct 144 ms 23888 KB Output is correct
73 Correct 35 ms 20948 KB Output is correct
74 Correct 128 ms 23892 KB Output is correct
75 Correct 45 ms 9808 KB Output is correct
76 Correct 2 ms 5208 KB Output is correct
77 Correct 259 ms 11604 KB Output is correct
78 Correct 482 ms 12712 KB Output is correct
79 Correct 330 ms 13140 KB Output is correct
80 Correct 498 ms 17044 KB Output is correct
81 Correct 518 ms 16976 KB Output is correct
82 Correct 578 ms 16980 KB Output is correct
83 Correct 506 ms 16976 KB Output is correct
84 Correct 481 ms 16720 KB Output is correct
85 Correct 545 ms 16840 KB Output is correct
86 Correct 496 ms 16916 KB Output is correct
87 Correct 514 ms 16980 KB Output is correct
88 Correct 527 ms 17484 KB Output is correct
89 Correct 521 ms 18328 KB Output is correct
90 Correct 535 ms 21852 KB Output is correct
91 Correct 481 ms 17340 KB Output is correct
92 Correct 495 ms 17488 KB Output is correct
93 Correct 516 ms 17492 KB Output is correct
94 Correct 335 ms 13176 KB Output is correct
95 Correct 318 ms 13136 KB Output is correct
96 Correct 351 ms 13136 KB Output is correct
97 Correct 340 ms 13136 KB Output is correct
98 Correct 329 ms 13140 KB Output is correct
99 Correct 342 ms 13088 KB Output is correct
100 Correct 328 ms 12944 KB Output is correct
101 Correct 366 ms 13036 KB Output is correct
102 Correct 343 ms 13136 KB Output is correct
103 Correct 355 ms 13152 KB Output is correct
104 Correct 357 ms 13136 KB Output is correct
105 Correct 331 ms 13224 KB Output is correct
106 Correct 304 ms 13644 KB Output is correct
107 Correct 303 ms 14164 KB Output is correct
108 Correct 316 ms 15440 KB Output is correct
109 Correct 310 ms 18000 KB Output is correct
110 Correct 2 ms 5212 KB Output is correct
111 Correct 2 ms 5212 KB Output is correct
112 Correct 3 ms 5176 KB Output is correct
113 Correct 694 ms 16280 KB Output is correct
114 Correct 647 ms 16532 KB Output is correct
115 Correct 751 ms 19796 KB Output is correct
116 Correct 832 ms 21584 KB Output is correct
117 Correct 808 ms 21584 KB Output is correct
118 Correct 811 ms 21360 KB Output is correct
119 Correct 779 ms 21588 KB Output is correct
120 Correct 817 ms 21592 KB Output is correct
121 Correct 782 ms 21356 KB Output is correct
122 Correct 873 ms 21588 KB Output is correct
123 Correct 46 ms 9848 KB Output is correct
124 Correct 471 ms 20564 KB Output is correct
125 Correct 377 ms 18260 KB Output is correct
126 Correct 603 ms 21060 KB Output is correct
127 Correct 563 ms 21116 KB Output is correct
128 Correct 575 ms 21152 KB Output is correct
129 Correct 538 ms 20980 KB Output is correct
130 Correct 520 ms 21072 KB Output is correct
131 Correct 143 ms 23380 KB Output is correct
132 Correct 159 ms 24400 KB Output is correct
133 Correct 144 ms 20560 KB Output is correct
134 Correct 400 ms 17248 KB Output is correct
135 Correct 366 ms 17072 KB Output is correct
136 Correct 344 ms 17156 KB Output is correct
137 Correct 213 ms 22468 KB Output is correct
138 Correct 284 ms 22480 KB Output is correct
139 Correct 234 ms 22472 KB Output is correct
140 Correct 210 ms 22540 KB Output is correct
141 Correct 208 ms 22472 KB Output is correct
142 Correct 242 ms 22548 KB Output is correct
143 Correct 39 ms 13168 KB Output is correct
144 Correct 508 ms 21112 KB Output is correct