Submission #317771

# Submission time Handle Problem Language Result Execution time Memory
317771 2020-10-30T11:06:56 Z amunduzbaev Ideal city (IOI12_city) C++14
32 / 100
12 ms 1536 KB
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1e9;
ll n, ans;
vector<pair<int, int > > ps;
vector<vector<int > > edges;
vector<pair<int, pair<int, int > > > cur;

int dfs(int u,int p){
	int size = cur[u].second.second - cur[u].second.first+1;
	for(auto x:edges[u]){
		if(x==p) continue;
		size += dfs(x, u);
	}
	ans = ans + (ll)(n-size)*(ll)(size) % mod;
	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}});
		//cout<<ps[i].first<<" "<<ps[i].second << " "<<ps[j].second<<"\n";
		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++)
		swap(ps[i].first, ps[i].second);
	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

ans 174


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:10: 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;
      |      ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 384 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 256 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 384 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 416 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 416 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 1152 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 1536 KB Output isn't correct
2 Halted 0 ms 0 KB -