#include "rainbow.h"
//#define _GLIBCXX_DEBUG
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//
//using namespace __gnu_pbds;
//
//template<typename T>
//using ordered_set = tree<T, null_type, less < T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using normal_queue = priority_queue<T, vector<T>, greater<>>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define trace(x) cout << #x << ": " << (x) << endl;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define uniq(x) x.resize(unique(all(x)) - begin(x))
#define sz(s) ((int) size(s))
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define int128 __int128
#define pb push_back
#define popb pop_back
#define eb emplace_back
#define fi first
#define se second
#define itn int
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
template<typename T>
bool ckmn(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template<typename T>
bool ckmx(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
int bit(int x, int b) {
return (x >> b) & 1;
}
int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }
const ll infL = 3e18;
const int infI = 1000000000 + 7;
const int infM = 0x3f3f3f3f; //a little bigger than 1e9
const ll infML = 0x3f3f3f3f3f3f3f3fLL; //4.5e18
template<typename T>
void make_uniq(vector<T> &v) {
sort(all(v));
v.resize(unique(all(v)) - begin(v));
}
struct Fenwick {
vector<vector<ll>> t;
vector<vector<int>> yy;
int n;
Fenwick() = default;
void init(int a) {
n = a;
yy.resize(n);
for (int i = 0; i < n; ++i) yy[i].clear();
t.resize(n);
}
void fake_add(int x, int y) {
assert(x < n);
assert(x >= 0);
for (int i = x; i < n; i |= (i + 1))
yy[i].pb(y);
}
void build() {
for (int i = 0; i < n; ++i) {
make_uniq(yy[i]);
t[i].assign(sz(yy[i]) + 2, 0);
}
}
void add(int x, int y) {
assert(x < n);
assert(x >= 0);
for (int i = x; i < n; i |= (i + 1))
for (int j = lower_bound(all(yy[i]), y) - begin(yy[i]); j < sz(yy[i]); j |= (j + 1))
++t[i][j];
}
ll get(int x, int y) {
ll ans = 0;
assert(x < n);
for (int i = x; i > -1; i = ((i + 1) & i) - 1)
for (int j = upper_bound(all(yy[i]), y) - begin(yy[i]) - 1; j > -1; j = ((j + 1) & j) - 1)
ans += t[i][j];
return ans;
}
ll get(int x1, int y1, int x2, int y2) {
if (x1 > x2 || y1 > y2) return 0;
return get(x2, y2) - get(x1 - 1, y2) - get(x2, y1 - 1) + get(x1 - 1, y1 - 1);
}
};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int n, m;
vector<pair<int, int>> yy;
Fenwick fx, fy, fv, cv;
int mnx = infI, mny = infI, mxx = -infI, mxy = -infI;
auto here = [](int x, int y) -> int {
auto it = lower_bound(all(yy), mp(x, y));
if (it == end(yy)) return false;
return (*it) == mp(x, y);
};
void init(int R, int C, int sr, int sc, int M, char *S) {
n = R, m = C;
--sr, --sc;
fx.init(n + 1), fy.init(n + 1), fv.init(n + 1), cv.init(n + 1);
mnx = infI, mny = infI, mxx = -infI, mxy = -infI;
yy.clear();
yy = {{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;
yy.emplace_back(sr, sc);
}
make_uniq(yy);
mnx = infI, mny = infI, mxx = -infI, mxy = -infI;
vector<pii > pv, px, py, cvv;
for (auto [x, y]: yy) {
ckmx(mxx, x);
ckmx(mxy, y);
ckmn(mnx, x);
ckmn(mny, y);
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if (nx == x) {
if (ny >= 0) {
py.emplace_back(x, min(y, ny));
pv.emplace_back(x, max(y, ny)), pv.emplace_back(x + 1, max(y, ny));
}
} else {
if (nx >= 0) {
px.emplace_back(min(nx, x), y);
pv.emplace_back(max(nx, x), y), pv.emplace_back(max(nx, x), y + 1);
}
}
}
cvv.emplace_back(x, y);
}
make_uniq(pv), make_uniq(px), make_uniq(py), make_uniq(cvv);
for (auto [x, y]: pv)
fv.fake_add(x, y);
for (auto [x, y]: px)
fx.fake_add(x, y);
for (auto [x, y]: py)
fy.fake_add(x, y);
for (auto [x, y]: cvv)
cv.fake_add(x, y);
fv.build(), fx.build(), fy.build(), cv.build();
for (auto [x, y]: pv){
// cout << x << " " << y << endl;
fv.add(x, y);
}
for (auto [x, y]: px)
fx.add(x, y);
for (auto [x, y]: py)
fy.add(x, y);
for (auto [x, y]: cvv)
cv.add(x, y);
}
int smart(int ar, int ac, int br, int bc) {
int x1 = ar, y1 = ac, x2 = br, y2 = bc;
--x1, --y1;
ll C = 1 + (mnx > x1 && mxx < x2 - 1 && mny > y1 && mxy < y2 - 1);
ll V = (x2 - x1) * 2 + (y2 - y1) * 2 + fv.get(x1 + 1, y1 + 1, x2 - 1, y2 - 1);
ll E = (x2 - x1) * 2 + (y2 - y1) * 2 + fx.get(x1, y1, x2 - 2, y2 - 1) + fy.get(x1, y1, x2 - 1, y2 - 2);
ll U = cv.get(x1, y1, x2 - 1, y2 - 1);
ll F = E - V + C + 1;
return int(F - U - 1);
}
int stupid(int x1, int y1, int x2, int y2) {
--x1, --y1;
--x2, --y2;
assert(x1 <= x2 && y1 <= y2);
set<pii > used;
ll C = 0;
function<void(int, int)> dfs = [&](int x, int y) {
if (used.count(mp(x, y))) return;
used.insert(mp(x, y));
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if (!here(nx, ny) && x1 <= nx && nx <= x2 && y1 <= ny && ny <= y2) {
dfs(nx, ny);
}
}
};
for (int x = x1; x <= x2; ++x) {
for (int y = y1; y <= y2; ++y) {
if (!here(x, y) && !used.count(mp(x, y))) {
++C;
dfs(x, y);
}
}
}
return int(C);
}
int colour(int ar, int ac, int br, int bc) {
int x1 = ar, y1 = ac, x2 = br, y2 = bc;
int sm = smart(x1, y1, x2, y2);
return sm;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
600 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
580 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
6 ms |
724 KB |
Output is correct |
14 |
Correct |
6 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
213 ms |
12360 KB |
Output is correct |
4 |
Correct |
289 ms |
21308 KB |
Output is correct |
5 |
Correct |
291 ms |
21076 KB |
Output is correct |
6 |
Correct |
225 ms |
16440 KB |
Output is correct |
7 |
Correct |
239 ms |
15484 KB |
Output is correct |
8 |
Correct |
74 ms |
4800 KB |
Output is correct |
9 |
Correct |
281 ms |
21288 KB |
Output is correct |
10 |
Correct |
287 ms |
21000 KB |
Output is correct |
11 |
Correct |
232 ms |
16380 KB |
Output is correct |
12 |
Correct |
259 ms |
19188 KB |
Output is correct |
13 |
Correct |
209 ms |
21456 KB |
Output is correct |
14 |
Correct |
212 ms |
21024 KB |
Output is correct |
15 |
Correct |
219 ms |
16396 KB |
Output is correct |
16 |
Correct |
223 ms |
15012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
648 ms |
126652 KB |
Output is correct |
3 |
Correct |
272 ms |
107776 KB |
Output is correct |
4 |
Correct |
581 ms |
122928 KB |
Output is correct |
5 |
Correct |
328 ms |
103128 KB |
Output is correct |
6 |
Correct |
159 ms |
72868 KB |
Output is correct |
7 |
Correct |
246 ms |
79132 KB |
Output is correct |
8 |
Correct |
152 ms |
20624 KB |
Output is correct |
9 |
Correct |
160 ms |
19628 KB |
Output is correct |
10 |
Correct |
82 ms |
37176 KB |
Output is correct |
11 |
Correct |
152 ms |
44400 KB |
Output is correct |
12 |
Correct |
680 ms |
126632 KB |
Output is correct |
13 |
Correct |
267 ms |
107820 KB |
Output is correct |
14 |
Correct |
698 ms |
122908 KB |
Output is correct |
15 |
Correct |
338 ms |
103108 KB |
Output is correct |
16 |
Correct |
144 ms |
71440 KB |
Output is correct |
17 |
Correct |
272 ms |
82780 KB |
Output is correct |
18 |
Correct |
643 ms |
118248 KB |
Output is correct |
19 |
Correct |
467 ms |
115652 KB |
Output is correct |
20 |
Correct |
589 ms |
123004 KB |
Output is correct |
21 |
Correct |
160 ms |
20568 KB |
Output is correct |
22 |
Correct |
191 ms |
19696 KB |
Output is correct |
23 |
Correct |
109 ms |
37180 KB |
Output is correct |
24 |
Correct |
189 ms |
44184 KB |
Output is correct |
25 |
Correct |
659 ms |
126632 KB |
Output is correct |
26 |
Correct |
292 ms |
107832 KB |
Output is correct |
27 |
Correct |
589 ms |
123016 KB |
Output is correct |
28 |
Correct |
362 ms |
103132 KB |
Output is correct |
29 |
Correct |
142 ms |
71364 KB |
Output is correct |
30 |
Correct |
263 ms |
82696 KB |
Output is correct |
31 |
Correct |
628 ms |
118292 KB |
Output is correct |
32 |
Correct |
453 ms |
115688 KB |
Output is correct |
33 |
Correct |
527 ms |
123080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
600 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
580 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
6 ms |
724 KB |
Output is correct |
14 |
Correct |
6 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
679 ms |
17348 KB |
Output is correct |
19 |
Correct |
251 ms |
4492 KB |
Output is correct |
20 |
Correct |
173 ms |
3788 KB |
Output is correct |
21 |
Correct |
205 ms |
4268 KB |
Output is correct |
22 |
Correct |
212 ms |
4244 KB |
Output is correct |
23 |
Correct |
235 ms |
4404 KB |
Output is correct |
24 |
Correct |
214 ms |
3964 KB |
Output is correct |
25 |
Correct |
223 ms |
4176 KB |
Output is correct |
26 |
Correct |
226 ms |
4280 KB |
Output is correct |
27 |
Correct |
308 ms |
14060 KB |
Output is correct |
28 |
Correct |
262 ms |
10056 KB |
Output is correct |
29 |
Correct |
323 ms |
13760 KB |
Output is correct |
30 |
Correct |
575 ms |
33104 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
470 ms |
15264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
600 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
580 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
6 ms |
724 KB |
Output is correct |
14 |
Correct |
6 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
679 ms |
17348 KB |
Output is correct |
19 |
Correct |
251 ms |
4492 KB |
Output is correct |
20 |
Correct |
173 ms |
3788 KB |
Output is correct |
21 |
Correct |
205 ms |
4268 KB |
Output is correct |
22 |
Correct |
212 ms |
4244 KB |
Output is correct |
23 |
Correct |
235 ms |
4404 KB |
Output is correct |
24 |
Correct |
214 ms |
3964 KB |
Output is correct |
25 |
Correct |
223 ms |
4176 KB |
Output is correct |
26 |
Correct |
226 ms |
4280 KB |
Output is correct |
27 |
Correct |
308 ms |
14060 KB |
Output is correct |
28 |
Correct |
262 ms |
10056 KB |
Output is correct |
29 |
Correct |
323 ms |
13760 KB |
Output is correct |
30 |
Correct |
575 ms |
33104 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
470 ms |
15264 KB |
Output is correct |
33 |
Correct |
648 ms |
126652 KB |
Output is correct |
34 |
Correct |
272 ms |
107776 KB |
Output is correct |
35 |
Correct |
581 ms |
122928 KB |
Output is correct |
36 |
Correct |
328 ms |
103128 KB |
Output is correct |
37 |
Correct |
159 ms |
72868 KB |
Output is correct |
38 |
Correct |
246 ms |
79132 KB |
Output is correct |
39 |
Correct |
152 ms |
20624 KB |
Output is correct |
40 |
Correct |
160 ms |
19628 KB |
Output is correct |
41 |
Correct |
82 ms |
37176 KB |
Output is correct |
42 |
Correct |
152 ms |
44400 KB |
Output is correct |
43 |
Correct |
680 ms |
126632 KB |
Output is correct |
44 |
Correct |
267 ms |
107820 KB |
Output is correct |
45 |
Correct |
698 ms |
122908 KB |
Output is correct |
46 |
Correct |
338 ms |
103108 KB |
Output is correct |
47 |
Correct |
144 ms |
71440 KB |
Output is correct |
48 |
Correct |
272 ms |
82780 KB |
Output is correct |
49 |
Correct |
643 ms |
118248 KB |
Output is correct |
50 |
Correct |
467 ms |
115652 KB |
Output is correct |
51 |
Correct |
589 ms |
123004 KB |
Output is correct |
52 |
Correct |
160 ms |
20568 KB |
Output is correct |
53 |
Correct |
191 ms |
19696 KB |
Output is correct |
54 |
Correct |
109 ms |
37180 KB |
Output is correct |
55 |
Correct |
189 ms |
44184 KB |
Output is correct |
56 |
Correct |
659 ms |
126632 KB |
Output is correct |
57 |
Correct |
292 ms |
107832 KB |
Output is correct |
58 |
Correct |
589 ms |
123016 KB |
Output is correct |
59 |
Correct |
362 ms |
103132 KB |
Output is correct |
60 |
Correct |
142 ms |
71364 KB |
Output is correct |
61 |
Correct |
263 ms |
82696 KB |
Output is correct |
62 |
Correct |
628 ms |
118292 KB |
Output is correct |
63 |
Correct |
453 ms |
115688 KB |
Output is correct |
64 |
Correct |
527 ms |
123080 KB |
Output is correct |
65 |
Correct |
213 ms |
12360 KB |
Output is correct |
66 |
Correct |
289 ms |
21308 KB |
Output is correct |
67 |
Correct |
291 ms |
21076 KB |
Output is correct |
68 |
Correct |
225 ms |
16440 KB |
Output is correct |
69 |
Correct |
239 ms |
15484 KB |
Output is correct |
70 |
Correct |
74 ms |
4800 KB |
Output is correct |
71 |
Correct |
281 ms |
21288 KB |
Output is correct |
72 |
Correct |
287 ms |
21000 KB |
Output is correct |
73 |
Correct |
232 ms |
16380 KB |
Output is correct |
74 |
Correct |
259 ms |
19188 KB |
Output is correct |
75 |
Correct |
209 ms |
21456 KB |
Output is correct |
76 |
Correct |
212 ms |
21024 KB |
Output is correct |
77 |
Correct |
219 ms |
16396 KB |
Output is correct |
78 |
Correct |
223 ms |
15012 KB |
Output is correct |
79 |
Correct |
565 ms |
20792 KB |
Output is correct |
80 |
Correct |
604 ms |
19884 KB |
Output is correct |
81 |
Correct |
360 ms |
39032 KB |
Output is correct |
82 |
Correct |
423 ms |
44588 KB |
Output is correct |
83 |
Correct |
1029 ms |
126748 KB |
Output is correct |
84 |
Correct |
600 ms |
107892 KB |
Output is correct |
85 |
Correct |
1062 ms |
123088 KB |
Output is correct |
86 |
Correct |
723 ms |
103304 KB |
Output is correct |
87 |
Correct |
350 ms |
73560 KB |
Output is correct |
88 |
Correct |
504 ms |
83076 KB |
Output is correct |
89 |
Correct |
887 ms |
118428 KB |
Output is correct |
90 |
Correct |
957 ms |
115864 KB |
Output is correct |
91 |
Correct |
837 ms |
123200 KB |
Output is correct |