Submission #402628

# Submission time Handle Problem Language Result Execution time Memory
402628 2021-05-12T05:16:17 Z qwerasdfzxcl Fireworks (APIO16_fireworks) C++14
100 / 100
1058 ms 194728 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
struct Line{
    ll x, y, a;
    Line(){}
    Line(ll _x, ll _y, ll _a): x(_x), y(_y), a(_a) {}
    bool operator<(const Line &L) const{
        if (x==L.x) return a<L.a;
        return x<L.x;
    }
};
struct Graph{
    multiset<ll> pt;
    ll of, mx;
    Graph() {}
};
vector<Graph> st, ts;
vector<pair<int, int>> adj[300300];
int idx[300300];
ll dp[300300], ans[300300];

void st_merge(int v, int w){
    st[v].mx += st[w].mx;
    for (auto val:st[w].pt){
        st[v].pt.insert(val-st[w].of+st[v].of);
    }
}

void ts_merge(int v, int w){
    ts[v].mx += ts[w].mx;
    for (auto val:ts[w].pt){
        ts[v].pt.insert(val-ts[w].of+ts[v].of);
    }
}

void dfs(int s, ll cur){
    if(adj[s].empty()){
        Graph tmp;
        tmp.of = 0, tmp.mx = 1;
        tmp.pt.insert(cur);
        tmp.pt.insert(cur);
        st.push_back(tmp);
        ts.push_back(tmp);
        idx[s] = (int)st.size()-1;
        dp[s] = cur;
        ans[s] = 0;
        return;
    }
    for (auto &v:adj[s]) dfs(v.first, cur+v.second);
    for (auto &v:adj[s]){
        //printf("%d %d %d\n", s, idx[v.first], st[idx[v.first]].pt.size());
        st[idx[v.first]].of += v.second;
        for (int i=2;i<=st[idx[v.first]].mx;i++) st[idx[v.first]].pt.erase(--st[idx[v.first]].pt.end());
        st[idx[v.first]].mx = 1;
        vector<ll> tmp;
        for (int i=0;i<2;i++){
            tmp.push_back(*(--st[idx[v.first]].pt.end()));
            tmp.back() += v.second;
            st[idx[v.first]].pt.erase(--st[idx[v.first]].pt.end());
        }
        for (int i=0;i<2;i++) st[idx[v.first]].pt.insert(tmp[i]);
    }
    int mx = 0;
    for (auto &v:adj[s]) if (mx<(int)st[idx[v.first]].pt.size()) mx = (int)st[idx[v.first]].pt.size(), idx[s] = idx[v.first];
    for (auto &v:adj[s]) if (idx[s]!=idx[v.first]) st_merge(idx[s], idx[v.first]);
    auto iter = st[idx[s]].pt.end();
    for (int i=st[idx[s]].mx;i>=0;i--) --iter;
    dp[s] = (*iter)-st[idx[s]].of;
    //printf("%d %lld\n", s, dp[s]);
    //printf("%lld\n", st[idx[s]].of);
    //for (auto &val:st[idx[s]].pt) printf("%lld ", val);
    //printf("\n");

    for (auto &v:adj[s]){
        //printf("%d %d %d\n", s, idx[v.first], st[idx[v.first]].pt.size());
        ts[idx[v.first]].of += v.second;
        for (int i=2;i<=ts[idx[v.first]].mx;i++) ts[idx[v.first]].pt.erase(--ts[idx[v.first]].pt.end());
        ts[idx[v.first]].mx = 1;
        vector<ll> tmp;
        for (int i=0;i<2;i++){
            tmp.push_back(*(--ts[idx[v.first]].pt.end()));
            tmp.back() += v.second;
            ts[idx[v.first]].pt.erase(--ts[idx[v.first]].pt.end());
        }
        for (int i=0;i<2;i++) ts[idx[v.first]].pt.insert(tmp[i]);
        if (*(--ts[idx[v.first]].pt.end())<=dp[s]+ts[idx[v.first]].of) ans[s] += ans[v.first]+(dp[s]+ts[idx[v.first]].of-*(--ts[idx[v.first]].pt.end()));
        else{
            auto iter = --ts[idx[v.first]].pt.end();
            ll tval = ans[v.first], d=1;
            while(iter!=ts[idx[v.first]].pt.begin() && *iter>dp[s]+ts[idx[v.first]].of){
                --iter, --d;
                auto tmpiter = iter;
                ++tmpiter;
                tval += d*(*iter-*tmpiter);
            }
            if (*iter>dp[s]+ts[idx[v.first]].of){
                --d;
                tval += d*(dp[s]+ts[idx[v.first]].of-(*iter));
            }
            else{
                tval += d*(dp[s]+ts[idx[v.first]].of-(*iter));
            }
            ans[s] += tval;
        }
    }
    for (auto &v:adj[s]) if (idx[s]!=idx[v.first]) ts_merge(idx[s], idx[v.first]);
}


