#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;
const int MX=2e5+5;
int nn;
int r,c;
vector<int> seg[4][MX*4]; ///정점,세로,가로
///가로선분
int f(int lef,int rig,int x,int y,int z,int w,int k,int lev){
if(x>y || lef>y ||x>rig)return 0;
if(lef>=x && rig<=y){
return upper_bound(seg[k][lev].begin(),seg[k][lev].end(),w)-lower_bound(seg[k][lev].begin(),seg[k][lev].end(),z);
}
int mid=(lef+rig)/2;
return f(lef,mid,x,y,z,w,k,lev*2)+f(mid+1,rig,x,y,z,w,k,lev*2+1);
}
void upd(int x,int y){
seg[0][nn+x-1].push_back(y);
}
void upd2(int x,int y){ ///세로 선분
seg[1][nn+x-1].push_back(y);
}
void upd3(int x,int y){///가로선분
seg[2][nn+x-1].push_back(y);
}
void upd4(int x,int y){
seg[3][nn+x-1].push_back(y);
}
int mix,mxx;
int mxy,miy;
void init(int R, int C, int sr, int sc, int M, char *S) {
r=R+2;
c=C+2;
for(nn=1 ; nn<c ; nn*=2);
int i;
int y=sr;
int x=sc;
upd4(x,y);
upd(x,y);
upd(x+1,y);
upd(x,y+1);
upd(x+1,y+1);
upd2(x,y);
upd2(x+1,y);
upd3(x,y);
upd3(x,y+1);
mix=x;
mxx=x+1;
miy=y;
mxy=y+1;
for(i=0 ; i<M ; i++){
if(S[i]=='E'){
x++;
upd(x+1,y);
upd(x+1,y+1);
upd2(x+1,y);
upd3(x,y);
upd3(x,y+1);
}
if(S[i]=='W'){
x--;
upd(x,y);
upd(x,y+1);
upd2(x,y);
upd3(x,y);
upd3(x,y+1);
}
if(S[i]=='S'){
y++;
upd(x,y+1);
upd(x+1,y+1);
upd2(x,y);
upd2(x+1,y);
upd3(x,y+1);
}
if(S[i]=='N'){
y--;
upd(x,y);
upd(x+1,y);
upd2(x,y);
upd2(x+1,y);
upd3(x,y);
}
upd4(x,y);
mix=min(x,mix);
mxx=max(mxx,x+1);
miy=min(miy,y);
mxy=max(mxy,y+1);
}
for(i=1 ; i<=c ; i++){
for(int j=0 ; j<4 ; j++){
sort(seg[j][nn+i-1].begin(),seg[j][nn+i-1].end());
seg[j][nn+i-1].erase(unique(seg[j][nn+i-1].begin(),seg[j][nn+i-1].end()),seg[j][nn+i-1].end());
}
}
for(i=nn-1 ; i>=1 ; i--){
for(int j=0 ; j<4 ; j++){
seg[j][i].resize(seg[j][i*2].size()+seg[j][i*2+1].size());
}
merge(seg[0][i*2].begin(),seg[0][i*2].end(),seg[0][i*2+1].begin(),seg[0][i*2+1].end(),seg[0][i].begin());
merge(seg[1][i*2].begin(),seg[1][i*2].end(),seg[1][i*2+1].begin(),seg[1][i*2+1].end(),seg[1][i].begin());
merge(seg[2][i*2].begin(),seg[2][i*2].end(),seg[2][i*2+1].begin(),seg[2][i*2+1].end(),seg[2][i].begin());
merge(seg[3][i*2].begin(),seg[3][i*2].end(),seg[3][i*2+1].begin(),seg[3][i*2+1].end(),seg[3][i].begin());
for(int j=0 ; j<4 ; j++){
sort(seg[j][i].begin(),seg[j][i].end());
}
}
/*
for(i=1 ; i<2*nn ; i++){
cout<<i<<endl;
for(auto k: seg[0][i])cout<<k<<" ";
cout<<endl;
}*/
}
int colour(int ar, int ac, int br, int bc) {
int A=f(1,nn,ac+1,bc,ar,br,1,1);
int B=f(1,nn,ac,bc,ar+1,br,2,1);
int C=f(1,nn,ac+1,bc,ar,ar,0,1)+f(1,nn,ac+1,bc,br+1,br+1,0,1);
int D=f(1,nn,ac,ac,ar+1,br,0,1)+f(1,nn,bc+1,bc+1,ar+1,br,0,1);
int E=f(1,nn,ac,ac,ar,ar,0,1)+f(1,nn,ac,ac,br+1,br+1,0,1)+f(1,nn,bc+1,bc+1,ar,ar,0,1)+f(1,nn,bc+1,bc+1,br+1,br+1,0,1);
int F=f(1,nn,ac+1,bc,ar+1,br,0,1);
int G=f(1,nn,ac,bc,ar,br,3,1);
int v=C+D+F+4;
int e=A+B+C+2+D+2;
// cout<<A<<" "<<B<<" "<<C<<" "<<D<<" "<<E<<" "<<F<<" "<<G<<endl;
// cout<<v<<" "<<e<<endl;
if(G==0)return 1;
int CP=1;
if(mix>ac && mxx<=bc && miy>ar && mxy<=br)CP=2;
return e-v-G+CP;
}
Compilation message
rainbow.cpp: In function 'int colour(int, int, int, int)':
rainbow.cpp:133:9: warning: unused variable 'E' [-Wunused-variable]
int E=f(1,nn,ac,ac,ar,ar,0,1)+f(1,nn,ac,ac,br+1,br+1,0,1)+f(1,nn,bc+1,bc+1,ar,ar,0,1)+f(1,nn,bc+1,bc+1,br+1,br+1,0,1);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
75512 KB |
Output is correct |
2 |
Correct |
55 ms |
75640 KB |
Output is correct |
3 |
Correct |
57 ms |
75512 KB |
Output is correct |
4 |
Correct |
53 ms |
75512 KB |
Output is correct |
5 |
Correct |
57 ms |
75768 KB |
Output is correct |
6 |
Correct |
51 ms |
75512 KB |
Output is correct |
7 |
Correct |
51 ms |
75512 KB |
Output is correct |
8 |
Correct |
52 ms |
75512 KB |
Output is correct |
9 |
Correct |
51 ms |
75512 KB |
Output is correct |
10 |
Correct |
49 ms |
75512 KB |
Output is correct |
11 |
Correct |
56 ms |
75640 KB |
Output is correct |
12 |
Correct |
57 ms |
75640 KB |
Output is correct |
13 |
Correct |
58 ms |
75768 KB |
Output is correct |
14 |
Correct |
57 ms |
75896 KB |
Output is correct |
15 |
Correct |
50 ms |
75512 KB |
Output is correct |
16 |
Correct |
50 ms |
75512 KB |
Output is correct |
17 |
Correct |
51 ms |
75512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
75512 KB |
Output is correct |
2 |
Correct |
51 ms |
75512 KB |
Output is correct |
3 |
Correct |
1187 ms |
113260 KB |
Output is correct |
4 |
Correct |
1716 ms |
137720 KB |
Output is correct |
5 |
Correct |
1667 ms |
135548 KB |
Output is correct |
6 |
Correct |
1194 ms |
118264 KB |
Output is correct |
7 |
Correct |
1363 ms |
117112 KB |
Output is correct |
8 |
Correct |
684 ms |
78960 KB |
Output is correct |
9 |
Correct |
1758 ms |
137592 KB |
Output is correct |
10 |
Correct |
1737 ms |
135288 KB |
Output is correct |
11 |
Correct |
1311 ms |
118048 KB |
Output is correct |
12 |
Correct |
662 ms |
133880 KB |
Output is correct |
13 |
Correct |
776 ms |
137592 KB |
Output is correct |
14 |
Correct |
849 ms |
135236 KB |
Output is correct |
15 |
Correct |
763 ms |
118136 KB |
Output is correct |
16 |
Correct |
1232 ms |
119928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
75512 KB |
Output is correct |
2 |
Correct |
281 ms |
137556 KB |
Output is correct |
3 |
Correct |
336 ms |
120408 KB |
Output is correct |
4 |
Correct |
296 ms |
126968 KB |
Output is correct |
5 |
Correct |
250 ms |
113520 KB |
Output is correct |
6 |
Correct |
126 ms |
86904 KB |
Output is correct |
7 |
Correct |
162 ms |
94840 KB |
Output is correct |
8 |
Correct |
228 ms |
117368 KB |
Output is correct |
9 |
Correct |
207 ms |
110584 KB |
Output is correct |
10 |
Correct |
88 ms |
82552 KB |
Output is correct |
11 |
Correct |
161 ms |
98516 KB |
Output is correct |
12 |
Correct |
276 ms |
137592 KB |
Output is correct |
13 |
Correct |
326 ms |
120664 KB |
Output is correct |
14 |
Correct |
290 ms |
127096 KB |
Output is correct |
15 |
Correct |
248 ms |
113648 KB |
Output is correct |
16 |
Correct |
119 ms |
85368 KB |
Output is correct |
17 |
Correct |
163 ms |
94968 KB |
Output is correct |
18 |
Correct |
285 ms |
121184 KB |
Output is correct |
19 |
Correct |
305 ms |
129012 KB |
Output is correct |
20 |
Correct |
295 ms |
129008 KB |
Output is correct |
21 |
Correct |
225 ms |
117368 KB |
Output is correct |
22 |
Correct |
205 ms |
110584 KB |
Output is correct |
23 |
Correct |
88 ms |
82552 KB |
Output is correct |
24 |
Correct |
160 ms |
98428 KB |
Output is correct |
25 |
Correct |
278 ms |
137592 KB |
Output is correct |
26 |
Correct |
326 ms |
120536 KB |
Output is correct |
27 |
Correct |
283 ms |
126972 KB |
Output is correct |
28 |
Correct |
236 ms |
113648 KB |
Output is correct |
29 |
Correct |
118 ms |
85368 KB |
Output is correct |
30 |
Correct |
173 ms |
94840 KB |
Output is correct |
31 |
Correct |
272 ms |
121208 KB |
Output is correct |
32 |
Correct |
293 ms |
129012 KB |
Output is correct |
33 |
Correct |
300 ms |
129012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
75512 KB |
Output is correct |
2 |
Correct |
55 ms |
75640 KB |
Output is correct |
3 |
Correct |
57 ms |
75512 KB |
Output is correct |
4 |
Correct |
53 ms |
75512 KB |
Output is correct |
5 |
Correct |
57 ms |
75768 KB |
Output is correct |
6 |
Correct |
51 ms |
75512 KB |
Output is correct |
7 |
Correct |
51 ms |
75512 KB |
Output is correct |
8 |
Correct |
52 ms |
75512 KB |
Output is correct |
9 |
Correct |
51 ms |
75512 KB |
Output is correct |
10 |
Correct |
49 ms |
75512 KB |
Output is correct |
11 |
Correct |
56 ms |
75640 KB |
Output is correct |
12 |
Correct |
57 ms |
75640 KB |
Output is correct |
13 |
Correct |
58 ms |
75768 KB |
Output is correct |
14 |
Correct |
57 ms |
75896 KB |
Output is correct |
15 |
Correct |
50 ms |
75512 KB |
Output is correct |
16 |
Correct |
50 ms |
75512 KB |
Output is correct |
17 |
Correct |
51 ms |
75512 KB |
Output is correct |
18 |
Correct |
1211 ms |
94164 KB |
Output is correct |
19 |
Correct |
521 ms |
79736 KB |
Output is correct |
20 |
Correct |
350 ms |
78968 KB |
Output is correct |
21 |
Correct |
415 ms |
79224 KB |
Output is correct |
22 |
Correct |
432 ms |
79224 KB |
Output is correct |
23 |
Correct |
514 ms |
79836 KB |
Output is correct |
24 |
Correct |
414 ms |
79352 KB |
Output is correct |
25 |
Correct |
439 ms |
79352 KB |
Output is correct |
26 |
Correct |
446 ms |
79352 KB |
Output is correct |
27 |
Correct |
629 ms |
92152 KB |
Output is correct |
28 |
Correct |
597 ms |
87224 KB |
Output is correct |
29 |
Correct |
729 ms |
91512 KB |
Output is correct |
30 |
Correct |
1081 ms |
106024 KB |
Output is correct |
31 |
Correct |
55 ms |
75640 KB |
Output is correct |
32 |
Correct |
1082 ms |
92540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
75512 KB |
Output is correct |
2 |
Correct |
55 ms |
75640 KB |
Output is correct |
3 |
Correct |
57 ms |
75512 KB |
Output is correct |
4 |
Correct |
53 ms |
75512 KB |
Output is correct |
5 |
Correct |
57 ms |
75768 KB |
Output is correct |
6 |
Correct |
51 ms |
75512 KB |
Output is correct |
7 |
Correct |
51 ms |
75512 KB |
Output is correct |
8 |
Correct |
52 ms |
75512 KB |
Output is correct |
9 |
Correct |
51 ms |
75512 KB |
Output is correct |
10 |
Correct |
49 ms |
75512 KB |
Output is correct |
11 |
Correct |
56 ms |
75640 KB |
Output is correct |
12 |
Correct |
57 ms |
75640 KB |
Output is correct |
13 |
Correct |
58 ms |
75768 KB |
Output is correct |
14 |
Correct |
57 ms |
75896 KB |
Output is correct |
15 |
Correct |
50 ms |
75512 KB |
Output is correct |
16 |
Correct |
50 ms |
75512 KB |
Output is correct |
17 |
Correct |
51 ms |
75512 KB |
Output is correct |
18 |
Correct |
1211 ms |
94164 KB |
Output is correct |
19 |
Correct |
521 ms |
79736 KB |
Output is correct |
20 |
Correct |
350 ms |
78968 KB |
Output is correct |
21 |
Correct |
415 ms |
79224 KB |
Output is correct |
22 |
Correct |
432 ms |
79224 KB |
Output is correct |
23 |
Correct |
514 ms |
79836 KB |
Output is correct |
24 |
Correct |
414 ms |
79352 KB |
Output is correct |
25 |
Correct |
439 ms |
79352 KB |
Output is correct |
26 |
Correct |
446 ms |
79352 KB |
Output is correct |
27 |
Correct |
629 ms |
92152 KB |
Output is correct |
28 |
Correct |
597 ms |
87224 KB |
Output is correct |
29 |
Correct |
729 ms |
91512 KB |
Output is correct |
30 |
Correct |
1081 ms |
106024 KB |
Output is correct |
31 |
Correct |
55 ms |
75640 KB |
Output is correct |
32 |
Correct |
1082 ms |
92540 KB |
Output is correct |
33 |
Correct |
281 ms |
137556 KB |
Output is correct |
34 |
Correct |
336 ms |
120408 KB |
Output is correct |
35 |
Correct |
296 ms |
126968 KB |
Output is correct |
36 |
Correct |
250 ms |
113520 KB |
Output is correct |
37 |
Correct |
126 ms |
86904 KB |
Output is correct |
38 |
Correct |
162 ms |
94840 KB |
Output is correct |
39 |
Correct |
228 ms |
117368 KB |
Output is correct |
40 |
Correct |
207 ms |
110584 KB |
Output is correct |
41 |
Correct |
88 ms |
82552 KB |
Output is correct |
42 |
Correct |
161 ms |
98516 KB |
Output is correct |
43 |
Correct |
276 ms |
137592 KB |
Output is correct |
44 |
Correct |
326 ms |
120664 KB |
Output is correct |
45 |
Correct |
290 ms |
127096 KB |
Output is correct |
46 |
Correct |
248 ms |
113648 KB |
Output is correct |
47 |
Correct |
119 ms |
85368 KB |
Output is correct |
48 |
Correct |
163 ms |
94968 KB |
Output is correct |
49 |
Correct |
285 ms |
121184 KB |
Output is correct |
50 |
Correct |
305 ms |
129012 KB |
Output is correct |
51 |
Correct |
295 ms |
129008 KB |
Output is correct |
52 |
Correct |
225 ms |
117368 KB |
Output is correct |
53 |
Correct |
205 ms |
110584 KB |
Output is correct |
54 |
Correct |
88 ms |
82552 KB |
Output is correct |
55 |
Correct |
160 ms |
98428 KB |
Output is correct |
56 |
Correct |
278 ms |
137592 KB |
Output is correct |
57 |
Correct |
326 ms |
120536 KB |
Output is correct |
58 |
Correct |
283 ms |
126972 KB |
Output is correct |
59 |
Correct |
236 ms |
113648 KB |
Output is correct |
60 |
Correct |
118 ms |
85368 KB |
Output is correct |
61 |
Correct |
173 ms |
94840 KB |
Output is correct |
62 |
Correct |
272 ms |
121208 KB |
Output is correct |
63 |
Correct |
293 ms |
129012 KB |
Output is correct |
64 |
Correct |
300 ms |
129012 KB |
Output is correct |
65 |
Correct |
2167 ms |
121300 KB |
Output is correct |
66 |
Correct |
2132 ms |
114376 KB |
Output is correct |
67 |
Correct |
921 ms |
86264 KB |
Output is correct |
68 |
Correct |
1184 ms |
102008 KB |
Output is correct |
69 |
Correct |
1330 ms |
141304 KB |
Output is correct |
70 |
Correct |
997 ms |
124376 KB |
Output is correct |
71 |
Correct |
1030 ms |
130844 KB |
Output is correct |
72 |
Correct |
950 ms |
117228 KB |
Output is correct |
73 |
Correct |
726 ms |
89208 KB |
Output is correct |
74 |
Correct |
792 ms |
98680 KB |
Output is correct |
75 |
Correct |
864 ms |
125176 KB |
Output is correct |
76 |
Correct |
1151 ms |
132724 KB |
Output is correct |
77 |
Correct |
1290 ms |
132720 KB |
Output is correct |
78 |
Correct |
1187 ms |
113260 KB |
Output is correct |
79 |
Correct |
1716 ms |
137720 KB |
Output is correct |
80 |
Correct |
1667 ms |
135548 KB |
Output is correct |
81 |
Correct |
1194 ms |
118264 KB |
Output is correct |
82 |
Correct |
1363 ms |
117112 KB |
Output is correct |
83 |
Correct |
684 ms |
78960 KB |
Output is correct |
84 |
Correct |
1758 ms |
137592 KB |
Output is correct |
85 |
Correct |
1737 ms |
135288 KB |
Output is correct |
86 |
Correct |
1311 ms |
118048 KB |
Output is correct |
87 |
Correct |
662 ms |
133880 KB |
Output is correct |
88 |
Correct |
776 ms |
137592 KB |
Output is correct |
89 |
Correct |
849 ms |
135236 KB |
Output is correct |
90 |
Correct |
763 ms |
118136 KB |
Output is correct |
91 |
Correct |
1232 ms |
119928 KB |
Output is correct |