Submission #20691

# Submission time Handle Problem Language Result Execution time Memory
20691 2017-02-13T14:02:27 Z Namnamseo Fireworks (APIO16_fireworks) C++14
100 / 100
1989 ms 82944 KB
#include <bits/stdc++.h>
using namespace std;
void read(int& x){ scanf("%d",&x); }
typedef long long ll;
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define pb push_back
#define all(x) (x).begin(),(x).end()

typedef pair<int,ll> pp;

int n;
vector<pp> edge[300010];

void in(){
    int m;

    read(n, m);
    n += m;
    for(int i=2; i<=n; ++i){
        int a;
        ll b;
        read(a,b);
        edge[a].pb({i, b});
    }
}

struct tree_result {
    map<ll,ll> dy_points;
    ll start_val, start_grad, end_grad;
    tree_result(){
        start_grad = 0;
        end_grad = 0;
        start_val = 0;
    }
    void applyDistance(ll d){
        if(dy_points.size() == 0u){
            dy_points[d] = 2;
            start_val = d;
            start_grad = -1;
            end_grad = 1;
            return;
        }
        ll cur_grad = end_grad, bef_grad = -1, change_pt=-1, bef_pt=-1;
        for(auto it = --dy_points.end();;){
            bef_grad = cur_grad;
            cur_grad -= it->second;
            bef_pt = change_pt;
            change_pt = it->first;
            if(cur_grad <= -1) break;
            it = --dy_points.erase(it);
        }
        dy_points.erase(--dy_points.end());
        start_val += d;
        if(bef_grad == 0){
            if(cur_grad != -1){
                dy_points[change_pt] = -1 - cur_grad;
            }
            dy_points[change_pt + d] = 1;
            dy_points[bef_pt    + d] = 1;
        } else {
            if(cur_grad != -1){
                dy_points[change_pt] = -1 - cur_grad;
            }
            dy_points[change_pt + d] = 2;
        }
        end_grad = 1;
    }
    tree_result operator+(tree_result& r){
        tree_result ret;
        ret.start_val = start_val + r.start_val;
        ret.start_grad = start_grad + r.start_grad;
        ret.end_grad = end_grad + r.end_grad;
        auto &a = dy_points, &b = r.dy_points;
        if(a.size() < b.size()) swap(a, b);
        swap(ret.dy_points, a);
        for(auto it=b.begin(); it!=b.end();){
            ret.dy_points[it->first] += it->second;
            it = b.erase(it);
        }
        return ret;
    }
};

int par[300010];

tree_result res[300010];

int sI[300010], top;
pp last[300010];

void dfs(int x){
    sI[top++]=x;
    while(top){
        x=sI[top-1];
        if(last[x].first){
            int y, d; tie(y, d) = last[x];
            res[y].applyDistance(d);
            if(res[x].dy_points.size() == 0u){
                res[x] = res[y];
            } else {
                res[x] = res[x] + res[y];
            }
            res[y].dy_points.clear();
            last[x] = {0, 0};
        }
        if(edge[x].size() == 0u){
            --top;
            continue;
        } else {
            int y, d; tie(y, d) = edge[x].back();
            edge[x].pop_back();
            if(y == par[x]) continue;
            par[y] = x;
            last[x] = {y, d};
            sI[top++] = y;
        }
    }
}

int main()
{
    in();
    dfs(1);
    auto res = ::res[1];
    ll val = res.start_val;
    ll grad = res.start_grad;
    ll min_val = 1LL << 62;
    ll last_pt = 0;
    for(auto tmp : res.dy_points){
        ll cur_pt, cur_dg;
        tie(cur_pt, cur_dg) = tmp;
        val += grad * (cur_pt - last_pt);
        min_val = min(min_val, val);
        last_pt = cur_pt;
        grad += cur_dg;
    }
    printf("%lld\n", min_val);
    return 0;
}

Compilation message

fireworks.cpp: In function 'void read(int&)':
fireworks.cpp:3:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(int& x){ scanf("%d",&x); }
                                  ^
fireworks.cpp: In function 'void read(ll&)':
fireworks.cpp:5:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(ll& x){ scanf("%lld",&x); }
                                   ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 37184 KB Output is correct
