답안 #363484

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
363484 2021-02-06T08:37:59 Z 8e7 이상적인 도시 (IOI12_city) C++14
100 / 100
57 ms 21728 KB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <set>
#include <queue>
#define ll long long
#define maxn 100005
#define mod 1000000000
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

vector<pii> a;
vector<int> px[maxn], py[maxn];

struct seg{
	int ind = 0, l = 0, r = 0;
	seg(int a, int b, int c) {
		ind = a, l = b, r = c;
	}
	seg();
};
vector<seg> gx[maxn], gy[maxn];



vector<int> adj[maxn];
ll cnt[maxn], siz[maxn];
ll ans = 0, tot;
void dfs(int n, int par) {
	//cout << n << " " << par << endl;
	siz[n] = cnt[n];
	for (int v:adj[n]) {
		if (v != par) {
			dfs(v, n);
			siz[n] += siz[v];
		}
	}
	ans += siz[n] * (tot - siz[n]);
}
int DistanceSum(int N, int *X, int *Y) {
	tot = N;
	int mx = 2147483647, my = 2147483647;
	for (int i = 0;i < N;i++) mx = min(mx, X[i]), my = min(my, Y[i]);
	for (int i = 0;i < N;i++) {
		a.push_back(make_pair(X[i] - mx, Y[i] - my));
		//cout << X[i] - mx << " " << Y[i] - my << endl;
		px[X[i] - mx].push_back(Y[i] - my);
		py[Y[i] - my].push_back(X[i] - mx);
	}
	int ind = 0, st = 0, num = 0;
	for (int i = 0;i < N;i++) {
		sort(px[i].begin(), px[i].end());

		for (int j = 0;j < px[i].size();j++) {
			if (!(j && px[i][j] == px[i][j - 1] + 1)) {
				if (j) {
					gx[i].push_back(seg(ind, st, px[i][j - 1]));
					cnt[ind++] = num;
					num = 0;
				}
				st = px[i][j];
			}
			num++;
		}
		if (px[i].size() ) {
			gx[i].push_back(seg(ind, st, px[i].back()));
			cnt[ind++] = num;
			num = 0;
		}
	}
	for (int i = 0;i < N;i++) {
		int cur = 0;
		//cout << i << endl;
		for (seg j: gx[i]) {
			//cout << j.ind << " " << j.l << " " << j.r << ' ' << cnt[j.ind] << endl;
			while (cur < gx[i + 1].size() && gx[i + 1][cur].l <= j.r) {
				if (gx[i + 1][cur].r >= j.l) {
					adj[j.ind].push_back(gx[i + 1][cur].ind);
					adj[gx[i + 1][cur].ind].push_back(j.ind);
				}
				cur++;
			}
			if (cur > 0) cur--;
		}
	}
	dfs(0, -1);
	for(int i = 0;i < N;i++) adj[i].clear(), cnt[i] = 0, siz[i] = 0;
	ind = 0, st = 0, num = 0;
	for (int i = 0;i < N;i++) {
		sort(py[i].begin(), py[i].end());

		for (int j = 0;j < py[i].size();j++) {
			if (!(j && py[i][j] == py[i][j - 1] + 1)) {
				if (j) {
					gy[i].push_back(seg(ind, st, py[i][j - 1]));
					cnt[ind++] = num;
					num = 0;
				}
				st = py[i][j];
			}
			num++;
		}
		if (py[i].size() ) {
			gy[i].push_back(seg(ind, st, py[i].back()));
			cnt[ind++] = num;
			num = 0;
		}
	}
	for (int i = 0;i < N;i++) {
		int cur = 0;
		//cout << i << endl;
		for (seg j: gy[i]) {
			//cout << j.ind << " " << j.l << " " << j.r << ' ' << cnt[j.ind] << endl;
			while (cur < gy[i + 1].size() && gy[i + 1][cur].l <= j.r) {
				if (gy[i + 1][cur].r >= j.l) {
					adj[j.ind].push_back(gy[i + 1][cur].ind);
					adj[gy[i + 1][cur].ind].push_back(j.ind);
				}
				cur++;
			}
			if (cur > 0) cur--;
		}
	}
	dfs(0, -1);
	return ans % mod;
}
/*

int main() {
	io
	int n;
	cin >> n;
	int x[n], y[n];
	for (int i = 0;i < n;i++) cin >> x[i];
	for (int i = 0;i <n ;i++) cin >> y[i];
	cout << DistanceSum(n, x, y);
}
/*
11
2 2 3 3 4 4 4 4 5 5 5
5 6 3 6 3 4 5 6 3 4 6

6
0 0 1 1 1 1
0 1 1 2 3 4
 */

Compilation message

city.cpp:143:1: warning: "/*" within comment [-Wcomment]
  143 | /*
      |  
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for (int j = 0;j < px[i].size();j++) {
      |                  ~~^~~~~~~~~~~~~~
city.cpp:81:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<seg>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |    while (cur < gx[i + 1].size() && gx[i + 1][cur].l <= j.r) {
      |           ~~~~^~~~~~~~~~~~~~~~~~
city.cpp:97:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |   for (int j = 0;j < py[i].size();j++) {
      |                  ~~^~~~~~~~~~~~~~
city.cpp:119:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<seg>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |    while (cur < gy[i + 1].size() && gy[i + 1][cur].l <= j.r) {
      |           ~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12012 KB Output is correct
2 Correct 9 ms 12012 KB Output is correct
3 Correct 10 ms 12012 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 9 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 11 ms 12140 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 11 ms 12140 KB Output is correct
5 Correct 10 ms 12288 KB Output is correct
6 Correct 9 ms 12268 KB Output is correct
7 Correct 10 ms 12268 KB Output is correct
8 Correct 9 ms 12268 KB Output is correct
9 Correct 10 ms 12140 KB Output is correct
10 Correct 10 ms 12140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 13292 KB Output is correct
2 Correct 15 ms 13288 KB Output is correct
3 Correct 26 ms 14692 KB Output is correct
4 Correct 26 ms 14692 KB Output is correct
5 Correct 46 ms 17504 KB Output is correct
6 Correct 43 ms 17376 KB Output is correct
7 Correct 46 ms 17396 KB Output is correct
8 Correct 44 ms 17376 KB Output is correct
9 Correct 45 ms 17376 KB Output is correct
10 Correct 53 ms 21728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 13676 KB Output is correct
2 Correct 17 ms 13548 KB Output is correct
3 Correct 40 ms 16100 KB Output is correct
4 Correct 30 ms 15588 KB Output is correct
5 Correct 56 ms 19936 KB Output is correct
6 Correct 47 ms 18528 KB Output is correct
7 Correct 57 ms 20064 KB Output is correct
8 Correct 49 ms 18528 KB Output is correct
9 Correct 47 ms 18016 KB Output is correct
10 Correct 47 ms 17888 KB Output is correct