Submission #3165

#TimeUsernameProblemLanguageResultExecution timeMemory
3165cki86201Ideal city (IOI12_city)C++98
100 / 100
104 ms10260 KiB
#include<algorithm> #include<stdio.h> using namespace std; int ord[100010]; int *X,*Y; int N; int gro[2][100010]; int wei[2][100010]; int len[2]; int C; int st[2][100010],en[2][200020], next[2][200020], c[2]; bool check[2][100010]; long long ans; bool comp1(const int a,const int b){return X[a]!=X[b]?X[a]<X[b]:Y[a]<Y[b];} bool comp2(const int a,const int b){return Y[a]!=Y[b]?Y[a]<Y[b]:X[a]<X[b];} void addedge(int t,int s,int d) { ++c[t]; next[t][c[t]]=st[t][s]; st[t][s]=c[t]; en[t][c[t]]=d; } void make_edge(int t) { t?sort(ord,ord+N,comp1):sort(ord,ord+N,comp2); int i; for(i=0;i<N;i++){ if(i && gro[t^1][ord[i]]==gro[t^1][ord[i-1]] && gro[t][ord[i]]!=gro[t][ord[i-1]]){ int s=gro[t][ord[i]],d=gro[t][ord[i-1]]; addedge(t,s,d); addedge(t,d,s); } } } int dfs(int t,int x) { check[t][x]=1; int i,cnt=0; for(i=st[t][x];i;i=next[t][i]){ if(check[t][en[t][i]])continue; int x=dfs(t,en[t][i]); cnt+=x; ans+=(long long)x*(N-x); } return cnt+wei[t][x]; } int DistanceSum(int Nn, int *Xx, int *Yy){ X=Xx,Y=Yy,N=Nn; int i,cnt=0; for(i=0;i<N;i++)ord[i]=i; sort(ord,ord+N,comp1); for(i=0;i<N;i++){ if(!i || (X[ord[i]]!=X[ord[i-1]] || Y[ord[i]]-Y[ord[i-1]]!=1)){ wei[0][len[0]]=cnt; ++len[0]; cnt=0; } gro[0][ord[i]]=len[0]; cnt++; if(i==N-1)wei[0][len[0]]=cnt; } sort(ord,ord+N,comp2);cnt=0; for(i=0;i<N;i++){ if(!i || (Y[ord[i]]!=Y[ord[i-1]] || X[ord[i]]-X[ord[i-1]]!=1)){ wei[1][len[1]]=cnt; ++len[1]; cnt=0; } gro[1][ord[i]]=len[1]; cnt++; if(i==N-1)wei[1][len[1]]=cnt; } make_edge(0); make_edge(1); dfs(0,1); dfs(1,1); return (int)(ans%1000000000ll); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...