답안 #1030774

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1030774 2024-07-22T09:50:26 Z NeroZein Tourism (JOI23_tourism) C++17
100 / 100
890 ms 24416 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) {
    pp.push_back(*itr);
    if ((*itr)[0] < l) {
      psh.push_back({(*itr)[0], l - 1, (*itr)[2]});
    } 
    if ((*itr)[1] > r) {
      psh.push_back({r + 1, (*itr)[1], (*itr)[2]});
    }
  }
  itr++;
  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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5164 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 3 ms 5212 KB Output is correct
5 Correct 3 ms 5224 KB Output is correct
6 Correct 4 ms 5212 KB Output is correct
7 Correct 3 ms 5464 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5176 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 4 ms 5216 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 3 ms 5208 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 3 ms 5208 KB Output is correct
20 Correct 4 ms 5208 KB Output is correct
21 Correct 3 ms 5156 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 5208 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 3 ms 5212 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5164 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 3 ms 5212 KB Output is correct
5 Correct 3 ms 5224 KB Output is correct
6 Correct 4 ms 5212 KB Output is correct
7 Correct 3 ms 5464 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5176 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 4 ms 5216 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 3 ms 5208 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 3 ms 5208 KB Output is correct
20 Correct 4 ms 5208 KB Output is correct
21 Correct 3 ms 5156 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 5208 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 3 ms 5212 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 7 ms 5428 KB Output is correct
31 Correct 7 ms 5208 KB Output is correct
32 Correct 8 ms 5276 KB Output is correct
33 Correct 8 ms 5468 KB Output is correct
34 Correct 8 ms 5468 KB Output is correct
35 Correct 8 ms 5436 KB Output is correct
36 Correct 9 ms 5468 KB Output is correct
37 Correct 8 ms 5468 KB Output is correct
38 Correct 5 ms 5468 KB Output is correct
39 Correct 4 ms 5468 KB Output is correct
40 Correct 5 ms 5548 KB Output is correct
41 Correct 6 ms 5464 KB Output is correct
42 Correct 7 ms 5468 KB Output is correct
43 Correct 5 ms 5468 KB Output is correct
44 Correct 6 ms 5468 KB Output is correct
45 Correct 9 ms 5468 KB Output is correct
46 Correct 6 ms 5468 KB Output is correct
47 Correct 6 ms 5372 KB Output is correct
48 Correct 6 ms 5212 KB Output is correct
49 Correct 6 ms 5408 KB Output is correct
50 Correct 6 ms 5468 KB Output is correct
51 Correct 5 ms 5536 KB Output is correct
52 Correct 6 ms 5468 KB Output is correct
53 Correct 6 ms 5468 KB Output is correct
54 Correct 5 ms 5524 KB Output is correct
55 Correct 6 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 9 ms 5452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 3 ms 5208 KB Output is correct
4 Correct 108 ms 16980 KB Output is correct
5 Correct 84 ms 19540 KB Output is correct
6 Correct 112 ms 21328 KB Output is correct
7 Correct 134 ms 24144 KB Output is correct
8 Correct 143 ms 24380 KB Output is correct
9 Correct 130 ms 24236 KB Output is correct
10 Correct 140 ms 24188 KB Output is correct
11 Correct 156 ms 24316 KB Output is correct
12 Correct 143 ms 23632 KB Output is correct
13 Correct 142 ms 23632 KB Output is correct
14 Correct 145 ms 23748 KB Output is correct
15 Correct 35 ms 20948 KB Output is correct
16 Correct 130 ms 24416 KB Output is correct
17 Correct 50 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 274 ms 11796 KB Output is correct
3 Correct 411 ms 12628 KB Output is correct
4 Correct 345 ms 13140 KB Output is correct
5 Correct 504 ms 16784 KB Output is correct
6 Correct 524 ms 16712 KB Output is correct
7 Correct 546 ms 16724 KB Output is correct
8 Correct 516 ms 16860 KB Output is correct
9 Correct 526 ms 17060 KB Output is correct
10 Correct 523 ms 16464 KB Output is correct
11 Correct 484 ms 16208 KB Output is correct
12 Correct 534 ms 16604 KB Output is correct
13 Correct 540 ms 17172 KB Output is correct
14 Correct 507 ms 17652 KB Output is correct
15 Correct 533 ms 20820 KB Output is correct
16 Correct 506 ms 17492 KB Output is correct
17 Correct 495 ms 17492 KB Output is correct
18 Correct 498 ms 17480 KB Output is correct
19 Correct 315 ms 13136 KB Output is correct
20 Correct 349 ms 13136 KB Output is correct
21 Correct 325 ms 12920 KB Output is correct
22 Correct 345 ms 13136 KB Output is correct
23 Correct 333 ms 13136 KB Output is correct
24 Correct 350 ms 13140 KB Output is correct
25 Correct 336 ms 13140 KB Output is correct
26 Correct 364 ms 13140 KB Output is correct
27 Correct 347 ms 13000 KB Output is correct
28 Correct 334 ms 13120 KB Output is correct
29 Correct 343 ms 13140 KB Output is correct
30 Correct 318 ms 13396 KB Output is correct
31 Correct 336 ms 13812 KB Output is correct
32 Correct 359 ms 13984 KB Output is correct
33 Correct 387 ms 15552 KB Output is correct
34 Correct 355 ms 18000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5212 KB Output is correct
2 Correct 2 ms 5164 KB Output is correct
3 Correct 3 ms 5180 KB Output is correct
4 Correct 657 ms 15360 KB Output is correct
5 Correct 736 ms 15952 KB Output is correct
6 Correct 828 ms 18692 KB Output is correct
7 Correct 840 ms 20352 KB Output is correct
8 Correct 890 ms 20448 KB Output is correct
9 Correct 842 ms 20560 KB Output is correct
10 Correct 804 ms 19024 KB Output is correct
11 Correct 814 ms 18560 KB Output is correct
12 Correct 779 ms 18512 KB Output is correct
13 Correct 809 ms 18688 KB Output is correct
14 Correct 42 ms 8272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5164 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 3 ms 5212 KB Output is correct
5 Correct 3 ms 5224 KB Output is correct
6 Correct 4 ms 5212 KB Output is correct
7 Correct 3 ms 5464 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5176 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 4 ms 5216 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 3 ms 5208 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 3 ms 5208 KB Output is correct
20 Correct 4 ms 5208 KB Output is correct
21 Correct 3 ms 5156 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 5208 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 3 ms 5212 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 7 ms 5428 KB Output is correct
31 Correct 7 ms 5208 KB Output is correct
32 Correct 8 ms 5276 KB Output is correct
33 Correct 8 ms 5468 KB Output is correct
34 Correct 8 ms 5468 KB Output is correct
35 Correct 8 ms 5436 KB Output is correct
36 Correct 9 ms 5468 KB Output is correct
37 Correct 8 ms 5468 KB Output is correct
38 Correct 5 ms 5468 KB Output is correct
39 Correct 4 ms 5468 KB Output is correct
40 Correct 5 ms 5548 KB Output is correct
41 Correct 6 ms 5464 KB Output is correct
42 Correct 7 ms 5468 KB Output is correct
43 Correct 5 ms 5468 KB Output is correct
44 Correct 6 ms 5468 KB Output is correct
45 Correct 9 ms 5468 KB Output is correct
46 Correct 6 ms 5468 KB Output is correct
47 Correct 6 ms 5372 KB Output is correct
48 Correct 6 ms 5212 KB Output is correct
49 Correct 6 ms 5408 KB Output is correct
50 Correct 6 ms 5468 KB Output is correct
51 Correct 5 ms 5536 KB Output is correct
52 Correct 6 ms 5468 KB Output is correct
53 Correct 6 ms 5468 KB Output is correct
54 Correct 5 ms 5524 KB Output is correct
55 Correct 6 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 9 ms 5452 KB Output is correct
59 Correct 2 ms 5212 KB Output is correct
60 Correct 3 ms 5212 KB Output is correct
61 Correct 3 ms 5208 KB Output is correct
62 Correct 108 ms 16980 KB Output is correct
63 Correct 84 ms 19540 KB Output is correct
64 Correct 112 ms 21328 KB Output is correct
65 Correct 134 ms 24144 KB Output is correct
66 Correct 143 ms 24380 KB Output is correct
67 Correct 130 ms 24236 KB Output is correct
68 Correct 140 ms 24188 KB Output is correct
69 Correct 156 ms 24316 KB Output is correct
70 Correct 143 ms 23632 KB Output is correct
71 Correct 142 ms 23632 KB Output is correct
72 Correct 145 ms 23748 KB Output is correct
73 Correct 35 ms 20948 KB Output is correct
74 Correct 130 ms 24416 KB Output is correct
75 Correct 50 ms 9812 KB Output is correct
76 Correct 2 ms 5212 KB Output is correct
77 Correct 274 ms 11796 KB Output is correct
78 Correct 411 ms 12628 KB Output is correct
79 Correct 345 ms 13140 KB Output is correct
80 Correct 504 ms 16784 KB Output is correct
81 Correct 524 ms 16712 KB Output is correct
82 Correct 546 ms 16724 KB Output is correct
83 Correct 516 ms 16860 KB Output is correct
84 Correct 526 ms 17060 KB Output is correct
85 Correct 523 ms 16464 KB Output is correct
86 Correct 484 ms 16208 KB Output is correct
87 Correct 534 ms 16604 KB Output is correct
88 Correct 540 ms 17172 KB Output is correct
89 Correct 507 ms 17652 KB Output is correct
90 Correct 533 ms 20820 KB Output is correct
91 Correct 506 ms 17492 KB Output is correct
92 Correct 495 ms 17492 KB Output is correct
93 Correct 498 ms 17480 KB Output is correct
94 Correct 315 ms 13136 KB Output is correct
95 Correct 349 ms 13136 KB Output is correct
96 Correct 325 ms 12920 KB Output is correct
97 Correct 345 ms 13136 KB Output is correct
98 Correct 333 ms 13136 KB Output is correct
99 Correct 350 ms 13140 KB Output is correct
100 Correct 336 ms 13140 KB Output is correct
101 Correct 364 ms 13140 KB Output is correct
102 Correct 347 ms 13000 KB Output is correct
103 Correct 334 ms 13120 KB Output is correct
104 Correct 343 ms 13140 KB Output is correct
105 Correct 318 ms 13396 KB Output is correct
106 Correct 336 ms 13812 KB Output is correct
107 Correct 359 ms 13984 KB Output is correct
108 Correct 387 ms 15552 KB Output is correct
109 Correct 355 ms 18000 KB Output is correct
110 Correct 3 ms 5212 KB Output is correct
111 Correct 2 ms 5164 KB Output is correct
112 Correct 3 ms 5180 KB Output is correct
113 Correct 657 ms 15360 KB Output is correct
114 Correct 736 ms 15952 KB Output is correct
115 Correct 828 ms 18692 KB Output is correct
116 Correct 840 ms 20352 KB Output is correct
117 Correct 890 ms 20448 KB Output is correct
118 Correct 842 ms 20560 KB Output is correct
119 Correct 804 ms 19024 KB Output is correct
120 Correct 814 ms 18560 KB Output is correct
121 Correct 779 ms 18512 KB Output is correct
122 Correct 809 ms 18688 KB Output is correct
123 Correct 42 ms 8272 KB Output is correct
124 Correct 439 ms 20784 KB Output is correct
125 Correct 367 ms 18028 KB Output is correct
126 Correct 545 ms 21072 KB Output is correct
127 Correct 568 ms 21084 KB Output is correct
128 Correct 551 ms 20936 KB Output is correct
129 Correct 528 ms 21040 KB Output is correct
130 Correct 589 ms 21112 KB Output is correct
131 Correct 140 ms 23376 KB Output is correct
132 Correct 137 ms 24400 KB Output is correct
133 Correct 165 ms 20564 KB Output is correct
134 Correct 356 ms 17208 KB Output is correct
135 Correct 379 ms 17148 KB Output is correct
136 Correct 388 ms 17212 KB Output is correct
137 Correct 216 ms 22472 KB Output is correct
138 Correct 222 ms 22476 KB Output is correct
139 Correct 231 ms 22496 KB Output is correct
140 Correct 210 ms 22492 KB Output is correct
141 Correct 222 ms 22552 KB Output is correct
142 Correct 211 ms 22476 KB Output is correct
143 Correct 43 ms 13260 KB Output is correct
144 Correct 597 ms 20820 KB Output is correct