Submission #1113088

# Submission time Handle Problem Language Result Execution time Memory
1113088 2024-11-15T16:55:34 Z guagua0407 Fireworks (APIO16_fireworks) C++17
100 / 100
203 ms 89824 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=6e5+5;
vector<int> adj[mxn];
priority_queue<ll> pq[mxn];
ll c[mxn];
ll val[mxn];
int n,m;

void dfs(int v,int p=0){
    if(v>n){
        pq[v].push(c[v]);
        pq[v].push(c[v]);
        return;
    }
    ll sum=0;
    for(auto u:adj[v]){
        dfs(u,v);
        val[v]+=val[u];
        sum+=pq[u].top();
        if(pq[v].size()<pq[u].size()) swap(pq[v],pq[u]);
        while(!pq[u].empty()){
            pq[v].push(pq[u].top());
            pq[u].pop();
        }
    }
    int sz=(int)adj[v].size();
    ll mx=pq[v].top();
    val[v]+=mx*sz-sum;
    for(int i=sz-1;i>=1;i--){
        ll r=pq[v].top();
        pq[v].pop();
        val[v]-=1ll*(r-pq[v].top())*i;
    }
    ll a=pq[v].top();
    pq[v].pop();
    ll b=pq[v].top();
    pq[v].pop();
    pq[v].push(a+c[v]);
    pq[v].push(b+c[v]);
}

signed main() {_
    cin>>n>>m;
    for(int i=2;i<=n+m;i++){
        int p;
        cin>>p>>c[i];
        adj[p].push_back(i);
    }
    dfs(1);
    cout<<val[1]<<'\n';
    return 0;
}
//maybe its multiset not set
//yeeorz
//diaoborz

Compilation message

fireworks.cpp: In function 'void setIO(std::string)':
fireworks.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fireworks.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 33104 KB Output is correct
2 Correct 23 ms 33340 KB Output is correct
3 Correct 24 ms 33104 KB Output is correct
4 Correct 24 ms 33360 KB Output is correct
5 Correct 26 ms 33116 KB Output is correct
6 Correct 25 ms 33360 KB Output is correct
7 Correct 25 ms 33104 KB Output is correct
8 Correct 31 ms 33360 KB Output is correct
9 Correct 24 ms 33104 KB Output is correct
10 Correct 24 ms 33376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 33324 KB Output is correct
2 Correct 23 ms 33332 KB Output is correct
3 Correct 24 ms 33248 KB Output is correct
4 Correct 25 ms 33360 KB Output is correct
5 Correct 25 ms 33360 KB Output is correct
6 Correct 26 ms 33104 KB Output is correct
7 Correct 25 ms 33344 KB Output is correct
8 Correct 24 ms 33360 KB Output is correct
9 Correct 25 ms 33328 KB Output is correct
10 Correct 25 ms 33096 KB Output is correct
11 Correct 23 ms 33360 KB Output is correct
12 Correct 27 ms 33364 KB Output is correct
13 Correct 24 ms 33360 KB Output is correct
14 Correct 25 ms 33152 KB Output is correct
15 Correct 27 ms 33352 KB Output is correct
16 Correct 24 ms 33260 KB Output is correct
17 Correct 24 ms 33372 KB Output is correct
18 Correct 22 ms 33360 KB Output is correct
19 Correct 22 ms 33372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 33104 KB Output is correct
2 Correct 23 ms 33340 KB Output is correct
3 Correct 24 ms 33104 KB Output is correct
4 Correct 24 ms 33360 KB Output is correct
5 Correct 26 ms 33116 KB Output is correct
6 Correct 25 ms 33360 KB Output is correct
7 Correct 25 ms 33104 KB Output is correct
8 Correct 31 ms 33360 KB Output is correct
9 Correct 24 ms 33104 KB Output is correct
10 Correct 24 ms 33376 KB Output is correct
11 Correct 25 ms 33324 KB Output is correct
12 Correct 23 ms 33332 KB Output is correct
13 Correct 24 ms 33248 KB Output is correct
14 Correct 25 ms 33360 KB Output is correct
15 Correct 25 ms 33360 KB Output is correct
16 Correct 26 ms 33104 KB Output is correct
17 Correct 25 ms 33344 KB Output is correct
18 Correct 24 ms 33360 KB Output is correct
19 Correct 25 ms 33328 KB Output is correct
20 Correct 25 ms 33096 KB Output is correct
21 Correct 23 ms 33360 KB Output is correct
22 Correct 27 ms 33364 KB Output is correct
23 Correct 24 ms 33360 KB Output is correct
24 Correct 25 ms 33152 KB Output is correct
25 Correct 27 ms 33352 KB Output is correct
26 Correct 24 ms 33260 KB Output is correct
27 Correct 24 ms 33372 KB Output is correct
28 Correct 22 ms 33360 KB Output is correct
29 Correct 22 ms 33372 KB Output is correct
30 Correct 22 ms 33360 KB Output is correct
31 Correct 22 ms 33352 KB Output is correct
32 Correct 22 ms 33360 KB Output is correct
33 Correct 23 ms 33480 KB Output is correct
34 Correct 27 ms 33372 KB Output is correct
35 Correct 23 ms 33616 KB Output is correct
36 Correct 24 ms 33616 KB Output is correct
37 Correct 23 ms 33608 KB Output is correct
38 Correct 23 ms 33608 KB Output is correct
39 Correct 24 ms 33608 KB Output is correct
40 Correct 24 ms 34128 KB Output is correct
41 Correct 23 ms 34128 KB Output is correct
42 Correct 23 ms 33572 KB Output is correct
43 Correct 23 ms 33872 KB Output is correct
44 Correct 24 ms 34040 KB Output is correct
45 Correct 24 ms 34040 KB Output is correct
46 Correct 23 ms 33616 KB Output is correct
47 Correct 23 ms 33628 KB Output is correct
48 Correct 23 ms 33616 KB Output is correct
49 Correct 23 ms 33708 KB Output is correct
50 Correct 23 ms 33420 KB Output is correct
51 Correct 27 ms 33536 KB Output is correct
52 Correct 23 ms 33608 KB Output is correct
53 Correct 23 ms 33608 KB Output is correct
54 Correct 31 ms 33628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 33104 KB Output is correct
2 Correct 23 ms 33340 KB Output is correct
3 Correct 24 ms 33104 KB Output is correct
4 Correct 24 ms 33360 KB Output is correct
5 Correct 26 ms 33116 KB Output is correct
6 Correct 25 ms 33360 KB Output is correct
7 Correct 25 ms 33104 KB Output is correct
8 Correct 31 ms 33360 KB Output is correct
9 Correct 24 ms 33104 KB Output is correct
10 Correct 24 ms 33376 KB Output is correct
11 Correct 25 ms 33324 KB Output is correct
12 Correct 23 ms 33332 KB Output is correct
13 Correct 24 ms 33248 KB Output is correct
14 Correct 25 ms 33360 KB Output is correct
15 Correct 25 ms 33360 KB Output is correct
16 Correct 26 ms 33104 KB Output is correct
17 Correct 25 ms 33344 KB Output is correct
18 Correct 24 ms 33360 KB Output is correct
19 Correct 25 ms 33328 KB Output is correct
20 Correct 25 ms 33096 KB Output is correct
21 Correct 23 ms 33360 KB Output is correct
22 Correct 27 ms 33364 KB Output is correct
23 Correct 24 ms 33360 KB Output is correct
24 Correct 25 ms 33152 KB Output is correct
25 Correct 27 ms 33352 KB Output is correct
26 Correct 24 ms 33260 KB Output is correct
27 Correct 24 ms 33372 KB Output is correct
28 Correct 22 ms 33360 KB Output is correct
29 Correct 22 ms 33372 KB Output is correct
30 Correct 22 ms 33360 KB Output is correct
31 Correct 22 ms 33352 KB Output is correct
32 Correct 22 ms 33360 KB Output is correct
33 Correct 23 ms 33480 KB Output is correct
34 Correct 27 ms 33372 KB Output is correct
35 Correct 23 ms 33616 KB Output is correct
36 Correct 24 ms 33616 KB Output is correct
37 Correct 23 ms 33608 KB Output is correct
38 Correct 23 ms 33608 KB Output is correct
39 Correct 24 ms 33608 KB Output is correct
40 Correct 24 ms 34128 KB Output is correct
41 Correct 23 ms 34128 KB Output is correct
42 Correct 23 ms 33572 KB Output is correct
43 Correct 23 ms 33872 KB Output is correct
44 Correct 24 ms 34040 KB Output is correct
45 Correct 24 ms 34040 KB Output is correct
46 Correct 23 ms 33616 KB Output is correct
47 Correct 23 ms 33628 KB Output is correct
48 Correct 23 ms 33616 KB Output is correct
49 Correct 23 ms 33708 KB Output is correct
50 Correct 23 ms 33420 KB Output is correct
51 Correct 27 ms 33536 KB Output is correct
52 Correct 23 ms 33608 KB Output is correct
53 Correct 23 ms 33608 KB Output is correct
54 Correct 31 ms 33628 KB Output is correct
55 Correct 28 ms 34324 KB Output is correct
56 Correct 44 ms 37068 KB Output is correct
57 Correct 59 ms 39560 KB Output is correct
58 Correct 75 ms 41332 KB Output is correct
59 Correct 91 ms 44116 KB Output is correct
60 Correct 127 ms 46784 KB Output is correct
61 Correct 137 ms 49084 KB Output is correct
62 Correct 153 ms 50624 KB Output is correct
63 Correct 198 ms 53948 KB Output is correct
64 Correct 203 ms 54972 KB Output is correct
65 Correct 121 ms 89824 KB Output is correct
66 Correct 122 ms 89672 KB Output is correct
67 Correct 109 ms 52296 KB Output is correct
68 Correct 150 ms 70976 KB Output is correct
69 Correct 191 ms 68292 KB Output is correct
70 Correct 174 ms 68284 KB Output is correct
71 Correct 156 ms 63936 KB Output is correct
72 Correct 169 ms 64532 KB Output is correct
73 Correct 157 ms 60344 KB Output is correct
74 Correct 140 ms 60596 KB Output is correct
75 Correct 148 ms 59244 KB Output is correct
76 Correct 149 ms 59196 KB Output is correct
77 Correct 150 ms 58808 KB Output is correct
78 Correct 147 ms 56636 KB Output is correct
79 Correct 135 ms 57936 KB Output is correct