답안 #757819

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
757819 2023-06-13T19:03:41 Z taher Vinjete (COI22_vinjete) C++17
100 / 100
801 ms 20748 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "C:\GCC\debug.h"
#else
#define debug(...) void(42)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<array<int, 3>>> adj(n);
  vector<int> all;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    int l, r;
    cin >> l >> r;
    --l, --r;
    all.push_back(l);
    all.push_back(r);
    all.push_back(r + 1);
    adj[u].push_back({v, l, r});
    adj[v].push_back({u, l, r});
  }
  sort(all.begin(), all.end());
  all.erase(unique(all.begin(), all.end()), all.end());
  m = (int) all.size();

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (int) adj[i].size(); j++) {
      adj[i][j][1] = lower_bound(all.begin(), all.end(), adj[i][j][1]) - all.begin();
      adj[i][j][2] = lower_bound(all.begin(), all.end(), adj[i][j][2]) - all.begin();
    }
  }
  debug(all);
  int B = sqrt(m);
  int number_of_blocks = (m + B - 1) / B;
  vector<int> small(m);
  vector<int> large(number_of_blocks);
  vector<int> couches(number_of_blocks);

  auto Insert = [&](int l, int r) {
    int bL = l / B;
    int bR = r / B;

    if (bL == bR) {
      for (int i = l; i <= r; i++) {
        int add = all[i + 1] - all[i];
        if (small[i] == 0) {
          large[bL] += add;
        }
        ++small[i];
      }
    } else {
      for (int i = l; i < (bL + 1) * B; i++) {
        int add = all[i + 1] - all[i];
        if (small[i] == 0) {
          large[bL] += add;
        }
        ++small[i];
      }

      for (int i = bL + 1; i < bR; i++) {
        ++couches[i];
      }

      for (int i = bR * B; i <= r; i++) {
        int add = all[i + 1] - all[i];
        if (small[i] == 0) {
          large[bR] += add;
        }
        ++small[i];
      }
    }
  };

  auto Delete = [&](int l, int r) {
    int bL = l / B;
    int bR = r / B;

    if (bL == bR) {
      for (int i = l; i <= r; i++) {
        int add = all[i + 1] - all[i];
        if (small[i] == 1) {
          large[bL] -= add;
        }
        --small[i];
      }
    } else {
      for (int i = l; i < (bL + 1) * B; i++) {
        int add = all[i + 1] - all[i];
        if (small[i] == 1) {
          large[bL] -= add;
        }
        --small[i];
      }

      for (int i = bL + 1; i < bR; i++) {
        --couches[i];
      }

      for (int i = bR * B; i <= r; i++) {
        int add = all[i + 1] - all[i];
        if (small[i] == 1) {
          large[bR] -= add;
        }
        --small[i];
      }
    }
  };

  auto Compute = [&]() {
    int ans = 0;
    for (int i = 0; i < number_of_blocks; i++) {
      if (couches[i] > 0) {
        assert(i != number_of_blocks - 1);
        ans += all[(i + 1) * B] - all[i * B];
      } else {
        ans += large[i];
      }
    }
    return ans;
  };

  vector<int> res(n);

  function<void(int, int)> Dfs = [&](int u, int prev) {
    if (u > 0) {
      res[u] = Compute();
    }
    /*
    debug(u);
    debug(couches);
    debug(large);
    debug(small);
    */

    for (auto v : adj[u]) {
      if (v[0] == prev) {
        continue;
      }

      Insert(v[1], v[2]);

      Dfs(v[0], u);

      Delete(v[1], v[2]);
    }
  };
  Dfs(0, -1);

  for (int i = 1; i < n; i++) {
    cout << res[i] << '\n';
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 3 ms 456 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 324 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 212 KB Output is correct
24 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 3 ms 456 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 324 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 212 KB Output is correct
24 Correct 1 ms 320 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 2 ms 504 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
28 Correct 2 ms 500 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 488 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 368 KB Output is correct
44 Correct 2 ms 380 KB Output is correct
45 Correct 3 ms 340 KB Output is correct
46 Correct 3 ms 340 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 3 ms 340 KB Output is correct
49 Correct 2 ms 340 KB Output is correct
50 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 3 ms 456 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 324 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 212 KB Output is correct
24 Correct 1 ms 320 KB Output is correct
25 Correct 37 ms 7744 KB Output is correct
26 Correct 38 ms 7780 KB Output is correct
27 Correct 87 ms 7872 KB Output is correct
28 Correct 79 ms 7876 KB Output is correct
29 Correct 53 ms 7916 KB Output is correct
30 Correct 49 ms 7956 KB Output is correct
31 Correct 6 ms 1236 KB Output is correct
32 Correct 6 ms 1312 KB Output is correct
33 Correct 7 ms 1324 KB Output is correct
34 Correct 6 ms 1364 KB Output is correct
35 Correct 41 ms 4216 KB Output is correct
36 Correct 41 ms 4352 KB Output is correct
37 Correct 84 ms 4380 KB Output is correct
38 Correct 88 ms 4512 KB Output is correct
39 Correct 52 ms 4512 KB Output is correct
40 Correct 56 ms 4160 KB Output is correct
41 Correct 6 ms 852 KB Output is correct
42 Correct 6 ms 904 KB Output is correct
43 Correct 46 ms 3832 KB Output is correct
44 Correct 42 ms 3836 KB Output is correct
45 Correct 126 ms 4040 KB Output is correct
46 Correct 129 ms 4140 KB Output is correct
47 Correct 55 ms 3960 KB Output is correct
48 Correct 60 ms 4156 KB Output is correct
49 Correct 6 ms 724 KB Output is correct
50 Correct 6 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 3 ms 456 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 324 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 212 KB Output is correct
24 Correct 1 ms 320 KB Output is correct
25 Correct 37 ms 7744 KB Output is correct
26 Correct 38 ms 7780 KB Output is correct
27 Correct 87 ms 7872 KB Output is correct
28 Correct 79 ms 7876 KB Output is correct
29 Correct 53 ms 7916 KB Output is correct
30 Correct 49 ms 7956 KB Output is correct
31 Correct 6 ms 1236 KB Output is correct
32 Correct 6 ms 1312 KB Output is correct
33 Correct 7 ms 1324 KB Output is correct
34 Correct 6 ms 1364 KB Output is correct
35 Correct 41 ms 4216 KB Output is correct
36 Correct 41 ms 4352 KB Output is correct
37 Correct 84 ms 4380 KB Output is correct
38 Correct 88 ms 4512 KB Output is correct
39 Correct 52 ms 4512 KB Output is correct
40 Correct 56 ms 4160 KB Output is correct
41 Correct 6 ms 852 KB Output is correct
42 Correct 6 ms 904 KB Output is correct
43 Correct 46 ms 3832 KB Output is correct
44 Correct 42 ms 3836 KB Output is correct
45 Correct 126 ms 4040 KB Output is correct
46 Correct 129 ms 4140 KB Output is correct
47 Correct 55 ms 3960 KB Output is correct
48 Correct 60 ms 4156 KB Output is correct
49 Correct 6 ms 724 KB Output is correct
50 Correct 6 ms 716 KB Output is correct
51 Correct 95 ms 19092 KB Output is correct
52 Correct 121 ms 19132 KB Output is correct
53 Correct 222 ms 19392 KB Output is correct
54 Correct 250 ms 19400 KB Output is correct
55 Correct 134 ms 19300 KB Output is correct
56 Correct 133 ms 19368 KB Output is correct
57 Correct 20 ms 4256 KB Output is correct
58 Correct 20 ms 4176 KB Output is correct
59 Correct 20 ms 4196 KB Output is correct
60 Correct 26 ms 4208 KB Output is correct
61 Correct 131 ms 10392 KB Output is correct
62 Correct 110 ms 10680 KB Output is correct
63 Correct 329 ms 10396 KB Output is correct
64 Correct 253 ms 10584 KB Output is correct
65 Correct 165 ms 10748 KB Output is correct
66 Correct 149 ms 10308 KB Output is correct
67 Correct 25 ms 2476 KB Output is correct
68 Correct 24 ms 2344 KB Output is correct
69 Correct 146 ms 9176 KB Output is correct
70 Correct 135 ms 9224 KB Output is correct
71 Correct 472 ms 9504 KB Output is correct
72 Correct 453 ms 9540 KB Output is correct
73 Correct 200 ms 9236 KB Output is correct
74 Correct 164 ms 9252 KB Output is correct
75 Correct 25 ms 2224 KB Output is correct
76 Correct 31 ms 2196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 3 ms 456 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 324 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 212 KB Output is correct
24 Correct 1 ms 320 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 2 ms 504 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
28 Correct 2 ms 500 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 488 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 368 KB Output is correct
44 Correct 2 ms 380 KB Output is correct
45 Correct 3 ms 340 KB Output is correct
46 Correct 3 ms 340 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 3 ms 340 KB Output is correct
49 Correct 2 ms 340 KB Output is correct
50 Correct 2 ms 340 KB Output is correct
51 Correct 37 ms 7744 KB Output is correct
52 Correct 38 ms 7780 KB Output is correct
53 Correct 87 ms 7872 KB Output is correct
54 Correct 79 ms 7876 KB Output is correct
55 Correct 53 ms 7916 KB Output is correct
56 Correct 49 ms 7956 KB Output is correct
57 Correct 6 ms 1236 KB Output is correct
58 Correct 6 ms 1312 KB Output is correct
59 Correct 7 ms 1324 KB Output is correct
60 Correct 6 ms 1364 KB Output is correct
61 Correct 41 ms 4216 KB Output is correct
62 Correct 41 ms 4352 KB Output is correct
63 Correct 84 ms 4380 KB Output is correct
64 Correct 88 ms 4512 KB Output is correct
65 Correct 52 ms 4512 KB Output is correct
66 Correct 56 ms 4160 KB Output is correct
67 Correct 6 ms 852 KB Output is correct
68 Correct 6 ms 904 KB Output is correct
69 Correct 46 ms 3832 KB Output is correct
70 Correct 42 ms 3836 KB Output is correct
71 Correct 126 ms 4040 KB Output is correct
72 Correct 129 ms 4140 KB Output is correct
73 Correct 55 ms 3960 KB Output is correct
74 Correct 60 ms 4156 KB Output is correct
75 Correct 6 ms 724 KB Output is correct
76 Correct 6 ms 716 KB Output is correct
77 Correct 95 ms 19092 KB Output is correct
78 Correct 121 ms 19132 KB Output is correct
79 Correct 222 ms 19392 KB Output is correct
80 Correct 250 ms 19400 KB Output is correct
81 Correct 134 ms 19300 KB Output is correct
82 Correct 133 ms 19368 KB Output is correct
83 Correct 20 ms 4256 KB Output is correct
84 Correct 20 ms 4176 KB Output is correct
85 Correct 20 ms 4196 KB Output is correct
86 Correct 26 ms 4208 KB Output is correct
87 Correct 131 ms 10392 KB Output is correct
88 Correct 110 ms 10680 KB Output is correct
89 Correct 329 ms 10396 KB Output is correct
90 Correct 253 ms 10584 KB Output is correct
91 Correct 165 ms 10748 KB Output is correct
92 Correct 149 ms 10308 KB Output is correct
93 Correct 25 ms 2476 KB Output is correct
94 Correct 24 ms 2344 KB Output is correct
95 Correct 146 ms 9176 KB Output is correct
96 Correct 135 ms 9224 KB Output is correct
97 Correct 472 ms 9504 KB Output is correct
98 Correct 453 ms 9540 KB Output is correct
99 Correct 200 ms 9236 KB Output is correct
100 Correct 164 ms 9252 KB Output is correct
101 Correct 25 ms 2224 KB Output is correct
102 Correct 31 ms 2196 KB Output is correct
103 Correct 102 ms 19536 KB Output is correct
104 Correct 116 ms 19500 KB Output is correct
105 Correct 358 ms 20748 KB Output is correct
106 Correct 355 ms 20684 KB Output is correct
107 Correct 165 ms 19892 KB Output is correct
108 Correct 167 ms 19836 KB Output is correct
109 Correct 22 ms 4296 KB Output is correct
110 Correct 23 ms 4296 KB Output is correct
111 Correct 24 ms 4320 KB Output is correct
112 Correct 24 ms 4320 KB Output is correct
113 Correct 118 ms 11172 KB Output is correct
114 Correct 154 ms 10824 KB Output is correct
115 Correct 400 ms 11908 KB Output is correct
116 Correct 420 ms 12104 KB Output is correct
117 Correct 197 ms 11148 KB Output is correct
118 Correct 204 ms 10764 KB Output is correct
119 Correct 32 ms 2640 KB Output is correct
120 Correct 35 ms 2592 KB Output is correct
121 Correct 121 ms 9624 KB Output is correct
122 Correct 147 ms 9600 KB Output is correct
123 Correct 774 ms 11064 KB Output is correct
124 Correct 801 ms 10916 KB Output is correct
125 Correct 215 ms 9816 KB Output is correct
126 Correct 200 ms 9852 KB Output is correct
127 Correct 33 ms 2320 KB Output is correct
128 Correct 32 ms 2268 KB Output is correct