int main(){
    int n, m;
    scanf("%d %d", &n, &m);
    fill(idx, idx+n+m+1, -1);
    for (int i=2;i<=n+m;i++){
        int p, c;
        scanf("%d %d", &p, &c);
        adj[p].push_back({i, c});
    }
    dfs(1, 0);
    //for (int i=1;i<=n+m;i++) printf("%lld ", dp[i]);
    //printf("\n");
    printf("%lld\n", ans[1]);
    return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:114:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         scanf("%d %d", &p, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 5 ms 7372 KB Output is correct
5 Correct 5 ms 7372 KB Output is correct
6 Correct 5 ms 7372 KB Output is correct
7 Correct 5 ms 7368 KB Output is correct
8 Correct 5 ms 7372 KB Output is correct
9 Correct 6 ms 7372 KB Output is correct
10 Correct 5 ms 7372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 5 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 5 ms 7356 KB Output is correct
6 Correct 6 ms 7424 KB Output is correct
7 Correct 5 ms 7352 KB Output is correct
8 Correct 5 ms 7372 KB Output is correct
9 Correct 5 ms 7424 KB Output is correct
10 Correct 5 ms 7372 KB Output is correct
11 Correct 5 ms 7372 KB Output is correct
12 Correct 5 ms 7428 KB Output is correct
13 Correct 6 ms 7352 KB Output is correct
14 Correct 5 ms 7360 KB Output is correct
15 Correct 5 ms 7372 KB Output is correct
16 Correct 6 ms 7480 KB Output is correct
17 Correct 6 ms 7372 KB Output is correct
18 Correct 5 ms 7484 KB Output is correct
19 Correct 5 ms 7488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 5 ms 7372 KB Output is correct
5 Correct 5 ms 7372 KB Output is correct
6 Correct 5 ms 7372 KB Output is correct
7 Correct 5 ms 7368 KB Output is correct
8 Correct 5 ms 7372 KB Output is correct
9 Correct 6 ms 7372 KB Output is correct
10 Correct 5 ms 7372 KB Output is correct
11 Correct 4 ms 7372 KB Output is correct
12 Correct 4 ms 7372 KB Output is correct
13 Correct 5 ms 7372 KB Output is correct
14 Correct 4 ms 7372 KB Output is correct
15 Correct 5 ms 7356 KB Output is correct
16 Correct 6 ms 7424 KB Output is correct
17 Correct 5 ms 7352 KB Output is correct
18 Correct 5 ms 7372 KB Output is correct
19 Correct 5 ms 7424 KB Output is correct
20 Correct 5 ms 7372 KB Output is correct
21 Correct 5 ms 7372 KB Output is correct
22 Correct 5 ms 7428 KB Output is correct
23 Correct 6 ms 7352 KB Output is correct
24 Correct 5 ms 7360 KB Output is correct
25 Correct 5 ms 7372 KB Output is correct
26 Correct 6 ms 7480 KB Output is correct
27 Correct 6 ms 7372 KB Output is correct
28 Correct 5 ms 7484 KB Output is correct
29 Correct 5 ms 7488 KB Output is correct
30 Correct 5 ms 7372 KB Output is correct
31 Correct 6 ms 7632 KB Output is correct
32 Correct 7 ms 7756 KB Output is correct
33 Correct 8 ms 7884 KB Output is correct
34 Correct 9 ms 8268 KB Output is correct
35 Correct 10 ms 8268 KB Output is correct
36 Correct 11 ms 8392 KB Output is correct
37 Correct 15 ms 8652 KB Output is correct
38 Correct 13 ms 8728 KB Output is correct
39 Correct 13 ms 8740 KB Output is correct
40 Correct 9 ms 8652 KB Output is correct
41 Correct 9 ms 8648 KB Output is correct
42 Correct 8 ms 7756 KB Output is correct
43 Correct 12 ms 9156 KB Output is correct
44 Correct 13 ms 9032 KB Output is correct
45 Correct 12 ms 9016 KB Output is correct
46 Correct 15 ms 10020 KB Output is correct
47 Correct 15 ms 10044 KB Output is correct
48 Correct 14 ms 9776 KB Output is correct
49 Correct 15 ms 9876 KB Output is correct
50 Correct 13 ms 9404 KB Output is correct
51 Correct 16 ms 9352 KB Output is correct
52 Correct 13 ms 9668 KB Output is correct
53 Correct 18 ms 9624 KB Output is correct
54 Correct 14 ms 10040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 5 ms 7372 KB Output is correct
5 Correct 5 ms 7372 KB Output is correct
6 Correct 5 ms 7372 KB Output is correct
7 Correct 5 ms 7368 KB Output is correct
8 Correct 5 ms 7372 KB Output is correct
9 Correct 6 ms 7372 KB Output is correct
10 Correct 5 ms 7372 KB Output is correct
11 Correct 4 ms 7372 KB Output is correct
12 Correct 4 ms 7372 KB Output is correct
13 Correct 5 ms 7372 KB Output is correct
14 Correct 4 ms 7372 KB Output is correct
15 Correct 5 ms 7356 KB Output is correct
16 Correct 6 ms 7424 KB Output is correct
17 Correct 5 ms 7352 KB Output is correct
18 Correct 5 ms 7372 KB Output is correct
19 Correct 5 ms 7424 KB Output is correct
20 Correct 5 ms 7372 KB Output is correct
21 Correct 5 ms 7372 KB Output is correct
22 Correct 5 ms 7428 KB Output is correct
23 Correct 6 ms 7352 KB Output is correct
24 Correct 5 ms 7360 KB Output is correct
25 Correct 5 ms 7372 KB Output is correct
26 Correct 6 ms 7480 KB Output is correct
27 Correct 6 ms 7372 KB Output is correct
28 Correct 5 ms 7484 KB Output is correct
29 Correct 5 ms 7488 KB Output is correct
30 Correct 5 ms 7372 KB Output is correct
31 Correct 6 ms 7632 KB Output is correct
32 Correct 7 ms 7756 KB Output is correct
33 Correct 8 ms 7884 KB Output is correct
34 Correct 9 ms 8268 KB Output is correct
35 Correct 10 ms 8268 KB Output is correct
36 Correct 11 ms 8392 KB Output is correct
37 Correct 15 ms 8652 KB Output is correct
38 Correct 13 ms 8728 KB Output is correct
39 Correct 13 ms 8740 KB Output is correct
40 Correct 9 ms 8652 KB Output is correct
41 Correct 9 ms 8648 KB Output is correct
42 Correct 8 ms 7756 KB Output is correct
43 Correct 12 ms 9156 KB Output is correct
44 Correct 13 ms 9032 KB Output is correct
45 Correct 12 ms 9016 KB Output is correct
46 Correct 15 ms 10020 KB Output is correct
47 Correct 15 ms 10044 KB Output is correct
48 Correct 14 ms 9776 KB Output is correct
49 Correct 15 ms 9876 KB Output is correct
50 Correct 13 ms 9404 KB Output is correct
51 Correct 16 ms 9352 KB Output is correct
52 Correct 13 ms 9668 KB Output is correct
53 Correct 18 ms 9624 KB Output is correct
54 Correct 14 ms 10040 KB Output is correct
55 Correct 25 ms 10940 KB Output is correct
56 Correct 101 ms 21800 KB Output is correct
57 Correct 167 ms 32580 KB Output is correct
58 Correct 217 ms 40208 KB Output is correct
59 Correct 288 ms 50928 KB Output is correct
60 Correct 392 ms 62260 KB Output is correct
61 Correct 458 ms 69756 KB Output is correct
62 Correct 482 ms 77376 KB Output is correct
63 Correct 581 ms 92412 KB Output is correct
64 Correct 631 ms 94136 KB Output is correct
65 Correct 247 ms 88524 KB Output is correct
66 Correct 247 ms 88556 KB Output is correct
67 Correct 261 ms 27896 KB Output is correct
68 Correct 534 ms 114252 KB Output is correct
69 Correct 645 ms 111004 KB Output is correct
70 Correct 642 ms 110864 KB Output is correct
71 Correct 889 ms 194608 KB Output is correct
72 Correct 914 ms 194728 KB Output is correct
73 Correct 908 ms 170268 KB Output is correct
74 Correct 886 ms 170292 KB Output is correct
75 Correct 879 ms 168768 KB Output is correct
76 Correct 867 ms 169216 KB Output is correct
77 Correct 882 ms 164256 KB Output is correct
78 Correct 913 ms 158892 KB Output is correct
79 Correct 1058 ms 169492 KB Output is correct