Submission #687709

# Submission time Handle Problem Language Result Execution time Memory
687709 2023-01-26T21:32:18 Z QwertyPi Paths (RMI21_paths) C++14
68 / 100
575 ms 23672 KB
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#define int long long
using namespace std;

const int MAXN = 2e5 + 11;
vector<pair<int, int>> G[MAXN];
int w[MAXN], mx_dis[MAXN];
int to[MAXN], a[MAXN], l[MAXN], r[MAXN];
int leaf_cnt = 0;
void dfs(int v, int pa = -1){
    int sons_cnt = 0;
    l[v] = MAXN, r[v] = -1;
    for(auto& [u, we] : G[v]){
        if(u != pa){
            sons_cnt++;
            w[u] = we; dfs(u, v);
            if(mx_dis[u] + w[u] > mx_dis[v]){
                to[v] = to[u];
                mx_dis[v] = mx_dis[u] + w[u];
            }
            l[v] = min(l[v], l[u]), r[v] = max(r[v], r[u]);
        }
    }
    if(sons_cnt == 0){
        to[v] = ++leaf_cnt; l[v] = r[v] = leaf_cnt;
    }
    a[to[v]] += w[v];
}

namespace Treap{

    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    struct node{
        int key, size, sum, prior;
        node *ll, *rr;
        node(int key) : key(key), size(1), sum(key), prior(rng()), ll(nullptr), rr(nullptr) {};
    };

    int size(node* t){
        return t ? t->size : 0;
    }
    int sum(node* t){
        return t ? t->sum : 0;
    }
    void maintain(node*& t){
        if(!t) return;
        t->size = size(t->ll) + 1 + size(t->rr);
        t->sum = sum(t->ll) + t->key + sum(t->rr);
    }
    void _crawl(node* t){
        if(!t) return;
        if(t->ll) _crawl(t->ll);
        cout << t->key << ' ';
        if(t->rr) _crawl(t->rr);
    }
    void crawl(node* t){
        _crawl(t); cout << endl;
    }
    void split_size(node* t, node*& l, node*& r, int l_size){
        if(!t) return void(l = r = nullptr);
        if(size(t->ll) >= l_size) split_size(t->ll, l, t->ll, l_size), r = t;
        else split_size(t->rr, t->rr, r, l_size - size(t->ll) - 1), l = t;
        maintain(l); maintain(r);
    }
    void split_key(node* t, node*& l, node*& r, int key){
        if(!t) return void(l = r = nullptr);
        if(t->key >= key) split_key(t->ll, l, t->ll, key), r = t;
        else split_key(t->rr, t->rr, r, key), l = t; 
        maintain(l); maintain(r);
    }
    void merge(node*& t, node* l, node* r){
        if(!l || !r) t = l ? l : r;
        else if(l->prior > r->prior) merge(l->rr, l->rr, r), t = l;
        else merge(r->ll, l, r->ll), t = r;
        maintain(t);
    }
    node* subtree_min(node* t){
        while(t->ll) t = t->ll;
        return t;
    }
    node *a = nullptr;
    void add(int key){
        node *l, *v, *r;
        v = new node(key);
        split_key(a, l, r, key);
        merge(r, v, r);
        merge(a, l, r);
    }
    void erase(int key){
        node *l, *m, *r;
        split_key(a, l, m, key);
        split_size(m, m, r, 1);
        delete m;
        merge(a, l, r);
    }
    int kth_max_sum(int k){
        node *l, *r;
        split_size(a, l, r, max(0LL, size(a) - k));
        int res = sum(r);
        merge(a, l, r);
        return res;
    }
};

namespace Segtree{
    int t[MAXN << 2], a[MAXN];
    int cmp(int q1, int q2){
        if(q1 == -1 || q2 == -1) return q1 == -1 ? q2 : q1;
        else return a[q1] > a[q2] ? q1 : q2;
    }
    void upd(int i, int va, int v, int l, int r){
        if(l == r) { a[i] = va; t[v] = i; return; }
        int m = (l + r) >> 1;
        if(i <= m) upd(i, va, v * 2 + 1, l, m);
        else upd(i, va, v * 2 + 2, m + 1, r);
        t[v] = cmp(t[v * 2 + 1], t[v * 2 + 2]);
    }
    int qry_max(int ql, int qr, int v, int l, int r){
        if(qr < l || r < ql) return -1;
        if(ql <= l && r <= qr) return t[v];
        int m = (l + r) >> 1;
        int q1 = qry_max(ql, qr, v * 2 + 1, l, m);
        int q2 = qry_max(ql, qr, v * 2 + 2, m + 1, r);
        return cmp(q1, q2);
    }
};

