답안 #1043011

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1043011 2024-08-03T17:43:52 Z juicy Tourism (JOI23_tourism) C++17
100 / 100
217 ms 26056 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 1e5 + 5;

int n, m, q;
int tin[N], head[N], sz[N], s[N], c[N], res[N], pr[N];
set<array<int, 3>> st[N];
vector<int> g[N];

void upd(int i, int x) {
  for (; i <= m; i += i & -i) {
    s[i] += x;
  }
}

int qry(int i) {
  int res = 0;
  for (; i; i -= i & -i) {
    res += s[i];
  }
  return res;
}

void dfs(int u) {
  for (int &v : g[u]) {
    if (v == pr[u]) {
      swap(v, g[u].back());
      g[u].pop_back();
      break;
    }
  } 
  sz[u] = 1;
  for (int v : g[u]) {
    pr[v] = u;
    dfs(v);
    sz[u] += sz[v];
  } 
}

int order;

void hld(int u, int h) {
  tin[u] = ++order;
  head[u] = h;
  if (g[u].size()) {
    int x = *max_element(g[u].begin(), g[u].end(), [&](int i, int j) {
      return sz[i] < sz[j];
    });
    hld(x, h);
    for (int v : g[u]) {
      if (v ^ x) {
        hld(v, v);
      }
    }
  }
}

void add(int l, int r, int id, set<array<int, 3>> &st) {
  auto it = st.lower_bound(array<int, 3>{l});
  if (it != st.begin() && (*prev(it))[1] >= l) {
    --it;
  }
  for (; it != st.end() && (*it)[0] <= r; it = st.erase(it)) {
    auto [L, R, c] = *it;
    upd(c, L - R - 1);
    if (L < l) {
      upd(c, l - L);
      st.insert({L, l - 1, c});
    }
    if (R > r) {
      upd(c, R - r);
      st.insert({r + 1, R, c});
    }
  }
  st.insert({l, r, id});
  upd(id, r - l + 1);
}

