답안 #785951

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
785951 2023-07-17T20:10:02 Z KiaRez Janjetina (COCI21_janjetina) C++17
50 / 110
300 ms 25296 KB
/*
    IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef pair<pii, int> piii;
typedef pair<ll, pll> lpll;
typedef pair<pll, ll> plll;
typedef pair<pii, pii> piipii;
typedef pair<pll, pll> pllpll;
typedef long double ld;

#define int long long
#define SQ									   350
#define endl                                   '\n'
#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define PQ                                     priority_queue
#define size(x)                                ((ll)x.size())
#define all(x)                                 (x).begin(),(x).end()
#define kill(x)		                           cout << x << '\n', exit(0);
#define set_dec(x)	                           cout << fixed << setprecision(x);
#define ios				                       ios_base::sync_with_stdio(false), cin.tie(0)
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define file_io(x,y)	                       freopen(x, "r", stdin); freopen(y, "w", stdout);

const int N = 4e5+25, lg=18;
ll Mod = 1e9+7;
//ll Mod = 998244353;

ll MOD(ll a, ll mod=Mod)                   {return (a>=0&&a<mod ? a : ((a%mod+mod)%mod));}
ll max(ll a, ll b)                             {return (a>b ? a : b);}
ll min(ll a, ll b)                             {return (a<b ? a : b);}
ll abso(ll a)                                  {return (a<0?-a:a);}
inline ll poww(ll a, ll b) {
    ll ans = 1;
    a=MOD(a);
    while (b) {
        if (b & 1) ans = MOD(ans*a);
        b >>= 1;
        a = MOD(a*a);
    }
    return ans;
}

ll ans = 0;
int n, k, subt[N], mark[N];
ll c[N];
vector<pii> adj[N];
vector<vector<pii>> vec;

/*inline ll lowbit(int x) { return x & (-x); }
inline void update(int x, int d) { while (x <= n+2) { c[x] += d; x += lowbit(x); } }
inline ll query(int x) {int res = 0; while(x) { res += c[x]; x -= lowbit(x); } return res; }
*/
void update(int x, ll y){x++;for(int i=x;i<=n+2;i+=(i&-i)){c[i]+=y;}}
ll query(int x){
	ll ret=0;x++;
	for(;x;x-=(x&-x)){ret+=c[x];}
	return ret;
}
int findCentroid(int v, int p, int sz) {
	for(auto u : adj[v]) {
		if(u.F == p || mark[u.F]) continue;
		if(subt[u.F] > sz/2) return findCentroid(u.F, v, sz);
	}
	return v;
}

void calcSubt(int v, int p) {
	subt[v] = 1;
	for(auto u : adj[v]) {
		if(mark[u.F] || u.F == p) continue;
		calcSubt(u.F, v);
		subt[v] += subt[u.F];
	}
}

void calcCandidates(int v, int p, int mx, int l) {
	vec.back().pb({mx, l});
	if(mx - l >= k) {ans++;}
	for(auto u : adj[v]) {
		if(u.F == p || mark[u.F]) continue;
		calcCandidates(u.F, v, max(u.S, mx), l+1);
	}
}

void solve(int v) {
	calcSubt(v, 0);
	int cent = findCentroid(v, 0, subt[v]);

	while(size(vec)) vec.pop_back();

	vector<pii> vec2;
	mark[cent] = 1;
	for(auto u : adj[cent]) {
		if(mark[u.F]) continue;
		vec.pb({});
		calcCandidates(u.F, cent, u.S, 1);
		for(auto it : vec.back()) vec2.pb(it);
	}

	sort(all(vec2));
	for(auto it : vec2) {
		if(it.F-k-it.S>=0) ans += ((ll)query(it.F-it.S-k));
		update(it.S, 1);
	}
	for(auto it : vec2) update(it.S, -1);
	
	for(int i=0; i<size(vec); i++) {
		vec2 = vec[i];
		sort(all(vec2));
		for(auto it : vec2) {
			if(it.F-it.S-k>=0) ans -= ((ll)query(it.F-it.S-k));
			update(it.S, 1);
		}
		for(auto it : vec2) update(it.S, -1);
	}

	mark[cent] = 1;
	for(auto u : adj[cent]) {
		if(mark[u.F]) continue;
		solve(u.F);
	}
}

signed main () {
	ios;

	cin>>n>>k;
	for(int v,u,w,i=1; i<n; i++) {
		cin>>v>>u>>w;
		adj[v].pb({u, w});
		adj[u].pb({v, w});
	}

	solve(1);

	cout<<2*ans<<endl;

	//return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 8 ms 9812 KB Output is correct
5 Correct 6 ms 9824 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 6 ms 9888 KB Output is correct
9 Correct 6 ms 9812 KB Output is correct
10 Correct 6 ms 9812 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 6 ms 9812 KB Output is correct
13 Correct 5 ms 9812 KB Output is correct
14 Correct 6 ms 9780 KB Output is correct
15 Correct 5 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9812 KB Output is correct
18 Correct 8 ms 9812 KB Output is correct
19 Correct 6 ms 9776 KB Output is correct
20 Correct 6 ms 9812 KB Output is correct
21 Correct 7 ms 9812 KB Output is correct
22 Correct 6 ms 9760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 20 ms 11316 KB Output is correct
6 Correct 125 ms 17544 KB Output is correct
7 Correct 237 ms 25212 KB Output is correct
8 Correct 261 ms 25224 KB Output is correct
9 Correct 246 ms 25196 KB Output is correct
10 Correct 291 ms 25220 KB Output is correct
11 Correct 234 ms 25116 KB Output is correct
12 Correct 275 ms 25296 KB Output is correct
13 Correct 237 ms 25128 KB Output is correct
14 Correct 277 ms 25164 KB Output is correct
15 Correct 286 ms 25180 KB Output is correct
16 Correct 285 ms 25144 KB Output is correct
17 Correct 292 ms 25164 KB Output is correct
18 Correct 277 ms 25092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 8 ms 9812 KB Output is correct
5 Correct 6 ms 9824 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 6 ms 9888 KB Output is correct
9 Correct 6 ms 9812 KB Output is correct
10 Correct 6 ms 9812 KB Output is correct
11 Correct 6 ms 9812 KB Output is correct
12 Correct 6 ms 9812 KB Output is correct
13 Correct 5 ms 9812 KB Output is correct
14 Correct 6 ms 9780 KB Output is correct
15 Correct 5 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 5 ms 9812 KB Output is correct
18 Correct 8 ms 9812 KB Output is correct
19 Correct 6 ms 9776 KB Output is correct
20 Correct 6 ms 9812 KB Output is correct
21 Correct 7 ms 9812 KB Output is correct
22 Correct 6 ms 9760 KB Output is correct
23 Correct 4 ms 9684 KB Output is correct
24 Correct 4 ms 9684 KB Output is correct
25 Correct 5 ms 9684 KB Output is correct
26 Correct 6 ms 9812 KB Output is correct
27 Correct 20 ms 11316 KB Output is correct
28 Correct 125 ms 17544 KB Output is correct
29 Correct 237 ms 25212 KB Output is correct
30 Correct 261 ms 25224 KB Output is correct
31 Correct 246 ms 25196 KB Output is correct
32 Correct 291 ms 25220 KB Output is correct
33 Correct 234 ms 25116 KB Output is correct
34 Correct 275 ms 25296 KB Output is correct
35 Correct 237 ms 25128 KB Output is correct
36 Correct 277 ms 25164 KB Output is correct
37 Correct 286 ms 25180 KB Output is correct
38 Correct 285 ms 25144 KB Output is correct
39 Correct 292 ms 25164 KB Output is correct
40 Correct 277 ms 25092 KB Output is correct
41 Correct 5 ms 9684 KB Output is correct
42 Correct 257 ms 25148 KB Output is correct
43 Correct 263 ms 25168 KB Output is correct
44 Correct 237 ms 25100 KB Output is correct
45 Correct 286 ms 25268 KB Output is correct
46 Correct 227 ms 25136 KB Output is correct
47 Correct 273 ms 25120 KB Output is correct
48 Correct 271 ms 25112 KB Output is correct
49 Correct 276 ms 25100 KB Output is correct
50 Correct 300 ms 25140 KB Output is correct
51 Correct 275 ms 25128 KB Output is correct
52 Correct 117 ms 17024 KB Output is correct
53 Correct 131 ms 17040 KB Output is correct
54 Correct 96 ms 16592 KB Output is correct
55 Correct 122 ms 17124 KB Output is correct
56 Correct 134 ms 17200 KB Output is correct
57 Correct 228 ms 17372 KB Output is correct
58 Correct 264 ms 17364 KB Output is correct
59 Correct 232 ms 17620 KB Output is correct
60 Correct 280 ms 17652 KB Output is correct
61 Correct 259 ms 17476 KB Output is correct
62 Correct 191 ms 17124 KB Output is correct
63 Correct 226 ms 17260 KB Output is correct
64 Correct 265 ms 17388 KB Output is correct
65 Incorrect 12 ms 10068 KB Output isn't correct
66 Halted 0 ms 0 KB -