結果

問題 No.960 マンハッタン距離3
ユーザー chocoruskchocorusk
提出日時 2019-12-23 01:48:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 3,252 bytes
コンパイル時間 1,307 ms
コンパイル使用メモリ 121,376 KB
実行使用メモリ 6,656 KB
最終ジャッジ日時 2024-09-14 23:20:28
合計ジャッジ時間 11,034 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 179 WA * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
	ll w, h;
	cin>>w>>h;
	int n;
	cin>>n;
	ll x[100010], y[100010];
	ll x1[100010], y1[100010];
	for(int i=0; i<n; i++){
		cin>>x[i]>>y[i];
		x1[i]=x[i]+y[i], y1[i]=x[i]-y[i];
	}
	sort(x1, x1+n);
	sort(y1, y1+n);
	ll d=max(x1[n-1]-x1[0], y1[n-1]-y1[0]);
	set<P> st;
	ll px[2], py[2];
	if(d%2!=0){
		cout<<0<<endl;
		return 0;
	}
	if(x1[0]==x1[n-1]){
		sort(x, x+n);
		sort(y, y+n);
		ll ans=x[0]*y[0]+(w-x[n-1]+1)*(h-y[n-1]+1);
		if(n==2) ans+=x[n-1]-x[0]-1;
		cout<<ans<<endl;
		return 0;
	}
	if(y1[0]==y1[n-1]){
		sort(x, x+n);
		sort(y, y+n);
		ll ans=(w-x[n-1]+1)*y[0]+x[0]*(h-y[n-1]+1);
		if(n==2) ans+=x[n-1]-x[0]-1;
		cout<<ans<<endl;
		return 0;
	}
	{
		px[0]=x1[n-1]-d/2, px[1]=x1[0]+d/2;
		py[0]=y1[n-1]-d/2, py[1]=y1[0]+d/2;
		for(int i=0; i<2; i++){
			for(int j=0; j<2; j++){
				ll x0=(px[i]+py[j])/2, y0=(px[i]-py[j])/2;
				if(x0*2!=px[i]+py[j]) continue;
				bool dame=0;
				for(int k=0; k<n; k++){
					if(abs(x[k]-x0)+abs(y[k]-y0)!=abs(x[0]-x0)+abs(y[0]-y0)){
						dame=1;
						break;
					}
				}
				if(!dame){
					st.insert({x0, y0});
				}
			}
		}
	}
	if(st.empty()){
		cout<<0<<endl;
		return 0;
	}
	const ll INF=1e18;
	ll ans=0;
	for(auto p:st){
		ll x0=p.first, y0=p.second;
		y0+=INF;
		bool dame1=0, dame2=0;
		for(int k=0; k<n; k++){
			if(abs(x[k]-x0)+abs(y[k]-y0)!=abs(x[0]-x0)+abs(y[0]-y0)){
				dame1=1;
				break;
			}
		}
		y0-=2*INF;
		for(int k=0; k<n; k++){
			if(abs(x[k]-x0)+abs(y[k]-y0)!=abs(x[0]-x0)+abs(y[0]-y0)){
				dame2=1;
				break;
			}
		}
		y0+=INF;
		if(!dame1 && !dame2){
			if(1<=x0 && x0<=w) cout<<h<<endl;
			else cout<<0<<endl;
			return 0;
		}else if(!dame1){
			if(1<=x0 && x0<=w) ans+=max(0ll, h-y0+1);
		}else if(!dame2){
			if(1<=x0 && x0<=w) ans+=max(0ll, y0);
		}
		x0+=INF;
		dame1=0, dame2=0;
		for(int k=0; k<n; k++){
			if(abs(x[k]-x0)+abs(y[k]-y0)!=abs(x[0]-x0)+abs(y[0]-y0)){
				dame1=1;
				break;
			}
		}
		x0-=2*INF;
		for(int k=0; k<n; k++){
			if(abs(x[k]-x0)+abs(y[k]-y0)!=abs(x[0]-x0)+abs(y[0]-y0)){
				dame2=1;
				break;
			}
		}
		x0+=INF;
		if(!dame1 && !dame2){
			if(1<=y0 && y0<=h) cout<<w<<endl;
			else cout<<0<<endl;
			return 0;
		}else if(!dame1){
			if(1<=y0 && y0<=h) ans+=max(0ll, w-x0+1);
		}else if(!dame2){
			if(1<=y0 && y0<=h) ans+=max(0ll, x0);
		}
	}
	if(st.size()==1){
		cout<<max(1ll, ans)<<endl;
		return 0;
	}
	assert(st.size()==2);
	ll ax[2], ay[2];
	int c=0;
	for(auto p:st){
		ax[c]=p.first, ay[c]=p.second;
		c++;
	}
	if(ay[0]<ay[1]){
		ll mn=max({0ll, 1-ax[0], 1-ay[0]}), mx=min({ax[1]-ax[0], w-ax[0], h-ay[0]});
		ans+=max(0ll, mx-mn-1);
	}else{
		ll mn=max({0ll, 1-ax[0], ay[0]-h}), mx=min({ax[1]-ax[0], w-ax[0], ay[0]-1});
		ans+=max(0ll, mx-mn-1);
	}
	cout<<ans<<endl;
	return 0;
}
0