Submission #155418

#TimeUsernameProblemLanguageResultExecution timeMemory
155418aer0parkIdeal city (IOI12_city)C++14
100 / 100
61 ms7544 KiB
#include <vector> #include <algorithm> using namespace std; #define MAXN 100005 #define pb push_back #define sz(v) ((int)(v).size()) #define connect(a,b) con[a].pb(b),con[b].pb(a) typedef long long lld; static const int MOD = 1e9; static int N,M,ans; static vector<int> con[MAXN]; static struct Z{ int x,y; bool operator < (const Z &ot)const{ return y!=ot.y?y<ot.y:x<ot.x; } } A[MAXN]; static struct NODE{ int y,x1,x2; } T[MAXN]; int dfs(int n,int par) { int size=T[n].x2-T[n].x1+1,i,k; for (i=sz(con[n]);i--;){ k = con[n][i]; if (par == k) continue; size += dfs(k,n); } ans = (ans+(lld)size*(N-size))%MOD; return size; } void proc() { int i,j,s,e; sort(A+1,A+N+1); M = 0; for (i=j=1;i<=N;i++){ if (i == N || A[i].y != A[i+1].y || A[i].y == A[i+1].y && A[i].x+1 < A[i+1].x){ T[++M].y = A[i].y; T[M].x1 = A[j].x; T[M].x2 = A[i].x; j = i+1; } } for (i=1;i<=M;i++) con[i].clear(); for (i=1,s=e=2;i<=M;i++){ while (e <= M && (T[e].y <= T[i].y || T[e].y == T[i].y+1 && T[e].x1 <= T[i].x2)) e++; while (s <= M && (T[s].y <= T[i].y || T[s].y == T[i].y+1 && T[s].x2 < T[i].x1)) s++; for (j=s;j<e;j++) connect(i,j); } dfs(1,0); } int DistanceSum(int n, int *x, int *y) { int i; N = n; for (i=1;i<=N;i++) A[i].x = x[i-1], A[i].y = y[i-1]; proc(); for (i=1;i<=N;i++) swap(A[i].x,A[i].y); proc(); return ans; }

Compilation message (stderr)

city.cpp: In function 'void proc()':
city.cpp:43:58: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   if (i == N || A[i].y != A[i+1].y || A[i].y == A[i+1].y && A[i].x+1 < A[i+1].x){
                                       ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
city.cpp:52:60: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   while (e <= M && (T[e].y <= T[i].y || T[e].y == T[i].y+1 && T[e].x1 <= T[i].x2)) e++;
                                         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
city.cpp:53:60: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   while (s <= M && (T[s].y <= T[i].y || T[s].y == T[i].y+1 && T[s].x2 < T[i].x1)) s++;
                                         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...