int ans[MAXN]; int N, K; 
void dfs2(int v, int pa = -1){
    ans[v] = Treap::kth_max_sum(K);
    for(auto& [u, we] : G[v]){
        if(u != pa){
            using Segtree::a;
            int bl = 1, br = leaf_cnt, sl = l[u], sr = r[u];
            int q1 = Segtree::qry_max(bl, sl - 1, 0, 1, leaf_cnt), q2 = Segtree::qry_max(sr + 1, br, 0, 1, leaf_cnt);
            int qo = Segtree::cmp(q1, q2); Treap::erase(a[qo]); Treap::add(a[qo] + w[u]); Segtree::upd(qo, a[qo] + w[u], 0, 1, leaf_cnt);
            int qn = Segtree::qry_max(sl, sr, 0, 1, leaf_cnt); Treap::erase(a[qn]); Treap::add(a[qn] - w[u]); Segtree::upd(qn, a[qn] - w[u], 0, 1, leaf_cnt);
            dfs2(u, v);
            Treap::erase(a[qo]); Treap::add(a[qo] - w[u]); Segtree::upd(qo, a[qo] - w[u], 0, 1, leaf_cnt);
            Treap::erase(a[qn]); Treap::add(a[qn] + w[u]); Segtree::upd(qn, a[qn] + w[u], 0, 1, leaf_cnt);
        }
    }
}

int32_t main(){
    cin.tie(0); cout.tie(0);
    cin >> N >> K; int sum_w = 0;
    for(int i = 0; i < N - 1; i++){
        int u, v, w; cin >> u >> v >> w;
        G[u].push_back({v, w});
        G[v].push_back({u, w});
        sum_w += w;
    }
    
    if(N == 2){
        cout << sum_w << endl;
        cout << sum_w << endl;
        return 0;
    }

    int rt = 1;
    if(G[rt].size() == 1) rt = G[rt][0].first;
    dfs(rt);
    for(int i = 1; i <= leaf_cnt; i++) Treap::add(a[i]);
    for(int i = 1; i <= leaf_cnt; i++) Segtree::upd(i, a[i], 0, 1, leaf_cnt);
    dfs2(rt);
    for(int i = 1; i <= N; i++) cout << ans[i] << '\n';
}

Compilation message

Main.cpp: In function 'void dfs(long long int, long long int)':
Main.cpp:15:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   15 |     for(auto& [u, we] : G[v]){
      |               ^
Main.cpp: In function 'void dfs2(long long int, long long int)':
Main.cpp:133:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  133 |     for(auto& [u, we] : G[v]){
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 4984 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 4984 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 6 ms 5212 KB Output is correct
9 Correct 6 ms 5204 KB Output is correct
10 Correct 5 ms 5076 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 7 ms 5196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 4984 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 6 ms 5212 KB Output is correct
9 Correct 6 ms 5204 KB Output is correct
10 Correct 5 ms 5076 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 7 ms 5196 KB Output is correct
13 Correct 9 ms 5332 KB Output is correct
14 Correct 8 ms 5412 KB Output is correct
15 Correct 8 ms 5332 KB Output is correct
16 Correct 12 ms 5332 KB Output is correct
17 Correct 8 ms 5332 KB Output is correct
18 Correct 8 ms 5304 KB Output is correct
19 Correct 10 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 556 ms 20932 KB Output is correct
2 Correct 532 ms 23308 KB Output is correct
3 Correct 406 ms 16844 KB Output is correct
4 Correct 536 ms 20932 KB Output is correct
5 Correct 533 ms 22056 KB Output is correct
6 Correct 528 ms 21092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 4984 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 6 ms 5212 KB Output is correct
9 Correct 6 ms 5204 KB Output is correct
10 Correct 5 ms 5076 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 7 ms 5196 KB Output is correct
13 Correct 9 ms 5332 KB Output is correct
14 Correct 8 ms 5412 KB Output is correct
15 Correct 8 ms 5332 KB Output is correct
16 Correct 12 ms 5332 KB Output is correct
17 Correct 8 ms 5332 KB Output is correct
18 Correct 8 ms 5304 KB Output is correct
19 Correct 10 ms 5332 KB Output is correct
20 Correct 556 ms 20932 KB Output is correct
21 Correct 532 ms 23308 KB Output is correct
22 Correct 406 ms 16844 KB Output is correct
23 Correct 536 ms 20932 KB Output is correct
24 Correct 533 ms 22056 KB Output is correct
25 Correct 528 ms 21092 KB Output is correct
26 Correct 561 ms 21320 KB Output is correct
27 Correct 549 ms 23256 KB Output is correct
28 Correct 497 ms 23672 KB Output is correct
29 Correct 436 ms 17000 KB Output is correct
30 Incorrect 575 ms 21352 KB Output isn't correct
31 Halted 0 ms 0 KB -