2 Correct 13 ms 37184 KB Output is correct
3 Correct 6 ms 37184 KB Output is correct
4 Correct 6 ms 37184 KB Output is correct
5 Correct 6 ms 37184 KB Output is correct
6 Correct 9 ms 37184 KB Output is correct
7 Correct 3 ms 37184 KB Output is correct
8 Correct 3 ms 37184 KB Output is correct
9 Correct 13 ms 37184 KB Output is correct
10 Correct 9 ms 37184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 37184 KB Output is correct
2 Correct 9 ms 37184 KB Output is correct
3 Correct 6 ms 37184 KB Output is correct
4 Correct 6 ms 37184 KB Output is correct
5 Correct 3 ms 37184 KB Output is correct
6 Correct 9 ms 37184 KB Output is correct
7 Correct 3 ms 37184 KB Output is correct
8 Correct 13 ms 37184 KB Output is correct
9 Correct 9 ms 37184 KB Output is correct
10 Correct 6 ms 37184 KB Output is correct
11 Correct 6 ms 37184 KB Output is correct
12 Correct 13 ms 37184 KB Output is correct
13 Correct 9 ms 37184 KB Output is correct
14 Correct 6 ms 37184 KB Output is correct
15 Correct 6 ms 37184 KB Output is correct
16 Correct 3 ms 37184 KB Output is correct
17 Correct 6 ms 37184 KB Output is correct
18 Correct 3 ms 37184 KB Output is correct
19 Correct 9 ms 37184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 37184 KB Output is correct
2 Correct 13 ms 37184 KB Output is correct
3 Correct 6 ms 37184 KB Output is correct
4 Correct 6 ms 37184 KB Output is correct
5 Correct 6 ms 37184 KB Output is correct
6 Correct 9 ms 37184 KB Output is correct
7 Correct 3 ms 37184 KB Output is correct
8 Correct 3 ms 37184 KB Output is correct
9 Correct 13 ms 37184 KB Output is correct
10 Correct 9 ms 37184 KB Output is correct
11 Correct 9 ms 37184 KB Output is correct
12 Correct 9 ms 37184 KB Output is correct
13 Correct 6 ms 37184 KB Output is correct
14 Correct 6 ms 37184 KB Output is correct
15 Correct 3 ms 37184 KB Output is correct
16 Correct 9 ms 37184 KB Output is correct
17 Correct 3 ms 37184 KB Output is correct
18 Correct 13 ms 37184 KB Output is correct
19 Correct 9 ms 37184 KB Output is correct
20 Correct 6 ms 37184 KB Output is correct
21 Correct 6 ms 37184 KB Output is correct
22 Correct 13 ms 37184 KB Output is correct
23 Correct 9 ms 37184 KB Output is correct
24 Correct 6 ms 37184 KB Output is correct
25 Correct 6 ms 37184 KB Output is correct
26 Correct 3 ms 37184 KB Output is correct
27 Correct 6 ms 37184 KB Output is correct
28 Correct 3 ms 37184 KB Output is correct
29 Correct 9 ms 37184 KB Output is correct
30 Correct 3 ms 37184 KB Output is correct
31 Correct 6 ms 37316 KB Output is correct
32 Correct 9 ms 37316 KB Output is correct
33 Correct 9 ms 37316 KB Output is correct
34 Correct 6 ms 37316 KB Output is correct
35 Correct 6 ms 37448 KB Output is correct
36 Correct 9 ms 37448 KB Output is correct
37 Correct 6 ms 37448 KB Output is correct
38 Correct 9 ms 37448 KB Output is correct
39 Correct 13 ms 37448 KB Output is correct
40 Correct 9 ms 37316 KB Output is correct
41 Correct 13 ms 37316 KB Output is correct
42 Correct 16 ms 37316 KB Output is correct
43 Correct 3 ms 37448 KB Output is correct
44 Correct 9 ms 37448 KB Output is correct
45 Correct 6 ms 37448 KB Output is correct
46 Correct 3 ms 37580 KB Output is correct
47 Correct 13 ms 37580 KB Output is correct
48 Correct 13 ms 37588 KB Output is correct
49 Correct 16 ms 37720 KB Output is correct
50 Correct 6 ms 37720 KB Output is correct
51 Correct 9 ms 37720 KB Output is correct
52 Correct 13 ms 37592 KB Output is correct
53 Correct 16 ms 37600 KB Output is correct
54 Correct 6 ms 37920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 37184 KB Output is correct
2 Correct 13 ms 37184 KB Output is correct
3 Correct 6 ms 37184 KB Output is correct
4 Correct 6 ms 37184 KB Output is correct
5 Correct 6 ms 37184 KB Output is correct
6 Correct 9 ms 37184 KB Output is correct
7 Correct 3 ms 37184 KB Output is correct
8 Correct 3 ms 37184 KB Output is correct
9 Correct 13 ms 37184 KB Output is correct
10 Correct 9 ms 37184 KB Output is correct
11 Correct 9 ms 37184 KB Output is correct
12 Correct 9 ms 37184 KB Output is correct
13 Correct 6 ms 37184 KB Output is correct
14 Correct 6 ms 37184 KB Output is correct
15 Correct 3 ms 37184 KB Output is correct
16 Correct 9 ms 37184 KB Output is correct
17 Correct 3 ms 37184 KB Output is correct
18 Correct 13 ms 37184 KB Output is correct
19 Correct 9 ms 37184 KB Output is correct
20 Correct 6 ms 37184 KB Output is correct
21 Correct 6 ms 37184 KB Output is correct
22 Correct 13 ms 37184 KB Output is correct
23 Correct 9 ms 37184 KB Output is correct
24 Correct 6 ms 37184 KB Output is correct
25 Correct 6 ms 37184 KB Output is correct
26 Correct 3 ms 37184 KB Output is correct
27 Correct 6 ms 37184 KB Output is correct
28 Correct 3 ms 37184 KB Output is correct
29 Correct 9 ms 37184 KB Output is correct
30 Correct 3 ms 37184 KB Output is correct
31 Correct 6 ms 37316 KB Output is correct
32 Correct 9 ms 37316 KB Output is correct
33 Correct 9 ms 37316 KB Output is correct
34 Correct 6 ms 37316 KB Output is correct
35 Correct 6 ms 37448 KB Output is correct
36 Correct 9 ms 37448 KB Output is correct
37 Correct 6 ms 37448 KB Output is correct
38 Correct 9 ms 37448 KB Output is correct
39 Correct 13 ms 37448 KB Output is correct
40 Correct 9 ms 37316 KB Output is correct
41 Correct 13 ms 37316 KB Output is correct
42 Correct 16 ms 37316 KB Output is correct
43 Correct 3 ms 37448 KB Output is correct
44 Correct 9 ms 37448 KB Output is correct
45 Correct 6 ms 37448 KB Output is correct
46 Correct 3 ms 37580 KB Output is correct
47 Correct 13 ms 37580 KB Output is correct
48 Correct 13 ms 37588 KB Output is correct
49 Correct 16 ms 37720 KB Output is correct
50 Correct 6 ms 37720 KB Output is correct
51 Correct 9 ms 37720 KB Output is correct
52 Correct 13 ms 37592 KB Output is correct
53 Correct 16 ms 37600 KB Output is correct
54 Correct 6 ms 37920 KB Output is correct
55 Correct 23 ms 37976 KB Output is correct
56 Correct 156 ms 39956 KB Output is correct
57 Correct 316 ms 41936 KB Output is correct
58 Correct 423 ms 43256 KB Output is correct
59 Correct 889 ms 45236 KB Output is correct
60 Correct 1079 ms 47216 KB Output is correct
61 Correct 1493 ms 48536 KB Output is correct
62 Correct 1436 ms 49856 KB Output is correct
63 Correct 1276 ms 52232 KB Output is correct
64 Correct 1989 ms 53024 KB Output is correct
65 Correct 149 ms 46556 KB Output is correct
66 Correct 163 ms 46556 KB Output is correct
67 Correct 253 ms 46688 KB Output is correct
68 Correct 316 ms 53552 KB Output is correct
69 Correct 389 ms 54608 KB Output is correct
70 Correct 386 ms 54608 KB Output is correct
71 Correct 389 ms 62792 KB Output is correct
72 Correct 423 ms 62792 KB Output is correct
73 Correct 379 ms 62220 KB Output is correct
74 Correct 403 ms 62372 KB Output is correct
75 Correct 376 ms 62776 KB Output is correct
76 Correct 393 ms 62988 KB Output is correct
77 Correct 1049 ms 62476 KB Output is correct
78 Correct 489 ms 62276 KB Output is correct
79 Correct 563 ms 82944 KB Output is correct