#include <bits/stdc++.h>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
const char ch[4] = {'N', 'E', 'W', 'S'};
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, 1, -1, 0};
void mini(int &a, int b) {
if (a > b) a = b;
}
void maxi(int &a, int b) {
if (a < b) a = b;
}
struct node {
int v = 0, l = 0, r = 0;
};
const int L = (int)2e5 + 7;
const int MAX = (int)1e7 + 7;
struct segment_tree {
node t[MAX];
int ver[L + 7];
segment_tree () {};
int cnt = 0;
int build(int l, int r) {
int cur = cnt++;
if (l == r) return cur;
int m = (l + r) >> 1;
t[cur].l = build(l, m);
t[cur].r = build(m + 1, r);
return cur;
}
int lver = 0;
void build() {
ver[0] = build(1, L);
}
int update(int od, int l, int r, int p) {
int cur = cnt++;
t[cur].v = t[od].v + 1;
if (l == r) return cur;
int m = (l + r) >> 1;
if (p <= m) {
t[cur].r = t[od].r;
t[cur].l = update(t[od].l, l, m, p);
} else {
t[cur].l = t[od].l;
t[cur].r = update(t[od].r, m + 1, r, p);
}
return cur;
}
void upd(int vx, int p) {
if (p == -1) {
ver[vx] = ver[vx - 1];
lver = vx;
return ;
}
ver[vx] = update(ver[lver], 1, L, p);
lver = vx;
}
int get(int id, int l, int r, int u, int v) {
if (l > v or r < u) return 0;
if (l >= u and r <= v) return t[id].v;
int m = (l + r) >> 1;
return get(t[id].l, l, m, u, v) + get(t[id].r, m + 1, r, u, v);
}
int get(int x, int y, int u, int v) {
return get(ver[u], 1, L, y, v) - get(ver[x - 1], 1, L, y, v);
}
};
struct CTDL {
const int L = (int)2e5 + 3;
vec<vec<int>> row;
CTDL () {
row.resize(L + 7);
}
segment_tree IT;
void add(int x, int y) {
row[x].push_back(y);
}
void init() {
IT.build();
FOR(i, 1, L) {
if (sz(row[i])) {
unq(row[i]);
}
if (sz(row[i]) == 0) {
IT.upd(i, -1);
} else {
for(int j : row[i]) {
IT.upd(i, j);
}
}
}
}
long long get(int x, int y, int u, int v, bool rev) {
if (x > u or y > v) return 0;
int j = IT.get(x, y, u, v);
return rev ? 1LL * (u - x + 1) * (v - y + 1) - j : j;
}
};
const int N = (int)2e5 + 7;
int n, m;
CTDL orincells, cells, hori, vert; // hori = hang, vert = cot
int minX = INT_MAX, maxX = 0, minY = INT_MAX, maxY = 0;
void init(int R, int C, int sr, int sc, int M, char *S) {
n = R, m = C;
vector<pair<int, int>> rivers;
rivers.push_back(_mp(sr, sc));
for(int i = 0; i < M; ++i) {
for(int j = 0; j < 4; ++j) if (ch[j] == S[i]) {
sr += dx[j], sc += dy[j];
}
rivers.push_back(_mp(sr, sc));
}
unq(rivers);
for(auto cell : rivers) {
int x = cell.first, y = cell.second;
mini(minX, x);
mini(minY, y);
maxi(maxX, x);
maxi(maxY, y);
orincells.add(x, y);
FOR(a, 0, 1)
FOR(b, 0, 1) {
cells.add(x + a, y + b);
}
hori.add(x, y + 1);
hori.add(x + 1, y + 1);
vert.add(x + 1, y + 1);
vert.add(x + 1, y);
}
orincells.init();
cells.init();
hori.init();
vert.init();
}
int colour(int x, int y, int u, int v) {
long long R = orincells.get(x, y, u, v, 0);
if (R == 0) return 1;
if (R == 1LL * (u - x + 1) * (v - y + 1)) return 0;
int h = u - x + 1, w = v - y + 1;
long long V = 1LL * (h + 3) * (w + 3) - cells.get(x + 1, y + 1, u, v, 1);
long long E = 4 * (w + 2) + 4 * (h + 2) + 2 * (w - 1) + 2 * (h - 1);
E += hori.get(x + 1, y + 1, u, v + 1, 0) + vert.get(x + 1, y + 1, u + 1, v, 0);
int C = (minX > x and maxX < u and minY > y and maxY < v) ? 2 : 1;
long long F = 1 + C - V + E;
return F - (R + 2 * (h + 2) + 2 * (w + 2) - 4 + 1);
}
#ifdef LOCAL
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
int r, c, m, q, x, y;
cin >> r >> c >> m >> q >> x >> y;
// string s; cin >> s;
char s[m];
for(int i = 0; i < m; ++i) cin >> s[i];
init(r, c, x, y, m, s);
while (q--) {
int x, y, u, v;
cin >> x >> y >> u >> v;
cout << colour(x, y, u, v) << nl;
}
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
245 ms |
492048 KB |
Output is correct |
2 |
Correct |
232 ms |
491876 KB |
Output is correct |
3 |
Correct |
240 ms |
491872 KB |
Output is correct |
4 |
Correct |
239 ms |
491984 KB |
Output is correct |
5 |
Correct |
231 ms |
492128 KB |
Output is correct |
6 |
Correct |
225 ms |
491868 KB |
Output is correct |
7 |
Correct |
230 ms |
491944 KB |
Output is correct |
8 |
Correct |
231 ms |
491872 KB |
Output is correct |
9 |
Correct |
219 ms |
492000 KB |
Output is correct |
10 |
Correct |
228 ms |
491860 KB |
Output is correct |
11 |
Correct |
237 ms |
491904 KB |
Output is correct |
12 |
Correct |
241 ms |
491904 KB |
Output is correct |
13 |
Correct |
227 ms |
492092 KB |
Output is correct |
14 |
Correct |
233 ms |
492000 KB |
Output is correct |
15 |
Correct |
212 ms |
491868 KB |
Output is correct |
16 |
Correct |
232 ms |
491864 KB |
Output is correct |
17 |
Correct |
218 ms |
491932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
232 ms |
491864 KB |
Output is correct |
2 |
Correct |
218 ms |
491932 KB |
Output is correct |
3 |
Correct |
480 ms |
498580 KB |
Output is correct |
4 |
Correct |
574 ms |
500172 KB |
Output is correct |
5 |
Correct |
606 ms |
500152 KB |
Output is correct |
6 |
Correct |
485 ms |
499892 KB |
Output is correct |
7 |
Correct |
535 ms |
499128 KB |
Output is correct |
8 |
Correct |
390 ms |
495796 KB |
Output is correct |
9 |
Correct |
585 ms |
500152 KB |
Output is correct |
10 |
Correct |
587 ms |
500152 KB |
Output is correct |
11 |
Correct |
543 ms |
499876 KB |
Output is correct |
12 |
Correct |
414 ms |
500204 KB |
Output is correct |
13 |
Correct |
349 ms |
500152 KB |
Output is correct |
14 |
Correct |
390 ms |
500148 KB |
Output is correct |
15 |
Correct |
357 ms |
499896 KB |
Output is correct |
16 |
Correct |
492 ms |
499100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
491868 KB |
Output is correct |
2 |
Correct |
282 ms |
497812 KB |
Output is correct |
3 |
Correct |
302 ms |
505520 KB |
Output is correct |
4 |
Correct |
310 ms |
499864 KB |
Output is correct |
5 |
Correct |
294 ms |
499908 KB |
Output is correct |
6 |
Correct |
247 ms |
494276 KB |
Output is correct |
7 |
Correct |
261 ms |
495292 KB |
Output is correct |
8 |
Correct |
307 ms |
497608 KB |
Output is correct |
9 |
Correct |
290 ms |
496552 KB |
Output is correct |
10 |
Correct |
260 ms |
493516 KB |
Output is correct |
11 |
Correct |
275 ms |
495556 KB |
Output is correct |
12 |
Correct |
286 ms |
497864 KB |
Output is correct |
13 |
Correct |
301 ms |
505288 KB |
Output is correct |
14 |
Correct |
284 ms |
500168 KB |
Output is correct |
15 |
Correct |
274 ms |
500032 KB |
Output is correct |
16 |
Correct |
231 ms |
493852 KB |
Output is correct |
17 |
Correct |
232 ms |
495504 KB |
Output is correct |
18 |
Correct |
261 ms |
497608 KB |
Output is correct |
19 |
Correct |
263 ms |
501960 KB |
Output is correct |
20 |
Correct |
265 ms |
501880 KB |
Output is correct |
21 |
Correct |
260 ms |
497608 KB |
Output is correct |
22 |
Correct |
299 ms |
496580 KB |
Output is correct |
23 |
Correct |
257 ms |
493596 KB |
Output is correct |
24 |
Correct |
271 ms |
495560 KB |
Output is correct |
25 |
Correct |
284 ms |
497700 KB |
Output is correct |
26 |
Correct |
302 ms |
505496 KB |
Output is correct |
27 |
Correct |
293 ms |
499908 KB |
Output is correct |
28 |
Correct |
280 ms |
500088 KB |
Output is correct |
29 |
Correct |
252 ms |
493920 KB |
Output is correct |
30 |
Correct |
282 ms |
495556 KB |
Output is correct |
31 |
Correct |
287 ms |
497352 KB |
Output is correct |
32 |
Correct |
322 ms |
501956 KB |
Output is correct |
33 |
Correct |
292 ms |
501956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
245 ms |
492048 KB |
Output is correct |
2 |
Correct |
232 ms |
491876 KB |
Output is correct |
3 |
Correct |
240 ms |
491872 KB |
Output is correct |
4 |
Correct |
239 ms |
491984 KB |
Output is correct |
5 |
Correct |
231 ms |
492128 KB |
Output is correct |
6 |
Correct |
225 ms |
491868 KB |
Output is correct |
7 |
Correct |
230 ms |
491944 KB |
Output is correct |
8 |
Correct |
231 ms |
491872 KB |
Output is correct |
9 |
Correct |
219 ms |
492000 KB |
Output is correct |
10 |
Correct |
228 ms |
491860 KB |
Output is correct |
11 |
Correct |
237 ms |
491904 KB |
Output is correct |
12 |
Correct |
241 ms |
491904 KB |
Output is correct |
13 |
Correct |
227 ms |
492092 KB |
Output is correct |
14 |
Correct |
233 ms |
492000 KB |
Output is correct |
15 |
Correct |
212 ms |
491868 KB |
Output is correct |
16 |
Correct |
232 ms |
491864 KB |
Output is correct |
17 |
Correct |
218 ms |
491932 KB |
Output is correct |
18 |
Correct |
588 ms |
497876 KB |
Output is correct |
19 |
Correct |
314 ms |
495500 KB |
Output is correct |
20 |
Correct |
305 ms |
495408 KB |
Output is correct |
21 |
Correct |
312 ms |
495444 KB |
Output is correct |
22 |
Correct |
298 ms |
495568 KB |
Output is correct |
23 |
Correct |
322 ms |
495440 KB |
Output is correct |
24 |
Correct |
327 ms |
495436 KB |
Output is correct |
25 |
Correct |
313 ms |
495440 KB |
Output is correct |
26 |
Correct |
310 ms |
495440 KB |
Output is correct |
27 |
Correct |
371 ms |
497844 KB |
Output is correct |
28 |
Correct |
373 ms |
496820 KB |
Output is correct |
29 |
Correct |
382 ms |
498208 KB |
Output is correct |
30 |
Correct |
460 ms |
499868 KB |
Output is correct |
31 |
Correct |
226 ms |
492112 KB |
Output is correct |
32 |
Correct |
502 ms |
497700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
245 ms |
492048 KB |
Output is correct |
2 |
Correct |
232 ms |
491876 KB |
Output is correct |
3 |
Correct |
240 ms |
491872 KB |
Output is correct |
4 |
Correct |
239 ms |
491984 KB |
Output is correct |
5 |
Correct |
231 ms |
492128 KB |
Output is correct |
6 |
Correct |
225 ms |
491868 KB |
Output is correct |
7 |
Correct |
230 ms |
491944 KB |
Output is correct |
8 |
Correct |
231 ms |
491872 KB |
Output is correct |
9 |
Correct |
219 ms |
492000 KB |
Output is correct |
10 |
Correct |
228 ms |
491860 KB |
Output is correct |
11 |
Correct |
237 ms |
491904 KB |
Output is correct |
12 |
Correct |
241 ms |
491904 KB |
Output is correct |
13 |
Correct |
227 ms |
492092 KB |
Output is correct |
14 |
Correct |
233 ms |
492000 KB |
Output is correct |
15 |
Correct |
212 ms |
491868 KB |
Output is correct |
16 |
Correct |
232 ms |
491864 KB |
Output is correct |
17 |
Correct |
218 ms |
491932 KB |
Output is correct |
18 |
Correct |
588 ms |
497876 KB |
Output is correct |
19 |
Correct |
314 ms |
495500 KB |
Output is correct |
20 |
Correct |
305 ms |
495408 KB |
Output is correct |
21 |
Correct |
312 ms |
495444 KB |
Output is correct |
22 |
Correct |
298 ms |
495568 KB |
Output is correct |
23 |
Correct |
322 ms |
495440 KB |
Output is correct |
24 |
Correct |
327 ms |
495436 KB |
Output is correct |
25 |
Correct |
313 ms |
495440 KB |
Output is correct |
26 |
Correct |
310 ms |
495440 KB |
Output is correct |
27 |
Correct |
371 ms |
497844 KB |
Output is correct |
28 |
Correct |
373 ms |
496820 KB |
Output is correct |
29 |
Correct |
382 ms |
498208 KB |
Output is correct |
30 |
Correct |
460 ms |
499868 KB |
Output is correct |
31 |
Correct |
226 ms |
492112 KB |
Output is correct |
32 |
Correct |
502 ms |
497700 KB |
Output is correct |
33 |
Correct |
282 ms |
497812 KB |
Output is correct |
34 |
Correct |
302 ms |
505520 KB |
Output is correct |
35 |
Correct |
310 ms |
499864 KB |
Output is correct |
36 |
Correct |
294 ms |
499908 KB |
Output is correct |
37 |
Correct |
247 ms |
494276 KB |
Output is correct |
38 |
Correct |
261 ms |
495292 KB |
Output is correct |
39 |
Correct |
307 ms |
497608 KB |
Output is correct |
40 |
Correct |
290 ms |
496552 KB |
Output is correct |
41 |
Correct |
260 ms |
493516 KB |
Output is correct |
42 |
Correct |
275 ms |
495556 KB |
Output is correct |
43 |
Correct |
286 ms |
497864 KB |
Output is correct |
44 |
Correct |
301 ms |
505288 KB |
Output is correct |
45 |
Correct |
284 ms |
500168 KB |
Output is correct |
46 |
Correct |
274 ms |
500032 KB |
Output is correct |
47 |
Correct |
231 ms |
493852 KB |
Output is correct |
48 |
Correct |
232 ms |
495504 KB |
Output is correct |
49 |
Correct |
261 ms |
497608 KB |
Output is correct |
50 |
Correct |
263 ms |
501960 KB |
Output is correct |
51 |
Correct |
265 ms |
501880 KB |
Output is correct |
52 |
Correct |
260 ms |
497608 KB |
Output is correct |
53 |
Correct |
299 ms |
496580 KB |
Output is correct |
54 |
Correct |
257 ms |
493596 KB |
Output is correct |
55 |
Correct |
271 ms |
495560 KB |
Output is correct |
56 |
Correct |
284 ms |
497700 KB |
Output is correct |
57 |
Correct |
302 ms |
505496 KB |
Output is correct |
58 |
Correct |
293 ms |
499908 KB |
Output is correct |
59 |
Correct |
280 ms |
500088 KB |
Output is correct |
60 |
Correct |
252 ms |
493920 KB |
Output is correct |
61 |
Correct |
282 ms |
495556 KB |
Output is correct |
62 |
Correct |
287 ms |
497352 KB |
Output is correct |
63 |
Correct |
322 ms |
501956 KB |
Output is correct |
64 |
Correct |
292 ms |
501956 KB |
Output is correct |
65 |
Correct |
480 ms |
498580 KB |
Output is correct |
66 |
Correct |
574 ms |
500172 KB |
Output is correct |
67 |
Correct |
606 ms |
500152 KB |
Output is correct |
68 |
Correct |
485 ms |
499892 KB |
Output is correct |
69 |
Correct |
535 ms |
499128 KB |
Output is correct |
70 |
Correct |
390 ms |
495796 KB |
Output is correct |
71 |
Correct |
585 ms |
500152 KB |
Output is correct |
72 |
Correct |
587 ms |
500152 KB |
Output is correct |
73 |
Correct |
543 ms |
499876 KB |
Output is correct |
74 |
Correct |
414 ms |
500204 KB |
Output is correct |
75 |
Correct |
349 ms |
500152 KB |
Output is correct |
76 |
Correct |
390 ms |
500148 KB |
Output is correct |
77 |
Correct |
357 ms |
499896 KB |
Output is correct |
78 |
Correct |
492 ms |
499100 KB |
Output is correct |
79 |
Correct |
712 ms |
500664 KB |
Output is correct |
80 |
Correct |
676 ms |
499124 KB |
Output is correct |
81 |
Correct |
423 ms |
496460 KB |
Output is correct |
82 |
Correct |
441 ms |
498196 KB |
Output is correct |
83 |
Correct |
468 ms |
500412 KB |
Output is correct |
84 |
Correct |
450 ms |
508092 KB |
Output is correct |
85 |
Correct |
495 ms |
502708 KB |
Output is correct |
86 |
Correct |
410 ms |
503000 KB |
Output is correct |
87 |
Correct |
383 ms |
496492 KB |
Output is correct |
88 |
Correct |
361 ms |
498276 KB |
Output is correct |
89 |
Correct |
374 ms |
500144 KB |
Output is correct |
90 |
Correct |
502 ms |
504544 KB |
Output is correct |
91 |
Correct |
642 ms |
504512 KB |
Output is correct |