#include "rainbow.h"
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int N = 1 << 18, M = N * 40;
int tr[M], lv[M], rv[M], sz = 1, rt[N];
set<pair<int, int>> ss, ts;
map<int, vector<int>> mh, mv;
int lx, rx, ly, ry;
int add(int v, int p, int x, int l=0, int r=N) {
int cv = sz++;
tr[cv] = tr[v] + x;
if (r - l == 1)
lv[cv] = rv[cv] = -1;
else if (p < (l + r) / 2) {
lv[cv] = add(lv[v], p, x, l, (l + r) / 2);
rv[cv] = rv[v];
}
else {
lv[cv] = lv[v];
rv[cv] = add(rv[v], p, x, (l + r) / 2, r);
}
return cv;
}
int sum(int v, int cl, int cr, int l=0, int r=N) {
if (v == -1 || cr <= l || r <= cl)
return 0;
if (cl <= l && r <= cr)
return tr[v];
return sum(lv[v], cl, cr, l, (l + r) / 2) + sum(rv[v], cl, cr, (l + r) / 2, r);
}
void init(int r, int c, int sr, int sc, int m, char *s) {
lx = rx = sr;
ly = ry = sc;
ss.insert({sr, sc});
rep(i, m) {
if (s[i] == 'N') sr--;
else if (s[i] == 'S') sr++;
else if (s[i] == 'W') sc--;
else sc++;
lx = min(lx, sr);
rx = max(rx, sr);
ly = min(ly, sc);
ry = max(ry, sc);
ss.insert({sr, sc});
}
for (auto &p : ss) {
// cout << p.first << ' ' << p.second << '\n';
ts.insert(p);
if (p.first > 1) {
ts.insert({p.first - 1, p.second});
if (p.second > 1)
ts.insert({p.first - 1, p.second - 1});
}
if (p.second > 1)
ts.insert({p.first, p.second - 1});
}
int crt = 0, ls = 0;
for (auto &p : ts) {
while (ls < p.first)
rt[ls++] = crt;
int val = -1;
if (ss.count(p)) {
val += 1;
if (!ss.count({p.first, p.second + 1}))
mh[p.first].push_back(p.second);
if (!ss.count({p.first + 1, p.second}))
mv[p.second].push_back(p.first);
}
else {
if (ss.count({p.first, p.second + 1}))
val++;
if (ss.count({p.first + 1, p.second}))
val++;
}
crt = add(crt, p.second, val);
}
while (ls < N)
rt[ls++] = crt;
for (auto &p : mh)
sort(all(p.second));
for (auto &p : mv)
sort(all(p.second));
}
int colour(int ar, int ac, int br, int bc) {
ll ans = 1 + sum(rt[br - 1], ac, bc) - sum(rt[ar - 1], ac, bc);
if (mh.count(br))
ans += lower_bound(all(mh[br]), bc) - lower_bound(all(mh[br]), ac);
if (mv.count(bc))
ans += lower_bound(all(mv[bc]), br) - lower_bound(all(mv[bc]), ar);
if (ss.count({br, ac}))
ans--;
if (ss.count({ar, bc}))
ans--;
if (ss.count({br, bc}))
ans++;
if (ar < lx && rx < br && ac < ly && ry < bc)
ans++;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1408 KB |
Output is correct |
2 |
Correct |
6 ms |
1792 KB |
Output is correct |
3 |
Correct |
5 ms |
1536 KB |
Output is correct |
4 |
Correct |
3 ms |
1536 KB |
Output is correct |
5 |
Correct |
4 ms |
1920 KB |
Output is correct |
6 |
Correct |
2 ms |
1408 KB |
Output is correct |
7 |
Correct |
1 ms |
1408 KB |
Output is correct |
8 |
Correct |
1 ms |
1408 KB |
Output is correct |
9 |
Correct |
1 ms |
1408 KB |
Output is correct |
10 |
Correct |
1 ms |
1408 KB |
Output is correct |
11 |
Correct |
3 ms |
1536 KB |
Output is correct |
12 |
Correct |
4 ms |
1664 KB |
Output is correct |
13 |
Correct |
5 ms |
2176 KB |
Output is correct |
14 |
Correct |
6 ms |
2176 KB |
Output is correct |
15 |
Correct |
1 ms |
1408 KB |
Output is correct |
16 |
Correct |
1 ms |
1408 KB |
Output is correct |
17 |
Correct |
2 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1408 KB |
Output is correct |
2 |
Correct |
2 ms |
1408 KB |
Output is correct |
3 |
Correct |
394 ms |
36600 KB |
Output is correct |
4 |
Correct |
438 ms |
57976 KB |
Output is correct |
5 |
Correct |
492 ms |
55716 KB |
Output is correct |
6 |
Correct |
365 ms |
37908 KB |
Output is correct |
7 |
Correct |
428 ms |
38392 KB |
Output is correct |
8 |
Correct |
154 ms |
2168 KB |
Output is correct |
9 |
Correct |
472 ms |
57464 KB |
Output is correct |
10 |
Correct |
522 ms |
55416 KB |
Output is correct |
11 |
Correct |
388 ms |
38008 KB |
Output is correct |
12 |
Correct |
402 ms |
57080 KB |
Output is correct |
13 |
Correct |
338 ms |
57796 KB |
Output is correct |
14 |
Correct |
364 ms |
55672 KB |
Output is correct |
15 |
Correct |
258 ms |
37880 KB |
Output is correct |
16 |
Correct |
369 ms |
38520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1408 KB |
Output is correct |
2 |
Correct |
248 ms |
71164 KB |
Output is correct |
3 |
Correct |
235 ms |
71160 KB |
Output is correct |
4 |
Correct |
258 ms |
71160 KB |
Output is correct |
5 |
Correct |
173 ms |
53752 KB |
Output is correct |
6 |
Correct |
40 ms |
11000 KB |
Output is correct |
7 |
Correct |
75 ms |
20472 KB |
Output is correct |
8 |
Correct |
147 ms |
47100 KB |
Output is correct |
9 |
Correct |
138 ms |
42744 KB |
Output is correct |
10 |
Correct |
39 ms |
12280 KB |
Output is correct |
11 |
Correct |
106 ms |
32504 KB |
Output is correct |
12 |
Correct |
251 ms |
71160 KB |
Output is correct |
13 |
Correct |
236 ms |
71272 KB |
Output is correct |
14 |
Correct |
231 ms |
71288 KB |
Output is correct |
15 |
Correct |
173 ms |
53756 KB |
Output is correct |
16 |
Correct |
34 ms |
9208 KB |
Output is correct |
17 |
Correct |
73 ms |
20576 KB |
Output is correct |
18 |
Correct |
179 ms |
60664 KB |
Output is correct |
19 |
Correct |
202 ms |
57720 KB |
Output is correct |
20 |
Correct |
192 ms |
57720 KB |
Output is correct |
21 |
Correct |
148 ms |
47096 KB |
Output is correct |
22 |
Correct |
143 ms |
42744 KB |
Output is correct |
23 |
Correct |
39 ms |
12408 KB |
Output is correct |
24 |
Correct |
101 ms |
32504 KB |
Output is correct |
25 |
Correct |
249 ms |
71160 KB |
Output is correct |
26 |
Correct |
244 ms |
71288 KB |
Output is correct |
27 |
Correct |
255 ms |
71288 KB |
Output is correct |
28 |
Correct |
180 ms |
53880 KB |
Output is correct |
29 |
Correct |
34 ms |
9208 KB |
Output is correct |
30 |
Correct |
74 ms |
20592 KB |
Output is correct |
31 |
Correct |
182 ms |
60640 KB |
Output is correct |
32 |
Correct |
199 ms |
57720 KB |
Output is correct |
33 |
Correct |
208 ms |
57720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1408 KB |
Output is correct |
2 |
Correct |
6 ms |
1792 KB |
Output is correct |
3 |
Correct |
5 ms |
1536 KB |
Output is correct |
4 |
Correct |
3 ms |
1536 KB |
Output is correct |
5 |
Correct |
4 ms |
1920 KB |
Output is correct |
6 |
Correct |
2 ms |
1408 KB |
Output is correct |
7 |
Correct |
1 ms |
1408 KB |
Output is correct |
8 |
Correct |
1 ms |
1408 KB |
Output is correct |
9 |
Correct |
1 ms |
1408 KB |
Output is correct |
10 |
Correct |
1 ms |
1408 KB |
Output is correct |
11 |
Correct |
3 ms |
1536 KB |
Output is correct |
12 |
Correct |
4 ms |
1664 KB |
Output is correct |
13 |
Correct |
5 ms |
2176 KB |
Output is correct |
14 |
Correct |
6 ms |
2176 KB |
Output is correct |
15 |
Correct |
1 ms |
1408 KB |
Output is correct |
16 |
Correct |
1 ms |
1408 KB |
Output is correct |
17 |
Correct |
2 ms |
1408 KB |
Output is correct |
18 |
Correct |
512 ms |
34536 KB |
Output is correct |
19 |
Correct |
215 ms |
6392 KB |
Output is correct |
20 |
Correct |
164 ms |
5112 KB |
Output is correct |
21 |
Correct |
179 ms |
5368 KB |
Output is correct |
22 |
Correct |
202 ms |
5624 KB |
Output is correct |
23 |
Correct |
206 ms |
6320 KB |
Output is correct |
24 |
Correct |
168 ms |
5240 KB |
Output is correct |
25 |
Correct |
184 ms |
5496 KB |
Output is correct |
26 |
Correct |
205 ms |
5752 KB |
Output is correct |
27 |
Correct |
306 ms |
27128 KB |
Output is correct |
28 |
Correct |
214 ms |
15096 KB |
Output is correct |
29 |
Correct |
291 ms |
23988 KB |
Output is correct |
30 |
Correct |
402 ms |
64120 KB |
Output is correct |
31 |
Correct |
4 ms |
1536 KB |
Output is correct |
32 |
Correct |
464 ms |
25568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1408 KB |
Output is correct |
2 |
Correct |
6 ms |
1792 KB |
Output is correct |
3 |
Correct |
5 ms |
1536 KB |
Output is correct |
4 |
Correct |
3 ms |
1536 KB |
Output is correct |
5 |
Correct |
4 ms |
1920 KB |
Output is correct |
6 |
Correct |
2 ms |
1408 KB |
Output is correct |
7 |
Correct |
1 ms |
1408 KB |
Output is correct |
8 |
Correct |
1 ms |
1408 KB |
Output is correct |
9 |
Correct |
1 ms |
1408 KB |
Output is correct |
10 |
Correct |
1 ms |
1408 KB |
Output is correct |
11 |
Correct |
3 ms |
1536 KB |
Output is correct |
12 |
Correct |
4 ms |
1664 KB |
Output is correct |
13 |
Correct |
5 ms |
2176 KB |
Output is correct |
14 |
Correct |
6 ms |
2176 KB |
Output is correct |
15 |
Correct |
1 ms |
1408 KB |
Output is correct |
16 |
Correct |
1 ms |
1408 KB |
Output is correct |
17 |
Correct |
2 ms |
1408 KB |
Output is correct |
18 |
Correct |
512 ms |
34536 KB |
Output is correct |
19 |
Correct |
215 ms |
6392 KB |
Output is correct |
20 |
Correct |
164 ms |
5112 KB |
Output is correct |
21 |
Correct |
179 ms |
5368 KB |
Output is correct |
22 |
Correct |
202 ms |
5624 KB |
Output is correct |
23 |
Correct |
206 ms |
6320 KB |
Output is correct |
24 |
Correct |
168 ms |
5240 KB |
Output is correct |
25 |
Correct |
184 ms |
5496 KB |
Output is correct |
26 |
Correct |
205 ms |
5752 KB |
Output is correct |
27 |
Correct |
306 ms |
27128 KB |
Output is correct |
28 |
Correct |
214 ms |
15096 KB |
Output is correct |
29 |
Correct |
291 ms |
23988 KB |
Output is correct |
30 |
Correct |
402 ms |
64120 KB |
Output is correct |
31 |
Correct |
4 ms |
1536 KB |
Output is correct |
32 |
Correct |
464 ms |
25568 KB |
Output is correct |
33 |
Correct |
248 ms |
71164 KB |
Output is correct |
34 |
Correct |
235 ms |
71160 KB |
Output is correct |
35 |
Correct |
258 ms |
71160 KB |
Output is correct |
36 |
Correct |
173 ms |
53752 KB |
Output is correct |
37 |
Correct |
40 ms |
11000 KB |
Output is correct |
38 |
Correct |
75 ms |
20472 KB |
Output is correct |
39 |
Correct |
147 ms |
47100 KB |
Output is correct |
40 |
Correct |
138 ms |
42744 KB |
Output is correct |
41 |
Correct |
39 ms |
12280 KB |
Output is correct |
42 |
Correct |
106 ms |
32504 KB |
Output is correct |
43 |
Correct |
251 ms |
71160 KB |
Output is correct |
44 |
Correct |
236 ms |
71272 KB |
Output is correct |
45 |
Correct |
231 ms |
71288 KB |
Output is correct |
46 |
Correct |
173 ms |
53756 KB |
Output is correct |
47 |
Correct |
34 ms |
9208 KB |
Output is correct |
48 |
Correct |
73 ms |
20576 KB |
Output is correct |
49 |
Correct |
179 ms |
60664 KB |
Output is correct |
50 |
Correct |
202 ms |
57720 KB |
Output is correct |
51 |
Correct |
192 ms |
57720 KB |
Output is correct |
52 |
Correct |
148 ms |
47096 KB |
Output is correct |
53 |
Correct |
143 ms |
42744 KB |
Output is correct |
54 |
Correct |
39 ms |
12408 KB |
Output is correct |
55 |
Correct |
101 ms |
32504 KB |
Output is correct |
56 |
Correct |
249 ms |
71160 KB |
Output is correct |
57 |
Correct |
244 ms |
71288 KB |
Output is correct |
58 |
Correct |
255 ms |
71288 KB |
Output is correct |
59 |
Correct |
180 ms |
53880 KB |
Output is correct |
60 |
Correct |
34 ms |
9208 KB |
Output is correct |
61 |
Correct |
74 ms |
20592 KB |
Output is correct |
62 |
Correct |
182 ms |
60640 KB |
Output is correct |
63 |
Correct |
199 ms |
57720 KB |
Output is correct |
64 |
Correct |
208 ms |
57720 KB |
Output is correct |
65 |
Correct |
786 ms |
50484 KB |
Output is correct |
66 |
Correct |
714 ms |
46456 KB |
Output is correct |
67 |
Correct |
316 ms |
15864 KB |
Output is correct |
68 |
Correct |
410 ms |
36000 KB |
Output is correct |
69 |
Correct |
537 ms |
74744 KB |
Output is correct |
70 |
Correct |
607 ms |
74744 KB |
Output is correct |
71 |
Correct |
520 ms |
74876 KB |
Output is correct |
72 |
Correct |
447 ms |
57336 KB |
Output is correct |
73 |
Correct |
199 ms |
12664 KB |
Output is correct |
74 |
Correct |
241 ms |
24184 KB |
Output is correct |
75 |
Correct |
355 ms |
64248 KB |
Output is correct |
76 |
Correct |
483 ms |
61304 KB |
Output is correct |
77 |
Correct |
511 ms |
61204 KB |
Output is correct |
78 |
Correct |
394 ms |
36600 KB |
Output is correct |
79 |
Correct |
438 ms |
57976 KB |
Output is correct |
80 |
Correct |
492 ms |
55716 KB |
Output is correct |
81 |
Correct |
365 ms |
37908 KB |
Output is correct |
82 |
Correct |
428 ms |
38392 KB |
Output is correct |
83 |
Correct |
154 ms |
2168 KB |
Output is correct |
84 |
Correct |
472 ms |
57464 KB |
Output is correct |
85 |
Correct |
522 ms |
55416 KB |
Output is correct |
86 |
Correct |
388 ms |
38008 KB |
Output is correct |
87 |
Correct |
402 ms |
57080 KB |
Output is correct |
88 |
Correct |
338 ms |
57796 KB |
Output is correct |
89 |
Correct |
364 ms |
55672 KB |
Output is correct |
90 |
Correct |
258 ms |
37880 KB |
Output is correct |
91 |
Correct |
369 ms |
38520 KB |
Output is correct |