Submission #969166

# Submission time Handle Problem Language Result Execution time Memory
969166 2024-04-24T15:52:57 Z kilkuwu Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
635 ms 224776 KB
#include "rainbow.h"
#include <bits/stdc++.h>

#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif

constexpr int mxN = 2e5 + 5;

struct SegmentTree {
  struct Node {
    int v = 0, l = 0, r = 0;
  };

  std::vector<int> points[mxN];
  std::vector<Node> info = {Node(), Node()};
  int roots[mxN + 1];

  int copy(int k) {
    info.emplace_back(info[k]);
    return info.size() - 1;
  }

  void add_point(int x, int y) {
    points[x].push_back(y);
  }

  void build() {
    roots[0] = 1;
    for (int i = 0; i < mxN; i++) {
      roots[i + 1] = roots[i];
      std::sort(points[i].begin(), points[i].end());
      points[i].erase(std::unique(points[i].begin(), points[i].end()), points[i].end());

      for (int y : points[i]) {
        roots[i + 1] = update(roots[i + 1], 0, mxN, y, 1);
      }
    }
  }

  int update(int k, int l, int r, int p, int v) {
    int id = copy(k);
    info[id].v += v;
    if (r - l == 1) return id;
    int m = (l + r) / 2;
    if (p < m) {
      int left = update(info[id].l, l, m, p, v);
      info[id].l = left;
    } else {
      int right = update(info[id].r, m, r, p, v);
      info[id].r = right;
    }
    return id;
  }

  int query(int k, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) return info[k].v;
    int m = (l + r) / 2;
    int ans = 0;
    if (m > ql) {
      ans += query(info[k].l, l, m, ql, qr); 
    }
    if (m < qr) {
      ans += query(info[k].r, m, r, ql, qr);
    }
    return ans;
  }

  int query(int x1, int y1, int x2, int y2) {
    if (x1 >= x2 || y1 >= y2) return 0;
    return query(roots[x2], 0, mxN, y1, y2) - query(roots[x1], 0, mxN, y1, y2);
  }
} rivers, vertices, edges_ho, edges_ve;

void add_river(int x, int y) {
  dbg(x, y);
  rivers.add_point(x, y);
  vertices.add_point(x, y);
  vertices.add_point(x + 1, y);
  vertices.add_point(x, y + 1);
  vertices.add_point(x + 1, y + 1);
  edges_ho.add_point(x, y);
  edges_ho.add_point(x + 1, y);
  edges_ve.add_point(x, y);
  edges_ve.add_point(x, y + 1);
}

int mn_x, mx_x, mn_y, mx_y;

void init(int R, int C, int sr, int sc, int M, char *S) {
  mn_x = mx_x = sr;
  mn_y = mx_y = sc;

  add_river(sr, sc);
  for (int i = 0; i < M; i++) {
    if (S[i] == 'N') sr--;
    else if (S[i] == 'S') sr++;
    else if (S[i] == 'W') sc--;
    else sc++;
    add_river(sr, sc);
    mn_x = std::min(mn_x, sr);
    mx_x = std::max(mx_x, sr);
    mn_y = std::min(mn_y, sc);
    mx_y = std::max(mx_y, sc);
  }

  rivers.build();
  vertices.build();
  edges_ho.build();
  edges_ve.build();
}

int colour(int ar, int ac, int br, int bc) {
  dbg(ar, ac, br, bc);
  int E = edges_ho.query(ar + 1, ac, br + 1, bc + 1) + edges_ve.query(ar, ac + 1, br + 1, bc + 1);
  E += (br - ar + 1 + bc - ac + 1) * 2;
  int V = vertices.query(ar + 1, ac + 1, br + 1, bc + 1);
  V += (br - ar) * 2 + (bc - ac + 2) * 2;
  int R = rivers.query(ar, ac, br + 1, bc + 1);
  int C = (ar < mn_x && br > mx_x && ac < mn_y && bc > mx_y) ? 2 : 1;
  dbg(E, V, R, C);
  return E - V + C - R;
}

