Submission #1065162

# Submission time Handle Problem Language Result Execution time Memory
1065162 2024-08-19T01:55:44 Z abczz Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
766 ms 267200 KB
#include "rainbow.h"
#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <map>
#define ll long long
 
using namespace std;
 
vector <array<ll, 2>> V;
map <array<ll, 2>, ll> mp, vert, edger, edgec;
string T = "NESW";
vector <ll> X[200001], VR[200001], ER[200001], EC[200001], G[200001];
ll mnx = 1e18, mny = 1e18, mxx = -1e18, mxy = -1e18;
ll x, y, r, c, nx, ny, tot, e, v, f, dj[4][2] = {0, 0, 0, 1, 1, 0, 1, 1}, dr[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
 
struct SegTree2D{
  vector <ll> st[800004];
  void build(ll id, ll l, ll r) {
    if (l == r) {
      st[id] = X[l];
      return;
    }
    ll mid = (l+r)/2;
    build(id*2, l, mid);
    build(id*2+1, mid+1, r);
    int i = 0, j = 0;
    while (i < st[id*2].size() && j < st[id*2+1].size()) {
      if (st[id*2][i] <= st[id*2+1][j]) st[id].push_back(st[id*2][i++]);
      else st[id].push_back(st[id*2+1][j++]);
    }
    while (i < st[id*2].size()) st[id].push_back(st[id*2][i++]);
    while (j < st[id*2+1].size()) st[id].push_back(st[id*2+1][j++]);
  }
  ll query(ll id, ll l, ll r, ll xl, ll xr, ll yl, ll yr) {
    if (xr < l || r < xl || xl > xr || yl > yr) return 0;
    else if (xl <= l && r <= xr) {
      auto it = lower_bound(st[id].begin(), st[id].end(), yl);
      auto it2 = lower_bound(st[id].begin(), st[id].end(), yr+1);
      return (it2-st[id].begin())-(it-st[id].begin());
    }
    ll mid = (l+r)/2;
    return query(id*2, l, mid, xl, xr, yl, yr) + query(id*2+1, mid+1, r, xl, xr, yl, yr);
  }
}STV, STER, STEC, ST;

void init(int R, int C, int sr, int sc, int M, char *S) {
  r = R, c = C;
  mp.clear();
  V.clear();
  V.push_back({sr, sc});
  x = sr, y = sc;
  for (int j=0; j<M; ++j) {
    auto u = S[j];
    for (int i=0; i<4; ++i) {
      if (u == T[i]) {
        nx = x + dr[i][0], ny = y + dr[i][1];
        x = nx, y = ny;
        V.push_back({x, y});
        break;
      }
    }
  }
  for (int i=0; i<V.size(); ++i) {
    --V[i][0], --V[i][1];
    mnx = min(mnx, V[i][0]);
    mxx = max(mxx, V[i][0]);
    mny = min(mny, V[i][1]);
    mxy = max(mxy, V[i][1]);
    x = V[i][0], y = V[i][1];
    if (!mp.count({x, y})) ++mp[{x, y}];
    for (int j=0; j<4; ++j) {
      nx = x + dj[j][0], ny = y + dj[j][1];
      ++vert[{nx, ny}];
    }
    ++edger[{x, y}];
    ++edger[{x+1, y}];
    ++edgec[{x, y}];
    ++edgec[{x, y+1}];
  }
  for (auto it = edger.begin(); it != edger.end();) {
    auto nx = next(it);
    auto [x, y] = it->first;
    ER[x].push_back(y);
    it = nx;
  }
  for (auto it = edgec.begin(); it != edgec.end();) {
    auto nx = next(it);
    auto [x, y] = it->first;
    EC[y].push_back(x);
    it = nx;
  }
  for (auto it = vert.begin(); it != vert.end();) {
    auto nxt = next(it);
    auto [x, y] = it->first;
    VR[x].push_back(y);
    it = nxt;
  }
  for (auto it = mp.begin(); it != mp.end();) {
    auto nxt = next(it);
    auto [x, y] = it->first;
    G[x].push_back(y);
    it = nxt;
  }
  for (int i=0; i<=R; ++i) {
    sort(VR[i].begin(), VR[i].end());
    sort(ER[i].begin(), ER[i].end());
  }
  for (int i=0; i<=C; ++i) {
    sort(EC[i].begin(), EC[i].end());
  }
  for (int i=0; i<=R; ++i) swap(X[i], VR[i]);
  STV.build(1, 0, R);
  for (int i=0; i<=R; ++i) swap(X[i], ER[i]);
  STER.build(1, 0, R);
  for (int i=0; i<=C; ++i) swap(X[i], EC[i]);
  STEC.build(1, 0, C);
  for (int i=0; i<=R; ++i) swap(X[i], G[i]);
  ST.build(1, 0, R);
}
 
int colour(int ar, int ac, int br, int bc) {
  --ar, --ac;
  f = 0;
  if (ar < mnx && mxx+1 < br && ac < mny && mxy+1 < bc) f = 1;
  e = STER.query(1, 0, r, ar+1, br-1, ac, bc-1) + STEC.query(1, 0, c, ac+1, bc-1, ar, br-1) + (br-ar+bc-ac) * 2;
  v = STV.query(1, 0, r, ar+1, br-1, ac+1, bc-1) + (br-ar+bc-ac) * 2;
  f += e-v+1;
  return f-ST.query(1, 0, r, ar, br-1, ac, bc-1);
}

Compilation message

rainbow.cpp: In member function 'void SegTree2D::build(long long int, long long int, long long int)':
rainbow.cpp:29:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     while (i < st[id*2].size() && j < st[id*2+1].size()) {
      |            ~~^~~~~~~~~~~~~~~~~
rainbow.cpp:29:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     while (i < st[id*2].size() && j < st[id*2+1].size()) {
      |                                   ~~^~~~~~~~~~~~~~~~~~~
rainbow.cpp:33:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     while (i < st[id*2].size()) st[id].push_back(st[id*2][i++]);
      |            ~~^~~~~~~~~~~~~~~~~
rainbow.cpp:34:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     while (j < st[id*2+1].size()) st[id].push_back(st[id*2+1][j++]);
      |            ~~^~~~~~~~~~~~~~~~~~~
rainbow.cpp: In function 'void init(int, int, int, int, int, char*)':
rainbow.cpp:65:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |   for (int i=0; i<V.size(); ++i) {
      |                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 99156 KB Output is correct
2 Correct 33 ms 99668 KB Output is correct
3 Correct 30 ms 99164 KB Output is correct
4 Correct 31 ms 99152 KB Output is correct
5 Correct 34 ms 99876 KB Output is correct
6 Correct 29 ms 98908 KB Output is correct
7 Correct 30 ms 98900 KB Output is correct
8 Correct 29 ms 98900 KB Output is correct
9 Correct 29 ms 98908 KB Output is correct
10 Correct 29 ms 99036 KB Output is correct
11 Correct 31 ms 99412 KB Output is correct
12 Correct 33 ms 99612 KB Output is correct
13 Correct 37 ms 100208 KB Output is correct
14 Correct 36 ms 100436 KB Output is correct
15 Correct 29 ms 98908 KB Output is correct
16 Correct 30 ms 98900 KB Output is correct
17 Correct 31 ms 98904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 98900 KB Output is correct
2 Correct 31 ms 98904 KB Output is correct
3 Correct 289 ms 149824 KB Output is correct
4 Correct 458 ms 182072 KB Output is correct
5 Correct 424 ms 181168 KB Output is correct
6 Correct 335 ms 164712 KB Output is correct
7 Correct 370 ms 164284 KB Output is correct
8 Correct 115 ms 104376 KB Output is correct
9 Correct 404 ms 182196 KB Output is correct
10 Correct 405 ms 181024 KB Output is correct
11 Correct 362 ms 164896 KB Output is correct
12 Correct 313 ms 176772 KB Output is correct
13 Correct 315 ms 182028 KB Output is correct
14 Correct 347 ms 180908 KB Output is correct
15 Correct 318 ms 164796 KB Output is correct
16 Correct 311 ms 159760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 98908 KB Output is correct
2 Correct 339 ms 240508 KB Output is correct
3 Correct 348 ms 259052 KB Output is correct
4 Correct 353 ms 263632 KB Output is correct
5 Correct 266 ms 215488 KB Output is correct
6 Correct 113 ms 127692 KB Output is correct
7 Correct 163 ms 154116 KB Output is correct
8 Correct 214 ms 170528 KB Output is correct
9 Correct 220 ms 165888 KB Output is correct
10 Correct 98 ms 125124 KB Output is correct
11 Correct 188 ms 167144 KB Output is correct
12 Correct 341 ms 240696 KB Output is correct
13 Correct 338 ms 259008 KB Output is correct
14 Correct 320 ms 263592 KB Output is correct
15 Correct 256 ms 215488 KB Output is correct
16 Correct 122 ms 122300 KB Output is correct
17 Correct 181 ms 154308 KB Output is correct
18 Correct 275 ms 241856 KB Output is correct
19 Correct 344 ms 250512 KB Output is correct
20 Correct 348 ms 250212 KB Output is correct
21 Correct 228 ms 170564 KB Output is correct
22 Correct 206 ms 166044 KB Output is correct
23 Correct 100 ms 125124 KB Output is correct
24 Correct 183 ms 166836 KB Output is correct
25 Correct 324 ms 240732 KB Output is correct
26 Correct 341 ms 259092 KB Output is correct
27 Correct 333 ms 263616 KB Output is correct
28 Correct 267 ms 215440 KB Output is correct
29 Correct 107 ms 122304 KB Output is correct
30 Correct 169 ms 154304 KB Output is correct
31 Correct 283 ms 242056 KB Output is correct
32 Correct 333 ms 250296 KB Output is correct
33 Correct 344 ms 250272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 99156 KB Output is correct
2 Correct 33 ms 99668 KB Output is correct
3 Correct 30 ms 99164 KB Output is correct
4 Correct 31 ms 99152 KB Output is correct
5 Correct 34 ms 99876 KB Output is correct
6 Correct 29 ms 98908 KB Output is correct
7 Correct 30 ms 98900 KB Output is correct
8 Correct 29 ms 98900 KB Output is correct
9 Correct 29 ms 98908 KB Output is correct
10 Correct 29 ms 99036 KB Output is correct
11 Correct 31 ms 99412 KB Output is correct
12 Correct 33 ms 99612 KB Output is correct
13 Correct 37 ms 100208 KB Output is correct
14 Correct 36 ms 100436 KB Output is correct
15 Correct 29 ms 98908 KB Output is correct
16 Correct 30 ms 98900 KB Output is correct
17 Correct 31 ms 98904 KB Output is correct
18 Correct 710 ms 156764 KB Output is correct
19 Correct 158 ms 104532 KB Output is correct
20 Correct 130 ms 102740 KB Output is correct
21 Correct 139 ms 103252 KB Output is correct
22 Correct 158 ms 103768 KB Output is correct
23 Correct 143 ms 104532 KB Output is correct
24 Correct 155 ms 102992 KB Output is correct
25 Correct 157 ms 103508 KB Output is correct
26 Correct 161 ms 104020 KB Output is correct
27 Correct 389 ms 146880 KB Output is correct
28 Correct 326 ms 124864 KB Output is correct
29 Correct 386 ms 142956 KB Output is correct
30 Correct 628 ms 206576 KB Output is correct
31 Correct 31 ms 99164 KB Output is correct
32 Correct 601 ms 148384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 99156 KB Output is correct
2 Correct 33 ms 99668 KB Output is correct
3 Correct 30 ms 99164 KB Output is correct
4 Correct 31 ms 99152 KB Output is correct
5 Correct 34 ms 99876 KB Output is correct
6 Correct 29 ms 98908 KB Output is correct
7 Correct 30 ms 98900 KB Output is correct
8 Correct 29 ms 98900 KB Output is correct
9 Correct 29 ms 98908 KB Output is correct
10 Correct 29 ms 99036 KB Output is correct
11 Correct 31 ms 99412 KB Output is correct
12 Correct 33 ms 99612 KB Output is correct
13 Correct 37 ms 100208 KB Output is correct
14 Correct 36 ms 100436 KB Output is correct
15 Correct 29 ms 98908 KB Output is correct
16 Correct 30 ms 98900 KB Output is correct
17 Correct 31 ms 98904 KB Output is correct
18 Correct 710 ms 156764 KB Output is correct
19 Correct 158 ms 104532 KB Output is correct
20 Correct 130 ms 102740 KB Output is correct
21 Correct 139 ms 103252 KB Output is correct
22 Correct 158 ms 103768 KB Output is correct
23 Correct 143 ms 104532 KB Output is correct
24 Correct 155 ms 102992 KB Output is correct
25 Correct 157 ms 103508 KB Output is correct
26 Correct 161 ms 104020 KB Output is correct
27 Correct 389 ms 146880 KB Output is correct
28 Correct 326 ms 124864 KB Output is correct
29 Correct 386 ms 142956 KB Output is correct
30 Correct 628 ms 206576 KB Output is correct
31 Correct 31 ms 99164 KB Output is correct
32 Correct 601 ms 148384 KB Output is correct
33 Correct 339 ms 240508 KB Output is correct
34 Correct 348 ms 259052 KB Output is correct
35 Correct 353 ms 263632 KB Output is correct
36 Correct 266 ms 215488 KB Output is correct
37 Correct 113 ms 127692 KB Output is correct
38 Correct 163 ms 154116 KB Output is correct
39 Correct 214 ms 170528 KB Output is correct
40 Correct 220 ms 165888 KB Output is correct
41 Correct 98 ms 125124 KB Output is correct
42 Correct 188 ms 167144 KB Output is correct
43 Correct 341 ms 240696 KB Output is correct
44 Correct 338 ms 259008 KB Output is correct
45 Correct 320 ms 263592 KB Output is correct
46 Correct 256 ms 215488 KB Output is correct
47 Correct 122 ms 122300 KB Output is correct
48 Correct 181 ms 154308 KB Output is correct
49 Correct 275 ms 241856 KB Output is correct
50 Correct 344 ms 250512 KB Output is correct
51 Correct 348 ms 250212 KB Output is correct
52 Correct 228 ms 170564 KB Output is correct
53 Correct 206 ms 166044 KB Output is correct
54 Correct 100 ms 125124 KB Output is correct
55 Correct 183 ms 166836 KB Output is correct
56 Correct 324 ms 240732 KB Output is correct
57 Correct 341 ms 259092 KB Output is correct
58 Correct 333 ms 263616 KB Output is correct
59 Correct 267 ms 215440 KB Output is correct
60 Correct 107 ms 122304 KB Output is correct
61 Correct 169 ms 154304 KB Output is correct
62 Correct 283 ms 242056 KB Output is correct
63 Correct 333 ms 250296 KB Output is correct
64 Correct 344 ms 250272 KB Output is correct
65 Correct 289 ms 149824 KB Output is correct
66 Correct 458 ms 182072 KB Output is correct
67 Correct 424 ms 181168 KB Output is correct
68 Correct 335 ms 164712 KB Output is correct
69 Correct 370 ms 164284 KB Output is correct
70 Correct 115 ms 104376 KB Output is correct
71 Correct 404 ms 182196 KB Output is correct
72 Correct 405 ms 181024 KB Output is correct
73 Correct 362 ms 164896 KB Output is correct
74 Correct 313 ms 176772 KB Output is correct
75 Correct 315 ms 182028 KB Output is correct
76 Correct 347 ms 180908 KB Output is correct
77 Correct 318 ms 164796 KB Output is correct
78 Correct 311 ms 159760 KB Output is correct
79 Correct 521 ms 174076 KB Output is correct
80 Correct 571 ms 169408 KB Output is correct
81 Correct 354 ms 128456 KB Output is correct
82 Correct 510 ms 170216 KB Output is correct
83 Correct 644 ms 244164 KB Output is correct
84 Correct 766 ms 262544 KB Output is correct
85 Correct 606 ms 267200 KB Output is correct
86 Correct 556 ms 219072 KB Output is correct
87 Correct 357 ms 125948 KB Output is correct
88 Correct 440 ms 157892 KB Output is correct
89 Correct 492 ms 245560 KB Output is correct
90 Correct 724 ms 253888 KB Output is correct
91 Correct 672 ms 253632 KB Output is correct