Submission #943859

# Submission time Handle Problem Language Result Execution time Memory
943859 2024-03-12T02:49:31 Z Boycl07 Fireworks (APIO16_fireworks) C++17
100 / 100
183 ms 101796 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 1; (i) <= (n); ++i)
#define forn(i, l, r) for(int i = (l); i <= (r); ++i)
#define ford(i, r, l) for(int i = (r); i >= (l); --i)
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define FORD(i, n) for(int i = ((n) - 1); i >= 0; --i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task "firework"
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)

template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }

inline int readInt()       {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();int n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline ll readLong()       {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();ll  n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline string readString() {char c;while(c=getchar(),c==' '||c=='\n'||c=='\t');string s({c});while(c=getchar(),c!=EOF&&c!=' '&&c!='\n'&&c!='\t')s+=c;return s;}


const int N =  6e5 + 10;
const int M = 1e3 + 3;
const int N1 = 2e3 + 10;
const int K = 1e2 + 1;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LINF = 1e17 + 2;
const int block_size = 500;
const int LOG = 40;
const int offset = N;
const int LIM = 11 ;
const int AL = 26;

int n, m;

priority_queue<ll> q[N];
ll a[N], b[N], c[N];
vector<int> g[N];

void dfs(int u)
{
    if(u > n)
    {
        a[u] = 1;
        b[u] = -c[u];
        q[u].push(c[u]);
        q[u].push(c[u]);
    }
    else
    {
        for(int v : g[u])
        {
            dfs(v);
            if(sz(q[u]) < sz(q[v])) swap(q[u], q[v]);
            a[u] += a[v]; b[u] += b[v];

            while(sz(q[v]))
            {
                q[u].push(q[v].top());
                q[v].pop();
            }
        }
        while(a[u] > 1)
        {
            --a[u];
            b[u] += q[u].top();
            q[u].pop();
        }
        if(u != 1)
        {
            ll slope_1 = q[u].top(); q[u].pop();
            ll slope_0 = q[u].top(); q[u].pop();
            q[u].push(slope_1 + c[u]);
            q[u].push(slope_0 + c[u]);
            b[u] -= c[u];
        }
        else
        {
            --a[u];
            b[u] += q[u].top();
            q[u].pop();
        }
    }
}

void solve()
{
    cin >> n >> m;
    forn(i, 2, n + m)
    {
        int x;
        cin >> x >> c[i];
        g[x].pb(i);
    }

    dfs(1);
    cout << b[1] << endl;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int TC = 1;

    if(fopen(task".inp", "r"))
    {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }


    while(TC--)
    {
        solve();
        cout << endl;
    }

    return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fireworks.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39156 KB Output is correct
2 Correct 8 ms 39004 KB Output is correct
3 Correct 8 ms 39004 KB Output is correct
4 Correct 9 ms 39004 KB Output is correct
5 Correct 8 ms 39004 KB Output is correct
6 Correct 8 ms 39004 KB Output is correct
7 Correct 9 ms 39000 KB Output is correct
8 Correct 9 ms 39000 KB Output is correct
9 Correct 14 ms 39004 KB Output is correct
10 Correct 9 ms 39004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39004 KB Output is correct
2 Correct 9 ms 39004 KB Output is correct
3 Correct 9 ms 39004 KB Output is correct
4 Correct 10 ms 39004 KB Output is correct
5 Correct 10 ms 39004 KB Output is correct
6 Correct 9 ms 39004 KB Output is correct
7 Correct 10 ms 39004 KB Output is correct
8 Correct 8 ms 39004 KB Output is correct
9 Correct 9 ms 39004 KB Output is correct
10 Correct 8 ms 39004 KB Output is correct
11 Correct 8 ms 39004 KB Output is correct
12 Correct 8 ms 39004 KB Output is correct
13 Correct 8 ms 39004 KB Output is correct
14 Correct 8 ms 39260 KB Output is correct
15 Correct 8 ms 39004 KB Output is correct
16 Correct 8 ms 39000 KB Output is correct
17 Correct 8 ms 39000 KB Output is correct
18 Correct 9 ms 39004 KB Output is correct
19 Correct 8 ms 39004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39156 KB Output is correct
2 Correct 8 ms 39004 KB Output is correct
3 Correct 8 ms 39004 KB Output is correct
4 Correct 9 ms 39004 KB Output is correct
5 Correct 8 ms 39004 KB Output is correct
6 Correct 8 ms 39004 KB Output is correct
7 Correct 9 ms 39000 KB Output is correct
8 Correct 9 ms 39000 KB Output is correct
9 Correct 14 ms 39004 KB Output is correct
10 Correct 9 ms 39004 KB Output is correct
11 Correct 9 ms 39004 KB Output is correct
12 Correct 9 ms 39004 KB Output is correct
13 Correct 9 ms 39004 KB Output is correct
14 Correct 10 ms 39004 KB Output is correct
15 Correct 10 ms 39004 KB Output is correct
16 Correct 9 ms 39004 KB Output is correct
17 Correct 10 ms 39004 KB Output is correct
18 Correct 8 ms 39004 KB Output is correct
19 Correct 9 ms 39004 KB Output is correct
20 Correct 8 ms 39004 KB Output is correct
21 Correct 8 ms 39004 KB Output is correct
22 Correct 8 ms 39004 KB Output is correct
23 Correct 8 ms 39004 KB Output is correct
24 Correct 8 ms 39260 KB Output is correct
25 Correct 8 ms 39004 KB Output is correct
26 Correct 8 ms 39000 KB Output is correct
27 Correct 8 ms 39000 KB Output is correct
28 Correct 9 ms 39004 KB Output is correct
29 Correct 8 ms 39004 KB Output is correct
30 Correct 8 ms 39004 KB Output is correct
31 Correct 9 ms 39084 KB Output is correct
32 Correct 9 ms 39272 KB Output is correct
33 Correct 11 ms 39272 KB Output is correct
34 Correct 12 ms 39272 KB Output is correct
35 Correct 11 ms 39224 KB Output is correct
36 Correct 10 ms 39524 KB Output is correct
37 Correct 10 ms 39264 KB Output is correct
38 Correct 10 ms 39484 KB Output is correct
39 Correct 10 ms 39524 KB Output is correct
40 Correct 11 ms 40036 KB Output is correct
41 Correct 10 ms 40036 KB Output is correct
42 Correct 10 ms 39264 KB Output is correct
43 Correct 11 ms 39776 KB Output is correct
44 Correct 12 ms 39620 KB Output is correct
45 Correct 10 ms 39520 KB Output is correct
46 Correct 11 ms 39424 KB Output is correct
47 Correct 14 ms 39520 KB Output is correct
48 Correct 10 ms 39556 KB Output is correct
49 Correct 13 ms 39704 KB Output is correct
50 Correct 11 ms 39448 KB Output is correct
51 Correct 10 ms 39260 KB Output is correct
52 Correct 10 ms 39516 KB Output is correct
53 Correct 10 ms 39588 KB Output is correct
54 Correct 11 ms 39516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39156 KB Output is correct
2 Correct 8 ms 39004 KB Output is correct
3 Correct 8 ms 39004 KB Output is correct
4 Correct 9 ms 39004 KB Output is correct
5 Correct 8 ms 39004 KB Output is correct
6 Correct 8 ms 39004 KB Output is correct
7 Correct 9 ms 39000 KB Output is correct
8 Correct 9 ms 39000 KB Output is correct
9 Correct 14 ms 39004 KB Output is correct
10 Correct 9 ms 39004 KB Output is correct
11 Correct 9 ms 39004 KB Output is correct
12 Correct 9 ms 39004 KB Output is correct
13 Correct 9 ms 39004 KB Output is correct
14 Correct 10 ms 39004 KB Output is correct
15 Correct 10 ms 39004 KB Output is correct
16 Correct 9 ms 39004 KB Output is correct
17 Correct 10 ms 39004 KB Output is correct
18 Correct 8 ms 39004 KB Output is correct
19 Correct 9 ms 39004 KB Output is correct
20 Correct 8 ms 39004 KB Output is correct
21 Correct 8 ms 39004 KB Output is correct
22 Correct 8 ms 39004 KB Output is correct
23 Correct 8 ms 39004 KB Output is correct
24 Correct 8 ms 39260 KB Output is correct
25 Correct 8 ms 39004 KB Output is correct
26 Correct 8 ms 39000 KB Output is correct
27 Correct 8 ms 39000 KB Output is correct
28 Correct 9 ms 39004 KB Output is correct
29 Correct 8 ms 39004 KB Output is correct
30 Correct 8 ms 39004 KB Output is correct
31 Correct 9 ms 39084 KB Output is correct
32 Correct 9 ms 39272 KB Output is correct
33 Correct 11 ms 39272 KB Output is correct
34 Correct 12 ms 39272 KB Output is correct
35 Correct 11 ms 39224 KB Output is correct
36 Correct 10 ms 39524 KB Output is correct
37 Correct 10 ms 39264 KB Output is correct
38 Correct 10 ms 39484 KB Output is correct
39 Correct 10 ms 39524 KB Output is correct
40 Correct 11 ms 40036 KB Output is correct
41 Correct 10 ms 40036 KB Output is correct
42 Correct 10 ms 39264 KB Output is correct
43 Correct 11 ms 39776 KB Output is correct
44 Correct 12 ms 39620 KB Output is correct
45 Correct 10 ms 39520 KB Output is correct
46 Correct 11 ms 39424 KB Output is correct
47 Correct 14 ms 39520 KB Output is correct
48 Correct 10 ms 39556 KB Output is correct
49 Correct 13 ms 39704 KB Output is correct
50 Correct 11 ms 39448 KB Output is correct
51 Correct 10 ms 39260 KB Output is correct
52 Correct 10 ms 39516 KB Output is correct
53 Correct 10 ms 39588 KB Output is correct
54 Correct 11 ms 39516 KB Output is correct
55 Correct 13 ms 40028 KB Output is correct
56 Correct 29 ms 42192 KB Output is correct
57 Correct 44 ms 46456 KB Output is correct
58 Correct 56 ms 47816 KB Output is correct
59 Correct 84 ms 50152 KB Output is correct
60 Correct 126 ms 54396 KB Output is correct
61 Correct 127 ms 56264 KB Output is correct
62 Correct 143 ms 57544 KB Output is correct
63 Correct 166 ms 62452 KB Output is correct
64 Correct 183 ms 62996 KB Output is correct
65 Correct 111 ms 101676 KB Output is correct
66 Correct 102 ms 101796 KB Output is correct
67 Correct 92 ms 59732 KB Output is correct
68 Correct 127 ms 83048 KB Output is correct
69 Correct 167 ms 78512 KB Output is correct
70 Correct 164 ms 78332 KB Output is correct
71 Correct 139 ms 72564 KB Output is correct
72 Correct 137 ms 72984 KB Output is correct
73 Correct 125 ms 70120 KB Output is correct
74 Correct 117 ms 68904 KB Output is correct
75 Correct 123 ms 68764 KB Output is correct
76 Correct 137 ms 69508 KB Output is correct
77 Correct 148 ms 68976 KB Output is correct
78 Correct 125 ms 65472 KB Output is correct
79 Correct 114 ms 68292 KB Output is correct