답안 #711190

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
711190 2023-03-16T09:39:48 Z pls33 Fireworks (APIO16_fireworks) C++17
55 / 100
2000 ms 28232 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma region dalykai
using p32 = pair<int, int>;
using p32u = pair<uint32_t, uint32_t>;
using p64 = pair<int64_t, int64_t>;
using p64u = pair<uint64_t, uint64_t>;
using vi16 = vector<int16_t>;
using vi16u = vector<uint16_t>;
using vi32 = vector<int>;
using vi32u = vector<uint32_t>;
using vi64 = vector<int64_t>;
using vi64u = vector<uint64_t>;
using vp32 = vector<p32>;
using vp32u = vector<p32u>;
using vp64 = vector<p64>;
using vp64u = vector<p64u>;
using vvi32 = vector<vi32>;
using vvi32u = vector<vi32u>;
using vvi64 = vector<vi64>;
using vvi64u = vector<vi64u>;
using vvp32 = vector<vp32>;
using vvp32u = vector<vp32u>;
using vvp64 = vector<vp64>;
using vvp64u = vector<vp64u>;
using f80 = long double;
#pragma endregion

/*
 * Author: Seokhwan Choi
 * Time Complexity: O((N+M) (log (N+M))^2 )
 */

#include <stdio.h>
#include <queue>
#define MAXN 300100
int n, m;
int p[MAXN];
int c[MAXN];
struct ndata
{                       // contains data for subtree (y=f(x), where y is minimum cost when distance to all leaf node is x
    long long int a, b; // y=ax+b at large x
    vi64 *pq;           // saves slope changing points, slope change by 1 at each element
    ndata operator+(ndata r)
    {            // merge two data by adding them
        ndata s; // result(merged data)
        s.a = a + r.a;
        s.b = b + r.b;
        if (pq->size() > r.pq->size())
        { // merge smaller priority queue to larger priority queue
            s.pq = pq;

            // s.pq->resize(s.pq->size() + r.pq->size());
            // s.pq->insert(s.pq->end(), r.pq->begin(), r.pq->end());
            for (auto &i : *r.pq)
            {
                s.pq->push_back(i);
            }
            make_heap(s.pq->begin(), s.pq->end());
            // r.pq->clear();
        }
        else
        {
            s.pq = r.pq;

            for (auto &i : *pq)
            {
                s.pq->push_back(i);
            }
            make_heap(s.pq->begin(), s.pq->end());
            // pq->clear();
        }
        return s;
    }

    int64_t top()
    {
        return pq->front();
    }

    void pop()
    {
        pop_heap(pq->begin(), pq->end());
        pq->pop_back();
    }

    void push(int64_t val)
    {
        pq->push_back(val);
        push_heap(pq->begin(), pq->end());
    }
};
ndata d[MAXN];
int main()
{
#ifndef _AAAAAAAAA
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#else
    freopen("fireworks.in", "r", stdin);
#ifndef __linux__
    atexit([]()
           {
        freopen("con", "r", stdin);
        system("pause"); });
#endif
#endif

    int i;
    scanf("%d%d", &n, &m);
    for (i = 2; i <= n + m; i++)
    {
        scanf("%d%d", &p[i], &c[i]);
    }
    for (i = n + m; i > 0; i--)
    { // initiallize
        d[i].a = 0;
        d[i].b = 0;
        d[i].pq = new vi64();
    }
    for (i = n + m; i > n; i--)
    { // leaf nodes
        d[i].a = 1;
        d[i].b = -c[i];

        d[i].push(c[i]);          // slope is -1 if x<c[i], 1 if x>c[i]
        d[i].push(c[i]);          // slope changes by 2
        d[p[i]] = d[p[i]] + d[i]; // add the data to parent node
    }
    for (i = n; i > 1; i--)
    {
        // add edge to parent node
        while (d[i].a > 1)
        {                         // slope over 1 is useless because we can increase only one edge(edge toward parent node)
            d[i].a--;             // slope decrease by 1
            d[i].b += d[i].top(); // y=ax+b=(a-1)x+(b+x) at slope changing point
            d[i].pop();
        }
        long long int ta = d[i].top(); // increase length of slope -1 part by c[i]
        d[i].pop();
        long long int tb = d[i].top();
        d[i].pop();
        d[i].push(tb + c[i]); // move location of slope 0, 1 part by c[i]
        d[i].push(ta + c[i]);
        d[i].b -= c[i];           // y is decreased by c[i] at sufficiently large x (slope 1 part)
        d[p[i]] = d[p[i]] + d[i]; // add the data to parent node
    }
    while (d[1].a > 0)
    { // root node, y at slope 0 is the answer because it is minimum y
        d[1].a--;
        d[1].b += d[1].top();
        d[1].pop();
    }
    printf("%lld\n", d[1].b);
    return 0;
}

