Submission #419522

# Submission time Handle Problem Language Result Execution time Memory
419522 2021-06-07T08:26:29 Z ansol4328 Fireworks (APIO16_fireworks) C++14
100 / 100
325 ms 72500 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;

int N, M, C[300005];
vector<int> lst[300005];
P par[300005];
priority_queue<ll,vector<ll>,greater<ll>> inc[300005];
priority_queue<ll> Dec[300005];
ll minv[300005];

void combine(int v, int v1)
{
    ll val=par[v1].se;
    if(!Dec[v].empty()){
        val+=Dec[v].top(); Dec[v].pop();
    }
    Dec[v].push(val); val=par[v1].se;
    if(!inc[v].empty()) val+=inc[v].top();
    while(!inc[v].empty()) inc[v].pop();
    inc[v].push(val);
}

void move(int cur, int v)
{
    while(!inc[v].empty()){
        inc[cur].push(inc[v].top());
        inc[v].pop();
    }
    while(!Dec[v].empty()){
        Dec[cur].push(Dec[v].top());
        Dec[v].pop();
    }
    while(!Dec[cur].empty() && !inc[cur].empty() && Dec[cur].top()>inc[cur].top()){
        ll d=Dec[cur].top();
        ll i=inc[cur].top();
        Dec[cur].pop(); inc[cur].pop();
        Dec[cur].push(i); inc[cur].push(d);
    }
}

void add(int cur, int v)
{
    vector<ll> tmp;
    if(inc[v].top()<Dec[cur].top()){
        ll mv=minv[v]+minv[cur], x=Dec[cur].top();
        //calculate mv by pq
        priority_queue<ll,vector<ll>,greater<ll>> pq(inc[v]);
        while(!pq.empty() && pq.top()<x){
            mv+=x-pq.top(); pq.pop();
        }
        move(cur,v);
        if(x>inc[cur].top()){
            while(!inc[cur].empty() && inc[cur].top()<x){
                tmp.pb(inc[cur].top());
                mv-=x-inc[cur].top();
                inc[cur].pop();
            }
            for(ll x : tmp) inc[cur].push(x);
        }
        minv[cur]=mv;
    }
    else if(inc[cur].top()<Dec[v].top()){
        ll mv=minv[v]+minv[cur], x=inc[cur].top();
        priority_queue<ll> pq(Dec[v]);
        while(!pq.empty() && pq.top()>x){
            mv+=pq.top()-x; pq.pop();
        }
        move(cur,v);
        if(x<Dec[cur].top()){
            while(!Dec[cur].empty() && Dec[cur].top()>x){
                tmp.pb(Dec[cur].top());
                mv-=Dec[cur].top()-x;
                Dec[cur].pop();
            }
            for(ll x : tmp) Dec[cur].push(x);
        }
        minv[cur]=mv;
    }
    else{
        move(cur,v); minv[cur]+=minv[v];
    }
}

