답안 #317858

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
317858 2020-10-30T14:55:50 Z amunduzbaev 이상적인 도시 (IOI12_city) C++14
100 / 100
67 ms 7924 KB
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n, ans;
vector<pair<int, int>> ps;
vector<vector<int>> edges;
vector<pair<int, pair<int, int>>> cur;

int dfs(int u,int p){
	ll size = cur[u].second.second - cur[u].second.first + 1;
	for(auto x:edges[u]){
		if(x==p) continue;
		size += dfs(x, u);
	}
	ans = ((ll)ans + (ll)(n-size) * (ll)size) % 1000000000ll;
	return size;
}

void build_tree(){
	cur.clear();
	sort(ps.begin(), ps.end());

	for(int i=0;i<n;i++){
		int j=i;
		while(j+1 < n && ps[i].first == ps[j+1].first && ps[j].second+1 == ps[j+1].second) j++;
		cur.push_back({ps[i].first, {ps[i].second, ps[j].second}});
		i=j;
	}

	edges.clear();
	edges.resize(n);
	int j=0; 

	for(int i=0;i<cur.size();i++){
		while(j < cur.size() && cur[j].first <= cur[i].first) j++;
		if(j == cur.size()) break;
		while(j<n-1 && cur[j].second.second < cur[i].second.first && cur[j].first == cur[i].first+1 ) j++;
		while(j<n && cur[j].second.second <= cur[i].second.second && cur[j].first == cur[i].first+1 ){
			edges[i].push_back(j);
			edges[j].push_back(i);
			j++;
		}
		if(j != n && cur[j].second.first <= cur[i].second.second && cur[i].first+1 == cur[j].first){
			edges[i].push_back(j);
			edges[j].push_back(i);
		}
	}
	dfs(0, 0);
}
int DistanceSum(int N, int *x, int *y) {
	n=N;
	ps.resize(n);
	
	for(int i=0;i<n;i++)
		ps[i] = {x[i], y[i]};
	build_tree();

	for(int i=0;i<n;i++)
		ps[i] = {y[i], x[i]};
	build_tree();
	
	return ans;
}

/*

11
2 5
2 6
3 3
3 6
4 3
4 4
4 5
4 6
5 3
5 4
5 6

15
3 1
4 1
2 2
3 2
4 2
5 2
1 3
2 3
3 3
4 3
5 3
2 4
3 4
4 4
5 4

*/

Compilation message

city.cpp: In function 'void build_tree()':
city.cpp:35:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  for(int i=0;i<cur.size();i++){
      |              ~^~~~~~~~~~~
city.cpp:36:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |   while(j < cur.size() && cur[j].first <= cur[i].first) j++;
      |         ~~^~~~~~~~~~~~
city.cpp:37:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |   if(j == cur.size()) break;
      |      ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1152 KB Output is correct
2 Correct 9 ms 1280 KB Output is correct
3 Correct 23 ms 2432 KB Output is correct
4 Correct 23 ms 2432 KB Output is correct
5 Correct 48 ms 4344 KB Output is correct
6 Correct 48 ms 5164 KB Output is correct
7 Correct 67 ms 5368 KB Output is correct
8 Correct 51 ms 4984 KB Output is correct
9 Correct 45 ms 5368 KB Output is correct
10 Correct 52 ms 7924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 1536 KB Output is correct
2 Correct 11 ms 1408 KB Output is correct
3 Correct 33 ms 3260 KB Output is correct
4 Correct 28 ms 3000 KB Output is correct
5 Correct 60 ms 6128 KB Output is correct
6 Correct 54 ms 5944 KB Output is correct
7 Correct 62 ms 6896 KB Output is correct
8 Correct 57 ms 5952 KB Output is correct
9 Correct 55 ms 5688 KB Output is correct
10 Correct 51 ms 5560 KB Output is correct