Submission #778876

# Submission time Handle Problem Language Result Execution time Memory
778876 2023-07-10T22:40:04 Z danikoynov Fireworks (APIO16_fireworks) C++14
100 / 100
437 ms 123056 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 3e5 + 10;
const ll inf = 1e18;

struct slope_trick
{
    multiset < ll > left_set, right_set;
    ll left_offset, right_offset, height;

    slope_trick()
    {
        left_offset = right_offset = height = 0;
    }

    ll get_left()
    {
        if (left_set.empty())
            return - inf;
        return *left_set.rbegin() + left_offset;
    }

    ll get_right()
    {
        if (right_set.empty())
            return inf;
        return *right_set.begin() + right_offset;
    }

    void add_down_slope(ll value)
    {
        ll left = get_left();
        ll right = get_right();
        if (value >= left && value <= right)
        {
            left_set.insert(value - left_offset);
        }
        else if (value < left)
        {
            left_set.insert(value - left_offset);
        }
        else if (value > right)
        {

            right_set.erase(right_set.find(right - right_offset));
            right_set.insert(value - right_offset);
            left_set.insert(right - left_offset);
            height = height  + (value - right);
        }
    }

    void add_up_slope(ll value)
    {
        ///cout << "add up slope " << value << endl;
        ll left = get_left();
        ll right = get_right();

        if (value >= left && right <= right)
        {
            right_set.insert(value - right_offset);
        }
        else if (value < left)
        {
            left_set.erase(left_set.find(left - left_offset));
            left_set.insert(value - left_offset);
            right_set.insert(left - right_offset);
            height = height + (left - value);
        }
        else
        {
            right_set.insert(value - right_offset);
        }
    }

    void shift_height(ll value)
    {
        height += value;
    }

    void shift_function(ll value)
    {
        left_offset += value;
        right_offset += value;
    }

    void upperize()
    {
        assert(right_set.size() != 0);
        ll right = get_right();
        right_set.clear();
        right_set.insert(right - right_offset);
    }

    void shift_left_slope(ll value)
    {
        ///assert(left_set.size() != 0);
        if (left_set.size() == 0)
        {
            left_set.insert(value - left_offset);
            return;
        }
        ll left = get_left();
        left_offset -= value;
        left_set.erase(left_set.find(*left_set.rbegin()));
        left_set.insert(left - left_offset);
    }

    void print_function()
    {
        cout << "left slope:" << endl;
        for (ll v : left_set)
        cout << v + left_offset << " ";
        cout << endl;
        cout << "right slope:" << endl;
        for (ll v : right_set)
            cout << v + right_offset << " ";
        cout << endl;
        cout << "height: " << height << endl;
    }
};

int n, m, sub[maxn];
vector < pair < int, ll > > adj[maxn];
slope_trick fun[maxn];

void dfs(int v, ll len)
{
    int heavy = -1;

    sub[v] = 1;
    for (pair < int, ll > nb : adj[v])
    {
        int u = nb.first;
        ll w = nb.second;
        dfs(u, w);
        sub[v] += sub[u];
        if (heavy == -1 || sub[u] > sub[heavy])
            heavy = u;
    }

    if (heavy != -1)
      swap(fun[v], fun[heavy]);
    for (pair < int, ll > nb : adj[v])
    {
        int u = nb.first;

        if (u == heavy)
          continue;

        ///cout << "edge " << v << " " << u << endl;
        for (ll value : fun[u].left_set)
            fun[v].add_down_slope(value + fun[u].left_offset);

        for (ll value : fun[u].right_set)
            fun[v].add_up_slope(value + fun[u].right_offset);

        fun[v].shift_height(fun[u].height);
    }
    if (sub[v] > 1)
    {
        fun[v].upperize();

    }
    else
    {
        fun[v].add_up_slope(0);

    }
    //cout << "vertex " << v << " " << "fine" << endl;
    fun[v].shift_function(len);
    fun[v].shift_left_slope(len);



}
void solve()
{
    cin >> n >> m;
    for (int i = 2; i <= n + m; i ++)
    {
        int p;
        ll c;
        cin >> p >> c;
        adj[p].push_back({i, c});
    }

    dfs(1, 0);

    cout << fun[1].height << endl;

}

int main()
{
    solve();
    return 0;
}

Compilation message

fireworks.cpp: In member function 'void slope_trick::add_up_slope(ll)':
fireworks.cpp:77:36: warning: self-comparison always evaluates to true [-Wtautological-compare]
   77 |         if (value >= left && right <= right)
      |                              ~~~~~ ^~ ~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 42452 KB Output is correct