# Verdict Execution time Memory Grader output
1 Correct 8 ms 22616 KB Output is correct
2 Correct 11 ms 23572 KB Output is correct
3 Correct 11 ms 22620 KB Output is correct
4 Correct 10 ms 22868 KB Output is correct
5 Correct 11 ms 23908 KB Output is correct
6 Correct 7 ms 22360 KB Output is correct
7 Correct 7 ms 22328 KB Output is correct
8 Correct 7 ms 22364 KB Output is correct
9 Correct 7 ms 22360 KB Output is correct
10 Correct 7 ms 22360 KB Output is correct
11 Correct 10 ms 22876 KB Output is correct
12 Correct 11 ms 23576 KB Output is correct
13 Correct 12 ms 24660 KB Output is correct
14 Correct 14 ms 25308 KB Output is correct
15 Correct 7 ms 22364 KB Output is correct
16 Correct 7 ms 22360 KB Output is correct
17 Correct 7 ms 22360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 22360 KB Output is correct
2 Correct 7 ms 22360 KB Output is correct
3 Correct 344 ms 147436 KB Output is correct
4 Correct 394 ms 191904 KB Output is correct
5 Correct 370 ms 187676 KB Output is correct
6 Correct 344 ms 161908 KB Output is correct
7 Correct 358 ms 158860 KB Output is correct
8 Correct 208 ms 30832 KB Output is correct
9 Correct 397 ms 192272 KB Output is correct
10 Correct 419 ms 188004 KB Output is correct
11 Correct 379 ms 161160 KB Output is correct
12 Correct 263 ms 183308 KB Output is correct
13 Correct 284 ms 191576 KB Output is correct
14 Correct 292 ms 187580 KB Output is correct
15 Correct 255 ms 162068 KB Output is correct
16 Correct 323 ms 160396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 22364 KB Output is correct
2 Correct 136 ms 186728 KB Output is correct
3 Correct 157 ms 224276 KB Output is correct
4 Correct 154 ms 207148 KB Output is correct
5 Correct 133 ms 182892 KB Output is correct
6 Correct 64 ms 69776 KB Output is correct
7 Correct 91 ms 107200 KB Output is correct
8 Correct 155 ms 174488 KB Output is correct
9 Correct 137 ms 162840 KB Output is correct
10 Correct 53 ms 64868 KB Output is correct
11 Correct 92 ms 120224 KB Output is correct
12 Correct 132 ms 189064 KB Output is correct
13 Correct 150 ms 222480 KB Output is correct
14 Correct 154 ms 207212 KB Output is correct
15 Correct 129 ms 180148 KB Output is correct
16 Correct 55 ms 55484 KB Output is correct
17 Correct 91 ms 105964 KB Output is correct
18 Correct 152 ms 203192 KB Output is correct
19 Correct 151 ms 206244 KB Output is correct
20 Correct 163 ms 208304 KB Output is correct
21 Correct 146 ms 176000 KB Output is correct
22 Correct 134 ms 161820 KB Output is correct
23 Correct 53 ms 63948 KB Output is correct
24 Correct 102 ms 118952 KB Output is correct
25 Correct 133 ms 189676 KB Output is correct
26 Correct 147 ms 220692 KB Output is correct
27 Correct 155 ms 206308 KB Output is correct
28 Correct 139 ms 183084 KB Output is correct
29 Correct 53 ms 53656 KB Output is correct
30 Correct 87 ms 106920 KB Output is correct
31 Correct 151 ms 204168 KB Output is correct
32 Correct 158 ms 207604 KB Output is correct
33 Correct 171 ms 209068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 22616 KB Output is correct
2 Correct 11 ms 23572 KB Output is correct
3 Correct 11 ms 22620 KB Output is correct
4 Correct 10 ms 22868 KB Output is correct
5 Correct 11 ms 23908 KB Output is correct
6 Correct 7 ms 22360 KB Output is correct
7 Correct 7 ms 22328 KB Output is correct
8 Correct 7 ms 22364 KB Output is correct
9 Correct 7 ms 22360 KB Output is correct
10 Correct 7 ms 22360 KB Output is correct
11 Correct 10 ms 22876 KB Output is correct
12 Correct 11 ms 23576 KB Output is correct
13 Correct 12 ms 24660 KB Output is correct
14 Correct 14 ms 25308 KB Output is correct
15 Correct 7 ms 22364 KB Output is correct
16 Correct 7 ms 22360 KB Output is correct
17 Correct 7 ms 22360 KB Output is correct
18 Correct 502 ms 124468 KB Output is correct
19 Correct 201 ms 29668 KB Output is correct
20 Correct 156 ms 26396 KB Output is correct
21 Correct 176 ms 27164 KB Output is correct
22 Correct 176 ms 27588 KB Output is correct
23 Correct 191 ms 29332 KB Output is correct
24 Correct 164 ms 27284 KB Output is correct
25 Correct 175 ms 27848 KB Output is correct
26 Correct 179 ms 27976 KB Output is correct
27 Correct 318 ms 113400 KB Output is correct
28 Correct 247 ms 69064 KB Output is correct
29 Correct 293 ms 104400 KB Output is correct
30 Correct 394 ms 203956 KB Output is correct
31 Correct 12 ms 22360 KB Output is correct
32 Correct 354 ms 104248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 22616 KB Output is correct
2 Correct 11 ms 23572 KB Output is correct
3 Correct 11 ms 22620 KB Output is correct
4 Correct 10 ms 22868 KB Output is correct
5 Correct 11 ms 23908 KB Output is correct
6 Correct 7 ms 22360 KB Output is correct
7 Correct 7 ms 22328 KB Output is correct
8 Correct 7 ms 22364 KB Output is correct
9 Correct 7 ms 22360 KB Output is correct
10 Correct 7 ms 22360 KB Output is correct
11 Correct 10 ms 22876 KB Output is correct
12 Correct 11 ms 23576 KB Output is correct
13 Correct 12 ms 24660 KB Output is correct
14 Correct 14 ms 25308 KB Output is correct
15 Correct 7 ms 22364 KB Output is correct
16 Correct 7 ms 22360 KB Output is correct
17 Correct 7 ms 22360 KB Output is correct
18 Correct 502 ms 124468 KB Output is correct
19 Correct 201 ms 29668 KB Output is correct
20 Correct 156 ms 26396 KB Output is correct
21 Correct 176 ms 27164 KB Output is correct
22 Correct 176 ms 27588 KB Output is correct
23 Correct 191 ms 29332 KB Output is correct
24 Correct 164 ms 27284 KB Output is correct
25 Correct 175 ms 27848 KB Output is correct
26 Correct 179 ms 27976 KB Output is correct
27 Correct 318 ms 113400 KB Output is correct
28 Correct 247 ms 69064 KB Output is correct
29 Correct 293 ms 104400 KB Output is correct
30 Correct 394 ms 203956 KB Output is correct
31 Correct 12 ms 22360 KB Output is correct
32 Correct 354 ms 104248 KB Output is correct
33 Correct 136 ms 186728 KB Output is correct
34 Correct 157 ms 224276 KB Output is correct
35 Correct 154 ms 207148 KB Output is correct
36 Correct 133 ms 182892 KB Output is correct
37 Correct 64 ms 69776 KB Output is correct
38 Correct 91 ms 107200 KB Output is correct
39 Correct 155 ms 174488 KB Output is correct
40 Correct 137 ms 162840 KB Output is correct
41 Correct 53 ms 64868 KB Output is correct
42 Correct 92 ms 120224 KB Output is correct
43 Correct 132 ms 189064 KB Output is correct
44 Correct 150 ms 222480 KB Output is correct
45 Correct 154 ms 207212 KB Output is correct
46 Correct 129 ms 180148 KB Output is correct
47 Correct 55 ms 55484 KB Output is correct
48 Correct 91 ms 105964 KB Output is correct
49 Correct 152 ms 203192 KB Output is correct
50 Correct 151 ms 206244 KB Output is correct
51 Correct 163 ms 208304 KB Output is correct
52 Correct 146 ms 176000 KB Output is correct
53 Correct 134 ms 161820 KB Output is correct
54 Correct 53 ms 63948 KB Output is correct
55 Correct 102 ms 118952 KB Output is correct
56 Correct 133 ms 189676 KB Output is correct
57 Correct 147 ms 220692 KB Output is correct
58 Correct 155 ms 206308 KB Output is correct
59 Correct 139 ms 183084 KB Output is correct
60 Correct 53 ms 53656 KB Output is correct
61 Correct 87 ms 106920 KB Output is correct
62 Correct 151 ms 204168 KB Output is correct
63 Correct 158 ms 207604 KB Output is correct
64 Correct 171 ms 209068 KB Output is correct
65 Correct 344 ms 147436 KB Output is correct
66 Correct 394 ms 191904 KB Output is correct
67 Correct 370 ms 187676 KB Output is correct
68 Correct 344 ms 161908 KB Output is correct
69 Correct 358 ms 158860 KB Output is correct
70 Correct 208 ms 30832 KB Output is correct
71 Correct 397 ms 192272 KB Output is correct
72 Correct 419 ms 188004 KB Output is correct
73 Correct 379 ms 161160 KB Output is correct
74 Correct 263 ms 183308 KB Output is correct
75 Correct 284 ms 191576 KB Output is correct
76 Correct 292 ms 187580 KB Output is correct
77 Correct 255 ms 162068 KB Output is correct
78 Correct 323 ms 160396 KB Output is correct
79 Correct 617 ms 178528 KB Output is correct
80 Correct 635 ms 165468 KB Output is correct
81 Correct 264 ms 68652 KB Output is correct
82 Correct 338 ms 123424 KB Output is correct
83 Correct 432 ms 191824 KB Output is correct
84 Correct 417 ms 224776 KB Output is correct
85 Correct 416 ms 205944 KB Output is correct
86 Correct 400 ms 186916 KB Output is correct
87 Correct 295 ms 53428 KB Output is correct
88 Correct 337 ms 105892 KB Output is correct
89 Correct 399 ms 204920 KB Output is correct
90 Correct 459 ms 206500 KB Output is correct
91 Correct 441 ms 208732 KB Output is correct