#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
#define mp make_pair
#define f first
#define s second
#define ins insert
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
void ckmin(int& a, int b){
a = min(a, b);
}
void ckmax(int& a, int b){
a = max(a, b);
}
vector<char> dchar = vector<char>{'S', 'E', 'N', 'W'};
vi dx = vi{1, 0, -1, 0};
vi dy = vi{0, 1, 0, -1};
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
struct hashFunc{
size_t operator()(pi a) const {
ll res = ll(a.f)*200005+ll(a.s);
ll BIG_RAND = 102923014203249LL;
return __builtin_bswap64((unsigned long long)(res)*BIG_RAND);
}
};
const int mx = 200005;
struct Off2DBIT{
ll bt[mx*20];
vi all_ys;
int begin_index[mx];
int num[mx];
vpi all_updates;
void update(int X, int Y){
all_updates.pb(mp(X, Y));
}
void UPD(int X, int Y){
for(int i = X; i < mx; i+=i&-i){
int pos = lower_bound(begin(all_ys)+begin_index[i], begin(all_ys)+begin_index[i]+num[i], Y)-begin(all_ys)-begin_index[i]+1;
for(int j = pos; j <= num[i]; j+=j&-j){
bt[begin_index[i]-1+j]++;
}
}
}
vi to_add_ys[mx];
int last_num[mx];
void initBT(){
// cout << "all_updates: " << "\n";
// for(const auto& u: all_updates){
// cout << u.f << " " << u.s << "\n";
// }
for(auto &u: all_updates){
swap(u.s, u.f);
}
sort(all(all_updates));
for(const auto& u: all_updates){
for(int i = u.s; i < mx; i+=i&-i){
if(last_num[i] != u.f){
last_num[i] = u.f;
to_add_ys[i].pb(u.f);
}
}
}
int cur = 0;
for(int i = 1; i < mx; i++){
begin_index[i] = cur;
num[i] = sz(to_add_ys[i]);
for(const auto& u: to_add_ys[i]){
all_ys.pb(u);
}
vi v;
swap(to_add_ys[i], v);
cur+=num[i];
}
for(auto &u: all_updates){
swap(u.s, u.f);
}
for(const auto& u: all_updates){
UPD(u.f, u.s);
}
// cout << "all_ys: ";
// for(auto u: all_ys){
// cout << u << " ";
// }
// cout << "\n";
// for(int i = 1; i < 5; i++){
// cout << i << " " << begin_index[i] << " " << num[i] << "\n";
// }
// cout << "\n";
}
ll sum(int x_coor, int y_coor){
ll res = 0;
for(int i = x_coor; i > 0; i-=i&-i){
int pos = upper_bound(begin(all_ys)+begin_index[i], begin(all_ys)+begin_index[i]+num[i], y_coor)-begin(all_ys)-begin_index[i];
for(int j = pos; j > 0; j-=j&-j){
res+=bt[begin_index[i]-1+j];
}
}
// cout << "sum(" << x_coor << ", " << y_coor << "): " << res << "\n";
return res;
}
ll query(int xl, int yl, int xr, int yr){
return sum(xr, yr)-sum(xl-1, yr)-sum(xr, yl-1)+sum(xl-1, yl-1);
}
// ll query(int xl, int yl, int xr, int yr){
// ll res = 0;
// for(auto& u: all_updates){
// if(xl <= u.f && u.f <= xr && yl <= u.s && u.s <= yr){
// res++;
// }
// }
// return res;
// }
};
Off2DBIT bits[2][2];
gp_hash_table<pi, null_type, hashFunc> rects[4];
int min_sr, max_sr, min_sc, max_sc;
void init(int R, int C, int sr, int sc, int M, char *S) {
min_sr = sr;
min_sc = sc;
max_sr = sr;
max_sc = sc;
for(int i = 0; i <= M; i++){
for(int i = 0; i <= 1; i++){
for(int j = 0; j <= 1; j++){
for(int xl = sr-i; xl <= sr; xl++){
for(int yl = sc-j; yl <= sc; yl++){
if(1 <= xl && 1 <= yl){
rects[2*i+j][mp(xl, yl)];
}
}
}
}
}
if(i < M){
for(int d = 0; d < 4; d++){
if(S[i] == dchar[d]){
sr+=dx[d];
sc+=dy[d];
break;
}
}
}
ckmin(min_sr, sr);
ckmin(min_sc, sc);
ckmax(max_sr, sr);
ckmax(max_sc, sc);
}
for(int i = 0; i <= 1; i++){
for(int j = 0; j <= 1; j++){
// cout << "i, j = " << i << ", " << j << "\n";
for(auto u: rects[2*i+j]){
// cout << "RECT " << i << " " << j << " " << u.f << " " << u.s << "\n";
bits[i][j].update(u.f, u.s);
}
}
}
for(int i = 0; i <= 1; i++){
for(int j = 0; j <= 1; j++){
bits[i][j].initBT();
}
}
}
int colour(int ar, int ac, int br, int bc) {
ll ans = 0;
if(ar+1 <= min_sr && max_sr+1 <= br && ac+1 <= min_sc && max_sc+1 <= bc){
ans++;
}
for(int i = 0; i <= 1; i++){
for(int j = 0; j <= 1; j++){
ll val = ll(br-i-ar+1)*ll(bc-j-ac+1);
// cout << "val: " << val << "\n";
// cout << "querying " << i << " " << j << "\n";
val-=bits[i][j].query(ar, ac, br-i, bc-j);
if((i+j) % 2 == 0){
ans+=val;
}
else{
ans-=val;
}
// cout << i << " " << j << " " << val << "\n";
}
}
return (int)(ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
25548 KB |
Output is correct |
2 |
Correct |
30 ms |
25832 KB |
Output is correct |
3 |
Correct |
23 ms |
25680 KB |
Output is correct |
4 |
Correct |
22 ms |
25676 KB |
Output is correct |
5 |
Correct |
24 ms |
25932 KB |
Output is correct |
6 |
Correct |
19 ms |
25420 KB |
Output is correct |
7 |
Correct |
19 ms |
25436 KB |
Output is correct |
8 |
Correct |
20 ms |
25420 KB |
Output is correct |
9 |
Correct |
22 ms |
25420 KB |
Output is correct |
10 |
Correct |
19 ms |
25420 KB |
Output is correct |
11 |
Correct |
23 ms |
25584 KB |
Output is correct |
12 |
Correct |
27 ms |
25804 KB |
Output is correct |
13 |
Correct |
28 ms |
26096 KB |
Output is correct |
14 |
Correct |
27 ms |
26192 KB |
Output is correct |
15 |
Correct |
22 ms |
25420 KB |
Output is correct |
16 |
Correct |
21 ms |
25464 KB |
Output is correct |
17 |
Correct |
20 ms |
25460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
25464 KB |
Output is correct |
2 |
Correct |
20 ms |
25460 KB |
Output is correct |
3 |
Correct |
620 ms |
90680 KB |
Output is correct |
4 |
Correct |
954 ms |
139072 KB |
Output is correct |
5 |
Correct |
954 ms |
132536 KB |
Output is correct |
6 |
Correct |
784 ms |
89100 KB |
Output is correct |
7 |
Correct |
742 ms |
89204 KB |
Output is correct |
8 |
Correct |
107 ms |
29172 KB |
Output is correct |
9 |
Correct |
992 ms |
138456 KB |
Output is correct |
10 |
Correct |
991 ms |
131780 KB |
Output is correct |
11 |
Correct |
758 ms |
89252 KB |
Output is correct |
12 |
Correct |
804 ms |
134452 KB |
Output is correct |
13 |
Correct |
910 ms |
138992 KB |
Output is correct |
14 |
Correct |
847 ms |
131744 KB |
Output is correct |
15 |
Correct |
680 ms |
89136 KB |
Output is correct |
16 |
Correct |
677 ms |
111532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
25420 KB |
Output is correct |
2 |
Correct |
535 ms |
94336 KB |
Output is correct |
3 |
Correct |
280 ms |
65036 KB |
Output is correct |
4 |
Correct |
642 ms |
90604 KB |
Output is correct |
5 |
Correct |
366 ms |
69448 KB |
Output is correct |
6 |
Correct |
113 ms |
32144 KB |
Output is correct |
7 |
Correct |
163 ms |
38364 KB |
Output is correct |
8 |
Correct |
609 ms |
86796 KB |
Output is correct |
9 |
Correct |
537 ms |
73700 KB |
Output is correct |
10 |
Correct |
105 ms |
34564 KB |
Output is correct |
11 |
Correct |
272 ms |
52296 KB |
Output is correct |
12 |
Correct |
572 ms |
94400 KB |
Output is correct |
13 |
Correct |
286 ms |
64964 KB |
Output is correct |
14 |
Correct |
639 ms |
90648 KB |
Output is correct |
15 |
Correct |
347 ms |
69360 KB |
Output is correct |
16 |
Correct |
89 ms |
31524 KB |
Output is correct |
17 |
Correct |
178 ms |
38416 KB |
Output is correct |
18 |
Correct |
485 ms |
64920 KB |
Output is correct |
19 |
Correct |
448 ms |
81876 KB |
Output is correct |
20 |
Correct |
479 ms |
83268 KB |
Output is correct |
21 |
Correct |
614 ms |
86624 KB |
Output is correct |
22 |
Correct |
561 ms |
73752 KB |
Output is correct |
23 |
Correct |
104 ms |
34656 KB |
Output is correct |
24 |
Correct |
275 ms |
52320 KB |
Output is correct |
25 |
Correct |
567 ms |
94348 KB |
Output is correct |
26 |
Correct |
280 ms |
65080 KB |
Output is correct |
27 |
Correct |
638 ms |
90660 KB |
Output is correct |
28 |
Correct |
349 ms |
69408 KB |
Output is correct |
29 |
Correct |
89 ms |
31388 KB |
Output is correct |
30 |
Correct |
180 ms |
38328 KB |
Output is correct |
31 |
Correct |
424 ms |
65008 KB |
Output is correct |
32 |
Correct |
438 ms |
81808 KB |
Output is correct |
33 |
Correct |
466 ms |
83352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
25548 KB |
Output is correct |
2 |
Correct |
30 ms |
25832 KB |
Output is correct |
3 |
Correct |
23 ms |
25680 KB |
Output is correct |
4 |
Correct |
22 ms |
25676 KB |
Output is correct |
5 |
Correct |
24 ms |
25932 KB |
Output is correct |
6 |
Correct |
19 ms |
25420 KB |
Output is correct |
7 |
Correct |
19 ms |
25436 KB |
Output is correct |
8 |
Correct |
20 ms |
25420 KB |
Output is correct |
9 |
Correct |
22 ms |
25420 KB |
Output is correct |
10 |
Correct |
19 ms |
25420 KB |
Output is correct |
11 |
Correct |
23 ms |
25584 KB |
Output is correct |
12 |
Correct |
27 ms |
25804 KB |
Output is correct |
13 |
Correct |
28 ms |
26096 KB |
Output is correct |
14 |
Correct |
27 ms |
26192 KB |
Output is correct |
15 |
Correct |
22 ms |
25420 KB |
Output is correct |
16 |
Correct |
21 ms |
25464 KB |
Output is correct |
17 |
Correct |
20 ms |
25460 KB |
Output is correct |
18 |
Correct |
729 ms |
45176 KB |
Output is correct |
19 |
Correct |
280 ms |
28256 KB |
Output is correct |
20 |
Correct |
194 ms |
26512 KB |
Output is correct |
21 |
Correct |
223 ms |
26692 KB |
Output is correct |
22 |
Correct |
266 ms |
27092 KB |
Output is correct |
23 |
Correct |
253 ms |
28280 KB |
Output is correct |
24 |
Correct |
230 ms |
26536 KB |
Output is correct |
25 |
Correct |
246 ms |
26836 KB |
Output is correct |
26 |
Correct |
248 ms |
27084 KB |
Output is correct |
27 |
Correct |
412 ms |
43444 KB |
Output is correct |
28 |
Correct |
339 ms |
33132 KB |
Output is correct |
29 |
Correct |
432 ms |
39072 KB |
Output is correct |
30 |
Correct |
762 ms |
65616 KB |
Output is correct |
31 |
Correct |
23 ms |
25540 KB |
Output is correct |
32 |
Correct |
533 ms |
47884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
25548 KB |
Output is correct |
2 |
Correct |
30 ms |
25832 KB |
Output is correct |
3 |
Correct |
23 ms |
25680 KB |
Output is correct |
4 |
Correct |
22 ms |
25676 KB |
Output is correct |
5 |
Correct |
24 ms |
25932 KB |
Output is correct |
6 |
Correct |
19 ms |
25420 KB |
Output is correct |
7 |
Correct |
19 ms |
25436 KB |
Output is correct |
8 |
Correct |
20 ms |
25420 KB |
Output is correct |
9 |
Correct |
22 ms |
25420 KB |
Output is correct |
10 |
Correct |
19 ms |
25420 KB |
Output is correct |
11 |
Correct |
23 ms |
25584 KB |
Output is correct |
12 |
Correct |
27 ms |
25804 KB |
Output is correct |
13 |
Correct |
28 ms |
26096 KB |
Output is correct |
14 |
Correct |
27 ms |
26192 KB |
Output is correct |
15 |
Correct |
22 ms |
25420 KB |
Output is correct |
16 |
Correct |
21 ms |
25464 KB |
Output is correct |
17 |
Correct |
20 ms |
25460 KB |
Output is correct |
18 |
Correct |
729 ms |
45176 KB |
Output is correct |
19 |
Correct |
280 ms |
28256 KB |
Output is correct |
20 |
Correct |
194 ms |
26512 KB |
Output is correct |
21 |
Correct |
223 ms |
26692 KB |
Output is correct |
22 |
Correct |
266 ms |
27092 KB |
Output is correct |
23 |
Correct |
253 ms |
28280 KB |
Output is correct |
24 |
Correct |
230 ms |
26536 KB |
Output is correct |
25 |
Correct |
246 ms |
26836 KB |
Output is correct |
26 |
Correct |
248 ms |
27084 KB |
Output is correct |
27 |
Correct |
412 ms |
43444 KB |
Output is correct |
28 |
Correct |
339 ms |
33132 KB |
Output is correct |
29 |
Correct |
432 ms |
39072 KB |
Output is correct |
30 |
Correct |
762 ms |
65616 KB |
Output is correct |
31 |
Correct |
23 ms |
25540 KB |
Output is correct |
32 |
Correct |
533 ms |
47884 KB |
Output is correct |
33 |
Correct |
535 ms |
94336 KB |
Output is correct |
34 |
Correct |
280 ms |
65036 KB |
Output is correct |
35 |
Correct |
642 ms |
90604 KB |
Output is correct |
36 |
Correct |
366 ms |
69448 KB |
Output is correct |
37 |
Correct |
113 ms |
32144 KB |
Output is correct |
38 |
Correct |
163 ms |
38364 KB |
Output is correct |
39 |
Correct |
609 ms |
86796 KB |
Output is correct |
40 |
Correct |
537 ms |
73700 KB |
Output is correct |
41 |
Correct |
105 ms |
34564 KB |
Output is correct |
42 |
Correct |
272 ms |
52296 KB |
Output is correct |
43 |
Correct |
572 ms |
94400 KB |
Output is correct |
44 |
Correct |
286 ms |
64964 KB |
Output is correct |
45 |
Correct |
639 ms |
90648 KB |
Output is correct |
46 |
Correct |
347 ms |
69360 KB |
Output is correct |
47 |
Correct |
89 ms |
31524 KB |
Output is correct |
48 |
Correct |
178 ms |
38416 KB |
Output is correct |
49 |
Correct |
485 ms |
64920 KB |
Output is correct |
50 |
Correct |
448 ms |
81876 KB |
Output is correct |
51 |
Correct |
479 ms |
83268 KB |
Output is correct |
52 |
Correct |
614 ms |
86624 KB |
Output is correct |
53 |
Correct |
561 ms |
73752 KB |
Output is correct |
54 |
Correct |
104 ms |
34656 KB |
Output is correct |
55 |
Correct |
275 ms |
52320 KB |
Output is correct |
56 |
Correct |
567 ms |
94348 KB |
Output is correct |
57 |
Correct |
280 ms |
65080 KB |
Output is correct |
58 |
Correct |
638 ms |
90660 KB |
Output is correct |
59 |
Correct |
349 ms |
69408 KB |
Output is correct |
60 |
Correct |
89 ms |
31388 KB |
Output is correct |
61 |
Correct |
180 ms |
38328 KB |
Output is correct |
62 |
Correct |
424 ms |
65008 KB |
Output is correct |
63 |
Correct |
438 ms |
81808 KB |
Output is correct |
64 |
Correct |
466 ms |
83352 KB |
Output is correct |
65 |
Correct |
620 ms |
90680 KB |
Output is correct |
66 |
Correct |
954 ms |
139072 KB |
Output is correct |
67 |
Correct |
954 ms |
132536 KB |
Output is correct |
68 |
Correct |
784 ms |
89100 KB |
Output is correct |
69 |
Correct |
742 ms |
89204 KB |
Output is correct |
70 |
Correct |
107 ms |
29172 KB |
Output is correct |
71 |
Correct |
992 ms |
138456 KB |
Output is correct |
72 |
Correct |
991 ms |
131780 KB |
Output is correct |
73 |
Correct |
758 ms |
89252 KB |
Output is correct |
74 |
Correct |
804 ms |
134452 KB |
Output is correct |
75 |
Correct |
910 ms |
138992 KB |
Output is correct |
76 |
Correct |
847 ms |
131744 KB |
Output is correct |
77 |
Correct |
680 ms |
89136 KB |
Output is correct |
78 |
Correct |
677 ms |
111532 KB |
Output is correct |
79 |
Correct |
1029 ms |
90192 KB |
Output is correct |
80 |
Correct |
1038 ms |
77352 KB |
Output is correct |
81 |
Correct |
402 ms |
38232 KB |
Output is correct |
82 |
Correct |
624 ms |
55736 KB |
Output is correct |
83 |
Correct |
1022 ms |
97988 KB |
Output is correct |
84 |
Correct |
596 ms |
68668 KB |
Output is correct |
85 |
Correct |
1078 ms |
94400 KB |
Output is correct |
86 |
Correct |
689 ms |
72924 KB |
Output is correct |
87 |
Correct |
297 ms |
35016 KB |
Output is correct |
88 |
Correct |
488 ms |
42080 KB |
Output is correct |
89 |
Correct |
651 ms |
68444 KB |
Output is correct |
90 |
Correct |
857 ms |
85544 KB |
Output is correct |
91 |
Correct |
733 ms |
86808 KB |
Output is correct |