Submission #424086

#TimeUsernameProblemLanguageResultExecution timeMemory
424086cfalasIdeal city (IOI12_city)C++17
11 / 100
1026 ms708 KiB
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define INF 10000000
#define MOD 1000000000
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;

#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)

vi a, b;

ii diag[4] = {{0,-1}, {0,1}, {-1,0}, {1,0}};

ll paris(ii x){
	return ((ll)x.F)*(INT_MAX+1ll) + x.S;
}

ii unparis(ll x){
	return {x/(INT_MAX+1ll), x%(INT_MAX+1ll)};
}

int DistanceSum(int n, int *X, int *Y) {
	if(n<=2000){
		unordered_map<ll, vector<ll> > adj;
		FOR(i,n){
			FORi(j,i+1,n){
				if(abs(X[i]-X[j])+abs(Y[i]-Y[j])==1){
					adj[paris({X[i], Y[i]})].push_back(paris({X[j], Y[j]}));
					adj[paris({X[j], Y[j]})].push_back(paris({X[i], Y[i]}));
				}
			}
		}
		int ans=0;
		FOR(i,n){
			// bfs from i
			unordered_set<ll> used;
			queue<ll> q;
			q.push(paris({X[i], Y[i]}));
			used.insert(paris({X[i], Y[i]}));
			q.push(-1);
			int d=0;
			//cout<<X[i]<<" "<<Y[i]<<": "<<endl;
			while(q.size()>1){
				ll t = q.front();
				q.pop();
				if(t==-1){
					d++;
					q.push(-1);
					continue;
				}
				//cout<<t.F<<" "<<t.S<<" "<<d<<endl;
				ans+=d;
				ans%=MOD;

				FOA(x, adj[t]){
					if(!used.count(x)){
						q.push(x);
						used.insert(x);
					}
				}
			}
		}
		return ans/2;
	}
	ll ans=0;
	int minx=INT_MAX, maxx=0;
	int miny=INT_MAX, maxy=0;
	FOR(i,n){
		minx = min(minx, X[i]);
		maxx = max(maxx, X[i]);
		miny = min(miny, Y[i]);
		maxy = max(maxy, Y[i]);
	}
	int c=maxx-minx+1;
	int r=maxy-miny+1;
	FOR(i,n){
		X[i]-=minx;
		Y[i]-=miny;
		if(X[i]>0 && Y[i]>0){
			int xx = X[i];
			int yy = Y[i];
			ans+=(xx+yy - 2)*xx*yy/2;
			ans+=(xx*yy)*2;
			ans%=MOD;
		}
		if(X[i]>0 && Y[i]<r-1){
			int xx = X[i];
			int yy = r - Y[i]-1;
			ans+=(xx+yy - 2)*xx*yy/2;
			ans+=(xx*yy)*2;
			ans%=MOD;
		}
		if(X[i]<c-1 && Y[i]>0){
			int xx = c - X[i]-1;
			int yy = Y[i];
			ans+=(xx+yy - 2)*xx*yy/2;
			ans+=(xx*yy)*2;
			ans%=MOD;
		}
		if(X[i]<c-1 && Y[i]<r-1){
			int xx = c - X[i]-1;
			int yy = r - Y[i]-1;
			ans+=(xx+yy - 2)*xx*yy/2;
			ans+=(xx*yy)*2;
			ans%=MOD;
		}

		ans+=X[i]*(X[i]+1)/2;
		ans%=MOD;
		ans+=Y[i]*(Y[i]+1)/2;
		ans%=MOD;
		ans+=(c-X[i]-1)*(c-X[i])/2;
		ans%=MOD;
		ans+=(r-Y[i]-1)*(r-Y[i])/2;
		ans%=MOD;
	}
	return ans/2;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...