답안 #134620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
134620 2019-07-23T05:43:08 Z sealnot123 이상적인 도시 (IOI12_city) C++14
0 / 100
1000 ms 262148 KB

#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
typedef double D;
typedef long double LD;
const int N = 100005;
vector<int> compx, compy;
int lastx[N], lasty[N];
int orderx[N], ordery[N];
vector<int> g[N];
LL ans;
LL dp[N], sz[N];
int n;
int *XX, *YY;

void add_edge(int a, int b){
	g[a].pb(b); g[b].pb(a);
}

bool cmpx(int a, int b){
	return XX[a] < XX[b];
}

bool cmpy(int a, int b){
	return YY[a] < YY[b];
}

void dfs(int u, int v){
	/*printf("%d\n",u);*/
	for(int e : g[u]){
		if(e == v) continue;
		dfs(e, u);
		ans += (dp[e]+sz[e])*sz[u] + dp[u]*sz[e];
		dp[u] += dp[e] + sz[e];
		sz[u] += sz[e];
	}
}

int DistanceSum(int nn, int *X, int *Y) {
	n = nn;
	XX = X;
	YY = Y;
	int i,j,k,l,a,b,c,d;
	for(i=0;i<n;i++) compx.pb(X[i]), compy.pb(Y[i]);
	sort(all(compx)); sort(all(compy));
	compx.erase(unique(all(compx)), compx.end());
	compy.erase(unique(all(compy)), compy.end());
	for(i=0;i<n;i++) orderx[i] = ordery[i] = i;
	sort(orderx,orderx+n,cmpx);
	sort(ordery,ordery+n,cmpy);
	memset(lastx, -1, sizeof lastx);
	memset(lasty, -1, sizeof lasty);
	for(i = 0; i < n; i++){
		b = orderx[i];
/*		printf("%d ",b);*/
		c = lower_bound(all(compx), X[b]) - compx.begin();
		a = lower_bound(all(compy), Y[b]) - compy.begin();
		if(lasty[a] != -1) add_edge(lasty[a], c);
		lasty[a] = c;
		sz[b]++;
	}
/*	printf("\n");*/
	dfs(0,0);
	for(i=0;i<n;i++) g[i].clear();
	memset(dp, 0, sizeof dp);
	memset(sz, 0, sizeof sz);
	for(i = 0; i < n; i++){
		b = ordery[i];
/*		printf("%d ",b);*/
		c = lower_bound(all(compy), Y[b]) - compy.begin();
		a = lower_bound(all(compx), X[b]) - compx.begin();
		if(lastx[a] != -1) add_edge(lastx[a], c);
		lastx[a] = c;
		sz[b]++;
	}
/*	printf("\n");*/
	dfs(0,0);
  	return (int)ans;
}
/*
4
0 0
0 1
0 2
0 3
(10)

4
1 1
1 2
2 1
2 2
*/

Compilation message

city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:53:8: warning: unused variable 'j' [-Wunused-variable]
  int i,j,k,l,a,b,c,d;
        ^
city.cpp:53:10: warning: unused variable 'k' [-Wunused-variable]
  int i,j,k,l,a,b,c,d;
          ^
city.cpp:53:12: warning: unused variable 'l' [-Wunused-variable]
  int i,j,k,l,a,b,c,d;
            ^
city.cpp:53:20: warning: unused variable 'd' [-Wunused-variable]
  int i,j,k,l,a,b,c,d;
                    ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 242 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 378 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1079 ms 4344 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 246 ms 262144 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -