#include <bits/stdc++.h>
using namespace std;
char str[100001];
int r,c,sr,sc;
int l;
int arr[51][200001];
typedef pair<int,int> P;
vector<int> st[3];
vector<int> en[3];
vector<int> ts;
vector<int> te;
void init(int rr,int cc,int srr,int scc,int m,char s[]) {
r=rr;
c=cc;
sr=srr;
sc=scc;
l=m;
for(int i=0;i<m;i++) {
str[i]=s[i];
}
arr[sr][sc]=1;
vector<P> v;
int x=sr;
int y=sc;
v.push_back(P(x,y));
for(int i=0;i<m;i++) {
if (str[i]=='W') {
y--;
}
if (str[i]=='E') {
y++;
}
if (str[i]=='N') {
x--;
}
if (str[i]=='S') {
x++;
}
arr[x][y]=1;
v.push_back(P(x,y));
}
if (r==2) {
for(int j=1;j<=2;j++) {
bool flag=false;
for(int i=1;i<=c;i++) {
if (arr[j][i]==0) {
if (!flag) {
flag=true;
st[j].push_back(i);
}
}
if (arr[j][i]==1) {
if (flag) {
flag=false;
en[j].push_back(i-1);
}
}
}
if (arr[j][c]==0) {
en[j].push_back(c);
}
}
bool flag=false;
for(int i=1;i<=c;i++) {
if (arr[1][i]==1&&arr[2][i]==1) {
if (!flag) {
flag=true;
ts.push_back(i);
}
}
else {
if (flag) {
flag=false;
te.push_back(i-1);
}
}
}
if (arr[1][c]==1&&arr[2][c]==1) {
te.push_back(c);
}
}
}
struct UnionFind {
P p[51][51];
void init() {
for(int i=1;i<=r;i++) {
for(int j=1;j<=c;j++) {
p[i][j]=P(-1,-1);
}
}
}
P find(int x,int y) {
if (p[x][y]<P(0,0)) {
return P(x,y);
}
p[x][y]=find(p[x][y].first,p[x][y].second);
return p[x][y];
}
int merge(P a,P b) {
a=find(a.first,a.second);
b=find(b.first,b.second);
if (a==b) {
return 0;
}
p[b.first][b.second]=a;
return 1;
}
};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int solve1(int ar,int ac,int br,int bc) {
UnionFind uf;
uf.init();
int sum=0;
for(int i=ar;i<=br;i++) {
for(int j=ac;j<=bc;j++) {
if (arr[i][j]==0) {
sum++;
for(int k=0;k<4;k++) {
int x=i+dx[k];
int y=j+dy[k];
if (x>=ar&&x<=br&&y>=ac&&y<=bc&&arr[x][y]==0) {
sum-=uf.merge(P(i,j),P(x,y));
}
}
}
}
}
return sum;
}
int solve2(int ar,int ac,int br,int bc) {
if (ar==br) {
int fr=lower_bound(en[ar].begin(),en[ar].end(),ac)-en[ar].begin();
int ba=upper_bound(st[ar].begin(),st[ar].end(),bc)-st[ar].begin()-1;
int ret=ba-fr+1;
return ret;
}
else {
if (ac==bc) {
return (arr[1][ac]==1&&arr[2][ac]==1)?0:1;
}
int fr=lower_bound(te.begin(),te.end(),ac)-te.begin();
int ba=upper_bound(ts.begin(),ts.end(),bc)-ts.begin()-1;
int ret=ba-fr;
if (arr[1][ac]==0||arr[2][ac]==0) {
ret++;
}
if (arr[1][bc]==0||arr[2][bc]==0) {
ret++;
}
return ret;
}
}
int colour(int ar,int ac,int br,int bc) {
if (r<=50&&c<=50) {
return solve1(ar,ac,br,bc);
}
if (r==2) {
return solve2(ar,ac,br,bc);
}
}
Compilation message
rainbow.cpp: In function 'int colour(int, int, int, int)':
rainbow.cpp:168:1: warning: control reaches end of non-void function [-Wreturn-type]
168 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
9 ms |
39436 KB |
Output is correct |
3 |
Correct |
27 ms |
39456 KB |
Output is correct |
4 |
Correct |
19 ms |
39256 KB |
Output is correct |
5 |
Correct |
9 ms |
39516 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
15 ms |
31280 KB |
Output is correct |
12 |
Correct |
15 ms |
39492 KB |
Output is correct |
13 |
Correct |
10 ms |
39256 KB |
Output is correct |
14 |
Correct |
6 ms |
39516 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
28 ms |
3328 KB |
Output is correct |
4 |
Correct |
37 ms |
3796 KB |
Output is correct |
5 |
Correct |
47 ms |
3796 KB |
Output is correct |
6 |
Correct |
33 ms |
3796 KB |
Output is correct |
7 |
Correct |
35 ms |
4048 KB |
Output is correct |
8 |
Correct |
28 ms |
3796 KB |
Output is correct |
9 |
Correct |
34 ms |
3796 KB |
Output is correct |
10 |
Correct |
40 ms |
3792 KB |
Output is correct |
11 |
Correct |
34 ms |
3796 KB |
Output is correct |
12 |
Correct |
27 ms |
3792 KB |
Output is correct |
13 |
Correct |
27 ms |
3792 KB |
Output is correct |
14 |
Correct |
27 ms |
3792 KB |
Output is correct |
15 |
Correct |
38 ms |
3796 KB |
Output is correct |
16 |
Correct |
33 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
9 ms |
39436 KB |
Output is correct |
3 |
Correct |
27 ms |
39456 KB |
Output is correct |
4 |
Correct |
19 ms |
39256 KB |
Output is correct |
5 |
Correct |
9 ms |
39516 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
15 ms |
31280 KB |
Output is correct |
12 |
Correct |
15 ms |
39492 KB |
Output is correct |
13 |
Correct |
10 ms |
39256 KB |
Output is correct |
14 |
Correct |
6 ms |
39516 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Runtime error |
2 ms |
860 KB |
Execution killed with signal 11 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
9 ms |
39436 KB |
Output is correct |
3 |
Correct |
27 ms |
39456 KB |
Output is correct |
4 |
Correct |
19 ms |
39256 KB |
Output is correct |
5 |
Correct |
9 ms |
39516 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
15 ms |
31280 KB |
Output is correct |
12 |
Correct |
15 ms |
39492 KB |
Output is correct |
13 |
Correct |
10 ms |
39256 KB |
Output is correct |
14 |
Correct |
6 ms |
39516 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Runtime error |
2 ms |
860 KB |
Execution killed with signal 11 |
19 |
Halted |
0 ms |
0 KB |
- |