Submission #25848

# Submission time Handle Problem Language Result Execution time Memory
25848 2017-06-24T11:49:07 Z gs14004 Fireworks (APIO16_fireworks) C++11
100 / 100
449 ms 85100 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <functional>
#include <numeric>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <map>
#include <set>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const lint inf = 1e16;
 
int n, m, sz[300005];
lint dep[300005], c[300005];
 
vector<int> gph[300005];
 
struct func{
    priority_queue<lint> pq;
    lint cost;
    int slope;
    void init(){
        cost = 0;
        slope = -1;
        pq.push(0);
        pq.push(0);
    }
    void upperize(int x){
        cost += c[x];
        while(!pq.empty() && slope + (int)pq.size() > 1){
            pq.pop();
        }
        vector<lint> v;
        while(!pq.empty() && slope + (int)pq.size() >= 0){
            v.push_back(pq.top());
            pq.pop();
        }
        while(!v.empty()){
            pq.push(v.back() + c[x]);
            v.pop_back();
        }
    }
}dp[300005];
 
bool cmp(int a, int b){
    return sz[a] > sz[b];
}
 
void dfs(int x){
    if(x > n){
        sz[x] = 1;
        return;
    }
    for(int i=0; i<gph[x].size(); i++){
        dep[gph[x][i]] = dep[x] + c[gph[x][i]];
        dfs(gph[x][i]);
        sz[x] += sz[gph[x][i]];
    }
    sort(gph[x].begin(), gph[x].end(), cmp);
}
 
int solve(int x){
    if(x > n){
        dp[x].init();
        return x;
    }
    int ret = solve(gph[x][0]);
    dp[ret].upperize(gph[x][0]);
    for(int i=1; i<gph[x].size(); i++){
        int t = solve(gph[x][i]);
        dp[t].upperize(gph[x][i]);
        dp[ret].cost += dp[t].cost;
        dp[ret].slope += dp[t].slope;
        while(!dp[t].pq.empty()){
            dp[ret].pq.push(dp[t].pq.top());
            dp[t].pq.pop();
        }
    }
    return ret;
}
 
int main(){
    cin >> n >> m;
    for(int i=2; i<=n+m; i++){
        int p;
        scanf("%d %lld",&p,&c[i]);
        gph[p].push_back(i);
    }
    dfs(1);
    func ret = dp[solve(1)];
    ret.upperize(0);
    lint ansp = ret.pq.top();
    lint ans = ret.cost + ansp * ret.slope;
    while(!ret.pq.empty()){
        ans += ansp - ret.pq.top();
        ret.pq.pop();
    }
    printf("%lld",ans);
}

Compilation message

fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:67:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<gph[x].size(); i++){
                   ^
fireworks.cpp: In function 'int solve(int)':
fireworks.cpp:82:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=1; i<gph[x].size(); i++){
                   ^
fireworks.cpp: In function 'int main()':
fireworks.cpp:99:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %lld",&p,&c[i]);
                                  ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 28976 KB Output is correct
2 Correct 0 ms 28976 KB Output is correct
3 Correct 3 ms 28976 KB Output is correct
4 Correct 3 ms 28976 KB Output is correct
5 Correct 6 ms 28976 KB Output is correct
6 Correct 6 ms 28976 KB Output is correct
7 Correct 6 ms 28976 KB Output is correct
8 Correct 3 ms 28976 KB Output is correct
9 Correct 6 ms 28976 KB Output is correct
10 Correct 3 ms 28976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 28976 KB Output is correct
2 Correct 3 ms 28976 KB Output is correct
3 Correct 3 ms 28976 KB Output is correct
4 Correct 3 ms 28976 KB Output is correct
5 Correct 3 ms 28976 KB Output is correct
6 Correct 6 ms 28976 KB Output is correct
7 Correct 3 ms 28976 KB Output is correct
8 Correct 3 ms 28976 KB Output is correct
9 Correct 6 ms 28976 KB Output is correct
10 Correct 6 ms 28976 KB Output is correct
11 Correct 3 ms 28976 KB Output is correct
12 Correct 6 ms 28976 KB Output is correct
13 Correct 3 ms 28976 KB Output is correct
14 Correct 9 ms 28976 KB Output is correct
15 Correct 6 ms 28976 KB Output is correct
16 Correct 6 ms 28976 KB Output is correct
17 Correct 3 ms 28976 KB Output is correct
18 Correct 3 ms 28976 KB Output is correct
19 Correct 3 ms 28976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 28976 KB Output is correct
2 Correct 0 ms 28976 KB Output is correct
3 Correct 3 ms 28976 KB Output is correct
4 Correct 3 ms 28976 KB Output is correct
5 Correct 6 ms 28976 KB Output is correct
6 Correct 6 ms 28976 KB Output is correct
7 Correct 6 ms 28976 KB Output is correct
8 Correct 3 ms 28976 KB Output is correct
9 Correct 6 ms 28976 KB Output is correct
10 Correct 3 ms 28976 KB Output is correct
11 Correct 6 ms 28976 KB Output is correct
12 Correct 3 ms 28976 KB Output is correct
13 Correct 3 ms 28976 KB Output is correct
14 Correct 3 ms 28976 KB Output is correct
15 Correct 3 ms 28976 KB Output is correct
16 Correct 6 ms 28976 KB Output is correct
17 Correct 3 ms 28976 KB Output is correct
18 Correct 3 ms 28976 KB Output is correct
19 Correct 6 ms 28976 KB Output is correct
20 Correct 6 ms 28976 KB Output is correct
21 Correct 3 ms 28976 KB Output is correct
22 Correct 6 ms 28976 KB Output is correct
23 Correct 3 ms 28976 KB Output is correct
24 Correct 9 ms 28976 KB Output is correct
25 Correct 6 ms 28976 KB Output is correct
26 Correct 6 ms 28976 KB Output is correct
27 Correct 3 ms 28976 KB Output is correct
28 Correct 3 ms 28976 KB Output is correct
29 Correct 3 ms 28976 KB Output is correct
30 Correct 9 ms 28976 KB Output is correct
31 Correct 6 ms 28976 KB Output is correct
32 Correct 9 ms 29108 KB Output is correct
33 Correct 3 ms 29108 KB Output is correct
34 Correct 9 ms 29108 KB Output is correct
35 Correct 6 ms 29108 KB Output is correct
36 Correct 9 ms 29240 KB Output is correct
37 Correct 9 ms 29240 KB Output is correct
38 Correct 9 ms 29252 KB Output is correct
39 Correct 13 ms 29244 KB Output is correct
40 Correct 9 ms 29768 KB Output is correct
41 Correct 6 ms 29764 KB Output is correct
42 Correct 6 ms 29108 KB Output is correct
43 Correct 9 ms 29512 KB Output is correct
44 Correct 9 ms 29388 KB Output is correct
45 Correct 3 ms 29392 KB Output is correct
46 Correct 13 ms 29400 KB Output is correct
47 Correct 6 ms 29408 KB Output is correct
48 Correct 9 ms 29380 KB Output is correct
49 Correct 13 ms 29420 KB Output is correct
50 Correct 9 ms 29268 KB Output is correct
51 Correct 6 ms 29268 KB Output is correct
52 Correct 6 ms 29252 KB Output is correct
53 Correct 6 ms 29400 KB Output is correct
54 Correct 3 ms 29380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 28976 KB Output is correct
2 Correct 0 ms 28976 KB Output is correct
3 Correct 3 ms 28976 KB Output is correct
4 Correct 3 ms 28976 KB Output is correct
5 Correct 6 ms 28976 KB Output is correct
6 Correct 6 ms 28976 KB Output is correct
7 Correct 6 ms 28976 KB Output is correct
8 Correct 3 ms 28976 KB Output is correct
9 Correct 6 ms 28976 KB Output is correct
10 Correct 3 ms 28976 KB Output is correct
11 Correct 6 ms 28976 KB Output is correct
12 Correct 3 ms 28976 KB Output is correct
13 Correct 3 ms 28976 KB Output is correct
14 Correct 3 ms 28976 KB Output is correct
15 Correct 3 ms 28976 KB Output is correct
16 Correct 6 ms 28976 KB Output is correct
17 Correct 3 ms 28976 KB Output is correct
18 Correct 3 ms 28976 KB Output is correct
19 Correct 6 ms 28976 KB Output is correct
20 Correct 6 ms 28976 KB Output is correct
21 Correct 3 ms 28976 KB Output is correct
22 Correct 6 ms 28976 KB Output is correct
23 Correct 3 ms 28976 KB Output is correct
24 Correct 9 ms 28976 KB Output is correct
25 Correct 6 ms 28976 KB Output is correct
26 Correct 6 ms 28976 KB Output is correct
27 Correct 3 ms 28976 KB Output is correct
28 Correct 3 ms 28976 KB Output is correct
29 Correct 3 ms 28976 KB Output is correct
30 Correct 9 ms 28976 KB Output is correct
31 Correct 6 ms 28976 KB Output is correct
32 Correct 9 ms 29108 KB Output is correct
33 Correct 3 ms 29108 KB Output is correct
34 Correct 9 ms 29108 KB Output is correct
35 Correct 6 ms 29108 KB Output is correct
36 Correct 9 ms 29240 KB Output is correct
37 Correct 9 ms 29240 KB Output is correct
38 Correct 9 ms 29252 KB Output is correct
39 Correct 13 ms 29244 KB Output is correct
40 Correct 9 ms 29768 KB Output is correct
41 Correct 6 ms 29764 KB Output is correct
42 Correct 6 ms 29108 KB Output is correct
43 Correct 9 ms 29512 KB Output is correct
44 Correct 9 ms 29388 KB Output is correct
45 Correct 3 ms 29392 KB Output is correct
46 Correct 13 ms 29400 KB Output is correct
47 Correct 6 ms 29408 KB Output is correct
48 Correct 9 ms 29380 KB Output is correct
49 Correct 13 ms 29420 KB Output is correct
50 Correct 9 ms 29268 KB Output is correct
51 Correct 6 ms 29268 KB Output is correct
52 Correct 6 ms 29252 KB Output is correct
53 Correct 6 ms 29400 KB Output is correct
54 Correct 3 ms 29380 KB Output is correct
55 Correct 19 ms 29684 KB Output is correct
56 Correct 63 ms 31496 KB Output is correct
57 Correct 116 ms 33216 KB Output is correct
58 Correct 153 ms 34624 KB Output is correct
59 Correct 196 ms 36280 KB Output is correct
60 Correct 189 ms 37872 KB Output is correct
61 Correct 349 ms 39676 KB Output is correct
62 Correct 373 ms 40808 KB Output is correct
63 Correct 363 ms 42948 KB Output is correct
64 Correct 449 ms 43360 KB Output is correct
65 Correct 176 ms 85100 KB Output is correct
66 Correct 189 ms 85100 KB Output is correct
67 Correct 246 ms 38348 KB Output is correct
68 Correct 313 ms 64888 KB Output is correct
69 Correct 296 ms 61032 KB Output is correct
70 Correct 346 ms 61036 KB Output is correct
71 Correct 383 ms 56488 KB Output is correct
72 Correct 386 ms 56332 KB Output is correct
73 Correct 316 ms 52616 KB Output is correct
74 Correct 336 ms 52772 KB Output is correct
75 Correct 306 ms 53328 KB Output is correct
76 Correct 296 ms 53424 KB Output is correct
77 Correct 326 ms 52516 KB Output is correct
78 Correct 333 ms 50832 KB Output is correct
79 Correct 319 ms 53280 KB Output is correct