답안 #883165

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
883165 2023-12-04T17:00:09 Z nguyentunglam 무지개나라 (APIO17_rainbow) C++17
100 / 100
364 ms 286976 KB
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;

const int N = 2e5 + 10, S = 2e5 * 100 + 10;

vector<int> cell[N], one[N], two[N], three[N];

struct node {
  int left, right;
  int sum[4];
};

node T[S];

int r, c, cnt;

void up(int s, int l, int r, int pos, int val, int type) {
  if (l == r) {
    T[s].sum[type] += val;
    return;
  }
  int mid = l + r >> 1;
  if (pos <= mid) {
    T[++cnt] = T[T[s].left];
    T[s].left = cnt;
    up(T[s].left, l, mid, pos, val, type);
  }
  else {
    T[++cnt] = T[T[s].right];
    T[s].right = cnt;
    up(T[s].right, mid + 1, r, pos, val, type);
  }
  T[s].sum[type] = T[T[s].left].sum[type] + T[T[s].right].sum[type];
}

int get(int s, int l, int r, int from, int to, int type) {
  if (l > to || r < from || !s) return 0;
  if (from <= l && r <= to) return T[s].sum[type];
  int mid = l + r >> 1;
  return get(T[s].left, l, mid, from, to, type) + get(T[s].right, mid + 1, r, from, to, type);
}

int it[N];

bool check (int x, int y) {
  int idx = lower_bound(cell[x].begin(), cell[x].end(), y) - cell[x].begin();
  if (idx < cell[x].size() && cell[x][idx] == y) return 1;
  return 0;
}

int query (int x, int y, int u, int v, int type) {
  return get(it[u], 1, c, y, v, type) - get(it[x - 1], 1, c, y, v, type);
}

int x, y, minx, miny, maxx, maxy;


void init(int R, int C, int sr, int sc, int m, char *s) {
  r = R;
  c = C;

  x = sr, y = sc, minx = x, maxx = x, miny = y, maxy = y;
  auto add = [&] () {
    cell[x].push_back(y);
    one[x].push_back(y);
    one[x - 1].push_back(y);
    two[x].push_back(y);
    two[x].push_back(y - 1);
    three[x].push_back(y);
    three[x].push_back(y - 1);
    three[x - 1].push_back(y);
    three[x - 1].push_back(y - 1);
    minx = min(minx, x);
    maxx = max(maxx, x);
    miny = min(miny, y);
    maxy = max(maxy, y);
  };

  add();
  for(int i = 0; i < m; i++) {
    if (s[i] == 'N') x--;
    if (s[i] == 'S') x++;

    if (s[i] == 'W') y--;
    if (s[i] == 'E') y++;
    add();
  }


  auto zip = [&] (vector<int> &v) {
    sort(all(v));
    v.resize(unique(all(v)) - v.begin());
  };


  for(int i = 1; i <= r; i++) {
    T[it[i] = ++cnt] = T[it[i - 1]];

    zip(cell[i]);
    zip(one[i]);
    zip(two[i]);
    zip(three[i]);

    for(int &j : cell[i]) {
      up(it[i], 1, c, j, 1, 0);
    }
    for(int &j : one[i]) if (j) {
      up(it[i], 1, c, j, 1, 1);
    }
    for(int &j : two[i]) if (j) up(it[i], 1, c, j, 1, 2);
    for(int &j : three[i]) if (j) up(it[i], 1, c, j, 1, 3);
  }
}


int colour(int x, int y, int u, int v) {
  int E = (u - x + 2) * 2 + (v - y + 2) * 2;
  if (x < u) E += query(x, y, u - 1, v, 1);
  if (y < v) E += query(x, y, u, v - 1, 2);
  int R = (u - x + 3) * (v - y + 3) - (u - x + 1) * (v - y + 1) + query(x, y, u, v, 0);
  if (x < u && y < v) E -= query(x, y, u - 1, v - 1, 3);
  int ans = E + 1 - R;
  if (x < minx && maxx < u && y < miny && maxy < v) ans++;
  return ans;
}



#ifdef ngu
int32_t main() {
  freopen ("task.inp", "r", stdin);
  freopen ("task.out", "w", stdout);

  int r, c, m, q; cin >> r >> c >> m >> q;

  int sr, sc; cin >> sr >> sc;

  string s; cin >> s;

  init(r, c, sr, sc, m, &s[0]);

  for(int i = 1; i <= q; i++) {
    int ar, ac, br, bc; cin >> ar >> ac >> br >> bc;
    cout << colour(ar, ac, br, bc) << endl;
  }
}
#endif // ngu


Compilation message

rainbow.cpp: In function 'void up(int, int, int, int, int, int)':
rainbow.cpp:24:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   24 |   int mid = l + r >> 1;
      |             ~~^~~
rainbow.cpp: In function 'int get(int, int, int, int, int, int)':
rainbow.cpp:41:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   41 |   int mid = l + r >> 1;
      |             ~~^~~
rainbow.cpp: In function 'bool check(int, int)':
rainbow.cpp:49:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   if (idx < cell[x].size() && cell[x][idx] == y) return 1;
      |       ~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21636 KB Output is correct
