Submission #541703

# Submission time Handle Problem Language Result Execution time Memory
541703 2022-03-24T08:29:06 Z AlperenT Janjetina (COCI21_janjetina) C++17
110 / 110
404 ms 17532 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 1e5 + 5;
 
int n, k, a, b, w, treesz[N];
 
bool isblocked[N];
 
struct Edge{
    int b, w;
};
 
vector<Edge> tree[N];
 
struct Item{
    int w, d;
 
    bool operator < (const Item &sc) const{
        if(w == sc.w) return d < sc.d;
        else return w < sc.w;
    }
};
 
struct Fenwick{
    int tree[N];
 
    stack<pair<int, int>> hist;
 
    int getsum(int r){
        int sum = 0;
        for(int i = r; i > 0; i -= i & -i) sum += tree[i];
        return sum;
    }
 
    int query(int l, int r){
        if(l > r) return 0;
        return getsum(r) - getsum(l - 1);
    }
 
    void update(int pos, int val, bool addhist = true){
        if(addhist) hist.push({pos, val});
        for(int i = pos; i < N; i += i & -i) tree[i] += val;
    }
 
    void clear(){
        while(!hist.empty()){
            update(hist.top().first, -hist.top().second, false);
            hist.pop();
        }
    }
};
 
Fenwick fw;
 
int calcsz(int v, int p){
    treesz[v] = 1;
 
    for(auto e : tree[v]){
        if(e.b != p && !isblocked[e.b]) treesz[v] += calcsz(e.b, v);
    }
 
    return treesz[v];
}
 
int findcent(int v, int p, int sz){
    for(auto e : tree[v]){
        if(e.b != p && !isblocked[e.b] && treesz[e.b] >= sz / 2){
            return findcent(e.b, v, sz);
        }
    }
 
    return v;
}
 
void dfs(int v, int p, int d, int w, vector<Item> &vec){
    vec.push_back({w, d});
 
    for(auto e : tree[v]){
        if(e.b != p && !isblocked[e.b]){
            dfs(e.b, v, d + 1, max(w, e.w), vec);
        }
    }
}
 
long long centdecomp(int v){
    int sz = calcsz(v, 0);
 
    int cent = findcent(v, 0, sz);
 
    long long ans = 0;
 
    vector<Item> total, cur;
 
    for(auto e : tree[cent]){
        if(!isblocked[e.b]){
            cur.clear();
 
            dfs(e.b, cent, 1, e.w, cur);
 
            sort(cur.begin(), cur.end());
 
            for(auto itm : cur){
                if(itm.w - itm.d >= k) ans += 1;
 
                ans -= fw.query(0, itm.w - itm.d - k);
 
                fw.update(itm.d, 1);
 
                total.push_back(itm);
            }
 
            fw.clear();
        }
    }
 
    sort(total.begin(), total.end());
 
    for(auto itm : total){
        ans += fw.query(0, itm.w - itm.d - k);
 
        fw.update(itm.d, 1);
    }
 
    fw.clear();
 
    isblocked[cent] = true;
 
    for(auto e : tree[cent]){
        if(!isblocked[e.b]){
            ans += centdecomp(e.b);
        }
    }
 
    return ans;
}
 
int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
 
    cin >> n >> k;
 
    for(int i = 1; i <= n - 1; i++){
        cin >> a >> b >> w;
 
        tree[a].push_back({b, w});
        tree[b].push_back({a, w});
    }
 
    cout << centdecomp(1) * 2;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 4 ms 2772 KB Output is correct
5 Correct 4 ms 2772 KB Output is correct
6 Correct 4 ms 2772 KB Output is correct
7 Correct 4 ms 2812 KB Output is correct
8 Correct 5 ms 2772 KB Output is correct
9 Correct 3 ms 2692 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 3 ms 2732 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 3 ms 2772 KB Output is correct
14 Correct 4 ms 2772 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 3 ms 2684 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 4 ms 2720 KB Output is correct
20 Correct 4 ms 2772 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 3 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 4 ms 2772 KB Output is correct
5 Correct 27 ms 4172 KB Output is correct
6 Correct 162 ms 9884 KB Output is correct
7 Correct 300 ms 16808 KB Output is correct
8 Correct 373 ms 17460 KB Output is correct
9 Correct 336 ms 16984 KB Output is correct
10 Correct 377 ms 17340 KB Output is correct
11 Correct 330 ms 16828 KB Output is correct
12 Correct 356 ms 17532 KB Output is correct
13 Correct 360 ms 16960 KB Output is correct
14 Correct 357 ms 17312 KB Output is correct
15 Correct 404 ms 17280 KB Output is correct
16 Correct 359 ms 17256 KB Output is correct
17 Correct 366 ms 17200 KB Output is correct
18 Correct 341 ms 17244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 4 ms 2772 KB Output is correct
5 Correct 4 ms 2772 KB Output is correct
6 Correct 4 ms 2772 KB Output is correct
7 Correct 4 ms 2812 KB Output is correct
8 Correct 5 ms 2772 KB Output is correct
9 Correct 3 ms 2692 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 3 ms 2732 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 3 ms 2772 KB Output is correct
14 Correct 4 ms 2772 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 3 ms 2684 KB Output is correct
18 Correct 3 ms 2772 KB Output is correct
19 Correct 4 ms 2720 KB Output is correct
20 Correct 4 ms 2772 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 3 ms 2772 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 4 ms 2772 KB Output is correct
27 Correct 27 ms 4172 KB Output is correct
28 Correct 162 ms 9884 KB Output is correct
29 Correct 300 ms 16808 KB Output is correct
30 Correct 373 ms 17460 KB Output is correct
31 Correct 336 ms 16984 KB Output is correct
32 Correct 377 ms 17340 KB Output is correct
33 Correct 330 ms 16828 KB Output is correct
34 Correct 356 ms 17532 KB Output is correct
35 Correct 360 ms 16960 KB Output is correct
36 Correct 357 ms 17312 KB Output is correct
37 Correct 404 ms 17280 KB Output is correct
38 Correct 359 ms 17256 KB Output is correct
39 Correct 366 ms 17200 KB Output is correct
40 Correct 341 ms 17244 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 318 ms 16928 KB Output is correct
43 Correct 350 ms 17500 KB Output is correct
44 Correct 346 ms 16980 KB Output is correct
45 Correct 361 ms 17280 KB Output is correct
46 Correct 359 ms 17084 KB Output is correct
47 Correct 353 ms 17512 KB Output is correct
48 Correct 347 ms 16960 KB Output is correct
49 Correct 352 ms 17256 KB Output is correct
50 Correct 388 ms 17268 KB Output is correct
51 Correct 354 ms 17260 KB Output is correct
52 Correct 146 ms 10444 KB Output is correct
53 Correct 144 ms 10708 KB Output is correct
54 Correct 99 ms 10212 KB Output is correct
55 Correct 123 ms 10724 KB Output is correct
56 Correct 138 ms 10744 KB Output is correct
57 Correct 304 ms 11128 KB Output is correct
58 Correct 275 ms 11200 KB Output is correct
59 Correct 313 ms 11624 KB Output is correct
60 Correct 288 ms 11252 KB Output is correct
61 Correct 300 ms 11600 KB Output is correct
62 Correct 268 ms 10828 KB Output is correct
63 Correct 267 ms 11072 KB Output is correct
64 Correct 337 ms 11224 KB Output is correct
65 Correct 12 ms 3056 KB Output is correct
66 Correct 2 ms 2644 KB Output is correct