답안 #727418

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
727418 2023-04-20T15:49:18 Z model_code Tourism (JOI23_tourism) C++17
100 / 100
1807 ms 27108 KB
#include <iostream>
#include <vector>
#include <queue>
#include <map>
struct binary_indexed_tree{
  int N;
  std::vector<int> BIT;
  binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
  }
  void add(int i, int x){
    i++;
    while (i <= N){
      BIT[i] += x;
      i += i & -i;
    }
  }
  int sum(int i){
    int ans = 0;
    while (i > 0){
      ans += BIT[i];
      i -= i & -i;
    }
    return ans;
  }
  int sum(int L, int R){
    return sum(R) - sum(L);
  }
};
int main(){
  int N, M, Q;
  std::cin >> N >> M >> Q;
  std::vector<std::vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int A, B;
    std::cin >> A >> B;
    A--;
    B--;
    E[A].push_back(B);
    E[B].push_back(A);
  }
  std::vector<int> C(M);
  for (int i = 0; i < M; i++){
    std::cin >> C[i];
    C[i]--;
  }
  std::vector<int> L(Q), R(Q);
  for (int i = 0; i < Q; i++){
    std::cin >> L[i] >> R[i];
    L[i]--;
  }
  std::vector<int> p(N, -1);
  std::vector<std::vector<int>> c(N);
  std::queue<int> q;
  q.push(0);
  while (!q.empty()){
    int v = q.front();
    q.pop();
    for (int w : E[v]){
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        q.push(w);
      }
    }
  }
  std::vector<int> sz(N);
  auto dfs1 = [&](auto dfs1, int v = 0) -> void {
    sz[v] = 1;
    for (int &w : c[v]){
      dfs1(dfs1, w);
      sz[v] += sz[w];
      if (sz[w] > sz[c[v][0]]){
        std::swap(w, c[v][0]);
      }
    }
  };
  dfs1(dfs1);
  std::vector<int> in(N);
  std::vector<int> next(N);
  next[0] = 0;
  int t = 0;
  auto dfs2 = [&](auto dfs2, int v = 0) -> void {
    in[v] = t;
    t++;
    for (int w : c[v]){
      if (w == c[v][0]){
        next[w] = next[v];
      } else {
        next[w] = w;
      }
      dfs2(dfs2, w);
    }
  };
  dfs2(dfs2);
  std::vector<std::vector<int>> query(M);
  for (int i = 0; i < Q; i++){
    query[L[i]].push_back(i);
  }
  std::map<int, int> mp;
  mp[-1] = -1;
  mp[0] = M - 1;
  mp[N] = -1;
  std::vector<int> ans(Q, 0);
  binary_indexed_tree BIT(M - 1);
  for (int i = M - 2; i >= 0; i--){
    int u = C[i];
    int v = C[i + 1];
    if (u != v){
      std::vector<std::pair<int, int>> P;
      while (u != v){
        if (in[u] > in[v]){
          std::swap(u, v);
        }
        if (next[u] == next[v]){
          P.push_back(std::make_pair(in[u] + 1, in[v] + 1));
          break;
        }
        P.push_back(std::make_pair(in[next[v]], in[v] + 1));
        v = p[next[v]];
      }
      int cnt = P.size();
      for (int j = 0; j < cnt; j++){
        int l = P[j].first;
        int r = P[j].second;
        for (int k : {l, r}){
          if (mp.count(k) == 0){
            std::map<int, int>::iterator itr = std::prev(mp.lower_bound(k));
            int x = (*itr).second;
            mp[k] = x;
          }
        }
        while (true){
          std::map<int, int>::iterator itr = mp.lower_bound(l);
          int p1 = (*itr).first;
          if (p1 == r){
            break;
          }
          int p2 = (*std::next(itr)).first;
          int x = (*itr).second;
          if (x < M - 1){
            BIT.add(x, -(p2 - p1));
          }
          mp.erase(itr);
        }
        BIT.add(i, r - l);
        mp[l] = i;
        for (int k : {l, r}){
          std::map<int, int>::iterator itr = mp.lower_bound(k);
          if ((*itr).second == (*std::prev(itr)).second){
            mp.erase(itr);
          }
        }
      }
    }
    for (int j : query[i]){
      ans[j] = BIT.sum(i, R[j] - 1);
    }
  }
  for (int i = 0; i < Q; i++){
    ans[i]++;
  }
  for (int i = 0; i < Q; i++){
    std::cout << ans[i] << "\n";
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 3 ms 360 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 3 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 3 ms 360 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 3 ms 340 KB Output is correct
30 Correct 10 ms 596 KB Output is correct
31 Correct 8 ms 596 KB Output is correct
32 Correct 11 ms 596 KB Output is correct
33 Correct 11 ms 596 KB Output is correct
34 Correct 14 ms 656 KB Output is correct
35 Correct 15 ms 696 KB Output is correct
36 Correct 12 ms 596 KB Output is correct
37 Correct 15 ms 644 KB Output is correct
38 Correct 7 ms 708 KB Output is correct
39 Correct 8 ms 740 KB Output is correct
40 Correct 8 ms 724 KB Output is correct
41 Correct 6 ms 724 KB Output is correct
42 Correct 7 ms 724 KB Output is correct
43 Correct 8 ms 596 KB Output is correct
44 Correct 6 ms 636 KB Output is correct
45 Correct 9 ms 596 KB Output is correct
46 Correct 7 ms 724 KB Output is correct
47 Correct 10 ms 668 KB Output is correct
48 Correct 10 ms 672 KB Output is correct
49 Correct 10 ms 680 KB Output is correct
50 Correct 7 ms 596 KB Output is correct
51 Correct 8 ms 596 KB Output is correct
52 Correct 8 ms 712 KB Output is correct
53 Correct 9 ms 704 KB Output is correct
54 Correct 7 ms 596 KB Output is correct
55 Correct 7 ms 596 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 3 ms 468 KB Output is correct
58 Correct 11 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 185 ms 15392 KB Output is correct
5 Correct 200 ms 18388 KB Output is correct
6 Correct 193 ms 20928 KB Output is correct
7 Correct 266 ms 24312 KB Output is correct
8 Correct 273 ms 24404 KB Output is correct
9 Correct 323 ms 24296 KB Output is correct
10 Correct 388 ms 24324 KB Output is correct
11 Correct 397 ms 24308 KB Output is correct
12 Correct 294 ms 23580 KB Output is correct
13 Correct 290 ms 23460 KB Output is correct
14 Correct 294 ms 23532 KB Output is correct
15 Correct 120 ms 19552 KB Output is correct
16 Correct 275 ms 24112 KB Output is correct
17 Correct 100 ms 6584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 574 ms 9860 KB Output is correct
3 Correct 835 ms 11768 KB Output is correct
4 Correct 711 ms 12132 KB Output is correct
5 Correct 1147 ms 17656 KB Output is correct
6 Correct 1098 ms 17664 KB Output is correct
7 Correct 1089 ms 17724 KB Output is correct
8 Correct 1062 ms 17704 KB Output is correct
9 Correct 1042 ms 17716 KB Output is correct
10 Correct 1083 ms 17688 KB Output is correct
11 Correct 1030 ms 17728 KB Output is correct
12 Correct 950 ms 17916 KB Output is correct
13 Correct 1073 ms 18160 KB Output is correct
14 Correct 1077 ms 19084 KB Output is correct
15 Correct 1037 ms 22132 KB Output is correct
16 Correct 1018 ms 18148 KB Output is correct
17 Correct 1072 ms 18088 KB Output is correct
18 Correct 975 ms 18188 KB Output is correct
19 Correct 645 ms 16228 KB Output is correct
20 Correct 600 ms 16224 KB Output is correct
21 Correct 670 ms 16288 KB Output is correct
22 Correct 639 ms 16188 KB Output is correct
23 Correct 624 ms 16216 KB Output is correct
24 Correct 595 ms 16140 KB Output is correct
25 Correct 593 ms 16200 KB Output is correct
26 Correct 598 ms 16244 KB Output is correct
27 Correct 574 ms 16244 KB Output is correct
28 Correct 684 ms 16236 KB Output is correct
29 Correct 596 ms 16424 KB Output is correct
30 Correct 581 ms 16452 KB Output is correct
31 Correct 579 ms 16628 KB Output is correct
32 Correct 686 ms 17084 KB Output is correct
33 Correct 690 ms 18480 KB Output is correct
34 Correct 686 ms 20700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 1352 ms 14588 KB Output is correct
5 Correct 1379 ms 14888 KB Output is correct
6 Correct 1632 ms 19192 KB Output is correct
7 Correct 1807 ms 23964 KB Output is correct
8 Correct 1767 ms 22348 KB Output is correct
9 Correct 1773 ms 23368 KB Output is correct
10 Correct 1755 ms 23880 KB Output is correct
11 Correct 1727 ms 24380 KB Output is correct
12 Correct 1662 ms 23600 KB Output is correct
13 Correct 1731 ms 23680 KB Output is correct
14 Correct 141 ms 7656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 3 ms 360 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 3 ms 340 KB Output is correct
30 Correct 10 ms 596 KB Output is correct
31 Correct 8 ms 596 KB Output is correct
32 Correct 11 ms 596 KB Output is correct
33 Correct 11 ms 596 KB Output is correct
34 Correct 14 ms 656 KB Output is correct
35 Correct 15 ms 696 KB Output is correct
36 Correct 12 ms 596 KB Output is correct
37 Correct 15 ms 644 KB Output is correct
38 Correct 7 ms 708 KB Output is correct
39 Correct 8 ms 740 KB Output is correct
40 Correct 8 ms 724 KB Output is correct
41 Correct 6 ms 724 KB Output is correct
42 Correct 7 ms 724 KB Output is correct
43 Correct 8 ms 596 KB Output is correct
44 Correct 6 ms 636 KB Output is correct
45 Correct 9 ms 596 KB Output is correct
46 Correct 7 ms 724 KB Output is correct
47 Correct 10 ms 668 KB Output is correct
48 Correct 10 ms 672 KB Output is correct
49 Correct 10 ms 680 KB Output is correct
50 Correct 7 ms 596 KB Output is correct
51 Correct 8 ms 596 KB Output is correct
52 Correct 8 ms 712 KB Output is correct
53 Correct 9 ms 704 KB Output is correct
54 Correct 7 ms 596 KB Output is correct
55 Correct 7 ms 596 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 3 ms 468 KB Output is correct
58 Correct 11 ms 596 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 2 ms 340 KB Output is correct
62 Correct 185 ms 15392 KB Output is correct
63 Correct 200 ms 18388 KB Output is correct
64 Correct 193 ms 20928 KB Output is correct
65 Correct 266 ms 24312 KB Output is correct
66 Correct 273 ms 24404 KB Output is correct
67 Correct 323 ms 24296 KB Output is correct
68 Correct 388 ms 24324 KB Output is correct
69 Correct 397 ms 24308 KB Output is correct
70 Correct 294 ms 23580 KB Output is correct
71 Correct 290 ms 23460 KB Output is correct
72 Correct 294 ms 23532 KB Output is correct
73 Correct 120 ms 19552 KB Output is correct
74 Correct 275 ms 24112 KB Output is correct
75 Correct 100 ms 6584 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 574 ms 9860 KB Output is correct
78 Correct 835 ms 11768 KB Output is correct
79 Correct 711 ms 12132 KB Output is correct
80 Correct 1147 ms 17656 KB Output is correct
81 Correct 1098 ms 17664 KB Output is correct
82 Correct 1089 ms 17724 KB Output is correct
83 Correct 1062 ms 17704 KB Output is correct
84 Correct 1042 ms 17716 KB Output is correct
85 Correct 1083 ms 17688 KB Output is correct
86 Correct 1030 ms 17728 KB Output is correct
87 Correct 950 ms 17916 KB Output is correct
88 Correct 1073 ms 18160 KB Output is correct
89 Correct 1077 ms 19084 KB Output is correct
90 Correct 1037 ms 22132 KB Output is correct
91 Correct 1018 ms 18148 KB Output is correct
92 Correct 1072 ms 18088 KB Output is correct
93 Correct 975 ms 18188 KB Output is correct
94 Correct 645 ms 16228 KB Output is correct
95 Correct 600 ms 16224 KB Output is correct
96 Correct 670 ms 16288 KB Output is correct
97 Correct 639 ms 16188 KB Output is correct
98 Correct 624 ms 16216 KB Output is correct
99 Correct 595 ms 16140 KB Output is correct
100 Correct 593 ms 16200 KB Output is correct
101 Correct 598 ms 16244 KB Output is correct
102 Correct 574 ms 16244 KB Output is correct
103 Correct 684 ms 16236 KB Output is correct
104 Correct 596 ms 16424 KB Output is correct
105 Correct 581 ms 16452 KB Output is correct
106 Correct 579 ms 16628 KB Output is correct
107 Correct 686 ms 17084 KB Output is correct
108 Correct 690 ms 18480 KB Output is correct
109 Correct 686 ms 20700 KB Output is correct
110 Correct 1 ms 212 KB Output is correct
111 Correct 1 ms 212 KB Output is correct
112 Correct 2 ms 340 KB Output is correct
113 Correct 1352 ms 14588 KB Output is correct
114 Correct 1379 ms 14888 KB Output is correct
115 Correct 1632 ms 19192 KB Output is correct
116 Correct 1807 ms 23964 KB Output is correct
117 Correct 1767 ms 22348 KB Output is correct
118 Correct 1773 ms 23368 KB Output is correct
119 Correct 1755 ms 23880 KB Output is correct
120 Correct 1727 ms 24380 KB Output is correct
121 Correct 1662 ms 23600 KB Output is correct
122 Correct 1731 ms 23680 KB Output is correct
123 Correct 141 ms 7656 KB Output is correct
124 Correct 944 ms 23176 KB Output is correct
125 Correct 734 ms 20024 KB Output is correct
126 Correct 1184 ms 24108 KB Output is correct
127 Correct 1016 ms 21204 KB Output is correct
128 Correct 1043 ms 21248 KB Output is correct
129 Correct 1145 ms 21248 KB Output is correct
130 Correct 1016 ms 21256 KB Output is correct
131 Correct 350 ms 25332 KB Output is correct
132 Correct 285 ms 27108 KB Output is correct
133 Correct 303 ms 24756 KB Output is correct
134 Correct 675 ms 22692 KB Output is correct
135 Correct 659 ms 19724 KB Output is correct
136 Correct 695 ms 22540 KB Output is correct
137 Correct 447 ms 23924 KB Output is correct
138 Correct 456 ms 24076 KB Output is correct
139 Correct 452 ms 21188 KB Output is correct
140 Correct 501 ms 24036 KB Output is correct
141 Correct 482 ms 24064 KB Output is correct
142 Correct 506 ms 21204 KB Output is correct
143 Correct 212 ms 14780 KB Output is correct
144 Correct 1273 ms 23904 KB Output is correct