답안 #372902

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
372902 2021-03-02T08:23:11 Z sam571128 Janjetina (COCI21_janjetina) C++14
50 / 110
452 ms 13152 KB
#include <bits/stdc++.h>

#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 1e5+5;
vector<pair<int,int>> adj[N];
vector<pair<int,int>> vv;
int ans = 0;
int n,k;
int dis[N], mx[N], bit[N], arr[N];

void update(int pos, int val){
	pos++;
	while(pos < N){
		bit[pos] += val;
		pos += pos&-pos;
	}
}

int query(int pos){
	pos++;
	int res = 0;
	while(pos>0){
		res += bit[pos];
		pos -= pos&-pos;
	}
	return res;
}

void dfs(int u, int par){
	for(auto [v,w] : adj[u]){
		if(v==par) continue;
		dis[v] = dis[u]+1;
		mx[v] = max(mx[u],w);
		dfs(v,u);
	}
}

void solve(int num){
	sort(vv.begin(),vv.end());
	for(auto [a,b] : vv){
		if(a-b-k>=0) ans += num*query(a-b-k);
		update(b,1);
	}
	for(auto [a,b] : vv){
		update(b,-1);
	}
	vv.clear();
}

void solve(int l, int r){
	if(l >= r) return;
	int mid = l+r>>1;
	solve(l,mid);
	solve(mid+1,r);
	int ma = 0;
	for(int i = mid;i >= l;i--){
		vv.push_back({ma,mid-i});
		ma = max(ma,arr[i-1]);
	}
	ma = 0;
	for(int i = mid;i < r;i++){
		ma = max(ma,arr[i]);
		vv.push_back({ma,i-mid+1});
	}
	solve(1);
	ma = 0;
	for(int i = mid;i >= l;i--){
		vv.push_back({ma,mid-i});
		ma = max(ma,arr[i-1]);
	}
	solve(-1);
	ma = 0;
	for(int i = mid;i < r;i++){
		ma = max(ma,arr[i]);
		vv.push_back({ma,i-mid+1});
	}
	solve(-1);
}

signed main(){
	fastio
	cin >> n >> k;
	for(int i = 1;i < n;i++){
		int u,v,w;
		cin >> u >> v >> w;
		adj[u].push_back({v,w});
		adj[v].push_back({u,w});
		arr[i] = w;
	}
	if(n <= 1000){
		for(int i = 1;i <= n;i++){
			fill(mx,mx+n+1,0);
			dis[i] = 0;
			dfs(i,-1);
			for(int j = 1;j <= n;j++){
				if(mx[j]-dis[j] >= k&&i!=j){
					ans++;
				}
			}
		}
		cout << ans << "\n";
	}else{
		solve(1,n);
		cout << ans*2 << "\n";
	}
}

Compilation message

