답안 #1000990

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1000990 2024-06-18T12:20:48 Z LucaLucaM Tourism (JOI23_tourism) C++17
100 / 100
1080 ms 78240 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <set>
#warning That's the baby, that's not my baby

#define debug(x) #x << " = " << x << '\n'

typedef long long ll;

const int NMAX = 3e5;
const int LGMAX = 18;

int aib[NMAX + 1];
int answer[NMAX + 1];

int a[NMAX + 1];

std::vector<int> g[NMAX + 1];
std::set<std::pair<int, int>> queries[NMAX + 1];
std::vector<std::pair<int, int>> curQ[NMAX + 1];

int depth[NMAX + 1];
int tin[NMAX + 1], tout[NMAX + 1], timer;

int jump[LGMAX + 1][NMAX + 1];

int sum[NMAX + 1];
int added[NMAX + 1], added2[NMAX + 1];
int parent[NMAX + 1];

bool ancestor(int u, int v) {
  return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int lca(int u, int v) {
  if (ancestor(u, v)) {
    return u;
  }
  for (int k = LGMAX; k >= 0; k--) {
    if (jump[k][u] != 0 && !ancestor(jump[k][u], v)) {
      u = jump[k][u];
    }
  }
  return jump[0][u];
}

void dfs(int u, int p = 0) {
  depth[u] = depth[p] + 1;
  tin[u] = ++timer;
  jump[0][u] = p;
  for (const auto &v : g[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  tout[u] = timer;
}

void dfs1(int u, int p = 0) {
  parent[u] = p;
  for (const auto &v : g[u]) {
    if (v != p) {
      dfs1(v, u);
    }
  }
}

std::vector<std::pair<int, int>> updates;

void update(int pos, const int &val) {
  if (val > 0) {
    updates.push_back({pos, val});
  }
  for (; pos < NMAX; pos += pos & -pos) {
    aib[pos] += val;
  }
}

int query(int pos) {
  int ret = 0;
  for (; pos > 0; pos -= pos & -pos) {
    ret += aib[pos];
  }
  return ret;
}

bool cmp(int x, int y) {
  return tin[x] < tin[y];
}

void divide(int l, int r) {
  if (l >= r) {
    return;
  }
  int mid = (l + r) / 2;
  for (int i = mid + 1; i <= r; i++) {
    if (queries[i].empty()) {
      continue;
    }
    if ((*queries[i].rbegin()).first < l) {
      continue;
    }
    auto it = queries[i].lower_bound({l, 0});
    while (it != queries[i].end() && it -> first <= mid) {
      curQ[i].push_back(*it);
      it++;
    }
    for (const auto &[L, index] : curQ[i]) {
      queries[i].erase({L, index});
    }
  }


  std::vector<int> vert;
  for (int i = l; i <= r; i++) {
    vert.push_back(a[i]);
  }
  std::sort(vert.begin(), vert.end());
  vert.erase(std::unique(vert.begin(), vert.end()), vert.end());
  std::sort(vert.begin(), vert.end(), cmp);
  int sz = (int) vert.size();
  for (int i = 1; i < sz; i++) {
    vert.push_back(lca(vert[i - 1], vert[i]));
  }
  std::sort(vert.begin(), vert.end());
  vert.erase(std::unique(vert.begin(), vert.end()), vert.end());
  std::sort(vert.begin(), vert.end(), cmp);
  for (int i = 1; i < (int) vert.size(); i++) {
    int w = lca(vert[i - 1], vert[i]);
    g[w].push_back(vert[i]);
    g[vert[i]].push_back(w);
  }
  for (const auto &u : vert) {
    parent[u] = 0;
  }
  dfs1(a[mid]);
  for (const auto &u : vert) {
    added[u] = added2[u] = 0;
  }
  sum[mid] = 0;
  added[a[mid]] = added2[a[mid]] = mid;

  for (int i = mid - 1; i >= l; i--) {
    int u = a[i];
    int add = 0;
    while (!added[u]) {
      added[u] = i;
      add += std::abs(depth[u] - depth[parent[u]]);
      u = parent[u];
    }
    sum[i] = sum[i + 1] + add;
  }

  for (int i = mid + 1; i <= r; i++) {
    int u = a[i];
    while (!added2[u]) {
      if (added[u] != mid) {
        update(added[u] + 1, std::abs(depth[u] - depth[parent[u]]));
      }
      added2[u] = i;
      u = parent[u];
    }
    for (const auto &[L, index] : curQ[i]) {
      answer[index] = query(L) + sum[L];
    }
    curQ[i].clear();
  }

  for (const auto &[x, y] : updates) {
    update(x, -y);
  }
  updates.clear();
  for (const auto &u : vert) {
    g[u].clear();
  }

  if (l < r) {
    divide(l, mid);
    divide(mid + 1, r);
  }
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);

  int n, m, q;
  std::cin >> n >> m >> q;

  for (int i = 1; i < n; i++) {
    int u, v;
    std::cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(1);

  for (int k = 1; k <= LGMAX; k++) {
    for (int i = 1; i <= n; i++) {
      jump[k][i] = jump[k - 1][jump[k - 1][i]];
    }
  }

  for (int i = 1; i <= m; i++) {
    std::cin >> a[i];
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    std::cin >> l >> r;
    if (l != r) {
      queries[r].insert({l, i});
    } else {
      answer[i] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    g[i].clear();
  }

  divide(1, m);

  for (int i = 0; i < q; i++) {
    std::cout << answer[i] + 1 << '\n';
  }

  return 0;
}

Compilation message

tourism.cpp:7:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
    7 | #warning That's the baby, that's not my baby
      |  ^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 59736 KB Output is correct
2 Correct 9 ms 59740 KB Output is correct
3 Correct 8 ms 59740 KB Output is correct
4 Correct 9 ms 59880 KB Output is correct
5 Correct 8 ms 59996 KB Output is correct
6 Correct 9 ms 59996 KB Output is correct
7 Correct 8 ms 59836 KB Output is correct
8 Correct 8 ms 59996 KB Output is correct
9 Correct 10 ms 59904 KB Output is correct
10 Correct 11 ms 60028 KB Output is correct
11 Correct 10 ms 59996 KB Output is correct
12 Correct 8 ms 59996 KB Output is correct
13 Correct 9 ms 59996 KB Output is correct
14 Correct 9 ms 59996 KB Output is correct
15 Correct 10 ms 59972 KB Output is correct
16 Correct 8 ms 59996 KB Output is correct
17 Correct 8 ms 59996 KB Output is correct
18 Correct 8 ms 59996 KB Output is correct
19 Correct 9 ms 59996 KB Output is correct
20 Correct 8 ms 59980 KB Output is correct
21 Correct 8 ms 59856 KB Output is correct
22 Correct 8 ms 60020 KB Output is correct
23 Correct 9 ms 59996 KB Output is correct
24 Correct 10 ms 60128 KB Output is correct
25 Correct 9 ms 59996 KB Output is correct
26 Correct 8 ms 59996 KB Output is correct
27 Correct 8 ms 59860 KB Output is correct
28 Correct 7 ms 55640 KB Output is correct
29 Correct 10 ms 59996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 59736 KB Output is correct
2 Correct 9 ms 59740 KB Output is correct
3 Correct 8 ms 59740 KB Output is correct
4 Correct 9 ms 59880 KB Output is correct
5 Correct 8 ms 59996 KB Output is correct
6 Correct 9 ms 59996 KB Output is correct
7 Correct 8 ms 59836 KB Output is correct
8 Correct 8 ms 59996 KB Output is correct
9 Correct 10 ms 59904 KB Output is correct
10 Correct 11 ms 60028 KB Output is correct
11 Correct 10 ms 59996 KB Output is correct
12 Correct 8 ms 59996 KB Output is correct
13 Correct 9 ms 59996 KB Output is correct
14 Correct 9 ms 59996 KB Output is correct
15 Correct 10 ms 59972 KB Output is correct
16 Correct 8 ms 59996 KB Output is correct
17 Correct 8 ms 59996 KB Output is correct
18 Correct 8 ms 59996 KB Output is correct
19 Correct 9 ms 59996 KB Output is correct
20 Correct 8 ms 59980 KB Output is correct
21 Correct 8 ms 59856 KB Output is correct
22 Correct 8 ms 60020 KB Output is correct
23 Correct 9 ms 59996 KB Output is correct
24 Correct 10 ms 60128 KB Output is correct
25 Correct 9 ms 59996 KB Output is correct
26 Correct 8 ms 59996 KB Output is correct
27 Correct 8 ms 59860 KB Output is correct
28 Correct 7 ms 55640 KB Output is correct
29 Correct 10 ms 59996 KB Output is correct
30 Correct 14 ms 59996 KB Output is correct
31 Correct 13 ms 59996 KB Output is correct
32 Correct 20 ms 59996 KB Output is correct
33 Correct 15 ms 59996 KB Output is correct
34 Correct 16 ms 60048 KB Output is correct
35 Correct 18 ms 59996 KB Output is correct
36 Correct 21 ms 59992 KB Output is correct
37 Correct 19 ms 60092 KB Output is correct
38 Correct 14 ms 60248 KB Output is correct
39 Correct 15 ms 60120 KB Output is correct
40 Correct 15 ms 60252 KB Output is correct
41 Correct 17 ms 60252 KB Output is correct
42 Correct 13 ms 60252 KB Output is correct
43 Correct 18 ms 60184 KB Output is correct
44 Correct 15 ms 60248 KB Output is correct
45 Correct 20 ms 60244 KB Output is correct
46 Correct 14 ms 60252 KB Output is correct
47 Correct 14 ms 59996 KB Output is correct
48 Correct 19 ms 59992 KB Output is correct
49 Correct 15 ms 59996 KB Output is correct
50 Correct 15 ms 60208 KB Output is correct
51 Correct 13 ms 60228 KB Output is correct
52 Correct 14 ms 59996 KB Output is correct
53 Correct 14 ms 59996 KB Output is correct
54 Correct 14 ms 59992 KB Output is correct
55 Correct 19 ms 59996 KB Output is correct
56 Correct 10 ms 59996 KB Output is correct
57 Correct 9 ms 55900 KB Output is correct
58 Correct 19 ms 60116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 59740 KB Output is correct
2 Correct 10 ms 59996 KB Output is correct
3 Correct 11 ms 59992 KB Output is correct
4 Correct 366 ms 69668 KB Output is correct
5 Correct 236 ms 72532 KB Output is correct
6 Correct 330 ms 73648 KB Output is correct
7 Correct 439 ms 78152 KB Output is correct
8 Correct 415 ms 78156 KB Output is correct
9 Correct 445 ms 78156 KB Output is correct
10 Correct 430 ms 78212 KB Output is correct
11 Correct 430 ms 78124 KB Output is correct
12 Correct 352 ms 77664 KB Output is correct
13 Correct 335 ms 77544 KB Output is correct
14 Correct 323 ms 77652 KB Output is correct
15 Correct 40 ms 66128 KB Output is correct
16 Correct 400 ms 73360 KB Output is correct
17 Correct 87 ms 65228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 59872 KB Output is correct
2 Correct 406 ms 63288 KB Output is correct
3 Correct 666 ms 65116 KB Output is correct
4 Correct 517 ms 65240 KB Output is correct
5 Correct 814 ms 67556 KB Output is correct
6 Correct 832 ms 67636 KB Output is correct
7 Correct 869 ms 67828 KB Output is correct
8 Correct 871 ms 67540 KB Output is correct
9 Correct 831 ms 67756 KB Output is correct
10 Correct 833 ms 67680 KB Output is correct
11 Correct 815 ms 67624 KB Output is correct
12 Correct 831 ms 67804 KB Output is correct
13 Correct 813 ms 68240 KB Output is correct
14 Correct 817 ms 69380 KB Output is correct
15 Correct 835 ms 68792 KB Output is correct
16 Correct 834 ms 68220 KB Output is correct
17 Correct 830 ms 68480 KB Output is correct
18 Correct 812 ms 68112 KB Output is correct
19 Correct 903 ms 67608 KB Output is correct
20 Correct 878 ms 67628 KB Output is correct
21 Correct 882 ms 67884 KB Output is correct
22 Correct 912 ms 67704 KB Output is correct
23 Correct 953 ms 67696 KB Output is correct
24 Correct 931 ms 67524 KB Output is correct
25 Correct 976 ms 67868 KB Output is correct
26 Correct 986 ms 67824 KB Output is correct
27 Correct 997 ms 67672 KB Output is correct
28 Correct 909 ms 67672 KB Output is correct
29 Correct 919 ms 67628 KB Output is correct
30 Correct 986 ms 68016 KB Output is correct
31 Correct 945 ms 68284 KB Output is correct
32 Correct 899 ms 68776 KB Output is correct
33 Correct 955 ms 69668 KB Output is correct
34 Correct 950 ms 68720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 59996 KB Output is correct
2 Correct 9 ms 59996 KB Output is correct
3 Correct 9 ms 59940 KB Output is correct
4 Correct 722 ms 68188 KB Output is correct
5 Correct 702 ms 69404 KB Output is correct
6 Correct 809 ms 71336 KB Output is correct
7 Correct 871 ms 72412 KB Output is correct
8 Correct 857 ms 72404 KB Output is correct
9 Correct 915 ms 72268 KB Output is correct
10 Correct 886 ms 72504 KB Output is correct
11 Correct 832 ms 72340 KB Output is correct
12 Correct 846 ms 72300 KB Output is correct
13 Correct 836 ms 72404 KB Output is correct
14 Correct 90 ms 64884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 59736 KB Output is correct
2 Correct 9 ms 59740 KB Output is correct
3 Correct 8 ms 59740 KB Output is correct
4 Correct 9 ms 59880 KB Output is correct
5 Correct 8 ms 59996 KB Output is correct
6 Correct 9 ms 59996 KB Output is correct
7 Correct 8 ms 59836 KB Output is correct
8 Correct 8 ms 59996 KB Output is correct
9 Correct 10 ms 59904 KB Output is correct
10 Correct 11 ms 60028 KB Output is correct
11 Correct 10 ms 59996 KB Output is correct
12 Correct 8 ms 59996 KB Output is correct
13 Correct 9 ms 59996 KB Output is correct
14 Correct 9 ms 59996 KB Output is correct
15 Correct 10 ms 59972 KB Output is correct
16 Correct 8 ms 59996 KB Output is correct
17 Correct 8 ms 59996 KB Output is correct
18 Correct 8 ms 59996 KB Output is correct
19 Correct 9 ms 59996 KB Output is correct
20 Correct 8 ms 59980 KB Output is correct
21 Correct 8 ms 59856 KB Output is correct
22 Correct 8 ms 60020 KB Output is correct
23 Correct 9 ms 59996 KB Output is correct
24 Correct 10 ms 60128 KB Output is correct
25 Correct 9 ms 59996 KB Output is correct
26 Correct 8 ms 59996 KB Output is correct
27 Correct 8 ms 59860 KB Output is correct
28 Correct 7 ms 55640 KB Output is correct
29 Correct 10 ms 59996 KB Output is correct
30 Correct 14 ms 59996 KB Output is correct
31 Correct 13 ms 59996 KB Output is correct
32 Correct 20 ms 59996 KB Output is correct
33 Correct 15 ms 59996 KB Output is correct
34 Correct 16 ms 60048 KB Output is correct
35 Correct 18 ms 59996 KB Output is correct
36 Correct 21 ms 59992 KB Output is correct
37 Correct 19 ms 60092 KB Output is correct
38 Correct 14 ms 60248 KB Output is correct
39 Correct 15 ms 60120 KB Output is correct
40 Correct 15 ms 60252 KB Output is correct
41 Correct 17 ms 60252 KB Output is correct
42 Correct 13 ms 60252 KB Output is correct
43 Correct 18 ms 60184 KB Output is correct
44 Correct 15 ms 60248 KB Output is correct
45 Correct 20 ms 60244 KB Output is correct
46 Correct 14 ms 60252 KB Output is correct
47 Correct 14 ms 59996 KB Output is correct
48 Correct 19 ms 59992 KB Output is correct
49 Correct 15 ms 59996 KB Output is correct
50 Correct 15 ms 60208 KB Output is correct
51 Correct 13 ms 60228 KB Output is correct
52 Correct 14 ms 59996 KB Output is correct
53 Correct 14 ms 59996 KB Output is correct
54 Correct 14 ms 59992 KB Output is correct
55 Correct 19 ms 59996 KB Output is correct
56 Correct 10 ms 59996 KB Output is correct
57 Correct 9 ms 55900 KB Output is correct
58 Correct 19 ms 60116 KB Output is correct
59 Correct 8 ms 59740 KB Output is correct
60 Correct 10 ms 59996 KB Output is correct
61 Correct 11 ms 59992 KB Output is correct
62 Correct 366 ms 69668 KB Output is correct
63 Correct 236 ms 72532 KB Output is correct
64 Correct 330 ms 73648 KB Output is correct
65 Correct 439 ms 78152 KB Output is correct
66 Correct 415 ms 78156 KB Output is correct
67 Correct 445 ms 78156 KB Output is correct
68 Correct 430 ms 78212 KB Output is correct
69 Correct 430 ms 78124 KB Output is correct
70 Correct 352 ms 77664 KB Output is correct
71 Correct 335 ms 77544 KB Output is correct
72 Correct 323 ms 77652 KB Output is correct
73 Correct 40 ms 66128 KB Output is correct
74 Correct 400 ms 73360 KB Output is correct
75 Correct 87 ms 65228 KB Output is correct
76 Correct 8 ms 59872 KB Output is correct
77 Correct 406 ms 63288 KB Output is correct
78 Correct 666 ms 65116 KB Output is correct
79 Correct 517 ms 65240 KB Output is correct
80 Correct 814 ms 67556 KB Output is correct
81 Correct 832 ms 67636 KB Output is correct
82 Correct 869 ms 67828 KB Output is correct
83 Correct 871 ms 67540 KB Output is correct
84 Correct 831 ms 67756 KB Output is correct
85 Correct 833 ms 67680 KB Output is correct
86 Correct 815 ms 67624 KB Output is correct
87 Correct 831 ms 67804 KB Output is correct
88 Correct 813 ms 68240 KB Output is correct
89 Correct 817 ms 69380 KB Output is correct
90 Correct 835 ms 68792 KB Output is correct
91 Correct 834 ms 68220 KB Output is correct
92 Correct 830 ms 68480 KB Output is correct
93 Correct 812 ms 68112 KB Output is correct
94 Correct 903 ms 67608 KB Output is correct
95 Correct 878 ms 67628 KB Output is correct
96 Correct 882 ms 67884 KB Output is correct
97 Correct 912 ms 67704 KB Output is correct
98 Correct 953 ms 67696 KB Output is correct
99 Correct 931 ms 67524 KB Output is correct
100 Correct 976 ms 67868 KB Output is correct
101 Correct 986 ms 67824 KB Output is correct
102 Correct 997 ms 67672 KB Output is correct
103 Correct 909 ms 67672 KB Output is correct
104 Correct 919 ms 67628 KB Output is correct
105 Correct 986 ms 68016 KB Output is correct
106 Correct 945 ms 68284 KB Output is correct
107 Correct 899 ms 68776 KB Output is correct
108 Correct 955 ms 69668 KB Output is correct
109 Correct 950 ms 68720 KB Output is correct
110 Correct 9 ms 59996 KB Output is correct
111 Correct 9 ms 59996 KB Output is correct
112 Correct 9 ms 59940 KB Output is correct
113 Correct 722 ms 68188 KB Output is correct
114 Correct 702 ms 69404 KB Output is correct
115 Correct 809 ms 71336 KB Output is correct
116 Correct 871 ms 72412 KB Output is correct
117 Correct 857 ms 72404 KB Output is correct
118 Correct 915 ms 72268 KB Output is correct
119 Correct 886 ms 72504 KB Output is correct
120 Correct 832 ms 72340 KB Output is correct
121 Correct 846 ms 72300 KB Output is correct
122 Correct 836 ms 72404 KB Output is correct
123 Correct 90 ms 64884 KB Output is correct
124 Correct 829 ms 73092 KB Output is correct
125 Correct 621 ms 69968 KB Output is correct
126 Correct 987 ms 73536 KB Output is correct
127 Correct 942 ms 73504 KB Output is correct
128 Correct 958 ms 73468 KB Output is correct
129 Correct 942 ms 73584 KB Output is correct
130 Correct 950 ms 73444 KB Output is correct
131 Correct 629 ms 77428 KB Output is correct
132 Correct 626 ms 78240 KB Output is correct
133 Correct 663 ms 75868 KB Output is correct
134 Correct 1080 ms 55828 KB Output is correct
135 Correct 1066 ms 73612 KB Output is correct
136 Correct 1046 ms 73548 KB Output is correct
137 Correct 664 ms 72252 KB Output is correct
138 Correct 653 ms 73664 KB Output is correct
139 Correct 679 ms 70952 KB Output is correct
140 Correct 710 ms 71228 KB Output is correct
141 Correct 659 ms 69404 KB Output is correct
142 Correct 656 ms 72356 KB Output is correct
143 Correct 48 ms 61524 KB Output is correct
144 Correct 929 ms 59256 KB Output is correct