Submission #475302

# Submission time Handle Problem Language Result Execution time Memory
475302 2021-09-21T19:19:05 Z Vladth11 Fireworks (APIO16_fireworks) C++14
100 / 100
511 ms 78004 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;

const ll NMAX = 300001;
const ll VMAX = 1000001;
const ll INF = (1LL << 59);
const ll MOD = 998244353;
const ll BLOCK = 318;
const ll base = 31;
const ll nr_of_bits = 21;

vector <pii> v[NMAX];
ll n, m;
multiset <ll> st[NMAX];
ll slope[NMAX];
ll schema[NMAX];

void shift(ll node, ll d) {
    ll act = slope[node];
    ll last = 0;
    while(act > 0) {
        auto it = st[node].rbegin();
        last = *it;
        st[node].erase(st[node].find(*it));
        act--;
    }
    ll celalalt = *st[node].rbegin();
    st[node].erase(st[node].find(celalalt));
    st[node].insert(last + d);
    st[node].insert(celalalt + d);
    slope[node] = 1;
}

void merge(multiset <ll> &a, multiset <ll> &b) {
    if(a.size() < b.size()) {
        swap(a, b);
    }
    for(auto x : b) {
        a.insert(x);
    }
    b.clear();
}

void DFS(ll node, ll p) {
    if(v[node].size() == 1 && node != 1) {
        st[node].insert(0);
        st[node].insert(0);
        slope[node] = 1;
        return;
    }
    for(auto y : v[node]) {
        ll x = y.first;
        ll d = y.second;
        if(x == p)
            continue;
        DFS(x, node);
        shift(x, d);
        slope[node] += slope[x];
        schema[node] += schema[x];
        merge(st[node], st[x]);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll sum = 0;
    ll i;
    cin >> n >> m;
    for(i = 2; i <= n + m; i++) {
        ll p, l;
        cin >> p >> l;
        v[i].push_back({p, l});
        v[p].push_back({i, l});
        sum += 1LL * l;
    }
    DFS(1, 0);
    ll last;
    while(slope[1] > 0) {
        auto it = st[1].rbegin();
        last = *it;
        st[1].erase(st[1].find(*it));
        slope[1]--;
    }
    /// Acum cel din spate e primul cu slope 0
    ll panta = 0;
    vector <pii> v;
    v.push_back({0, 0});
    for(auto x : st[1]){
        if(x == 0){
            continue;
        }
        if(v.size() == 0 || v.back().first != x){
            v.push_back({x, 1});
        }else{
            v.back().second++;
        }
        panta++;
    }
    for(ll i = 1; i < v.size(); i++){
        sum -= 1LL * panta * 1LL * (v[i].first - v[i - 1].first);
        panta -= v[i].second;
    }
    cout << sum;
    return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:110:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |     for(ll i = 1; i < v.size(); i++){
      |                   ~~^~~~~~~~~~
fireworks.cpp:88:8: warning: variable 'last' set but not used [-Wunused-but-set-variable]
   88 |     ll last;
      |        ^~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21452 KB Output is correct
2 Correct 11 ms 21400 KB Output is correct
3 Correct 12 ms 21452 KB Output is correct
4 Correct 12 ms 21424 KB Output is correct
5 Correct 12 ms 21464 KB Output is correct
6 Correct 12 ms 21472 KB Output is correct
7 Correct 13 ms 21404 KB Output is correct
8 Correct 12 ms 21460 KB Output is correct
9 Correct 12 ms 21472 KB Output is correct
10 Correct 13 ms 21452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21440 KB Output is correct
2 Correct 12 ms 21452 KB Output is correct
3 Correct 12 ms 21452 KB Output is correct
4 Correct 13 ms 21428 KB Output is correct
5 Correct 12 ms 21452 KB Output is correct
6 Correct 12 ms 21452 KB Output is correct
7 Correct 13 ms 21452 KB Output is correct
8 Correct 12 ms 21388 KB Output is correct
9 Correct 12 ms 21440 KB Output is correct
10 Correct 12 ms 21404 KB Output is correct
11 Correct 13 ms 21452 KB Output is correct
12 Correct 12 ms 21400 KB Output is correct
13 Correct 12 ms 21452 KB Output is correct
14 Correct 12 ms 21468 KB Output is correct
15 Correct 12 ms 21452 KB Output is correct
16 Correct 12 ms 21452 KB Output is correct
17 Correct 13 ms 21416 KB Output is correct
18 Correct 12 ms 21484 KB Output is correct
19 Correct 12 ms 21368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21452 KB Output is correct
2 Correct 11 ms 21400 KB Output is correct
3 Correct 12 ms 21452 KB Output is correct
4 Correct 12 ms 21424 KB Output is correct
5 Correct 12 ms 21464 KB Output is correct
6 Correct 12 ms 21472 KB Output is correct
7 Correct 13 ms 21404 KB Output is correct
8 Correct 12 ms 21460 KB Output is correct
9 Correct 12 ms 21472 KB Output is correct
10 Correct 13 ms 21452 KB Output is correct
11 Correct 12 ms 21440 KB Output is correct
12 Correct 12 ms 21452 KB Output is correct
13 Correct 12 ms 21452 KB Output is correct
14 Correct 13 ms 21428 KB Output is correct
15 Correct 12 ms 21452 KB Output is correct
16 Correct 12 ms 21452 KB Output is correct
17 Correct 13 ms 21452 KB Output is correct
18 Correct 12 ms 21388 KB Output is correct
19 Correct 12 ms 21440 KB Output is correct
20 Correct 12 ms 21404 KB Output is correct
21 Correct 13 ms 21452 KB Output is correct
22 Correct 12 ms 21400 KB Output is correct
23 Correct 12 ms 21452 KB Output is correct
24 Correct 12 ms 21468 KB Output is correct
25 Correct 12 ms 21452 KB Output is correct
26 Correct 12 ms 21452 KB Output is correct
27 Correct 13 ms 21416 KB Output is correct
28 Correct 12 ms 21484 KB Output is correct
29 Correct 12 ms 21368 KB Output is correct
30 Correct 13 ms 21428 KB Output is correct
31 Correct 13 ms 21464 KB Output is correct
32 Correct 12 ms 21580 KB Output is correct
33 Correct 14 ms 21676 KB Output is correct
34 Correct 14 ms 21724 KB Output is correct
35 Correct 17 ms 21840 KB Output is correct
36 Correct 16 ms 21836 KB Output is correct
37 Correct 16 ms 21948 KB Output is correct
38 Correct 16 ms 21924 KB Output is correct
39 Correct 16 ms 21964 KB Output is correct
40 Correct 15 ms 22336 KB Output is correct
41 Correct 15 ms 22348 KB Output is correct
42 Correct 15 ms 21836 KB Output is correct
43 Correct 17 ms 22284 KB Output is correct
44 Correct 16 ms 22220 KB Output is correct
45 Correct 16 ms 22116 KB Output is correct
46 Correct 17 ms 22188 KB Output is correct
47 Correct 17 ms 22092 KB Output is correct
48 Correct 16 ms 22092 KB Output is correct
49 Correct 17 ms 22108 KB Output is correct
50 Correct 15 ms 22040 KB Output is correct
51 Correct 16 ms 22092 KB Output is correct
52 Correct 17 ms 22092 KB Output is correct
53 Correct 17 ms 22104 KB Output is correct
54 Correct 17 ms 22340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21452 KB Output is correct
2 Correct 11 ms 21400 KB Output is correct
3 Correct 12 ms 21452 KB Output is correct
4 Correct 12 ms 21424 KB Output is correct
5 Correct 12 ms 21464 KB Output is correct
6 Correct 12 ms 21472 KB Output is correct
7 Correct 13 ms 21404 KB Output is correct
8 Correct 12 ms 21460 KB Output is correct
9 Correct 12 ms 21472 KB Output is correct
10 Correct 13 ms 21452 KB Output is correct
11 Correct 12 ms 21440 KB Output is correct
12 Correct 12 ms 21452 KB Output is correct
13 Correct 12 ms 21452 KB Output is correct
14 Correct 13 ms 21428 KB Output is correct
15 Correct 12 ms 21452 KB Output is correct
16 Correct 12 ms 21452 KB Output is correct
17 Correct 13 ms 21452 KB Output is correct
18 Correct 12 ms 21388 KB Output is correct
19 Correct 12 ms 21440 KB Output is correct
20 Correct 12 ms 21404 KB Output is correct
21 Correct 13 ms 21452 KB Output is correct
22 Correct 12 ms 21400 KB Output is correct
23 Correct 12 ms 21452 KB Output is correct
24 Correct 12 ms 21468 KB Output is correct
25 Correct 12 ms 21452 KB Output is correct
26 Correct 12 ms 21452 KB Output is correct
27 Correct 13 ms 21416 KB Output is correct
28 Correct 12 ms 21484 KB Output is correct
29 Correct 12 ms 21368 KB Output is correct
30 Correct 13 ms 21428 KB Output is correct
31 Correct 13 ms 21464 KB Output is correct
32 Correct 12 ms 21580 KB Output is correct
33 Correct 14 ms 21676 KB Output is correct
34 Correct 14 ms 21724 KB Output is correct
35 Correct 17 ms 21840 KB Output is correct
36 Correct 16 ms 21836 KB Output is correct
37 Correct 16 ms 21948 KB Output is correct
38 Correct 16 ms 21924 KB Output is correct
39 Correct 16 ms 21964 KB Output is correct
40 Correct 15 ms 22336 KB Output is correct
41 Correct 15 ms 22348 KB Output is correct
42 Correct 15 ms 21836 KB Output is correct
43 Correct 17 ms 22284 KB Output is correct
44 Correct 16 ms 22220 KB Output is correct
45 Correct 16 ms 22116 KB Output is correct
46 Correct 17 ms 22188 KB Output is correct
47 Correct 17 ms 22092 KB Output is correct
48 Correct 16 ms 22092 KB Output is correct
49 Correct 17 ms 22108 KB Output is correct
50 Correct 15 ms 22040 KB Output is correct
51 Correct 16 ms 22092 KB Output is correct
52 Correct 17 ms 22092 KB Output is correct
53 Correct 17 ms 22104 KB Output is correct
54 Correct 17 ms 22340 KB Output is correct
55 Correct 22 ms 22852 KB Output is correct
56 Correct 56 ms 26528 KB Output is correct
57 Correct 98 ms 30288 KB Output is correct
58 Correct 129 ms 32776 KB Output is correct
59 Correct 177 ms 37036 KB Output is correct
60 Correct 283 ms 40152 KB Output is correct
61 Correct 250 ms 42816 KB Output is correct
62 Correct 290 ms 45580 KB Output is correct
63 Correct 369 ms 49560 KB Output is correct
64 Correct 374 ms 51336 KB Output is correct
65 Correct 175 ms 77892 KB Output is correct
66 Correct 171 ms 78004 KB Output is correct
67 Correct 183 ms 45248 KB Output is correct
68 Correct 301 ms 71812 KB Output is correct
69 Correct 375 ms 69172 KB Output is correct
70 Correct 392 ms 69096 KB Output is correct
71 Correct 379 ms 62656 KB Output is correct
72 Correct 388 ms 62848 KB Output is correct
73 Correct 377 ms 61456 KB Output is correct
74 Correct 379 ms 61572 KB Output is correct
75 Correct 409 ms 60476 KB Output is correct
76 Correct 447 ms 60600 KB Output is correct
77 Correct 438 ms 59920 KB Output is correct
78 Correct 453 ms 60796 KB Output is correct
79 Correct 511 ms 75908 KB Output is correct