Main.cpp: In function 'void dfs(long long int, long long int)':
Main.cpp:34:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   34 |  for(auto [v,w] : adj[u]){
      |           ^
Main.cpp: In function 'void solve(long long int)':
Main.cpp:44:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   44 |  for(auto [a,b] : vv){
      |           ^
Main.cpp:48:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   48 |  for(auto [a,b] : vv){
      |           ^
Main.cpp: In function 'void solve(long long int, long long int)':
Main.cpp:56:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   56 |  int mid = l+r>>1;
      |            ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 13 ms 2796 KB Output is correct
5 Correct 13 ms 2796 KB Output is correct
6 Correct 12 ms 2796 KB Output is correct
7 Correct 13 ms 2816 KB Output is correct
8 Correct 13 ms 2796 KB Output is correct
9 Correct 15 ms 2816 KB Output is correct
10 Correct 20 ms 2816 KB Output is correct
11 Correct 14 ms 2796 KB Output is correct
12 Correct 17 ms 2796 KB Output is correct
13 Correct 18 ms 2924 KB Output is correct
14 Correct 17 ms 2796 KB Output is correct
15 Correct 17 ms 2796 KB Output is correct
16 Correct 17 ms 2796 KB Output is correct
17 Correct 20 ms 2796 KB Output is correct
18 Correct 20 ms 2796 KB Output is correct
19 Correct 18 ms 2796 KB Output is correct
20 Correct 17 ms 2816 KB Output is correct
21 Correct 17 ms 2796 KB Output is correct
22 Correct 17 ms 2796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 15 ms 2944 KB Output is correct
5 Correct 31 ms 3692 KB Output is correct
6 Correct 190 ms 6756 KB Output is correct
7 Correct 355 ms 12000 KB Output is correct
8 Correct 409 ms 12532 KB Output is correct
9 Correct 377 ms 12000 KB Output is correct
10 Correct 423 ms 12384 KB Output is correct
11 Correct 356 ms 11944 KB Output is correct
12 Correct 410 ms 12512 KB Output is correct
13 Correct 405 ms 12004 KB Output is correct
14 Correct 415 ms 12644 KB Output is correct
15 Correct 417 ms 12260 KB Output is correct
16 Correct 423 ms 12512 KB Output is correct
17 Correct 405 ms 12512 KB Output is correct
18 Correct 413 ms 12384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 13 ms 2796 KB Output is correct
5 Correct 13 ms 2796 KB Output is correct
6 Correct 12 ms 2796 KB Output is correct
7 Correct 13 ms 2816 KB Output is correct
8 Correct 13 ms 2796 KB Output is correct
9 Correct 15 ms 2816 KB Output is correct
10 Correct 20 ms 2816 KB Output is correct
11 Correct 14 ms 2796 KB Output is correct
12 Correct 17 ms 2796 KB Output is correct
13 Correct 18 ms 2924 KB Output is correct
14 Correct 17 ms 2796 KB Output is correct
15 Correct 17 ms 2796 KB Output is correct
16 Correct 17 ms 2796 KB Output is correct
17 Correct 20 ms 2796 KB Output is correct
18 Correct 20 ms 2796 KB Output is correct
19 Correct 18 ms 2796 KB Output is correct
20 Correct 17 ms 2816 KB Output is correct
21 Correct 17 ms 2796 KB Output is correct
22 Correct 17 ms 2796 KB Output is correct
23 Correct 2 ms 2668 KB Output is correct
24 Correct 2 ms 2668 KB Output is correct
25 Correct 2 ms 2668 KB Output is correct
26 Correct 15 ms 2944 KB Output is correct
27 Correct 31 ms 3692 KB Output is correct
28 Correct 190 ms 6756 KB Output is correct
29 Correct 355 ms 12000 KB Output is correct
30 Correct 409 ms 12532 KB Output is correct
31 Correct 377 ms 12000 KB Output is correct
32 Correct 423 ms 12384 KB Output is correct
33 Correct 356 ms 11944 KB Output is correct
34 Correct 410 ms 12512 KB Output is correct
35 Correct 405 ms 12004 KB Output is correct
36 Correct 415 ms 12644 KB Output is correct
37 Correct 417 ms 12260 KB Output is correct
38 Correct 423 ms 12512 KB Output is correct
39 Correct 405 ms 12512 KB Output is correct
40 Correct 413 ms 12384 KB Output is correct
41 Correct 3 ms 2668 KB Output is correct
42 Correct 349 ms 12000 KB Output is correct
43 Correct 410 ms 12512 KB Output is correct
44 Correct 364 ms 12128 KB Output is correct
45 Correct 414 ms 12512 KB Output is correct
46 Correct 350 ms 12000 KB Output is correct
47 Correct 452 ms 12468 KB Output is correct
48 Correct 353 ms 12140 KB Output is correct
49 Correct 430 ms 12384 KB Output is correct
50 Correct 429 ms 12128 KB Output is correct
51 Correct 407 ms 12384 KB Output is correct
52 Correct 380 ms 12768 KB Output is correct
53 Incorrect 436 ms 13152 KB Output isn't correct
54 Halted 0 ms 0 KB -