QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#616313 | #3294. 网格 | DengDuck | 96 | 1898ms | 71036kb | C++14 | 2.3kb | 2024-10-06 01:38:02 | 2024-10-06 01:38:04 |
Judging History
answer
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define LL long long
#define pII pair<int,int>
#define Fi first
#define Se second
#define pb push_back
using namespace std;
const int N=4e5+5;
const int Dx[8]={0,0,1,-1,1,1,-1,-1};
const int Dy[8]={1,-1,0,0,-1,1,-1,1};
int n,m,c,TOT,Sz[N],Fa[N];
LL V,E,R;
inline int Fd(int x)
{
if(Fa[x]==x)return x;
return Fd(Fa[x]);
}
map<pII,int>Ma,Nam;
vector<pII>St;
pII P[N];
inline bool In(int x,int y)
{
return 1<=x&&x<=n&&1<=y&&y<=m;
}
inline int Num(int x,int y)
{
if(!In(x,y)||x==n||y==m)return 0;
if(Nam[{x,y}])return Nam[{x,y}];
Nam[{x,y}]=++TOT,Fa[TOT]=TOT,Sz[TOT]=1;
return TOT;
}
inline void Mrg(int x,int y,int op=0)
{
x=Fd(x),y=Fd(y);
if(x==y)return;
if(Sz[x]<Sz[y])swap(x,y);
if(op)St.pb({x,y});
Fa[y]=x,Sz[x]+=Sz[y];
R--;
}
inline void Rd(int &s)
{
s=0;char ch=getchar();
while(ch<'0'||ch>'9'){ch=getchar();}
while(ch>='0'&&ch<='9'){s=(s<<3)+(s<<1)+(ch-'0');ch=getchar();}
}
inline void Work()
{
Rd(n),Rd(m),Rd(c);
TOT=0,Sz[0]=1,Fa[0]=0;
St.clear(),Ma.clear(),Nam.clear();
V=1ll*n*m,E=1ll*(n-1)*m+1ll*n*(m-1),R=1ll*(n-1)*(m-1)+1;
for(int i=1,x,y;i<=c;i++)
{
Rd(x),Rd(y);
Ma[{x,y}]=1,V--;
for(int j=0;j<4;j++)
{
int X=x+Dx[j],Y=y+Dy[j];
if(In(X,Y)&&!Ma[{X,Y}])E--;
}
Mrg(Num(x-1,y),Num(x,y));
Mrg(Num(x,y-1),Num(x,y));
Mrg(Num(x-1,y-1),Num(x-1,y));
P[i]={x,y};
}
if(V-E+R>2)return puts("0"),void();
if(V<=2)return puts("-1"),void();
if(n==1||m==1)return puts("1"),void();
for(int i=1;i<=c;i++)
{
for(int j=0;j<8;j++)
{
int x=P[i].Fi+Dx[j],y=P[i].Se+Dy[j];
if(!In(x,y)||Ma[{x,y}])continue;
--V;
LL E2=E;
for(int j=0;j<4;j++)
{
int X=x+Dx[j],Y=y+Dy[j];
if(In(X,Y)&&!Ma[{X,Y}])E--;
}
int Cnt=TOT;
Mrg(Num(x-1,y),Num(x,y),1);
Mrg(Num(x,y-1),Num(x,y),1);
Mrg(Num(x-1,y-1),Num(x-1,y),1);
if(V-E+R>2)return puts("1"),void();
while(!St.empty())
{
pII t=St.back();St.pop_back();
++R;
Fa[t.Se]=t.Se,Sz[t.Fi]-=Sz[t.Se];
}
if(Num(x-1,y)>Cnt)Nam[{x-1,y}]=0;
if(Num(x,y-1)>Cnt)Nam[{x,y-1}]=0;
if(Num(x-1,y-1)>Cnt)Nam[{x-1,y-1}]=0;
if(Num(x,y)>Cnt)Nam[{x,y}]=0;
E=E2,V++,TOT=Cnt;
}
}
puts("2");
}
int main()
{
int T;Rd(T);
while(T--){Work();}
}
詳細信息
Pretests
Final Tests
Test #1:
score: 4
Accepted
time: 1ms
memory: 7644kb
input:
20 1 1 0 2 1 2 1 1 2 1 1 4 2 1 4 1 1 4 1 0 2 2 2 2 2 1 1 2 1 1 1 1 3 1 0 3 1 2 3 1 2 1 2 2 2 1 2 1 1 2 2 0 1 3 3 1 2 1 1 1 3 1 3 2 1 3 1 1 2 2 1 1 2 2 2 4 2 1 1 1 2 2 1 2 1 4 2 1 2 1 4 1 1 1 1 1 4 1 1 1 1 1 4 2 1 1 1 2 1 3 1 1 2 1 2 0
output:
-1 -1 -1 1 0 -1 1 -1 -1 2 -1 -1 1 -1 0 -1 1 -1 0 -1
result:
ok 20 numbers
Test #2:
score: 4
Accepted
time: 1ms
memory: 5716kb
input:
20 2 4 8 1 4 2 4 1 2 1 3 1 1 2 2 2 1 2 3 3 2 5 1 2 1 1 2 1 3 1 3 2 8 1 8 1 1 3 1 6 1 8 1 4 1 7 1 2 1 5 1 1 8 0 4 2 7 2 1 3 2 1 1 4 1 3 1 2 2 4 2 1 7 1 1 6 4 2 4 3 1 2 2 2 1 3 2 1 8 6 1 5 1 3 1 2 1 6 1 4 1 1 2 3 1 2 2 3 2 3 1 1 1 2 3 2 1 7 6 1 4 1 3 1 1 1 2 1 6 1 5 8 1 1 1 1 2 4 6 2 4 2 1 1 1 1 2 1 4...
output:
-1 -1 -1 1 -1 0 0 -1 1 1 -1 1 -1 -1 1 1 2 0 2 -1
result:
ok 20 numbers
Test #3:
score: 4
Accepted
time: 1ms
memory: 5504kb
input:
20 7 2 3 1 1 5 1 3 2 3 3 2 1 1 3 3 5 3 6 4 1 5 3 4 3 5 2 1 3 2 3 3 5 10 3 2 1 1 2 4 3 5 1 2 2 5 1 4 2 3 3 1 1 5 3 5 1 3 3 2 7 4 2 2 2 1 2 4 1 2 15 1 15 3 1 9 1 12 1 2 1 6 1 5 1 13 1 8 1 11 1 14 1 7 1 15 1 1 1 10 1 4 1 1 3 0 1 14 2 1 3 1 9 3 5 15 3 3 3 2 2 3 1 2 3 1 3 4 2 2 1 4 1 1 2 5 3 5 1 5 1 3 2 ...
output:
1 1 0 0 2 0 -1 1 0 -1 2 -1 0 0 0 0 0 0 0 -1
result:
ok 20 numbers
Test #4:
score: 4
Accepted
time: 1ms
memory: 7760kb
input:
19 5 6 2 4 4 5 2 30 1 1 11 1 5 6 6 4 3 4 2 5 6 5 1 2 3 1 5 6 5 15 4 3 3 5 4 4 6 4 2 1 5 5 1 2 2 3 6 2 5 4 4 2 4 5 3 4 2 2 5 1 4 7 10 4 3 4 1 4 4 3 4 3 3 2 1 1 3 2 5 3 7 1 1 15 2 0 9 3 2 9 1 9 2 9 3 0 7 4 3 2 1 2 3 5 3 6 5 27 1 5 1 2 2 1 2 2 5 1 3 2 3 4 5 2 5 3 6 1 4 1 1 4 2 4 6 5 2 3 3 3 5 4 4 3 6 2...
output:
1 0 1 0 1 2 1 2 1 0 2 1 1 0 0 1 0 0 1
result:
ok 19 numbers
Test #5:
score: 4
Accepted
time: 1ms
memory: 5648kb
input:
20 10 10 35 2 2 9 2 5 6 1 9 6 3 4 6 3 6 8 4 2 4 3 9 5 10 4 9 3 4 6 2 4 10 10 5 10 7 6 1 5 8 2 6 4 5 3 1 8 1 1 2 5 4 1 4 7 1 8 10 8 2 3 7 5 9 10 1 7 4 6 9 10 3 9 11 20 6 6 8 6 4 6 7 2 3 11 1 9 1 5 4 8 2 8 6 11 9 6 8 5 1 10 6 1 6 8 7 5 7 4 7 7 5 2 2 7 10 10 100 5 6 5 5 3 3 7 3 4 2 6 6 5 1 4 7 1 5 1 1 ...
output:
0 0 -1 1 0 1 1 0 1 0 0 2 2 0 2 0 1 0 0 1
result:
ok 20 numbers
Test #6:
score: 4
Accepted
time: 3ms
memory: 5752kb
input:
20 17 17 8 13 2 16 15 11 13 16 6 4 5 15 17 17 4 11 7 17 17 4 15 9 10 3 15 12 11 3 20 15 160 3 3 16 7 16 8 13 1 1 11 1 7 15 3 3 11 6 13 20 11 20 13 13 8 6 5 17 13 17 6 7 5 13 5 11 4 12 15 6 9 7 10 1 1 15 5 6 14 16 14 5 15 20 15 13 12 13 7 5 11 14 15 3 4 4 10 12 9 8 4 12 14 4 8 16 13 7 1 6 8 10 1 7 14...
output:
2 2 0 1 1 0 0 2 0 2 2 1 0 2 0 1 0 2 1 0
result:
ok 20 numbers
Test #7:
score: 4
Accepted
time: 7ms
memory: 5848kb
input:
20 32 31 992 26 25 7 10 22 29 11 20 1 7 8 5 11 21 5 28 26 10 26 30 26 7 9 19 18 24 20 26 6 3 9 16 25 25 3 6 25 21 24 20 8 20 6 11 19 29 32 20 8 1 9 8 22 8 6 28 26 29 16 5 17 4 18 28 19 6 28 11 14 7 4 6 1 23 29 3 25 18 21 10 1 22 10 18 29 20 5 24 4 16 23 2 15 31 30 1 16 2 26 1 4 27 2 9 14 24 19 31 14...
output:
-1 2 2 2 0 0 2 2 0 0 2 2 0 0 1 2 2 0 2 2
result:
ok 20 numbers
Test #8:
score: 4
Accepted
time: 2ms
memory: 7776kb
input:
20 10000 2 3 9765 1 10000 1 7719 1 400 50 5 304 15 116 23 19 10 305 15 243 37 2 10000 2 1 6458 2 2626 50 400 5 8 324 36 95 36 98 40 216 2 367 1 20000 0 100 200 5 2 1 31 84 17 194 1 199 72 52 10 2000 4 3 1090 10 1699 1 1954 7 1479 20 1000 5 12 445 16 215 19 195 19 855 1 1000 20000 1 1 3402 1 50 400 5...
output:
1 2 1 2 1 1 2 2 0 2 1 2 1 1 2 2 2 2 1 1
result:
ok 20 numbers
Test #9:
score: 4
Accepted
time: 0ms
memory: 7688kb
input:
20 20 1000 8 15 355 10 396 2 167 18 124 2 483 2 55 19 450 1 881 50 400 10 26 22 4 25 18 134 50 315 3 281 45 49 22 248 1 81 34 355 44 50 2000 10 7 166 9 1240 4 1239 5 1132 5 1131 4 1759 5 1997 9 100 200 15 3 52 88 95 8 178 87 94 16 4 4 51 76 57 68 9 10 50 12 131 100 165 28 20 50 198 10 51 3 53 200 10...
output:
2 2 2 2 1 2 2 1 2 1 2 1 1 1 1 0 1 1 2 1
result:
ok 20 numbers
Test #10:
score: 4
Accepted
time: 3ms
memory: 5656kb
input:
20 200 100 28 12 85 81 1 95 19 192 73 61 11 60 11 137 35 43 100 103 46 119 55 136 35 61 49 121 88 120 87 26 19 177 39 199 2 177 64 60 10 199 1 84 36 199 100 76 83 4 19 74 7 94 24 107 69 186 77 200 100 29 95 70 165 89 160 26 2 100 199 3 76 31 161 27 179 39 172 15 181 53 181 52 44 59 199 1 152 80 162 ...
output:
1 1 2 1 2 1 1 1 1 1 2 1 2 2 0 2 1 0 2 1
result:
ok 20 numbers
Test #11:
score: 4
Accepted
time: 201ms
memory: 8216kb
input:
20 100 200 290 62 71 95 63 47 129 90 54 92 44 41 55 6 11 74 1 24 164 72 108 12 156 52 66 99 45 6 26 9 74 42 43 18 161 20 197 50 84 84 133 29 119 12 15 99 15 77 45 94 92 8 63 68 85 34 101 30 163 88 195 32 191 44 150 84 197 19 88 66 128 10 100 8 88 80 148 60 138 81 185 88 66 53 19 25 178 83 44 5 140 1...
output:
1 2 2 2 1 2 1 1 1 1 2 2 0 1 2 1 1 1 2 1
result:
ok 20 numbers
Test #12:
score: 4
Accepted
time: 259ms
memory: 15260kb
input:
20 400 250 290 153 110 227 12 333 161 125 81 322 161 149 53 365 210 395 91 150 61 279 37 24 123 132 173 235 16 215 238 247 63 101 52 284 242 55 56 10 165 276 141 226 187 309 128 152 91 319 32 334 83 128 99 91 193 150 238 383 238 279 239 365 22 398 170 111 93 336 136 15 227 56 66 383 35 240 210 234 1...
output:
2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 0 2 2 2 2
result:
ok 20 numbers
Test #13:
score: 4
Accepted
time: 282ms
memory: 15708kb
input:
20 600 500 290 523 12 293 473 512 264 350 283 320 490 292 29 442 167 91 336 171 338 327 376 535 332 19 126 89 448 43 379 469 170 598 25 302 117 597 134 380 484 549 130 594 416 214 441 584 347 109 418 6 488 12 292 235 14 592 216 485 400 540 2 58 442 76 328 186 128 443 489 102 416 144 302 172 128 532 ...
output:
2 2 2 2 2 2 2 2 2 2 0 2 2 2 1 2 2 2 2 2
result:
ok 20 numbers
Test #14:
score: 4
Accepted
time: 300ms
memory: 15652kb
input:
20 1000 1000 290 619 978 634 23 462 811 213 493 972 634 921 856 621 256 270 950 41 224 267 348 56 576 420 43 779 573 539 126 643 89 820 694 133 431 190 917 566 58 759 718 960 768 26 361 814 28 340 580 134 922 190 417 346 770 37 785 908 729 233 984 802 104 774 164 176 863 242 487 101 618 267 970 951 ...
output:
2 2 2 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2
result:
ok 20 numbers
Test #15:
score: 4
Accepted
time: 237ms
memory: 21468kb
input:
20 20000 20000 100 4396 15423 6910 12273 17082 16964 15130 6615 8304 5948 10882 5798 780 14436 5165 4931 3109 11062 2478 11425 4575 2057 19414 19306 4396 11278 4394 4656 4879 12273 14173 13878 17915 2601 18316 3514 11952 9185 7529 5228 1585 3769 8034 8331 387 5038 14425 5799 18835 2787 6117 7956 227...
output:
2 2 1 0 1 2 1 0 0 1 0 0 2 1 1 2 2 1 1 2
result:
ok 20 numbers
Test #16:
score: 4
Accepted
time: 1898ms
memory: 71036kb
input:
20 100000 100000 500 82912 33958 11 33879 29395 33977 43 33910 28 33936 11 33992 29357 33997 43 33898 29388 33894 82908 33946 15028 33951 15030 33940 99996 33992 33 33919 29346 33992 29365 33974 34 33880 82914 33898 33 33906 50013 33919 99997 33899 50018 33954 29355 100000 50015 33935 24 33899 4 339...
output:
1 2 0 0 1 0 1 0 1 0 2 1 2 1 0 2 1 2 1 1
result:
ok 20 numbers
Test #17:
score: 4
Accepted
time: 1ms
memory: 5716kb
input:
20 1 1 0 1 1000000000 0 999999999 999999999 0 999999999 1 0 1000000000 999999999 0 1 999999999 0 2 1 0 2 999999999 0 1000000000 3 0 1000000000 1 0 2 1000000000 0 2 3 0 1000000000 2 0 1000000000 1000000000 0 999999999 1000000000 0 1 2 0 999999999 2 0 999999999 3 0 2 2 0 1 3 0
output:
-1 1 2 1 2 1 -1 2 2 1 2 2 2 2 2 -1 2 2 2 1
result:
ok 20 numbers
Test #18:
score: 4
Accepted
time: 1ms
memory: 5648kb
input:
20 1 1 1 1 1 1 5 1 1 1 999999999 1000000000 1 233 2333333 999999999 1000000000 0 2 1000000000 1 1 1 3 1 1 3 1 2 2 1 1 1 3 3 1 2 2 2 1000000000 1 1 1000000000 1000000000 1 1 2333333 1 999999999 1000000000 1 999999999 1000000000 5 5 1 3 3 1 1000000000 1 1 1000000000 3 1000000000 1 1 1 1000000000 1 0 1...
output:
-1 1 2 2 1 -1 1 2 1 0 2 2 1 2 1 -1 1 2 1 -1
result:
ok 20 numbers
Test #19:
score: 4
Accepted
time: 1ms
memory: 5648kb
input:
20 1 1 1 1 1 1 5 2 1 1 1 2 999999999 1000000000 2 233 2333333 1 999999999 999999999 1000000000 2 999999999 999999999 999999998 1000000000 2 1000000000 2 2 1 1 1 1 4 2 1 4 1 1 2 2 2 2 1 1 2 3 3 2 3 3 2 2 3 1000000000 2 3 33333333 2 33333333 1000000000 1 2 2333334 1 2333333 1 999999999 1000000000 2 99...
output:
-1 1 1 0 2 -1 0 1 1 0 2 0 2 1 0 -1 1 1 1 0
result:
ok 20 numbers
Test #20:
score: 4
Accepted
time: 1ms
memory: 7680kb
input:
20 1000000000 999999999 3 2 1 3 1 64262205 501694537 1000000000 1000000000 3 104007914 3620272 104007912 3620271 104007913 3620271 2 2 1 2 2 2 1000000000 3 2 1 1 2 2 999999999 1 999999999 3 1 2 1 1 1 999999998 999999999 1 1 580618293 1 999999999 2 1 28331699 2 1 1000000000 3 1 999999999 1 435063528 ...
output:
1 2 1 0 0 0 1 0 0 2 1 0 0 1 2 1 -1 -1 1 -1
result:
ok 20 numbers
Test #21:
score: 4
Accepted
time: 2ms
memory: 7720kb
input:
20 1000000000 999999999 10 829970101 672512538 829970102 672512538 349907600 671247209 261313286 745742893 637739740 496933621 997475676 80882182 637739739 496933622 123517947 242744364 263792054 171898468 637739739 496933621 2 1000000000 10 2 619577448 1 387868281 1 851264516 2 112009155 2 51967231...
output:
2 1 0 0 1 0 1 -1 1 1 -1 2 -1 0 0 -1 2 2 1 0
result:
ok 20 numbers
Test #22:
score: 4
Accepted
time: 3ms
memory: 5692kb
input:
20 1000000000 999999999 30 900975111 554163600 371763539 17484966 775395952 16728573 200178896 33338782 376569612 878038674 936711147 663465965 257927289 27421517 627892316 696360382 936711146 663465964 925149029 126067681 521769550 733454447 931660040 659336341 185749459 541827369 42140008 71971923...
output:
2 -1 0 -1 0 -1 0 0 2 1 2 1 0 1 1 0 -1 1 1 1
result:
ok 20 numbers
Test #23:
score: 4
Accepted
time: 52ms
memory: 6228kb
input:
20 1000000000 1000000000 300 999999977 2 999999984 9 999999968 19 999999992 1000000000 999999990 999999984 999999978 25 13 999999995 2 18 4 999999995 999999970 999999984 999999976 13 999999988 24 999999983 18 1000000000 999999989 999999984 21 6 999999980 999999968 32 999999980 999999997 999999968 99...
output:
1 2 2 1 1 1 1 1 1 1 1 1 1 2 2 0 2 1 1 0
result:
ok 20 numbers
Test #24:
score: 4
Accepted
time: 205ms
memory: 21588kb
input:
20 1000000000 1000000000 100 347739502 476099551 999999995 476099540 347739499 476099547 1 476099558 9 476099548 1 476099549 347739506 1000000000 6 476099548 347739501 476099539 347739499 476099562 3 476099562 686950414 476099555 13 476099545 999999996 476099548 686950414 3 999999997 476099541 10000...
output:
0 1 1 2 2 1 1 2 2 1 2 2 0 0 1 1 1 1 1 2
result:
ok 20 numbers
Test #25:
score: 0
Time Limit Exceeded
input:
20 1000000000 1000000000 500 16 999999942 999999945 999999976 5 999999942 12 999999984 999999931 999999951 999999969 999999999 999999938 999999974 999999941 999999989 999999958 6 999999952 999999960 9 999999938 999999963 999999936 999999998 999999971 999999931 999999940 999999960 999999923 999999965...