void upd(int u, int v, int id) {
  for (; head[u] != head[v]; ) {
    if (sz[head[u]] > sz[head[v]]) {
      swap(u, v);
    }
    int h = head[u];
    add(tin[h], tin[u], id, st[h]);
    u = pr[h];
  }
  if (sz[u] < sz[v]) {
    swap(u, v);
  }
  if (tin[u] + 1 <= tin[v]) {
    add(tin[u] + 1, tin[v], id, st[head[u]]);
  }
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  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);
  hld(1, 1);
  for (int i = 1; i <= m; ++i) {
    cin >> c[i];
  }
  vector<array<int, 3>> queries;
  for (int i = 1; i <= q; ++i) {
    int l, r; cin >> l >> r;
    queries.push_back({l, r, i});
  }
  sort(queries.begin(), queries.end(), [&](auto a, auto b) {
    return a[1] < b[1];
  });
  for (int i = 1, j = 0; i <= m; ++i) {
    if (i > 1) {
      upd(c[i - 1], c[i], i);
    } 
    while (j < q && queries[j][1] == i) {
      auto [l, r, id] = queries[j++];
      res[id] = qry(r) - qry(l) + 1;
    }
  }
  for (int i = 1; i <= q; ++i) {
    cout << res[i] << "\n";
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 2 ms 7516 KB Output is correct
6 Correct 3 ms 7512 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7516 KB Output is correct
11 Correct 3 ms 7516 KB Output is correct
12 Correct 3 ms 7544 KB Output is correct
13 Correct 3 ms 7516 KB Output is correct
14 Correct 3 ms 7516 KB Output is correct
15 Correct 3 ms 7472 KB Output is correct
16 Correct 3 ms 7516 KB Output is correct
17 Correct 3 ms 7516 KB Output is correct
18 Correct 3 ms 7516 KB Output is correct
19 Correct 3 ms 7516 KB Output is correct
20 Correct 3 ms 7460 KB Output is correct
21 Correct 4 ms 7516 KB Output is correct
22 Correct 3 ms 7512 KB Output is correct
23 Correct 3 ms 7516 KB Output is correct
24 Correct 3 ms 7516 KB Output is correct
25 Correct 3 ms 7360 KB Output is correct
26 Correct 3 ms 7516 KB Output is correct
27 Correct 3 ms 7516 KB Output is correct
28 Correct 2 ms 7516 KB Output is correct
29 Correct 3 ms 7516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 2 ms 7516 KB Output is correct
6 Correct 3 ms 7512 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7516 KB Output is correct
11 Correct 3 ms 7516 KB Output is correct
12 Correct 3 ms 7544 KB Output is correct
13 Correct 3 ms 7516 KB Output is correct
14 Correct 3 ms 7516 KB Output is correct
15 Correct 3 ms 7472 KB Output is correct
16 Correct 3 ms 7516 KB Output is correct
17 Correct 3 ms 7516 KB Output is correct
18 Correct 3 ms 7516 KB Output is correct
19 Correct 3 ms 7516 KB Output is correct
20 Correct 3 ms 7460 KB Output is correct
21 Correct 4 ms 7516 KB Output is correct
22 Correct 3 ms 7512 KB Output is correct
23 Correct 3 ms 7516 KB Output is correct
24 Correct 3 ms 7516 KB Output is correct
25 Correct 3 ms 7360 KB Output is correct
26 Correct 3 ms 7516 KB Output is correct
27 Correct 3 ms 7516 KB Output is correct
28 Correct 2 ms 7516 KB Output is correct
29 Correct 3 ms 7516 KB Output is correct
30 Correct 4 ms 7532 KB Output is correct
31 Correct 4 ms 7516 KB Output is correct
32 Correct 4 ms 7772 KB Output is correct
33 Correct 4 ms 7780 KB Output is correct
34 Correct 4 ms 7688 KB Output is correct
35 Correct 5 ms 7772 KB Output is correct
36 Correct 4 ms 8024 KB Output is correct
37 Correct 4 ms 7772 KB Output is correct
38 Correct 4 ms 7772 KB Output is correct
39 Correct 4 ms 7772 KB Output is correct
40 Correct 6 ms 7784 KB Output is correct
41 Correct 4 ms 7768 KB Output is correct
42 Correct 4 ms 7772 KB Output is correct
43 Correct 4 ms 7772 KB Output is correct
44 Correct 4 ms 7772 KB Output is correct
45 Correct 7 ms 7772 KB Output is correct
46 Correct 5 ms 7772 KB Output is correct
47 Correct 4 ms 7784 KB Output is correct
48 Correct 4 ms 7772 KB Output is correct
49 Correct 4 ms 7744 KB Output is correct
50 Correct 4 ms 7772 KB Output is correct
51 Correct 4 ms 7772 KB Output is correct
52 Correct 3 ms 7780 KB Output is correct
53 Correct 3 ms 7780 KB Output is correct
54 Correct 4 ms 7780 KB Output is correct
55 Correct 4 ms 7780 KB Output is correct
56 Correct 3 ms 7472 KB Output is correct
57 Correct 3 ms 7524 KB Output is correct
58 Correct 4 ms 7780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 2 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 54 ms 18852 KB Output is correct
5 Correct 53 ms 21536 KB Output is correct
6 Correct 54 ms 22988 KB Output is correct
7 Correct 74 ms 25796 KB Output is correct
8 Correct 72 ms 25800 KB Output is correct
9 Correct 70 ms 25864 KB Output is correct
10 Correct 70 ms 25792 KB Output is correct
11 Correct 72 ms 25800 KB Output is correct
12 Correct 70 ms 26056 KB Output is correct
13 Correct 69 ms 26056 KB Output is correct
14 Correct 80 ms 26056 KB Output is correct
15 Correct 34 ms 23244 KB Output is correct
16 Correct 79 ms 25540 KB Output is correct
17 Correct 27 ms 10960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 76 ms 13196 KB Output is correct
3 Correct 102 ms 14416 KB Output is correct
4 Correct 87 ms 14672 KB Output is correct
5 Correct 124 ms 18000 KB Output is correct
6 Correct 132 ms 18020 KB Output is correct
7 Correct 131 ms 18048 KB Output is correct
8 Correct 136 ms 18004 KB Output is correct
9 Correct 128 ms 18000 KB Output is correct
10 Correct 127 ms 17964 KB Output is correct
11 Correct 132 ms 18000 KB Output is correct
12 Correct 126 ms 18184 KB Output is correct
13 Correct 127 ms 18448 KB Output is correct
14 Correct 128 ms 19024 KB Output is correct
15 Correct 145 ms 20932 KB Output is correct
16 Correct 128 ms 18356 KB Output is correct
17 Correct 134 ms 18452 KB Output is correct
18 Correct 123 ms 18240 KB Output is correct
19 Correct 100 ms 15072 KB Output is correct
20 Correct 93 ms 14928 KB Output is correct
21 Correct 95 ms 14928 KB Output is correct
22 Correct 111 ms 14944 KB Output is correct
23 Correct 95 ms 15132 KB Output is correct
24 Correct 97 ms 14932 KB Output is correct
25 Correct 112 ms 15040 KB Output is correct
26 Correct 99 ms 14932 KB Output is correct
27 Correct 95 ms 14928 KB Output is correct
28 Correct 95 ms 14940 KB Output is correct
29 Correct 94 ms 14932 KB Output is correct
30 Correct 99 ms 15176 KB Output is correct
31 Correct 96 ms 15396 KB Output is correct
32 Correct 105 ms 15696 KB Output is correct
33 Correct 112 ms 16588 KB Output is correct
34 Correct 109 ms 17868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7512 KB Output is correct
2 Correct 2 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 165 ms 17436 KB Output is correct
5 Correct 167 ms 17544 KB Output is correct
6 Correct 206 ms 20420 KB Output is correct
7 Correct 210 ms 21860 KB Output is correct
8 Correct 207 ms 21700 KB Output is correct
9 Correct 200 ms 21696 KB Output is correct
10 Correct 206 ms 21616 KB Output is correct
11 Correct 217 ms 21708 KB Output is correct
12 Correct 196 ms 21824 KB Output is correct
13 Correct 196 ms 21764 KB Output is correct
14 Correct 27 ms 10960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 2 ms 7516 KB Output is correct
6 Correct 3 ms 7512 KB Output is correct
7 Correct 3 ms 7516 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7516 KB Output is correct
11 Correct 3 ms 7516 KB Output is correct
12 Correct 3 ms 7544 KB Output is correct
13 Correct 3 ms 7516 KB Output is correct
14 Correct 3 ms 7516 KB Output is correct
15 Correct 3 ms 7472 KB Output is correct
16 Correct 3 ms 7516 KB Output is correct
17 Correct 3 ms 7516 KB Output is correct
18 Correct 3 ms 7516 KB Output is correct
19 Correct 3 ms 7516 KB Output is correct
20 Correct 3 ms 7460 KB Output is correct
21 Correct 4 ms 7516 KB Output is correct
22 Correct 3 ms 7512 KB Output is correct
23 Correct 3 ms 7516 KB Output is correct
24 Correct 3 ms 7516 KB Output is correct
25 Correct 3 ms 7360 KB Output is correct
26 Correct 3 ms 7516 KB Output is correct
27 Correct 3 ms 7516 KB Output is correct
28 Correct 2 ms 7516 KB Output is correct
29 Correct 3 ms 7516 KB Output is correct
30 Correct 4 ms 7532 KB Output is correct
31 Correct 4 ms 7516 KB Output is correct
32 Correct 4 ms 7772 KB Output is correct
33 Correct 4 ms 7780 KB Output is correct
34 Correct 4 ms 7688 KB Output is correct
35 Correct 5 ms 7772 KB Output is correct
36 Correct 4 ms 8024 KB Output is correct
37 Correct 4 ms 7772 KB Output is correct
38 Correct 4 ms 7772 KB Output is correct
39 Correct 4 ms 7772 KB Output is correct
40 Correct 6 ms 7784 KB Output is correct
41 Correct 4 ms 7768 KB Output is correct
42 Correct 4 ms 7772 KB Output is correct
43 Correct 4 ms 7772 KB Output is correct
44 Correct 4 ms 7772 KB Output is correct
45 Correct 7 ms 7772 KB Output is correct
46 Correct 5 ms 7772 KB Output is correct
47 Correct 4 ms 7784 KB Output is correct
48 Correct 4 ms 7772 KB Output is correct
49 Correct 4 ms 7744 KB Output is correct
50 Correct 4 ms 7772 KB Output is correct
51 Correct 4 ms 7772 KB Output is correct
52 Correct 3 ms 7780 KB Output is correct
53 Correct 3 ms 7780 KB Output is correct
54 Correct 4 ms 7780 KB Output is correct
55 Correct 4 ms 7780 KB Output is correct
56 Correct 3 ms 7472 KB Output is correct
57 Correct 3 ms 7524 KB Output is correct
58 Correct 4 ms 7780 KB Output is correct
59 Correct 3 ms 7516 KB Output is correct
60 Correct 2 ms 7516 KB Output is correct
61 Correct 3 ms 7516 KB Output is correct
62 Correct 54 ms 18852 KB Output is correct
63 Correct 53 ms 21536 KB Output is correct
64 Correct 54 ms 22988 KB Output is correct
65 Correct 74 ms 25796 KB Output is correct
66 Correct 72 ms 25800 KB Output is correct
67 Correct 70 ms 25864 KB Output is correct
68 Correct 70 ms 25792 KB Output is correct
69 Correct 72 ms 25800 KB Output is correct
70 Correct 70 ms 26056 KB Output is correct
71 Correct 69 ms 26056 KB Output is correct
72 Correct 80 ms 26056 KB Output is correct
73 Correct 34 ms 23244 KB Output is correct
74 Correct 79 ms 25540 KB Output is correct
75 Correct 27 ms 10960 KB Output is correct
76 Correct 3 ms 7512 KB Output is correct
77 Correct 76 ms 13196 KB Output is correct
78 Correct 102 ms 14416 KB Output is correct
79 Correct 87 ms 14672 KB Output is correct
80 Correct 124 ms 18000 KB Output is correct
81 Correct 132 ms 18020 KB Output is correct
82 Correct 131 ms 18048 KB Output is correct
83 Correct 136 ms 18004 KB Output is correct
84 Correct 128 ms 18000 KB Output is correct
85 Correct 127 ms 17964 KB Output is correct
86 Correct 132 ms 18000 KB Output is correct
87 Correct 126 ms 18184 KB Output is correct
88 Correct 127 ms 18448 KB Output is correct
89 Correct 128 ms 19024 KB Output is correct
90 Correct 145 ms 20932 KB Output is correct
91 Correct 128 ms 18356 KB Output is correct
92 Correct 134 ms 18452 KB Output is correct
93 Correct 123 ms 18240 KB Output is correct
94 Correct 100 ms 15072 KB Output is correct
95 Correct 93 ms 14928 KB Output is correct
96 Correct 95 ms 14928 KB Output is correct
97 Correct 111 ms 14944 KB Output is correct
98 Correct 95 ms 15132 KB Output is correct
99 Correct 97 ms 14932 KB Output is correct
100 Correct 112 ms 15040 KB Output is correct
101 Correct 99 ms 14932 KB Output is correct
102 Correct 95 ms 14928 KB Output is correct
103 Correct 95 ms 14940 KB Output is correct
104 Correct 94 ms 14932 KB Output is correct
105 Correct 99 ms 15176 KB Output is correct
106 Correct 96 ms 15396 KB Output is correct
107 Correct 105 ms 15696 KB Output is correct
108 Correct 112 ms 16588 KB Output is correct
109 Correct 109 ms 17868 KB Output is correct
110 Correct 2 ms 7512 KB Output is correct
111 Correct 2 ms 7516 KB Output is correct
112 Correct 3 ms 7516 KB Output is correct
113 Correct 165 ms 17436 KB Output is correct
114 Correct 167 ms 17544 KB Output is correct
115 Correct 206 ms 20420 KB Output is correct
116 Correct 210 ms 21860 KB Output is correct
117 Correct 207 ms 21700 KB Output is correct
118 Correct 200 ms 21696 KB Output is correct
119 Correct 206 ms 21616 KB Output is correct
120 Correct 217 ms 21708 KB Output is correct
121 Correct 196 ms 21824 KB Output is correct
122 Correct 196 ms 21764 KB Output is correct
123 Correct 27 ms 10960 KB Output is correct
124 Correct 132 ms 20836 KB Output is correct
125 Correct 102 ms 18688 KB Output is correct
126 Correct 152 ms 21444 KB Output is correct
127 Correct 148 ms 21448 KB Output is correct
128 Correct 156 ms 21332 KB Output is correct
129 Correct 156 ms 21256 KB Output is correct
130 Correct 166 ms 21448 KB Output is correct
131 Correct 77 ms 24772 KB Output is correct
132 Correct 75 ms 25800 KB Output is correct
133 Correct 85 ms 21964 KB Output is correct
134 Correct 115 ms 18372 KB Output is correct
135 Correct 122 ms 18372 KB Output is correct
136 Correct 126 ms 18672 KB Output is correct
137 Correct 68 ms 22340 KB Output is correct
138 Correct 73 ms 22272 KB Output is correct
139 Correct 72 ms 22168 KB Output is correct
140 Correct 69 ms 22340 KB Output is correct
141 Correct 65 ms 22332 KB Output is correct
142 Correct 68 ms 22336 KB Output is correct
143 Correct 41 ms 15552 KB Output is correct
144 Correct 168 ms 20932 KB Output is correct