#include "rainbow.h"
#include <bits/stdc++.h>
#define f first
#define s second
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)(x).size()
#define m_p make_pair
#define fast_iati ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll>pll;
const int N=200'000;
int n,m;
struct dsu{
int p[N],sz[N];
void make(int v){
p[v]=v;
sz[v]=1;
}
int get(int v){
return p[v]=(p[v]!=v?get(p[v]):v);
}
bool mg(int a,int b){
a=get(a),b=get(b);
if(a==b) return 0;
if(sz[a]>sz[b]) swap(a,b);
p[a]=b;sz[b]+=sz[a];
return 1;
}
};
dsu in;
map<pii,int> plased;
bool is[51][51];
char dx[4]={'W','E','N','S'};
int tx[4]={0,0,-1,1};
int ty[4]={-1,1,0,0};
vec<pii> go[2];
vec<int> cl[2];
void init(int R, int C, int sr, int sc, int M, char *S) {
n=R,m=C;
--sr;--sc;
plased[{sr,sc}]=1;
if(n<=50 && m<=50)
is[sr][sc]=1;
if(n<=2)
cl[sr].pb(sc);
for(int i=0;i<M;i++){
// cout<<"P "<<S[i]<<endl;
for(int j=0;j<4;j++){
if(dx[j]==S[i]){
sr+=tx[j];sc+=ty[j];
// cout<<"E "<<endl;
}
}
plased[{sr,sc}]=1;
// go[sr].pb
if(n<=2)
cl[sr].pb(sc);
// cout<<"ALO "<<sr+1<<' '<<sc+1<<endl;
if(n<=50 && m<=50)
is[sr][sc]=1;
}
if(n<=50 && m<=50){
}
else if(n==2){
for(int i=0;i<2;i++){
vec<pii> wow;
int last=-1;
sort(all(cl[i]));
for(auto &z : cl[i]){
if(z-last-1>=0)
wow.pb({last+1,z-1});
last=z;
}
go[i]=wow;
}
}
else{
}
}
int ask1(int x,int x1,int y,int y1){
// if(x1==y1 || y==y1) return 0;
// --x1;--y1;
--x;--x1;--y;--y1;
for(int i=x;i<=x1;i++){
for(int j=y;j<=y1;j++){
if(!is[i][j]){
in.make(i*m+j);
// cout<<"IN "<<i<<' '<<j<<endl;
}
}
}
int ans=0;
for(int i=x;i<=x1;i++){
for(int j=y;j<=y1;j++){
if(is[i][j]) continue;
// cout<<"N "<<endl;
for(int t=0;t<4;t++){
int ni=i+tx[t],nj=j+ty[t];
if(ni>=x && ni<=x1 && nj>=y && nj<=y1 && !is[ni][nj])
in.mg(i*m+j,ni*m+nj);
}
}
}
for(int i=x;i<=x1;i++){
for(int j=y;j<=y1;j++){
if(is[i][j]) continue;
// cout<<"HERE "<<i<<' '<<j<<' '<<endl;
if(in.get(i*m+j)==(i*m+j))
ans++;
}
}
return ans;
}
int ask2(int x,int x1,int y,int y1){
// x--;x1--;y--;y1--;
if(y==y1){
if(x==x1){
return 1-binary_search(all(cl[x]),y);
}
if(plased[{x,y}] && plased[{x1,y}]) return 0;
return 1;
}
if(x==x1){
auto l=lower_bound(all(go[x]),m_p(y,y-1))-go[x].begin();
if(l && go[x][l-1].s>=y) l--;
auto r=lower_bound(all(go[x]),m_p(y1+1,y1-1))-go[x].begin();
if(r==sz(go[r]) || go[x][r].f>y1) --r;
return r-l+1;
}
else{
int sh=lower_bound(all(cl[0]),y)-cl[0].begin();
int sh1=lower_bound(all(cl[1]),y)-cl[1].begin();
int ok=0;
if(sh!=sz(cl[0]) && sh1!=sz(cl[1]) && max(cl[0][sh],cl[1][sh1])<=y1)
ok=0;
else
ok=1;
if(ok) return 1;
return ask2(0,0,y,y1)+ask2(1,1,y,y1)-(!plased[{0,y}] && !plased[{1,y}])
-(!plased[{0,y1}] && !plased[{1,y1}])+ok;
}
}
int colour(int ar, int ac, int br, int bc) {
if(n<=50 && m<=50)
return ask1(ar,br,ac,bc);
if(n==2)
return ask2(ar-1,br-1,ac-1,bc-1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
13 ms |
392 KB |
Output is correct |
3 |
Correct |
45 ms |
332 KB |
Output is correct |
4 |
Correct |
44 ms |
344 KB |
Output is correct |
5 |
Correct |
12 ms |
408 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
34 ms |
352 KB |
Output is correct |
12 |
Correct |
26 ms |
332 KB |
Output is correct |
13 |
Correct |
14 ms |
420 KB |
Output is correct |
14 |
Correct |
4 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
128 ms |
9896 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
26 ms |
6604 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
13 ms |
392 KB |
Output is correct |
3 |
Correct |
45 ms |
332 KB |
Output is correct |
4 |
Correct |
44 ms |
344 KB |
Output is correct |
5 |
Correct |
12 ms |
408 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
34 ms |
352 KB |
Output is correct |
12 |
Correct |
26 ms |
332 KB |
Output is correct |
13 |
Correct |
14 ms |
420 KB |
Output is correct |
14 |
Correct |
4 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Incorrect |
55 ms |
4236 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
13 ms |
392 KB |
Output is correct |
3 |
Correct |
45 ms |
332 KB |
Output is correct |
4 |
Correct |
44 ms |
344 KB |
Output is correct |
5 |
Correct |
12 ms |
408 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
34 ms |
352 KB |
Output is correct |
12 |
Correct |
26 ms |
332 KB |
Output is correct |
13 |
Correct |
14 ms |
420 KB |
Output is correct |
14 |
Correct |
4 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Incorrect |
55 ms |
4236 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |