제출 #1127216

#제출 시각아이디문제언어결과실행 시간메모리
1127216steveonalexJanjetina (COCI21_janjetina)C++20
110 / 110
242 ms17740 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){ while(a > 0 && b > 0){ if (a > b) a %= b; else b %= a; } return a + b; } ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ll mask){return __builtin_ctzll(mask);} int logOf(ll mask){return __lg(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T& container, string separator = " ", string finish = "\n"){ for(auto item: container) cout << item << separator; cout << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } struct FenwickTree{ int n; vector<ll> a; FenwickTree(int _n){ n = _n; a.resize(n+1); } void update(int i, ll v){ while(i <= n){ a[i] += v; i += LASTBIT(i); } } ll get(int i){ ll ans = 0; while(i > 0){ ans += a[i]; i -= LASTBIT(i); } return ans; } ll get(int l, int r){return get(r) - get(l-1);} int binary_lift(ll x){ int p = MASK(logOf(n)), idx =0; while(p > 0){ if (idx + p <= n && x > a[idx + p]){ idx += p; x -= a[idx]; } p >>= 1; } return idx + 1; } }; const int N = 1e5 + 69; int n, k; vector<pair<int, int>> graph[N]; int weight[N]; ll ans = 0; ll count_pair(vector<pair<int, int>> a){ if (a.size() == 1) return 0; int ma = 0; for(pair<int, int> i: a) maximize(ma, i.second); FenwickTree bit(ma + 1); sort(ALL(a)); ll ans = 0; for(pair<int, int> i: a){ int cur = i.first - k - i.second; if (cur >= 0) ans += bit.get(min(cur + 1, ma + 1)); bit.update(i.second + 1, 1); } return ans; } namespace CentroidDecomposition{ bool banned[N]; int sz[N]; int h[N], ma[N]; void find_sz(int u, int p){ sz[u] = 1; for(pair<int, int> v: graph[u]) if (v.first != p && !banned[v.second]){ find_sz(v.first, u); sz[u] += sz[v.first]; } } int find_centroid(int u){ for(pair<int, int> v: graph[u]) if (!banned[v.second]) { if (sz[v.first] * 2 > sz[u]) { sz[u] -= sz[v.first]; sz[v.first] += sz[u]; return find_centroid(v.first); } } return u; } void dfs(int u, int p){ for(pair<int, int> v: graph[u]) if (v.first != p && !banned[v.second]){ h[v.first] = h[u] + 1; ma[v.first] = max(ma[u], weight[v.second]); dfs(v.first, u); } } void get_point(int u, int p, vector<pair<int, int>> &skr){ skr.push_back({ma[u], h[u]}); for(pair<int, int> v: graph[u]) if (v.first != p && !banned[v.second]){ get_point(v.first, u, skr); } } void go(int u, int layer){ find_sz(u, 0); u = find_centroid(u); h[u] = 0, ma[u] = 0; dfs(u, 0); vector<vector<pair<int, int>>> ski; vector<pair<int, int>> tot; tot.push_back({0, 0}); for(pair<int, int> v: graph[u]) if (!banned[v.second]){ vector<pair<int, int>> skr; get_point(v.first, u, skr); for(pair<int, int> j: skr) tot.push_back(j); ski.push_back(skr); } // cout << "->> Node: " << u << "\n"; // for(pair<int, int> j: tot) cout << j.first << " " << j.second << "\n"; // cout << "Plus: " << count_pair(tot) << "\n"; ans += count_pair(tot); for(auto j: ski){ ans -= count_pair(j); } for(pair<int, int> v: graph[u]) if (!banned[v.second]){ banned[v.second] = true; go(v.first, layer + 1); } } } void solve(){ cin >> n >> k; for(int i = 1; i < n; ++i){ int u, v; cin >> u >> v >> weight[i]; graph[u].push_back({v, i}); graph[v].push_back({u, i}); } CentroidDecomposition::go(1, 0); cout << ans * 2 << "\n"; } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t start = clock(); solve(); cerr << "Time elapsed: " << clock() - start << "ms!\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...