Submission #16465

# Submission time Handle Problem Language Result Execution time Memory
16465 2015-08-26T05:48:41 Z gs14004 두 섬간의 연결 (kriii1_2) C++14
1 / 1
60 ms 1868 KB
#include <cstdio>
typedef long long lint;

struct disj{
	int pa[100005], sz[100005];
	void init(int n){
		for(int i=0; i<=n; i++){
			pa[i] = i;
			sz[i] = 1;
		}
	}
	int find(int x){
		return pa[x] = (pa[x] == x ? x : find(pa[x]));
	}
	int getsize(int x){
		return sz[find(x)];
	}
	bool uni(int p, int q){
		p = find(p);
		q = find(q);
		pa[q] = p;
		sz[p] += sz[q];
	}
}disj;

lint f(int x){
	return 1ll * x * (x-1) / 2;
}

int main(){
	int n;
	scanf("%d",&n);
	disj.init(n);
	lint p1 = 0, p2 = 0;
	for(int i=0; i<n-1; i++){
		int x;
		scanf("%d",&x);
		p1 -= f(disj.getsize(x));
		p1 -= f(disj.getsize(x+1));
		p1 += f(disj.getsize(x) + disj.getsize(x+1));
		p2 += f(disj.getsize(x)) * disj.getsize(x+1);
		p2 += f(disj.getsize(x+1)) * disj.getsize(x);
		p2 += 1ll * disj.getsize(x) * disj.getsize(x+1);
		disj.uni(x, x+1);
		printf("%lld %lld\n",p1, p2);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 45 ms 1868 KB Output is correct
2 Correct 35 ms 1868 KB Output is correct
3 Correct 60 ms 1868 KB Output is correct
4 Correct 46 ms 1868 KB Output is correct
5 Correct 0 ms 1868 KB Output is correct
6 Correct 57 ms 1868 KB Output is correct