2 Correct 7 ms 21736 KB Output is correct
3 Correct 5 ms 21592 KB Output is correct
4 Correct 6 ms 21592 KB Output is correct
5 Correct 7 ms 21596 KB Output is correct
6 Correct 5 ms 21592 KB Output is correct
7 Correct 5 ms 21596 KB Output is correct
8 Correct 4 ms 21592 KB Output is correct
9 Correct 4 ms 21596 KB Output is correct
10 Correct 4 ms 21596 KB Output is correct
11 Correct 6 ms 21724 KB Output is correct
12 Correct 7 ms 21596 KB Output is correct
13 Correct 6 ms 21596 KB Output is correct
14 Correct 7 ms 21884 KB Output is correct
15 Correct 4 ms 21596 KB Output is correct
16 Correct 5 ms 21596 KB Output is correct
17 Correct 4 ms 21596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 21596 KB Output is correct
2 Correct 4 ms 21596 KB Output is correct
3 Correct 183 ms 152120 KB Output is correct
4 Correct 279 ms 236952 KB Output is correct
5 Correct 267 ms 232420 KB Output is correct
6 Correct 206 ms 181428 KB Output is correct
7 Correct 231 ms 175140 KB Output is correct
8 Correct 75 ms 29620 KB Output is correct
9 Correct 272 ms 234400 KB Output is correct
10 Correct 279 ms 232752 KB Output is correct
11 Correct 212 ms 181064 KB Output is correct
12 Correct 193 ms 230304 KB Output is correct
13 Correct 202 ms 236580 KB Output is correct
14 Correct 200 ms 232728 KB Output is correct
15 Correct 195 ms 183240 KB Output is correct
16 Correct 184 ms 163988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 21596 KB Output is correct
2 Correct 199 ms 280428 KB Output is correct
3 Correct 148 ms 282704 KB Output is correct
4 Correct 142 ms 283240 KB Output is correct
5 Correct 136 ms 218520 KB Output is correct
6 Correct 64 ms 78508 KB Output is correct
7 Correct 77 ms 123216 KB Output is correct
8 Correct 135 ms 190792 KB Output is correct
9 Correct 123 ms 172344 KB Output is correct
10 Correct 39 ms 56916 KB Output is correct
11 Correct 89 ms 131288 KB Output is correct
12 Correct 190 ms 280488 KB Output is correct
13 Correct 153 ms 282808 KB Output is correct
14 Correct 141 ms 283372 KB Output is correct
15 Correct 133 ms 218300 KB Output is correct
16 Correct 62 ms 67884 KB Output is correct
17 Correct 79 ms 123720 KB Output is correct
18 Correct 143 ms 278916 KB Output is correct
19 Correct 149 ms 243324 KB Output is correct
20 Correct 151 ms 243512 KB Output is correct
21 Correct 136 ms 190908 KB Output is correct
22 Correct 121 ms 172180 KB Output is correct
23 Correct 38 ms 56948 KB Output is correct
24 Correct 90 ms 131124 KB Output is correct
25 Correct 190 ms 280484 KB Output is correct
26 Correct 149 ms 282964 KB Output is correct
27 Correct 141 ms 283476 KB Output is correct
28 Correct 136 ms 218264 KB Output is correct
29 Correct 50 ms 67664 KB Output is correct
30 Correct 85 ms 123532 KB Output is correct
31 Correct 145 ms 279124 KB Output is correct
32 Correct 149 ms 243384 KB Output is correct
33 Correct 150 ms 243384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21636 KB Output is correct
2 Correct 7 ms 21736 KB Output is correct
3 Correct 5 ms 21592 KB Output is correct
4 Correct 6 ms 21592 KB Output is correct
5 Correct 7 ms 21596 KB Output is correct
6 Correct 5 ms 21592 KB Output is correct
7 Correct 5 ms 21596 KB Output is correct
8 Correct 4 ms 21592 KB Output is correct
9 Correct 4 ms 21596 KB Output is correct
10 Correct 4 ms 21596 KB Output is correct
11 Correct 6 ms 21724 KB Output is correct
12 Correct 7 ms 21596 KB Output is correct
13 Correct 6 ms 21596 KB Output is correct
14 Correct 7 ms 21884 KB Output is correct
15 Correct 4 ms 21596 KB Output is correct
16 Correct 5 ms 21596 KB Output is correct
17 Correct 4 ms 21596 KB Output is correct
18 Correct 236 ms 97120 KB Output is correct
19 Correct 160 ms 24368 KB Output is correct
20 Correct 134 ms 22352 KB Output is correct
21 Correct 138 ms 22196 KB Output is correct
22 Correct 136 ms 24324 KB Output is correct
23 Correct 158 ms 24404 KB Output is correct
24 Correct 119 ms 22332 KB Output is correct
25 Correct 125 ms 22360 KB Output is correct
26 Correct 134 ms 24556 KB Output is correct
27 Correct 125 ms 84816 KB Output is correct
28 Correct 131 ms 54096 KB Output is correct
29 Correct 129 ms 79016 KB Output is correct
30 Correct 207 ms 166484 KB Output is correct
31 Correct 6 ms 21592 KB Output is correct
32 Correct 192 ms 84220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21636 KB Output is correct
2 Correct 7 ms 21736 KB Output is correct
3 Correct 5 ms 21592 KB Output is correct
4 Correct 6 ms 21592 KB Output is correct
5 Correct 7 ms 21596 KB Output is correct
6 Correct 5 ms 21592 KB Output is correct
7 Correct 5 ms 21596 KB Output is correct
8 Correct 4 ms 21592 KB Output is correct
9 Correct 4 ms 21596 KB Output is correct
10 Correct 4 ms 21596 KB Output is correct
11 Correct 6 ms 21724 KB Output is correct
12 Correct 7 ms 21596 KB Output is correct
13 Correct 6 ms 21596 KB Output is correct
14 Correct 7 ms 21884 KB Output is correct
15 Correct 4 ms 21596 KB Output is correct
16 Correct 5 ms 21596 KB Output is correct
17 Correct 4 ms 21596 KB Output is correct
18 Correct 236 ms 97120 KB Output is correct
19 Correct 160 ms 24368 KB Output is correct
20 Correct 134 ms 22352 KB Output is correct
21 Correct 138 ms 22196 KB Output is correct
22 Correct 136 ms 24324 KB Output is correct
23 Correct 158 ms 24404 KB Output is correct
24 Correct 119 ms 22332 KB Output is correct
25 Correct 125 ms 22360 KB Output is correct
26 Correct 134 ms 24556 KB Output is correct
27 Correct 125 ms 84816 KB Output is correct
28 Correct 131 ms 54096 KB Output is correct
29 Correct 129 ms 79016 KB Output is correct
30 Correct 207 ms 166484 KB Output is correct
31 Correct 6 ms 21592 KB Output is correct
32 Correct 192 ms 84220 KB Output is correct
33 Correct 199 ms 280428 KB Output is correct
34 Correct 148 ms 282704 KB Output is correct
35 Correct 142 ms 283240 KB Output is correct
36 Correct 136 ms 218520 KB Output is correct
37 Correct 64 ms 78508 KB Output is correct
38 Correct 77 ms 123216 KB Output is correct
39 Correct 135 ms 190792 KB Output is correct
40 Correct 123 ms 172344 KB Output is correct
41 Correct 39 ms 56916 KB Output is correct
42 Correct 89 ms 131288 KB Output is correct
43 Correct 190 ms 280488 KB Output is correct
44 Correct 153 ms 282808 KB Output is correct
45 Correct 141 ms 283372 KB Output is correct
46 Correct 133 ms 218300 KB Output is correct
47 Correct 62 ms 67884 KB Output is correct
48 Correct 79 ms 123720 KB Output is correct
49 Correct 143 ms 278916 KB Output is correct
50 Correct 149 ms 243324 KB Output is correct
51 Correct 151 ms 243512 KB Output is correct
52 Correct 136 ms 190908 KB Output is correct
53 Correct 121 ms 172180 KB Output is correct
54 Correct 38 ms 56948 KB Output is correct
55 Correct 90 ms 131124 KB Output is correct
56 Correct 190 ms 280484 KB Output is correct
57 Correct 149 ms 282964 KB Output is correct
58 Correct 141 ms 283476 KB Output is correct
59 Correct 136 ms 218264 KB Output is correct
60 Correct 50 ms 67664 KB Output is correct
61 Correct 85 ms 123532 KB Output is correct
62 Correct 145 ms 279124 KB Output is correct
63 Correct 149 ms 243384 KB Output is correct
64 Correct 150 ms 243384 KB Output is correct
65 Correct 183 ms 152120 KB Output is correct
66 Correct 279 ms 236952 KB Output is correct
67 Correct 267 ms 232420 KB Output is correct
68 Correct 206 ms 181428 KB Output is correct
69 Correct 231 ms 175140 KB Output is correct
70 Correct 75 ms 29620 KB Output is correct
71 Correct 272 ms 234400 KB Output is correct
72 Correct 279 ms 232752 KB Output is correct
73 Correct 212 ms 181064 KB Output is correct
74 Correct 193 ms 230304 KB Output is correct
75 Correct 202 ms 236580 KB Output is correct
76 Correct 200 ms 232728 KB Output is correct
77 Correct 195 ms 183240 KB Output is correct
78 Correct 184 ms 163988 KB Output is correct
79 Correct 354 ms 194276 KB Output is correct
80 Correct 364 ms 175808 KB Output is correct
81 Correct 154 ms 60500 KB Output is correct
82 Correct 191 ms 134632 KB Output is correct
83 Correct 330 ms 284032 KB Output is correct
84 Correct 231 ms 286544 KB Output is correct
85 Correct 230 ms 286976 KB Output is correct
86 Correct 216 ms 221868 KB Output is correct
87 Correct 109 ms 71252 KB Output is correct
88 Correct 161 ms 127056 KB Output is correct
89 Correct 224 ms 282452 KB Output is correct
90 Correct 277 ms 246832 KB Output is correct
91 Correct 331 ms 246968 KB Output is correct