int main()
{
    scanf("%d %d",&N,&M);
    for(int i=2 ; i<=N+M ; i++){
        scanf("%lld %lld",&par[i].fi,&par[i].se);
        lst[par[i].fi].pb(i);
        if(i>N) C[i]=i;
    }
    for(int i=N ; i>=1 ; i--){
        int cur=lst[i][0], v;
        combine(C[cur],cur); cur=C[cur];
        for(int j=1 ; j<lst[i].size() ; j++){
            v=lst[i][j];
            combine(C[v],v); v=C[v];
            int sz1=inc[cur].size()+Dec[cur].size();
            int sz2=inc[v].size()+Dec[v].size();
            if(sz1<sz2) swap(cur,v);
            add(cur,v);
        }
        C[i]=cur;
    }
    printf("%lld",minv[C[1]]);
    return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:101:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |         for(int j=1 ; j<lst[i].size() ; j++){
      |                       ~^~~~~~~~~~~~~~
fireworks.cpp:92:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
fireworks.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |         scanf("%lld %lld",&par[i].fi,&par[i].se);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 26028 KB Output is correct
2 Correct 16 ms 26048 KB Output is correct
3 Correct 18 ms 26060 KB Output is correct
4 Correct 18 ms 26140 KB Output is correct
5 Correct 18 ms 26136 KB Output is correct
6 Correct 18 ms 26044 KB Output is correct
7 Correct 18 ms 26128 KB Output is correct
8 Correct 18 ms 26060 KB Output is correct
9 Correct 20 ms 26108 KB Output is correct
10 Correct 18 ms 26084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 26060 KB Output is correct
2 Correct 17 ms 26140 KB Output is correct
3 Correct 20 ms 26084 KB Output is correct
4 Correct 22 ms 26060 KB Output is correct
5 Correct 18 ms 26136 KB Output is correct
6 Correct 17 ms 26048 KB Output is correct
7 Correct 18 ms 26100 KB Output is correct
8 Correct 18 ms 26140 KB Output is correct
9 Correct 17 ms 26140 KB Output is correct
10 Correct 18 ms 26200 KB Output is correct
11 Correct 18 ms 26140 KB Output is correct
12 Correct 17 ms 26164 KB Output is correct
13 Correct 17 ms 26148 KB Output is correct
14 Correct 18 ms 26144 KB Output is correct
15 Correct 18 ms 26060 KB Output is correct
16 Correct 18 ms 26212 KB Output is correct
17 Correct 17 ms 26060 KB Output is correct
18 Correct 17 ms 26096 KB Output is correct
19 Correct 17 ms 26132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 26028 KB Output is correct
2 Correct 16 ms 26048 KB Output is correct
3 Correct 18 ms 26060 KB Output is correct
4 Correct 18 ms 26140 KB Output is correct
5 Correct 18 ms 26136 KB Output is correct
6 Correct 18 ms 26044 KB Output is correct
7 Correct 18 ms 26128 KB Output is correct
8 Correct 18 ms 26060 KB Output is correct
9 Correct 20 ms 26108 KB Output is correct
10 Correct 18 ms 26084 KB Output is correct
11 Correct 18 ms 26060 KB Output is correct
12 Correct 17 ms 26140 KB Output is correct
13 Correct 20 ms 26084 KB Output is correct
14 Correct 22 ms 26060 KB Output is correct
15 Correct 18 ms 26136 KB Output is correct
16 Correct 17 ms 26048 KB Output is correct
17 Correct 18 ms 26100 KB Output is correct
18 Correct 18 ms 26140 KB Output is correct
19 Correct 17 ms 26140 KB Output is correct
20 Correct 18 ms 26200 KB Output is correct
21 Correct 18 ms 26140 KB Output is correct
22 Correct 17 ms 26164 KB Output is correct
23 Correct 17 ms 26148 KB Output is correct
24 Correct 18 ms 26144 KB Output is correct
25 Correct 18 ms 26060 KB Output is correct
26 Correct 18 ms 26212 KB Output is correct
27 Correct 17 ms 26060 KB Output is correct
28 Correct 17 ms 26096 KB Output is correct
29 Correct 17 ms 26132 KB Output is correct
30 Correct 17 ms 26148 KB Output is correct
31 Correct 20 ms 26188 KB Output is correct
32 Correct 18 ms 26264 KB Output is correct
33 Correct 18 ms 26316 KB Output is correct
34 Correct 22 ms 26316 KB Output is correct
35 Correct 19 ms 26408 KB Output is correct
36 Correct 23 ms 26444 KB Output is correct
37 Correct 20 ms 26572 KB Output is correct
38 Correct 21 ms 26620 KB Output is correct
39 Correct 21 ms 26532 KB Output is correct
40 Correct 19 ms 26396 KB Output is correct
41 Correct 20 ms 26472 KB Output is correct
42 Correct 19 ms 26400 KB Output is correct
43 Correct 20 ms 26572 KB Output is correct
44 Correct 20 ms 26572 KB Output is correct
45 Correct 21 ms 26576 KB Output is correct
46 Correct 25 ms 26852 KB Output is correct
47 Correct 23 ms 26792 KB Output is correct
48 Correct 21 ms 26712 KB Output is correct
49 Correct 22 ms 26664 KB Output is correct
50 Correct 21 ms 26572 KB Output is correct
51 Correct 21 ms 26540 KB Output is correct
52 Correct 21 ms 26700 KB Output is correct
53 Correct 23 ms 26720 KB Output is correct
54 Correct 21 ms 26620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 26028 KB Output is correct
2 Correct 16 ms 26048 KB Output is correct
3 Correct 18 ms 26060 KB Output is correct
4 Correct 18 ms 26140 KB Output is correct
5 Correct 18 ms 26136 KB Output is correct
6 Correct 18 ms 26044 KB Output is correct
7 Correct 18 ms 26128 KB Output is correct
8 Correct 18 ms 26060 KB Output is correct
9 Correct 20 ms 26108 KB Output is correct
10 Correct 18 ms 26084 KB Output is correct
11 Correct 18 ms 26060 KB Output is correct
12 Correct 17 ms 26140 KB Output is correct
13 Correct 20 ms 26084 KB Output is correct
14 Correct 22 ms 26060 KB Output is correct
15 Correct 18 ms 26136 KB Output is correct
16 Correct 17 ms 26048 KB Output is correct
17 Correct 18 ms 26100 KB Output is correct
18 Correct 18 ms 26140 KB Output is correct
19 Correct 17 ms 26140 KB Output is correct
20 Correct 18 ms 26200 KB Output is correct
21 Correct 18 ms 26140 KB Output is correct
22 Correct 17 ms 26164 KB Output is correct
23 Correct 17 ms 26148 KB Output is correct
24 Correct 18 ms 26144 KB Output is correct
25 Correct 18 ms 26060 KB Output is correct
26 Correct 18 ms 26212 KB Output is correct
27 Correct 17 ms 26060 KB Output is correct
28 Correct 17 ms 26096 KB Output is correct
29 Correct 17 ms 26132 KB Output is correct
30 Correct 17 ms 26148 KB Output is correct
31 Correct 20 ms 26188 KB Output is correct
32 Correct 18 ms 26264 KB Output is correct
33 Correct 18 ms 26316 KB Output is correct
34 Correct 22 ms 26316 KB Output is correct
35 Correct 19 ms 26408 KB Output is correct
36 Correct 23 ms 26444 KB Output is correct
37 Correct 20 ms 26572 KB Output is correct
38 Correct 21 ms 26620 KB Output is correct
39 Correct 21 ms 26532 KB Output is correct
40 Correct 19 ms 26396 KB Output is correct
41 Correct 20 ms 26472 KB Output is correct
42 Correct 19 ms 26400 KB Output is correct
43 Correct 20 ms 26572 KB Output is correct
44 Correct 20 ms 26572 KB Output is correct
45 Correct 21 ms 26576 KB Output is correct
46 Correct 25 ms 26852 KB Output is correct
47 Correct 23 ms 26792 KB Output is correct
48 Correct 21 ms 26712 KB Output is correct
49 Correct 22 ms 26664 KB Output is correct
50 Correct 21 ms 26572 KB Output is correct
51 Correct 21 ms 26540 KB Output is correct
52 Correct 21 ms 26700 KB Output is correct
53 Correct 23 ms 26720 KB Output is correct
54 Correct 21 ms 26620 KB Output is correct
55 Correct 28 ms 27304 KB Output is correct
56 Correct 52 ms 31024 KB Output is correct
57 Correct 95 ms 34360 KB Output is correct
58 Correct 113 ms 36740 KB Output is correct
59 Correct 169 ms 40048 KB Output is correct
60 Correct 178 ms 43512 KB Output is correct
61 Correct 221 ms 46264 KB Output is correct
62 Correct 226 ms 48212 KB Output is correct
63 Correct 310 ms 52660 KB Output is correct
64 Correct 325 ms 53812 KB Output is correct
65 Correct 130 ms 46184 KB Output is correct
66 Correct 157 ms 46188 KB Output is correct
67 Correct 129 ms 46276 KB Output is correct
68 Correct 187 ms 51888 KB Output is correct
69 Correct 239 ms 52480 KB Output is correct
70 Correct 282 ms 52352 KB Output is correct
71 Correct 291 ms 72500 KB Output is correct
72 Correct 288 ms 72372 KB Output is correct
73 Correct 280 ms 68424 KB Output is correct
74 Correct 286 ms 68708 KB Output is correct
75 Correct 292 ms 66352 KB Output is correct
76 Correct 288 ms 66360 KB Output is correct
77 Correct 291 ms 64128 KB Output is correct
78 Correct 303 ms 63220 KB Output is correct
79 Correct 265 ms 60152 KB Output is correct