Submission #62737

# Submission time Handle Problem Language Result Execution time Memory
62737 2018-07-29T23:53:04 Z Benq Fireworks (APIO16_fireworks) C++11
0 / 100
20 ms 14456 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 300001;

pl operator+(const pl& l, const pl& r) {
    return {l.f+r.f,l.s+r.s};
}

struct dat {
    pl st = {0,0};
    priority_queue<ll> p;
};

void comb(dat& a, dat& b) {
    if (sz(a.p) < sz(b.p)) swap(a.p,b.p);
    a.st = a.st+b.st;
    while (sz(b.p)) a.p.push(b.p.top()), b.p.pop();
}

int N,M,P[MX],C[MX];
dat tmp[MX];

void finish() {
    vl z;
    while (sz(tmp[1].p)) {
        z.pb(tmp[1].p.top());
        tmp[1].p.pop();
    }
    z.pb(0); reverse(all(z)); z.pop_back();
    ll cans = tmp[1].st.f;
    F0R(i,sz(z)-1) cans += (z[i+1]-z[i])*(tmp[1].st.s+i);
    cout << cans;
}

void prin(priority_queue<ll> p) {
    while (sz(p)) {
        cout << p.top() << " ";
        p.pop();
    }
    cout << "\n----\n\n";
}

void process(int x) {
    if (x > N) {
        tmp[x].st = {C[x],-1};
        tmp[x].p.push(C[x]); tmp[x].p.push(C[x]);
    } else {
        while (sz(tmp[x].p) > -tmp[x].st.s+1) tmp[x].p.pop(); 
        tmp[x].st.f += C[x];
        ll z1 = tmp[x].p.top(); tmp[x].p.pop(); 
        ll z0 = tmp[x].p.top(); tmp[x].p.pop();
        tmp[x].p.push(z0+C[x]), tmp[x].p.push(z1+C[x]);
    }
    /*cout << x << " " << tmp[x].st.f << " " << tmp[x].st.s << "\n";
    prin(tmp[x].p);*/
    comb(tmp[P[x]],tmp[x]);
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> N >> M;
    FOR(i,2,N+M+1) cin >> P[i] >> C[i];
    FORd(i,2,N+M+1) process(i);
    finish();
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# Verdict Execution time Memory Grader output
1 Correct 18 ms 14456 KB Output is correct
2 Incorrect 20 ms 14456 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 14456 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 14456 KB Output is correct
2 Incorrect 20 ms 14456 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 14456 KB Output is correct
2 Incorrect 20 ms 14456 KB Output isn't correct
3 Halted 0 ms 0 KB -