QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#603908 | #9170. Cycle Game | peter | RE | 15ms | 67104kb | C++14 | 3.7kb | 2024-10-01 20:54:06 | 2024-10-01 20:54:06 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int maxn=6e5+5;
int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={-1,0,1,-1,1,-1,0,1};
int fa[maxn],sz[maxn],n,m,q;
vector<pii> tr[maxn<<2];
map<pii,int> mp;
int st[maxn],top=0;
pii que[maxn];
int find(int x){
if(fa[x]==x) return x;
return find(fa[x]);
}
void merge(int x,int y){
x=find(x);
y=find(y);
if(x==y) return;
if(sz[x]>sz[y]) swap(x,y);
fa[x]=y;
sz[y]+=sz[x];
st[++top]=x;
}
void del(int lst){
while(top>lst){
int x=st[top];
top--;
sz[fa[x]]-=sz[x];
fa[x]=x;
}
}
int getid(int x,int y){
return x*(m+2)+y;
}
bool bk[maxn];
void update(int now,int l,int r,int ql,int qr,pii x){
if(ql>qr) return;
if(ql<=l&&qr>=r){
tr[now].push_back(x);
return;
}
int mid=(l+r)>>1;
if(ql<=mid) update(now<<1,l,mid,ql,qr,x);
if(qr>mid) update(now<<1|1,mid+1,r,ql,qr,x);
}
void dfs(int now,int l,int r){
int lst=top;
for(pii x : tr[now]) merge(x.first,x.second);
// printf("kk%d %d %d\n",now,l,r);
if(l==r){
int x=que[l].first,y=que[l].second,tt=-1;
bool flag=0;
for(int j=0;j<8;j++){
int nx=x+dx[j],ny=y+dy[j];
if(!bk[getid(nx,ny)]){
if(tt==-1) tt=find(getid(nx,ny));
else if(tt!=find(getid(nx,ny))){
// if(l==47) printf("\n%d %d\n",nx,ny);
flag=1;
break;
}
}else{
int ppt=top;
for(int t=0;t<8;t++){
int xx=nx+dx[t],yy=ny+dy[t];
if(xx<0||xx>n+1||yy<0||yy>m+1) continue;
if(bk[getid(xx,yy)]||(xx==x&&yy==y)) continue;
merge(getid(nx,ny),getid(xx,yy));
}
int ttt=-1;
for(int t=0;t<8;t++){
int xx=x+dx[t],yy=y+dy[t];
if((!bk[getid(xx,yy)])||(xx==nx&&yy==ny)){
if(ttt==-1) ttt=find(getid(xx,yy));
else if(ttt!=find(getid(xx,yy))){
// if(l==47) printf("\n%d %d %d %d\n",nx,ny,xx,yy);
flag=1;
break;
}
}
}
del(ppt);
if(flag) break;
}
}
if(flag){
for(int j=0;j<8;j++){
int nx=x+dx[j],ny=y+dy[j];
pii tmp=make_pair(getid(x,y),getid(nx,ny));
if(bk[getid(nx,ny)]) continue;
if(mp[tmp]==l){
// if(l==45) printf("\n%d %d : %d %d %d %d\n",l,q,x,y,nx,ny);
update(1,1,q,l+1,q,tmp);
}else{
// if(l==45) printf("\n%d %d : %d %d\n",l,mp[tmp]-1,tmp.first,tmp.second);
update(1,1,q,l+1,mp[tmp]-1,tmp);
}
}
// printf("%d : 0\n",l);
putchar('0');
}else{
bk[getid(x,y)]=1;
// printf("%d : 1\n",l);
putchar('1');
}
del(lst);
return;
}
int mid=(l+r)>>1;
dfs(now<<1,l,mid);
for(pii x : tr[now]) merge(x.first,x.second);
dfs(now<<1|1,mid+1,r);
del(lst);
}
int main(){
mp.clear();
scanf("%d %d %d",&n,&m,&q);
for(int i=1;i<=q;i++){
int x,y;
scanf("%d %d",&x,&y);
// if(x==9&&y==8) printf("%d : %d %d\n",i,x,y);
for(int j=0;j<8;j++){
int nx=x+dx[j],ny=y+dy[j];
pii t1=make_pair(getid(x,y),getid(nx,ny)),t2=make_pair(getid(nx,ny),getid(x,y));
if(mp.find(t1)==mp.end()) update(1,1,q,1,i-1,t1);
mp[t1]=mp[t2]=i;
}
bk[getid(x,y)]=1;
que[i]=make_pair(x,y);
}
// for(int i=0;i<=n+1;i++){
// for(int j=0;j<=m+1;j++) printf("%d %d : %d\n",i,j,getid(i,j));
// }
for(int i=0;i<=(n+2)*(m+2);i++) fa[i]=i;
for(int x=0;x<=n+1;x++){
for(int y=0;y<=m+1;y++){
for(int k=0;k<8;k++){
int nx=x+dx[k],ny=y+dy[k];
if(nx<0||nx>n+1||ny<0||ny>m+1) continue;
if(bk[getid(x,y)]||bk[getid(nx,ny)]) continue;
// printf("%d %d - %d %d\n",x,y,nx,ny);
merge(getid(x,y),getid(nx,ny));
}
}
}
// for(int i=1;i<=49;i++) printf("%d %d\n",que[i].first,que[i].second);
for(int i=1;i<=q;i++) bk[getid(que[i].first,que[i].second)]=0;
dfs(1,1,q);
puts("");
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 15ms
memory: 67104kb
input:
4 3 7 2 1 2 2 2 3 3 1 3 2 4 1 4 2
output:
1111111
result:
ok "1111111"
Test #2:
score: 0
Accepted
time: 9ms
memory: 65196kb
input:
3 3 8 1 1 1 2 1 3 2 3 3 3 3 2 3 1 2 1
output:
11111110
result:
ok "11111110"
Test #3:
score: 0
Accepted
time: 7ms
memory: 65276kb
input:
10 10 7 9 1 6 6 3 8 8 7 5 10 1 7 1 2
output:
1111111
result:
ok "1111111"
Test #4:
score: 0
Accepted
time: 11ms
memory: 65064kb
input:
9 10 50 1 9 1 6 2 3 3 1 7 4 9 4 1 3 2 5 9 2 7 9 5 6 8 10 9 5 5 5 4 10 9 7 5 9 3 2 4 5 1 1 4 7 3 6 2 8 4 3 8 6 5 10 4 8 5 4 7 2 9 6 4 2 7 8 5 2 3 5 9 1 6 1 1 5 9 9 5 8 6 3 8 8 8 4 7 7 7 1 3 7 2 2 3 10 6 9 8 3 7 6
output:
11111111111111111111111111111111111111111111111111
result:
ok "11111111111111111111111111111111111111111111111111"
Test #5:
score: 0
Accepted
time: 4ms
memory: 63212kb
input:
3 5 11 1 5 2 4 1 2 1 3 3 3 3 1 3 4 2 3 1 4 2 1 2 5
output:
11111111111
result:
ok "11111111111"
Test #6:
score: 0
Accepted
time: 3ms
memory: 65200kb
input:
7 9 12 7 3 2 3 6 2 2 2 4 2 2 8 5 7 4 4 6 8 2 7 7 2 1 9
output:
111111111111
result:
ok "111111111111"
Test #7:
score: 0
Accepted
time: 4ms
memory: 63064kb
input:
1 4 1 1 2
output:
1
result:
ok "1"
Test #8:
score: 0
Accepted
time: 7ms
memory: 63272kb
input:
9 8 67 5 5 8 3 9 5 7 4 5 1 9 3 4 2 2 5 1 7 7 8 7 2 8 5 6 1 8 8 4 4 5 4 1 5 3 4 6 7 2 3 3 7 5 7 2 4 2 7 1 3 7 3 2 8 6 6 6 2 6 3 7 5 9 6 7 6 3 6 1 1 6 4 3 1 5 3 8 7 2 1 4 1 8 4 8 6 3 5 5 8 1 6 1 2 4 6 9 4 1 4 3 3 4 8 8 1 4 7 9 8 3 8 6 5 6 8 3 2 2 2 7 1 9 2 4 3 1 8 4 5 8 2 7 7
output:
1111111111111111111111111111111111111111111110010101101000101101101
result:
ok "111111111111111111111111111111...1111111110010101101000101101101"
Test #9:
score: 0
Accepted
time: 3ms
memory: 65192kb
input:
3 10 3 3 9 2 5 2 7
output:
111
result:
ok "111"
Test #10:
score: -100
Runtime Error
input:
222212 1 21562 105762 1 167947 1 127551 1 117618 1 174844 1 139867 1 156729 1 30554 1 54488 1 151832 1 132914 1 109432 1 212091 1 136499 1 17818 1 48806 1 95752 1 66607 1 39930 1 23054 1 160823 1 169054 1 96680 1 150677 1 52895 1 93103 1 118079 1 79155 1 194811 1 141874 1 138763 1 2600 1 121471 1 17...