2 Correct 22 ms 42508 KB Output is correct
3 Correct 24 ms 42596 KB Output is correct
4 Correct 21 ms 42580 KB Output is correct
5 Correct 20 ms 42532 KB Output is correct
6 Correct 21 ms 42580 KB Output is correct
7 Correct 20 ms 42576 KB Output is correct
8 Correct 23 ms 42580 KB Output is correct
9 Correct 20 ms 42608 KB Output is correct
10 Correct 20 ms 42572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 42508 KB Output is correct
2 Correct 20 ms 42580 KB Output is correct
3 Correct 20 ms 42588 KB Output is correct
4 Correct 21 ms 42580 KB Output is correct
5 Correct 21 ms 42572 KB Output is correct
6 Correct 21 ms 42580 KB Output is correct
7 Correct 20 ms 42580 KB Output is correct
8 Correct 22 ms 42544 KB Output is correct
9 Correct 20 ms 42592 KB Output is correct
10 Correct 21 ms 42580 KB Output is correct
11 Correct 21 ms 42604 KB Output is correct
12 Correct 21 ms 42584 KB Output is correct
13 Correct 21 ms 42620 KB Output is correct
14 Correct 21 ms 42632 KB Output is correct
15 Correct 24 ms 42592 KB Output is correct
16 Correct 21 ms 42508 KB Output is correct
17 Correct 21 ms 42532 KB Output is correct
18 Correct 21 ms 42532 KB Output is correct
19 Correct 22 ms 42580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 42452 KB Output is correct
2 Correct 22 ms 42508 KB Output is correct
3 Correct 24 ms 42596 KB Output is correct
4 Correct 21 ms 42580 KB Output is correct
5 Correct 20 ms 42532 KB Output is correct
6 Correct 21 ms 42580 KB Output is correct
7 Correct 20 ms 42576 KB Output is correct
8 Correct 23 ms 42580 KB Output is correct
9 Correct 20 ms 42608 KB Output is correct
10 Correct 20 ms 42572 KB Output is correct
11 Correct 21 ms 42508 KB Output is correct
12 Correct 20 ms 42580 KB Output is correct
13 Correct 20 ms 42588 KB Output is correct
14 Correct 21 ms 42580 KB Output is correct
15 Correct 21 ms 42572 KB Output is correct
16 Correct 21 ms 42580 KB Output is correct
17 Correct 20 ms 42580 KB Output is correct
18 Correct 22 ms 42544 KB Output is correct
19 Correct 20 ms 42592 KB Output is correct
20 Correct 21 ms 42580 KB Output is correct
21 Correct 21 ms 42604 KB Output is correct
22 Correct 21 ms 42584 KB Output is correct
23 Correct 21 ms 42620 KB Output is correct
24 Correct 21 ms 42632 KB Output is correct
25 Correct 24 ms 42592 KB Output is correct
26 Correct 21 ms 42508 KB Output is correct
27 Correct 21 ms 42532 KB Output is correct
28 Correct 21 ms 42532 KB Output is correct
29 Correct 22 ms 42580 KB Output is correct
30 Correct 21 ms 42616 KB Output is correct
31 Correct 21 ms 42696 KB Output is correct
32 Correct 23 ms 42784 KB Output is correct
33 Correct 23 ms 42880 KB Output is correct
34 Correct 23 ms 42884 KB Output is correct
35 Correct 24 ms 43084 KB Output is correct
36 Correct 24 ms 43044 KB Output is correct
37 Correct 25 ms 43116 KB Output is correct
38 Correct 24 ms 43164 KB Output is correct
39 Correct 24 ms 43124 KB Output is correct
40 Correct 28 ms 43748 KB Output is correct
41 Correct 25 ms 43652 KB Output is correct
42 Correct 25 ms 42828 KB Output is correct
43 Correct 24 ms 43492 KB Output is correct
44 Correct 25 ms 43476 KB Output is correct
45 Correct 25 ms 43488 KB Output is correct
46 Correct 26 ms 43680 KB Output is correct
47 Correct 25 ms 43604 KB Output is correct
48 Correct 25 ms 43476 KB Output is correct
49 Correct 25 ms 43536 KB Output is correct
50 Correct 24 ms 43384 KB Output is correct
51 Correct 25 ms 43404 KB Output is correct
52 Correct 25 ms 43432 KB Output is correct
53 Correct 25 ms 43416 KB Output is correct
54 Correct 25 ms 43684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 42452 KB Output is correct
2 Correct 22 ms 42508 KB Output is correct
3 Correct 24 ms 42596 KB Output is correct
4 Correct 21 ms 42580 KB Output is correct
5 Correct 20 ms 42532 KB Output is correct
6 Correct 21 ms 42580 KB Output is correct
7 Correct 20 ms 42576 KB Output is correct
8 Correct 23 ms 42580 KB Output is correct
9 Correct 20 ms 42608 KB Output is correct
10 Correct 20 ms 42572 KB Output is correct
11 Correct 21 ms 42508 KB Output is correct
12 Correct 20 ms 42580 KB Output is correct
13 Correct 20 ms 42588 KB Output is correct
14 Correct 21 ms 42580 KB Output is correct
15 Correct 21 ms 42572 KB Output is correct
16 Correct 21 ms 42580 KB Output is correct
17 Correct 20 ms 42580 KB Output is correct
18 Correct 22 ms 42544 KB Output is correct
19 Correct 20 ms 42592 KB Output is correct
20 Correct 21 ms 42580 KB Output is correct
21 Correct 21 ms 42604 KB Output is correct
22 Correct 21 ms 42584 KB Output is correct
23 Correct 21 ms 42620 KB Output is correct
24 Correct 21 ms 42632 KB Output is correct
25 Correct 24 ms 42592 KB Output is correct
26 Correct 21 ms 42508 KB Output is correct
27 Correct 21 ms 42532 KB Output is correct
28 Correct 21 ms 42532 KB Output is correct
29 Correct 22 ms 42580 KB Output is correct
30 Correct 21 ms 42616 KB Output is correct
31 Correct 21 ms 42696 KB Output is correct
32 Correct 23 ms 42784 KB Output is correct
33 Correct 23 ms 42880 KB Output is correct
34 Correct 23 ms 42884 KB Output is correct
35 Correct 24 ms 43084 KB Output is correct
36 Correct 24 ms 43044 KB Output is correct
37 Correct 25 ms 43116 KB Output is correct
38 Correct 24 ms 43164 KB Output is correct
39 Correct 24 ms 43124 KB Output is correct
40 Correct 28 ms 43748 KB Output is correct
41 Correct 25 ms 43652 KB Output is correct
42 Correct 25 ms 42828 KB Output is correct
43 Correct 24 ms 43492 KB Output is correct
44 Correct 25 ms 43476 KB Output is correct
45 Correct 25 ms 43488 KB Output is correct
46 Correct 26 ms 43680 KB Output is correct
47 Correct 25 ms 43604 KB Output is correct
48 Correct 25 ms 43476 KB Output is correct
49 Correct 25 ms 43536 KB Output is correct
50 Correct 24 ms 43384 KB Output is correct
51 Correct 25 ms 43404 KB Output is correct
52 Correct 25 ms 43432 KB Output is correct
53 Correct 25 ms 43416 KB Output is correct
54 Correct 25 ms 43684 KB Output is correct
55 Correct 32 ms 44292 KB Output is correct
56 Correct 69 ms 49480 KB Output is correct
57 Correct 116 ms 54800 KB Output is correct
58 Correct 144 ms 58544 KB Output is correct
59 Correct 189 ms 63660 KB Output is correct
60 Correct 234 ms 69204 KB Output is correct
61 Correct 276 ms 72884 KB Output is correct
62 Correct 298 ms 76468 KB Output is correct
63 Correct 366 ms 83864 KB Output is correct
64 Correct 427 ms 84664 KB Output is correct
65 Correct 248 ms 114256 KB Output is correct
66 Correct 253 ms 114328 KB Output is correct
67 Correct 272 ms 58228 KB Output is correct
68 Correct 324 ms 104872 KB Output is correct
69 Correct 365 ms 101180 KB Output is correct
70 Correct 377 ms 101220 KB Output is correct
71 Correct 407 ms 123056 KB Output is correct
72 Correct 406 ms 123056 KB Output is correct
73 Correct 380 ms 109796 KB Output is correct
74 Correct 389 ms 110004 KB Output is correct
75 Correct 382 ms 108748 KB Output is correct
76 Correct 407 ms 108812 KB Output is correct
77 Correct 396 ms 106524 KB Output is correct
78 Correct 435 ms 103824 KB Output is correct
79 Correct 437 ms 108292 KB Output is correct