답안 #155418

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
155418 2019-09-28T02:52:17 Z aer0park 이상적인 도시 (IOI12_city) C++14
100 / 100
61 ms 7544 KB
#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

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++;
                                         ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 4 ms 2680 KB Output is correct
8 Correct 4 ms 2680 KB Output is correct
9 Correct 4 ms 2680 KB Output is correct
10 Correct 4 ms 2680 KB Output is correct
11 Correct 4 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 5 ms 2680 KB Output is correct
4 Correct 5 ms 2680 KB Output is correct
5 Correct 5 ms 2680 KB Output is correct
6 Correct 5 ms 2808 KB Output is correct
7 Correct 5 ms 2808 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 5 ms 2680 KB Output is correct
10 Correct 5 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 3064 KB Output is correct
2 Correct 12 ms 3064 KB Output is correct
3 Correct 24 ms 3620 KB Output is correct
4 Correct 24 ms 3576 KB Output is correct
5 Correct 45 ms 4344 KB Output is correct
6 Correct 45 ms 4472 KB Output is correct
7 Correct 46 ms 4472 KB Output is correct
8 Correct 46 ms 4344 KB Output is correct
9 Correct 45 ms 4592 KB Output is correct
10 Correct 48 ms 7544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 3448 KB Output is correct
2 Correct 13 ms 3448 KB Output is correct
3 Correct 31 ms 4752 KB Output is correct
4 Correct 28 ms 4572 KB Output is correct
5 Correct 61 ms 6932 KB Output is correct
6 Correct 51 ms 6008 KB Output is correct
7 Correct 59 ms 7040 KB Output is correct
8 Correct 51 ms 5880 KB Output is correct
9 Correct 49 ms 5624 KB Output is correct
10 Correct 49 ms 5540 KB Output is correct