codeforces 1236D
传送门:https://codeforces.com/contest/1236/problem/D
代码实现来至大佬:https://blog.csdn.net/weixin_39132605/article/details/102868652
1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int maxn=1e5+5; 5 6 set<int>s1[maxn],s2[maxn]; 7 int main() 8 { 9 int n,m,k; 10 scanf("%d%d%d",&n,&m,&k); 11 for(int i=1;i<=k;i++){ 12 int x,y; 13 scanf("%d%d",&x,&y); 14 s1[x].insert(y);s2[y].insert(x); 15 } 16 for(int i=1;i<=n;i++){ 17 s1[i].insert(0);s1[i].insert(m+1); 18 } 19 for(int i=1;i<=m;i++){ 20 s2[i].insert(0);s2[i].insert(n+1); 21 } 22 int nowx=1,nowy=1; 23 ll ans=1; 24 int nx=1,ny=1; 25 int maxx=n,maxy=m,minx=1,miny=1; 26 for(int i=0;;i++) 27 { 28 if(i%4==0){ 29 nx=nowx; 30 int pos=*s1[nowx].lower_bound(nowy); 31 ny=min(maxy,pos-1); 32 minx=nx+1; 33 } 34 else if(i%4==1){ 35 ny=nowy; 36 int pos=*s2[nowy].lower_bound(nowx); 37 nx=min(maxx,pos-1); 38 maxy=ny-1; 39 } 40 else if(i%4==2){ 41 nx=nowx; 42 auto it=s1[nowx].lower_bound(nowy); 43 it--; 44 int posy=*it; 45 ny=max(miny,posy+1); 46 maxx=nowx-1; 47 } 48 else if(i%4==3){ 49 ny=nowy; 50 auto it=s2[nowy].lower_bound(nowx); 51 it--; 52 int posx=*it; 53 nx=max(minx,posx+1); 54 miny=nowy+1; 55 } 56 ans+=abs(nx-nowx)+abs(ny-nowy); 57 if(nowx==nx&&nowy==ny){ 58 if(nowx==1&&nowy==1&&i%4==0){} 59 else{break;} 60 } 61 nowx=nx;nowy=ny; 62 } 63 if(ans==(ll)n*m-k){ 64 cout<<"YES"<<endl; 65 } 66 else{ 67 cout<<"NO"<<endl; 68 } 69 return 0; 70 }
纵使单枪匹马,也要勇闯天涯