Submission #16325

#TimeUsernameProblemLanguageResultExecution timeMemory
16325cometIdeal city (IOI12_city)C++98
100 / 100
107 ms8596 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pp; typedef vector<int> vec; typedef vector<vec> mat; #define pb push_back #define MOD 1000000000 struct pt{ int x,y,v; pt(){} pt(int x_,int y_):x(x_),y(y_){} bool operator<(const pt& r)const{ return x!=r.x?x<r.x:y<r.y; } }s[100000]; mat path; int n; int cnt[100000]; ll dfs(int v,int p,ll &ans){ ll ret=0; for(int i=0;i<path[v].size();i++){ int u=path[v][i]; if(u==p)continue; ret+=dfs(u,v,ans); } ret+=cnt[v]; ans+=(n-ret)*ret; ans%=MOD; return ret; } int f(int N, int *X, int *Y) { n=N; for(int i=0;i<N;i++)s[i]=pt(X[i],Y[i]); sort(s,s+N); int sz=0; for(int i=0;i<N;i++){ int p=i; while(s[i].x==s[i+1].x)i++; int v=p; while(v<=i){ while(v<i&&s[v].y+1==s[v+1].y){ s[v].v=sz; v++; } s[v].v=sz; cnt[sz++]=s[v].y-s[p].y+1; p=++v; } } path.clear(); path.assign(sz,vec()); for(int i=0;i<N;i++){ pt* it=lower_bound(s,s+N,pt(s[i].x-1,s[i].y)); if(it->x==s[i].x-1&&it->y==s[i].y)path[s[i].v].pb(it->v); it=lower_bound(s,s+N,pt(s[i].x+1,s[i].y)); if(it->x==s[i].x+1&&it->y==s[i].y)path[s[i].v].pb(it->v); } for(int i=0;i<sz;i++){ sort(path[i].begin(),path[i].end()); path[i].erase(unique(path[i].begin(),path[i].end()),path[i].end()); } ll ret=0; dfs(0,0,ret); return (int)ret; } int DistanceSum(int N, int *X, int *Y){ return (f(N,X,Y)+f(N,Y,X))%MOD; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...