#include <bits/stdc++.h>
#include "rainbow.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b){a = b; return true;}
return false;
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b){a = b; return true;}
return false;
}
template <class T>
void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
for(auto i: a) out << i << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct PST{
struct Node{
int child[2];
int sum;
Node(){memset(child, -1, sizeof child); sum = 0;}
};
int L, R;
vector<Node> a;
vector<int> version;
void add_child(int &x){
x = a.size();
a.push_back(Node());
}
PST(int _l, int _r){
L = _l, R = _r;
a.push_back(Node());
version.push_back(0);
build_tree(L, R, 0);
}
void build_tree(int l, int r, int id){
if (l == r) return;
int mid = (l + r) >> 1;
add_child(a[id].child[0]); add_child(a[id].child[1]);
build_tree(l, mid, a[id].child[0]);
build_tree(mid + 1, r, a[id].child[1]);
}
void update(int i, int v, int l, int r, int id, int prev_id){
a[id] = a[prev_id];
a[id].sum += v;
if (l == r) return;
int mid = (l + r) >> 1;
if (i <= mid){
add_child(a[id].child[0]);
update(i, v, l, mid, a[id].child[0], a[prev_id].child[0]);
}
else{
add_child(a[id].child[1]);
update(i, v, mid + 1, r, a[id].child[1], a[prev_id].child[1]);
}
}
void update(int i, int v){
int prev = version.back();
version.push_back(0);
add_child(version.back());
update(i, v, L, R, version.back(), prev);
}
int get(int u, int v, int l, int r, int id){
if (u <= l && r <= v) return a[id].sum;
int mid = (l + r) >> 1;
int ans = 0;
if (u <= mid) ans += get(u, v, l, mid, a[id].child[0]);
if (v > mid) ans += get(u, v, mid + 1, r, a[id].child[1]);
return ans;
}
int get(int l, int r, int ver){
if (l > r) return 0;
int id = version[ver];
return get(l, r, L, R, id);
}
int get_version(){return version.size();}
};
const int INF = 1e9 + 69;
const int N = 2e5 + 69;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
#define Point pair<int, int>
vector<Point> dragon, verti_edge, hori_edge, vertices;
int xL, xR, yL, yR;
int cur_face;
PST dragon_cnt(1, N), vertices_cnt(1, N), verti_edge_cnt(1, N), hori_edge_cnt(1, N);
vector<int> dragon_row, vertices_row, verti_edge_row, hori_edge_row;
void init(int n, int m, int sr, int sc, int k, char *S) {
Point coord = {sr, sc};
dragon.push_back(coord);
for(int i = 0; i<k; ++i){
if (S[i] == 'N') coord.first--;
else if (S[i] == 'S') coord.first++;
else if (S[i] == 'W') coord.second--;
else coord.second++;
dragon.push_back(coord);
}
remove_dup(dragon);
for(pair<int, int> i: dragon){
for(int d = 0; d < 4; ++d){
if (d <= 1) hori_edge.push_back({i.first - (d == 0), i.second});
else verti_edge.push_back({i.first, i.second - (d == 2)});
}
}
remove_dup(hori_edge); remove_dup(verti_edge);
for(pair<int, int> i: hori_edge) {
vertices.push_back({i.first, i.second-1});
vertices.push_back({i.first, i.second});
}
for(pair<int, int> i: verti_edge){
vertices.push_back({i.first-1, i.second});
vertices.push_back({i.first, i.second});
}
remove_dup(vertices);
/*---------------------------------------*/
dragon_row.push_back(0);
vector<vector<int>> sigma(N);
for(pair<int, int> i: dragon) sigma[i.first].push_back(i.second);
for(int i = 1; i<N; ++i){
for(int j: sigma[i]) dragon_cnt.update(j, 1);
dragon_row.push_back(dragon_cnt.get_version() - 1);
}
/*---------------------------------------*/
for(int i = 0; i<N; ++i) sigma[i].clear();
vertices_row.push_back(0);
for(pair<int, int> i: vertices)
if (i.first > 0 && i.second > 0)
sigma[i.first].push_back(i.second);
for(int i = 1; i<N; ++i){
for(int j: sigma[i]) vertices_cnt.update(j, 1);
vertices_row.push_back(vertices_cnt.get_version() - 1);
}
/*---------------------------------------*/
for(int i = 0; i<N; ++i) sigma[i].clear();
hori_edge_row.push_back(0);
for(pair<int, int> i: hori_edge)
if (i.first > 0 && i.second > 0)
sigma[i.first].push_back(i.second);
for(int i = 1; i<N; ++i){
for(int j: sigma[i]) hori_edge_cnt.update(j, 1);
hori_edge_row.push_back(hori_edge_cnt.get_version() - 1);
}
/*---------------------------------------*/
for(int i = 0; i<N; ++i) sigma[i].clear();
verti_edge_row.push_back(0);
for(pair<int, int> i: verti_edge)
if (i.first > 0 && i.second > 0)
sigma[i.first].push_back(i.second);
for(int i = 1; i<N; ++i){
for(int j: sigma[i]) verti_edge_cnt.update(j, 1);
verti_edge_row.push_back(verti_edge_cnt.get_version() - 1);
}
/*---------------------------------------*/
cur_face = 2 + hori_edge.size() + verti_edge.size() - vertices.size() - dragon.size();
xL = yL = INF;
xR = yR = 0;
for(pair<int, int> i: dragon){
maximize(xR, i.first); maximize(yR, i.second);
minimize(xL, i.first); minimize(yL, i.second);
}
}
int colour(int ar, int ac, int br, int bc) {
if (ar < xL && ac < yL && br > xR && bc > yR) return cur_face;
int v_cnt = ((br - ar) + (bc - ac)) * 2, e_cnt = v_cnt;
int block_cnt = dragon_cnt.get(ac, bc, dragon_row[br]) - dragon_cnt.get(ac, bc, dragon_row[ar - 1]);
v_cnt += vertices_cnt.get(ac, bc - 1, vertices_row[br-1]) - vertices_cnt.get(ac, bc - 1, vertices_row[ar-1]);
e_cnt += hori_edge_cnt.get(ac, bc, hori_edge_row[br-1]) - hori_edge_cnt.get(ac, bc, hori_edge_row[ar-1]);
e_cnt += verti_edge_cnt.get(ac, bc-1, verti_edge_row[br]) - verti_edge_cnt.get(ac, bc-1, verti_edge_row[ar-1]);
return 1 + e_cnt - v_cnt - block_cnt;
}
// int main(void){
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("input.inp", "r", stdin);
// int n, m, k; cin >> n >> m;
// int sr, sc; cin >> sr >> sc;
// cin >> k;
// string s; cin >> s;
// init(n, m, sr, sc, k, (char*) s.c_str());
// int q; cin >> q;
// while(q--) {
// int a, b, c, d; cin >> a >> b >> c >> d;
// cout << colour(a, b, c, d) << "\n";
// }
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
29272 KB |
Output is correct |
2 |
Correct |
27 ms |
34136 KB |
Output is correct |
3 |
Correct |
22 ms |
29012 KB |
Output is correct |
4 |
Correct |
22 ms |
29528 KB |
Output is correct |
5 |
Correct |
26 ms |
34364 KB |
Output is correct |
6 |
Correct |
22 ms |
34380 KB |
Output is correct |
7 |
Correct |
21 ms |
30380 KB |
Output is correct |
8 |
Correct |
21 ms |
28756 KB |
Output is correct |
9 |
Correct |
21 ms |
31320 KB |
Output is correct |
10 |
Correct |
22 ms |
32780 KB |
Output is correct |
11 |
Correct |
22 ms |
28488 KB |
Output is correct |
12 |
Correct |
22 ms |
29528 KB |
Output is correct |
13 |
Correct |
24 ms |
31064 KB |
Output is correct |
14 |
Correct |
26 ms |
33368 KB |
Output is correct |
15 |
Correct |
21 ms |
28472 KB |
Output is correct |
16 |
Correct |
24 ms |
31972 KB |
Output is correct |
17 |
Correct |
25 ms |
32556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
31972 KB |
Output is correct |
2 |
Correct |
25 ms |
32556 KB |
Output is correct |
3 |
Correct |
583 ms |
130252 KB |
Output is correct |
4 |
Correct |
686 ms |
192068 KB |
Output is correct |
5 |
Correct |
725 ms |
189660 KB |
Output is correct |
6 |
Correct |
614 ms |
158420 KB |
Output is correct |
7 |
Correct |
573 ms |
165792 KB |
Output is correct |
8 |
Correct |
347 ms |
32052 KB |
Output is correct |
9 |
Correct |
718 ms |
190660 KB |
Output is correct |
10 |
Correct |
709 ms |
188580 KB |
Output is correct |
11 |
Correct |
625 ms |
158232 KB |
Output is correct |
12 |
Correct |
312 ms |
188940 KB |
Output is correct |
13 |
Correct |
316 ms |
188712 KB |
Output is correct |
14 |
Correct |
320 ms |
188676 KB |
Output is correct |
15 |
Correct |
281 ms |
159524 KB |
Output is correct |
16 |
Correct |
579 ms |
135812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
28472 KB |
Output is correct |
2 |
Correct |
216 ms |
211176 KB |
Output is correct |
3 |
Correct |
210 ms |
214004 KB |
Output is correct |
4 |
Correct |
250 ms |
212800 KB |
Output is correct |
5 |
Correct |
164 ms |
176376 KB |
Output is correct |
6 |
Correct |
65 ms |
61216 KB |
Output is correct |
7 |
Correct |
111 ms |
98576 KB |
Output is correct |
8 |
Correct |
184 ms |
180216 KB |
Output is correct |
9 |
Correct |
181 ms |
168200 KB |
Output is correct |
10 |
Correct |
71 ms |
61460 KB |
Output is correct |
11 |
Correct |
127 ms |
127412 KB |
Output is correct |
12 |
Correct |
219 ms |
211960 KB |
Output is correct |
13 |
Correct |
212 ms |
214232 KB |
Output is correct |
14 |
Correct |
241 ms |
214060 KB |
Output is correct |
15 |
Correct |
181 ms |
179384 KB |
Output is correct |
16 |
Correct |
61 ms |
57888 KB |
Output is correct |
17 |
Correct |
105 ms |
99596 KB |
Output is correct |
18 |
Correct |
212 ms |
210164 KB |
Output is correct |
19 |
Correct |
201 ms |
192756 KB |
Output is correct |
20 |
Correct |
193 ms |
190612 KB |
Output is correct |
21 |
Correct |
181 ms |
177524 KB |
Output is correct |
22 |
Correct |
166 ms |
170256 KB |
Output is correct |
23 |
Correct |
68 ms |
62220 KB |
Output is correct |
24 |
Correct |
120 ms |
127752 KB |
Output is correct |
25 |
Correct |
215 ms |
212140 KB |
Output is correct |
26 |
Correct |
208 ms |
213740 KB |
Output is correct |
27 |
Correct |
234 ms |
211944 KB |
Output is correct |
28 |
Correct |
172 ms |
178828 KB |
Output is correct |
29 |
Correct |
60 ms |
58452 KB |
Output is correct |
30 |
Correct |
109 ms |
99384 KB |
Output is correct |
31 |
Correct |
208 ms |
210672 KB |
Output is correct |
32 |
Correct |
203 ms |
192752 KB |
Output is correct |
33 |
Correct |
195 ms |
192164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
29272 KB |
Output is correct |
2 |
Correct |
27 ms |
34136 KB |
Output is correct |
3 |
Correct |
22 ms |
29012 KB |
Output is correct |
4 |
Correct |
22 ms |
29528 KB |
Output is correct |
5 |
Correct |
26 ms |
34364 KB |
Output is correct |
6 |
Correct |
22 ms |
34380 KB |
Output is correct |
7 |
Correct |
21 ms |
30380 KB |
Output is correct |
8 |
Correct |
21 ms |
28756 KB |
Output is correct |
9 |
Correct |
21 ms |
31320 KB |
Output is correct |
10 |
Correct |
22 ms |
32780 KB |
Output is correct |
11 |
Correct |
22 ms |
28488 KB |
Output is correct |
12 |
Correct |
22 ms |
29528 KB |
Output is correct |
13 |
Correct |
24 ms |
31064 KB |
Output is correct |
14 |
Correct |
26 ms |
33368 KB |
Output is correct |
15 |
Correct |
21 ms |
28472 KB |
Output is correct |
16 |
Correct |
24 ms |
31972 KB |
Output is correct |
17 |
Correct |
25 ms |
32556 KB |
Output is correct |
18 |
Correct |
565 ms |
155224 KB |
Output is correct |
19 |
Correct |
200 ms |
32076 KB |
Output is correct |
20 |
Correct |
197 ms |
29272 KB |
Output is correct |
21 |
Correct |
200 ms |
29256 KB |
Output is correct |
22 |
Correct |
184 ms |
30552 KB |
Output is correct |
23 |
Correct |
207 ms |
31828 KB |
Output is correct |
24 |
Correct |
168 ms |
29272 KB |
Output is correct |
25 |
Correct |
175 ms |
30284 KB |
Output is correct |
26 |
Correct |
194 ms |
30792 KB |
Output is correct |
27 |
Correct |
324 ms |
103440 KB |
Output is correct |
28 |
Correct |
248 ms |
61772 KB |
Output is correct |
29 |
Correct |
339 ms |
98820 KB |
Output is correct |
30 |
Correct |
532 ms |
211644 KB |
Output is correct |
31 |
Correct |
23 ms |
31272 KB |
Output is correct |
32 |
Correct |
422 ms |
106252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
29272 KB |
Output is correct |
2 |
Correct |
27 ms |
34136 KB |
Output is correct |
3 |
Correct |
22 ms |
29012 KB |
Output is correct |
4 |
Correct |
22 ms |
29528 KB |
Output is correct |
5 |
Correct |
26 ms |
34364 KB |
Output is correct |
6 |
Correct |
22 ms |
34380 KB |
Output is correct |
7 |
Correct |
21 ms |
30380 KB |
Output is correct |
8 |
Correct |
21 ms |
28756 KB |
Output is correct |
9 |
Correct |
21 ms |
31320 KB |
Output is correct |
10 |
Correct |
22 ms |
32780 KB |
Output is correct |
11 |
Correct |
22 ms |
28488 KB |
Output is correct |
12 |
Correct |
22 ms |
29528 KB |
Output is correct |
13 |
Correct |
24 ms |
31064 KB |
Output is correct |
14 |
Correct |
26 ms |
33368 KB |
Output is correct |
15 |
Correct |
21 ms |
28472 KB |
Output is correct |
16 |
Correct |
24 ms |
31972 KB |
Output is correct |
17 |
Correct |
25 ms |
32556 KB |
Output is correct |
18 |
Correct |
565 ms |
155224 KB |
Output is correct |
19 |
Correct |
200 ms |
32076 KB |
Output is correct |
20 |
Correct |
197 ms |
29272 KB |
Output is correct |
21 |
Correct |
200 ms |
29256 KB |
Output is correct |
22 |
Correct |
184 ms |
30552 KB |
Output is correct |
23 |
Correct |
207 ms |
31828 KB |
Output is correct |
24 |
Correct |
168 ms |
29272 KB |
Output is correct |
25 |
Correct |
175 ms |
30284 KB |
Output is correct |
26 |
Correct |
194 ms |
30792 KB |
Output is correct |
27 |
Correct |
324 ms |
103440 KB |
Output is correct |
28 |
Correct |
248 ms |
61772 KB |
Output is correct |
29 |
Correct |
339 ms |
98820 KB |
Output is correct |
30 |
Correct |
532 ms |
211644 KB |
Output is correct |
31 |
Correct |
23 ms |
31272 KB |
Output is correct |
32 |
Correct |
422 ms |
106252 KB |
Output is correct |
33 |
Correct |
216 ms |
211176 KB |
Output is correct |
34 |
Correct |
210 ms |
214004 KB |
Output is correct |
35 |
Correct |
250 ms |
212800 KB |
Output is correct |
36 |
Correct |
164 ms |
176376 KB |
Output is correct |
37 |
Correct |
65 ms |
61216 KB |
Output is correct |
38 |
Correct |
111 ms |
98576 KB |
Output is correct |
39 |
Correct |
184 ms |
180216 KB |
Output is correct |
40 |
Correct |
181 ms |
168200 KB |
Output is correct |
41 |
Correct |
71 ms |
61460 KB |
Output is correct |
42 |
Correct |
127 ms |
127412 KB |
Output is correct |
43 |
Correct |
219 ms |
211960 KB |
Output is correct |
44 |
Correct |
212 ms |
214232 KB |
Output is correct |
45 |
Correct |
241 ms |
214060 KB |
Output is correct |
46 |
Correct |
181 ms |
179384 KB |
Output is correct |
47 |
Correct |
61 ms |
57888 KB |
Output is correct |
48 |
Correct |
105 ms |
99596 KB |
Output is correct |
49 |
Correct |
212 ms |
210164 KB |
Output is correct |
50 |
Correct |
201 ms |
192756 KB |
Output is correct |
51 |
Correct |
193 ms |
190612 KB |
Output is correct |
52 |
Correct |
181 ms |
177524 KB |
Output is correct |
53 |
Correct |
166 ms |
170256 KB |
Output is correct |
54 |
Correct |
68 ms |
62220 KB |
Output is correct |
55 |
Correct |
120 ms |
127752 KB |
Output is correct |
56 |
Correct |
215 ms |
212140 KB |
Output is correct |
57 |
Correct |
208 ms |
213740 KB |
Output is correct |
58 |
Correct |
234 ms |
211944 KB |
Output is correct |
59 |
Correct |
172 ms |
178828 KB |
Output is correct |
60 |
Correct |
60 ms |
58452 KB |
Output is correct |
61 |
Correct |
109 ms |
99384 KB |
Output is correct |
62 |
Correct |
208 ms |
210672 KB |
Output is correct |
63 |
Correct |
203 ms |
192752 KB |
Output is correct |
64 |
Correct |
195 ms |
192164 KB |
Output is correct |
65 |
Correct |
583 ms |
130252 KB |
Output is correct |
66 |
Correct |
686 ms |
192068 KB |
Output is correct |
67 |
Correct |
725 ms |
189660 KB |
Output is correct |
68 |
Correct |
614 ms |
158420 KB |
Output is correct |
69 |
Correct |
573 ms |
165792 KB |
Output is correct |
70 |
Correct |
347 ms |
32052 KB |
Output is correct |
71 |
Correct |
718 ms |
190660 KB |
Output is correct |
72 |
Correct |
709 ms |
188580 KB |
Output is correct |
73 |
Correct |
625 ms |
158232 KB |
Output is correct |
74 |
Correct |
312 ms |
188940 KB |
Output is correct |
75 |
Correct |
316 ms |
188712 KB |
Output is correct |
76 |
Correct |
320 ms |
188676 KB |
Output is correct |
77 |
Correct |
281 ms |
159524 KB |
Output is correct |
78 |
Correct |
579 ms |
135812 KB |
Output is correct |
79 |
Correct |
716 ms |
178348 KB |
Output is correct |
80 |
Correct |
797 ms |
168744 KB |
Output is correct |
81 |
Correct |
299 ms |
62228 KB |
Output is correct |
82 |
Correct |
403 ms |
127684 KB |
Output is correct |
83 |
Correct |
697 ms |
212728 KB |
Output is correct |
84 |
Correct |
702 ms |
212944 KB |
Output is correct |
85 |
Correct |
646 ms |
211432 KB |
Output is correct |
86 |
Correct |
619 ms |
177232 KB |
Output is correct |
87 |
Correct |
356 ms |
57980 KB |
Output is correct |
88 |
Correct |
502 ms |
99532 KB |
Output is correct |
89 |
Correct |
601 ms |
213488 KB |
Output is correct |
90 |
Correct |
792 ms |
191204 KB |
Output is correct |
91 |
Correct |
615 ms |
194804 KB |
Output is correct |