답안 #1107702

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1107702 2024-11-02T02:27:03 Z abczz Tourism (JOI23_tourism) C++17
100 / 100
382 ms 57348 KB
#include <iostream>
#include <array>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#define ll long long
 
using namespace std;
 
vector <ll> adj[100000];
vector <array<ll, 2>> Q[100000];
ll bl[100000][18];
ll n, m, q, a, b, cnt, sz[100000], A[100000], D[100000], G[100000], pos[100000], X[100000];
vector <ll> V[100000];
 
ll tot = 0;
struct BIT{
  ll st[100050];
  void build(){
    memset(st, 0, sizeof(st));
  }
  void update(ll id, ll w) {
    if (!id) return;
    tot += w;
    for (; id<100050; id+=id&-id) st[id] += w;
  }
  ll query(ll id) {
    ll ret = 0;
    for (; id; id-=id&-id) ret += st[id];
    return ret;
  }
}bit;
struct MPSegment{
  map <ll, array<ll, 2>> mp;
  vector <ll> lazy;
  void init(ll sz) {
    mp[0] = {sz-1, -1};
  }
  void update(ll id, ll l, ll r, ll w) {
    auto it = mp.lower_bound(l);
    if (it != mp.begin()) {
      it = prev(it);
      bit.update(it->second[1]+1, -(it->second[0]-(l-1)));
      mp[it->first] = {l-1, it->second[1]};
    }
    it = mp.lower_bound(l);
    while (it != mp.end()) {
      auto nx = next(it);
      if (it->first > r) break;
      if (it->second[0] <= r) {
        bit.update(it->second[1]+1, -(it->second[0]-it->first+1));
        mp.erase(it);
      }
      else {
        bit.update(it->second[1]+1, -(r+1-it->first));
        mp[r+1] = {it->second[0], it->second[1]};
        mp.erase(it);
        break;
      }
      it = nx;
    }
    mp[l] = {r, w};
    bit.update(w+1, r-l+1);
  }
}ST[100000];
ll lca(ll a, ll b) {
  if (a == -1) return b;
  else if (b == -1) return a;
  if (D[a] > D[b]) swap(a, b);
  ll db = D[b];
  for (int j=17; j>=0; --j) {
    if (db-(1LL<<j) >= D[a]) {
      db -= (1LL<<j);
      b = bl[b][j];
    }
  }
  for (int j=17; j>=0; --j) {
    if (bl[a][j] != bl[b][j]) {
      a = bl[a][j], b = bl[b][j];
    }
  }
  if (a != b) return bl[a][0];
  else return a;
}
struct LCATree{
  ll st[400000];
  void build(ll id, ll l, ll r) {
    if (l == r) {
      st[id] = lca(A[l], A[l+1]);
      return;
    }
    ll mid = (l+r)/2;
    build(id*2, l, mid);
    build(id*2+1, mid+1, r);
    st[id] = lca(st[id*2], st[id*2+1]);
  }
  ll query(ll id, ll l, ll r, ll ql, ll qr) {
    if (qr < l || r < ql) return -1;
    else if (ql <= l && r <= qr) return st[id];
    ll mid = (l+r)/2;
    return lca(query(id*2, l, mid, ql, qr), query(id*2+1, mid+1, r, ql, qr));
  }
}LCAST;
void dfs(ll u, ll p) {
  sz[u] = 1;
  for (auto v : adj[u]) {
    if (v == p) continue;
    D[v] = D[u] + 1;
    bl[v][0] = u;
    sz[u] += sz[v];
    dfs(v, u);
  }
}
void hld(ll u, ll p, ll k) {
  G[u] = k, pos[u] = V[k].size();
  V[k].push_back(u);
  ll mx = -1, id = -1;
  for (auto v : adj[u]) {
    if (v == p) continue;
    if (sz[v] > mx) mx = sz[v], id = v;
  }
  if (id != -1) hld(id, u, k);
  for (auto v : adj[u]) {
    if (v == p || v == id) continue;
    X[cnt+1] = u;
    hld(v, u, ++cnt);
  }
}
void jump(ll u, ll v, ll w) {
  if (G[u] == G[v]) {
    ST[G[u]].update(1, pos[v], pos[u], w);
    return;
  }
  ST[G[u]].update(1, 0, pos[u], w);
  jump(X[G[u]], v, w);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> n >> m >> q;
  bit.build();
  for (int i=0; i<n-1; ++i) {
    cin >> a >> b;
    --a, --b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(0, -1);
  hld(0, -1, 0);
  for (int j=1; j<18; ++j) {
    for (int i=0; i<n; ++i) {
      bl[i][j] = bl[bl[i][j-1]][j-1];
    }
  }
  for (int i=0; i<=cnt; ++i) {
    ST[i].init(V[i].size());
  }
  vector <ll> F(q, 0);
  for (int i=0; i<m; ++i) {
    cin >> A[i];
    --A[i];
  }
  if (m > 1) LCAST.build(1, 0, m-2);
  for (int i=0; i<q; ++i) {
    cin >> a >> b;
    --a, --b;
    Q[b].push_back({a, i});
  }
  for (int i=0; i<m; ++i) {
    jump(A[i], 0, i);
    for (auto [x, y] : Q[i]) {
      if (x == i) {
        F[y] = 1;
        continue;
      }
      F[y] = tot - bit.query(x) - (m == 1 ? 0 : D[LCAST.query(1, 0, m-2, x, i-1)]);
    }
  }
  for (int i=0; i<q; ++i) cout << F[i] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 20560 KB Output is correct
2 Correct 4 ms 20560 KB Output is correct
3 Correct 4 ms 20560 KB Output is correct
4 Correct 5 ms 20560 KB Output is correct
5 Correct 5 ms 20560 KB Output is correct
6 Correct 6 ms 20592 KB Output is correct
7 Correct 5 ms 20560 KB Output is correct
8 Correct 5 ms 20560 KB Output is correct
9 Correct 4 ms 20560 KB Output is correct
10 Correct 5 ms 20560 KB Output is correct
11 Correct 4 ms 20760 KB Output is correct
12 Correct 5 ms 20560 KB Output is correct
13 Correct 5 ms 20816 KB Output is correct
14 Correct 5 ms 20560 KB Output is correct
15 Correct 5 ms 20560 KB Output is correct
16 Correct 5 ms 20560 KB Output is correct
17 Correct 5 ms 20560 KB Output is correct
18 Correct 5 ms 20560 KB Output is correct
19 Correct 5 ms 20560 KB Output is correct
20 Correct 5 ms 20748 KB Output is correct
21 Correct 5 ms 20560 KB Output is correct
22 Correct 5 ms 20660 KB Output is correct
23 Correct 5 ms 20560 KB Output is correct
24 Correct 4 ms 20552 KB Output is correct
25 Correct 5 ms 20560 KB Output is correct
26 Correct 5 ms 20728 KB Output is correct
27 Correct 5 ms 22608 KB Output is correct
28 Correct 4 ms 20560 KB Output is correct
29 Correct 5 ms 20560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 20560 KB Output is correct
2 Correct 4 ms 20560 KB Output is correct
3 Correct 4 ms 20560 KB Output is correct
4 Correct 5 ms 20560 KB Output is correct
5 Correct 5 ms 20560 KB Output is correct
6 Correct 6 ms 20592 KB Output is correct
7 Correct 5 ms 20560 KB Output is correct
8 Correct 5 ms 20560 KB Output is correct
9 Correct 4 ms 20560 KB Output is correct
10 Correct 5 ms 20560 KB Output is correct
11 Correct 4 ms 20760 KB Output is correct
12 Correct 5 ms 20560 KB Output is correct
13 Correct 5 ms 20816 KB Output is correct
14 Correct 5 ms 20560 KB Output is correct
15 Correct 5 ms 20560 KB Output is correct
16 Correct 5 ms 20560 KB Output is correct
17 Correct 5 ms 20560 KB Output is correct
18 Correct 5 ms 20560 KB Output is correct
19 Correct 5 ms 20560 KB Output is correct
20 Correct 5 ms 20748 KB Output is correct
21 Correct 5 ms 20560 KB Output is correct
22 Correct 5 ms 20660 KB Output is correct
23 Correct 5 ms 20560 KB Output is correct
24 Correct 4 ms 20552 KB Output is correct
25 Correct 5 ms 20560 KB Output is correct
26 Correct 5 ms 20728 KB Output is correct
27 Correct 5 ms 22608 KB Output is correct
28 Correct 4 ms 20560 KB Output is correct
29 Correct 5 ms 20560 KB Output is correct
30 Correct 7 ms 20900 KB Output is correct
31 Correct 8 ms 20816 KB Output is correct
32 Correct 8 ms 21072 KB Output is correct
33 Correct 10 ms 20888 KB Output is correct
34 Correct 8 ms 21072 KB Output is correct
35 Correct 9 ms 21072 KB Output is correct
36 Correct 9 ms 21012 KB Output is correct
37 Correct 8 ms 21072 KB Output is correct
38 Correct 7 ms 21032 KB Output is correct
39 Correct 8 ms 21072 KB Output is correct
40 Correct 7 ms 21072 KB Output is correct
41 Correct 8 ms 21072 KB Output is correct
42 Correct 8 ms 20876 KB Output is correct
43 Correct 9 ms 21072 KB Output is correct
44 Correct 8 ms 20816 KB Output is correct
45 Correct 8 ms 20816 KB Output is correct
46 Correct 8 ms 20816 KB Output is correct
47 Correct 8 ms 20816 KB Output is correct
48 Correct 8 ms 20928 KB Output is correct
49 Correct 9 ms 20984 KB Output is correct
50 Correct 8 ms 21072 KB Output is correct
51 Correct 7 ms 21072 KB Output is correct
52 Correct 7 ms 21072 KB Output is correct
53 Correct 8 ms 21072 KB Output is correct
54 Correct 7 ms 21072 KB Output is correct
55 Correct 7 ms 21084 KB Output is correct
56 Correct 8 ms 22608 KB Output is correct
57 Correct 5 ms 20816 KB Output is correct
58 Correct 7 ms 20944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 20560 KB Output is correct
2 Correct 5 ms 22608 KB Output is correct
3 Correct 7 ms 22864 KB Output is correct
4 Correct 203 ms 43464 KB Output is correct
5 Correct 176 ms 50884 KB Output is correct
6 Correct 182 ms 52156 KB Output is correct
7 Correct 282 ms 55740 KB Output is correct
8 Correct 279 ms 55744 KB Output is correct
9 Correct 289 ms 55740 KB Output is correct
10 Correct 282 ms 55748 KB Output is correct
11 Correct 283 ms 55724 KB Output is correct
12 Correct 240 ms 55740 KB Output is correct
13 Correct 251 ms 55484 KB Output is correct
14 Correct 248 ms 55392 KB Output is correct
15 Correct 55 ms 52032 KB Output is correct
16 Correct 127 ms 55484 KB Output is correct
17 Correct 144 ms 29664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 20560 KB Output is correct
2 Correct 96 ms 38616 KB Output is correct
3 Correct 136 ms 39496 KB Output is correct
4 Correct 116 ms 42568 KB Output is correct
5 Correct 198 ms 48968 KB Output is correct
6 Correct 184 ms 49024 KB Output is correct
7 Correct 176 ms 48968 KB Output is correct
8 Correct 195 ms 49148 KB Output is correct
9 Correct 192 ms 49072 KB Output is correct
10 Correct 172 ms 49192 KB Output is correct
11 Correct 188 ms 49224 KB Output is correct
12 Correct 179 ms 49236 KB Output is correct
13 Correct 180 ms 49484 KB Output is correct
14 Correct 184 ms 50616 KB Output is correct
15 Correct 200 ms 54344 KB Output is correct
16 Correct 198 ms 49744 KB Output is correct
17 Correct 209 ms 49660 KB Output is correct
18 Correct 202 ms 49616 KB Output is correct
19 Correct 157 ms 43492 KB Output is correct
20 Correct 165 ms 43640 KB Output is correct
21 Correct 159 ms 43660 KB Output is correct
22 Correct 165 ms 43848 KB Output is correct
23 Correct 157 ms 43596 KB Output is correct
24 Correct 166 ms 43848 KB Output is correct
25 Correct 176 ms 43592 KB Output is correct
26 Correct 172 ms 43488 KB Output is correct
27 Correct 159 ms 43660 KB Output is correct
28 Correct 167 ms 43704 KB Output is correct
29 Correct 175 ms 43592 KB Output is correct
30 Correct 147 ms 43900 KB Output is correct
31 Correct 169 ms 44188 KB Output is correct
32 Correct 172 ms 44616 KB Output is correct
33 Correct 170 ms 46152 KB Output is correct
34 Correct 162 ms 48980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 20560 KB Output is correct
2 Correct 4 ms 22608 KB Output is correct
3 Correct 8 ms 22864 KB Output is correct
4 Correct 282 ms 44116 KB Output is correct
5 Correct 269 ms 44104 KB Output is correct
6 Correct 330 ms 52560 KB Output is correct
7 Correct 338 ms 54856 KB Output is correct
8 Correct 339 ms 54684 KB Output is correct
9 Correct 382 ms 54820 KB Output is correct
10 Correct 332 ms 54856 KB Output is correct
11 Correct 359 ms 54860 KB Output is correct
12 Correct 345 ms 54856 KB Output is correct
13 Correct 357 ms 54840 KB Output is correct
14 Correct 162 ms 29768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 20560 KB Output is correct
2 Correct 4 ms 20560 KB Output is correct
3 Correct 4 ms 20560 KB Output is correct
4 Correct 5 ms 20560 KB Output is correct
5 Correct 5 ms 20560 KB Output is correct
6 Correct 6 ms 20592 KB Output is correct
7 Correct 5 ms 20560 KB Output is correct
8 Correct 5 ms 20560 KB Output is correct
9 Correct 4 ms 20560 KB Output is correct
10 Correct 5 ms 20560 KB Output is correct
11 Correct 4 ms 20760 KB Output is correct
12 Correct 5 ms 20560 KB Output is correct
13 Correct 5 ms 20816 KB Output is correct
14 Correct 5 ms 20560 KB Output is correct
15 Correct 5 ms 20560 KB Output is correct
16 Correct 5 ms 20560 KB Output is correct
17 Correct 5 ms 20560 KB Output is correct
18 Correct 5 ms 20560 KB Output is correct
19 Correct 5 ms 20560 KB Output is correct
20 Correct 5 ms 20748 KB Output is correct
21 Correct 5 ms 20560 KB Output is correct
22 Correct 5 ms 20660 KB Output is correct
23 Correct 5 ms 20560 KB Output is correct
24 Correct 4 ms 20552 KB Output is correct
25 Correct 5 ms 20560 KB Output is correct
26 Correct 5 ms 20728 KB Output is correct
27 Correct 5 ms 22608 KB Output is correct
28 Correct 4 ms 20560 KB Output is correct
29 Correct 5 ms 20560 KB Output is correct
30 Correct 7 ms 20900 KB Output is correct
31 Correct 8 ms 20816 KB Output is correct
32 Correct 8 ms 21072 KB Output is correct
33 Correct 10 ms 20888 KB Output is correct
34 Correct 8 ms 21072 KB Output is correct
35 Correct 9 ms 21072 KB Output is correct
36 Correct 9 ms 21012 KB Output is correct
37 Correct 8 ms 21072 KB Output is correct
38 Correct 7 ms 21032 KB Output is correct
39 Correct 8 ms 21072 KB Output is correct
40 Correct 7 ms 21072 KB Output is correct
41 Correct 8 ms 21072 KB Output is correct
42 Correct 8 ms 20876 KB Output is correct
43 Correct 9 ms 21072 KB Output is correct
44 Correct 8 ms 20816 KB Output is correct
45 Correct 8 ms 20816 KB Output is correct
46 Correct 8 ms 20816 KB Output is correct
47 Correct 8 ms 20816 KB Output is correct
48 Correct 8 ms 20928 KB Output is correct
49 Correct 9 ms 20984 KB Output is correct
50 Correct 8 ms 21072 KB Output is correct
51 Correct 7 ms 21072 KB Output is correct
52 Correct 7 ms 21072 KB Output is correct
53 Correct 8 ms 21072 KB Output is correct
54 Correct 7 ms 21072 KB Output is correct
55 Correct 7 ms 21084 KB Output is correct
56 Correct 8 ms 22608 KB Output is correct
57 Correct 5 ms 20816 KB Output is correct
58 Correct 7 ms 20944 KB Output is correct
59 Correct 5 ms 20560 KB Output is correct
60 Correct 5 ms 22608 KB Output is correct
61 Correct 7 ms 22864 KB Output is correct
62 Correct 203 ms 43464 KB Output is correct
63 Correct 176 ms 50884 KB Output is correct
64 Correct 182 ms 52156 KB Output is correct
65 Correct 282 ms 55740 KB Output is correct
66 Correct 279 ms 55744 KB Output is correct
67 Correct 289 ms 55740 KB Output is correct
68 Correct 282 ms 55748 KB Output is correct
69 Correct 283 ms 55724 KB Output is correct
70 Correct 240 ms 55740 KB Output is correct
71 Correct 251 ms 55484 KB Output is correct
72 Correct 248 ms 55392 KB Output is correct
73 Correct 55 ms 52032 KB Output is correct
74 Correct 127 ms 55484 KB Output is correct
75 Correct 144 ms 29664 KB Output is correct
76 Correct 4 ms 20560 KB Output is correct
77 Correct 96 ms 38616 KB Output is correct
78 Correct 136 ms 39496 KB Output is correct
79 Correct 116 ms 42568 KB Output is correct
80 Correct 198 ms 48968 KB Output is correct
81 Correct 184 ms 49024 KB Output is correct
82 Correct 176 ms 48968 KB Output is correct
83 Correct 195 ms 49148 KB Output is correct
84 Correct 192 ms 49072 KB Output is correct
85 Correct 172 ms 49192 KB Output is correct
86 Correct 188 ms 49224 KB Output is correct
87 Correct 179 ms 49236 KB Output is correct
88 Correct 180 ms 49484 KB Output is correct
89 Correct 184 ms 50616 KB Output is correct
90 Correct 200 ms 54344 KB Output is correct
91 Correct 198 ms 49744 KB Output is correct
92 Correct 209 ms 49660 KB Output is correct
93 Correct 202 ms 49616 KB Output is correct
94 Correct 157 ms 43492 KB Output is correct
95 Correct 165 ms 43640 KB Output is correct
96 Correct 159 ms 43660 KB Output is correct
97 Correct 165 ms 43848 KB Output is correct
98 Correct 157 ms 43596 KB Output is correct
99 Correct 166 ms 43848 KB Output is correct
100 Correct 176 ms 43592 KB Output is correct
101 Correct 172 ms 43488 KB Output is correct
102 Correct 159 ms 43660 KB Output is correct
103 Correct 167 ms 43704 KB Output is correct
104 Correct 175 ms 43592 KB Output is correct
105 Correct 147 ms 43900 KB Output is correct
106 Correct 169 ms 44188 KB Output is correct
107 Correct 172 ms 44616 KB Output is correct
108 Correct 170 ms 46152 KB Output is correct
109 Correct 162 ms 48980 KB Output is correct
110 Correct 4 ms 20560 KB Output is correct
111 Correct 4 ms 22608 KB Output is correct
112 Correct 8 ms 22864 KB Output is correct
113 Correct 282 ms 44116 KB Output is correct
114 Correct 269 ms 44104 KB Output is correct
115 Correct 330 ms 52560 KB Output is correct
116 Correct 338 ms 54856 KB Output is correct
117 Correct 339 ms 54684 KB Output is correct
118 Correct 382 ms 54820 KB Output is correct
119 Correct 332 ms 54856 KB Output is correct
120 Correct 359 ms 54860 KB Output is correct
121 Correct 345 ms 54856 KB Output is correct
122 Correct 357 ms 54840 KB Output is correct
123 Correct 162 ms 29768 KB Output is correct
124 Correct 313 ms 54232 KB Output is correct
125 Correct 212 ms 51288 KB Output is correct
126 Correct 319 ms 54292 KB Output is correct
127 Correct 320 ms 54144 KB Output is correct
128 Correct 324 ms 54344 KB Output is correct
129 Correct 334 ms 54344 KB Output is correct
130 Correct 316 ms 54344 KB Output is correct
131 Correct 261 ms 54460 KB Output is correct
132 Correct 288 ms 55740 KB Output is correct
133 Correct 250 ms 51880 KB Output is correct
134 Correct 319 ms 48712 KB Output is correct
135 Correct 317 ms 48796 KB Output is correct
136 Correct 313 ms 48712 KB Output is correct
137 Correct 235 ms 57348 KB Output is correct
138 Correct 232 ms 57276 KB Output is correct
139 Correct 260 ms 57276 KB Output is correct
140 Correct 225 ms 57276 KB Output is correct
141 Correct 215 ms 57276 KB Output is correct
142 Correct 216 ms 57288 KB Output is correct
143 Correct 62 ms 49152 KB Output is correct
144 Correct 222 ms 53844 KB Output is correct