Compilation message

fireworks.cpp:8: warning: ignoring '#pragma region dalykai' [-Wunknown-pragmas]
    8 | #pragma region dalykai
      | 
fireworks.cpp:32: warning: ignoring '#pragma endregion ' [-Wunknown-pragmas]
   32 | #pragma endregion
      | 
fireworks.cpp: In function 'int main()':
fireworks.cpp:115:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         scanf("%d%d", &p[i], &c[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 388 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 388 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 472 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 3 ms 596 KB Output is correct
36 Correct 3 ms 724 KB Output is correct
37 Correct 3 ms 724 KB Output is correct
38 Correct 5 ms 724 KB Output is correct
39 Correct 4 ms 724 KB Output is correct
40 Correct 1 ms 596 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 2 ms 596 KB Output is correct
43 Correct 27 ms 772 KB Output is correct
44 Correct 23 ms 816 KB Output is correct
45 Correct 21 ms 784 KB Output is correct
46 Correct 8 ms 928 KB Output is correct
47 Correct 8 ms 980 KB Output is correct
48 Correct 31 ms 844 KB Output is correct
49 Correct 35 ms 968 KB Output is correct
50 Correct 69 ms 776 KB Output is correct
51 Correct 68 ms 784 KB Output is correct
52 Correct 51 ms 792 KB Output is correct
53 Correct 46 ms 848 KB Output is correct
54 Correct 198 ms 844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 388 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 472 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 3 ms 596 KB Output is correct
36 Correct 3 ms 724 KB Output is correct
37 Correct 3 ms 724 KB Output is correct
38 Correct 5 ms 724 KB Output is correct
39 Correct 4 ms 724 KB Output is correct
40 Correct 1 ms 596 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 2 ms 596 KB Output is correct
43 Correct 27 ms 772 KB Output is correct
44 Correct 23 ms 816 KB Output is correct
45 Correct 21 ms 784 KB Output is correct
46 Correct 8 ms 928 KB Output is correct
47 Correct 8 ms 980 KB Output is correct
48 Correct 31 ms 844 KB Output is correct
49 Correct 35 ms 968 KB Output is correct
50 Correct 69 ms 776 KB Output is correct
51 Correct 68 ms 784 KB Output is correct
52 Correct 51 ms 792 KB Output is correct
53 Correct 46 ms 848 KB Output is correct
54 Correct 198 ms 844 KB Output is correct
55 Correct 14 ms 1596 KB Output is correct
56 Correct 79 ms 5308 KB Output is correct
57 Correct 146 ms 8664 KB Output is correct
58 Correct 188 ms 10996 KB Output is correct
59 Correct 383 ms 14700 KB Output is correct
60 Correct 376 ms 17884 KB Output is correct
61 Correct 511 ms 20852 KB Output is correct
62 Correct 498 ms 22664 KB Output is correct
63 Correct 532 ms 26896 KB Output is correct
64 Correct 798 ms 28232 KB Output is correct
65 Correct 73 ms 18988 KB Output is correct
66 Correct 77 ms 18980 KB Output is correct
67 Correct 76 ms 19128 KB Output is correct
68 Execution timed out 2041 ms 24152 KB Time limit exceeded
69 Halted 0 ms 0 KB -