답안 #464672

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
464672 2021-08-13T16:12:54 Z blue Worst Reporter 4 (JOI21_worst_reporter4) C++17
79 / 100
741 ms 53188 KB
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <map>
using namespace std;

const int maxN = 200'000;

int N;
int A[1+maxN];
int H[1+maxN];
long long C[1+maxN];

vector<int> indegree(1+maxN, 0);
vector<int> subtree(1+maxN, 1);
vector<int> children[1+maxN];


queue<int> tbv;
map<int, long long>* X[1+maxN];

void map_insert(int u, int l, long long v)
{
    if(X[u]->find(l) == X[u]->end()) (*X[u])[l] = 0;
    (*X[u])[l] += v;
}


void merge(int u, int v)
{
    if(X[u] == X[v]) return;

    if(subtree[u] < subtree[v]) swap(u, v);
    for(pair<int, long long> c: *X[v])
        map_insert(u, c.first, c.second);

    X[v]->clear();
    X[v] = X[u];
}


int main()
{
    cin >> N;
    for(int i = 1; i <= N; i++)
    {
        cin >> A[i] >> H[i] >> C[i];
        children[ A[i] ].push_back(i);

        indegree[ A[i] ]++;
        X[i] = new map<int, long long>;
    }

    for(int i = 1; i <= N; i++)
    {
        if(indegree[i] == 0) tbv.push(i);
    }

    //First two subtasks!!!!!
    indegree[1]--;


    while(!tbv.empty())
    {
        int u = tbv.front();
        tbv.pop();
        // cerr << "u = " << u << '\n';


        int max_child = -1;
        for(int v: children[u])
        {
            if(v == A[u] || v == u) continue;
            if(indegree[v] == 0)
            {
                if(max_child == -1 || subtree[v] > subtree[max_child])
                    max_child = v;
            }
        }

        indegree[ A[u] ]--;
        subtree[ A[u] ] += subtree[u];
        if(indegree[ A[u] ] == 0)
            tbv.push( A[u] );

        // cerr << "max child = " << max_child << '\n';

        if(max_child == -1)
        {
            map_insert(u, H[u]+1, +C[u]);
        }
        else
        {
            X[u] = X[max_child];
            for(int v: children[u])
            {
                if(indegree[v] == 0)
                {
                    merge(v, u);
                }
            }

            map_insert(u, 1, +C[u]);
            map_insert(u, H[u]+1, +C[u]);

            // cerr << "intermediate: ";
            // for(pair<int, long long> q: *X[u])
            // {
            //     cerr << q.first << ' ' << q.second << " | ";
            // }
            // cerr << '\n';

            long long Y = +C[u];
            while(Y > 0)
            {
                map<int, long long>::iterator it = X[u]->upper_bound(H[u]);
                it--;

                // cerr << "it = " << it->first << ' ' << it->second << '\n';

                auto q = *it;
                X[u]->erase(it);

                long long delta_stair = min(q.second, Y);
                q.second -= delta_stair;
                Y -= delta_stair;

                if(q.second > 0)
                    map_insert(u, q.first, q.second);
            }
        }

        // for(pair<int, long long> q: *X[u])
        // {
        //     cerr << q.first << ' ' << q.second << " | ";
        // }
        // cerr << '\n';
    }



    long long ans = 0;
    for(pair<int, long long> k: *X[1])
        if(k.first == 1)
            ans += k.second;

    cout << ans << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6476 KB Output is correct
2 Correct 4 ms 6476 KB Output is correct
3 Correct 4 ms 6596 KB Output is correct
4 Correct 4 ms 6588 KB Output is correct
5 Correct 14 ms 7528 KB Output is correct
6 Correct 13 ms 7500 KB Output is correct
7 Correct 12 ms 7372 KB Output is correct
8 Correct 15 ms 7500 KB Output is correct
9 Correct 14 ms 7508 KB Output is correct
10 Correct 12 ms 7368 KB Output is correct
11 Correct 13 ms 7372 KB Output is correct
12 Correct 13 ms 7312 KB Output is correct
13 Correct 11 ms 7244 KB Output is correct
14 Correct 13 ms 7336 KB Output is correct
15 Correct 13 ms 7244 KB Output is correct
16 Correct 15 ms 7500 KB Output is correct
17 Correct 13 ms 7372 KB Output is correct
18 Correct 10 ms 7368 KB Output is correct
19 Correct 13 ms 7372 KB Output is correct
20 Correct 12 ms 7388 KB Output is correct
21 Correct 11 ms 7372 KB Output is correct
22 Correct 12 ms 7372 KB Output is correct
23 Correct 11 ms 7376 KB Output is correct
24 Correct 14 ms 7372 KB Output is correct
25 Correct 12 ms 7372 KB Output is correct
26 Correct 12 ms 7244 KB Output is correct
27 Correct 13 ms 7372 KB Output is correct
28 Correct 12 ms 7372 KB Output is correct
29 Correct 12 ms 7492 KB Output is correct
30 Correct 14 ms 7756 KB Output is correct
31 Correct 13 ms 7756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6476 KB Output is correct
2 Correct 4 ms 6476 KB Output is correct
3 Correct 4 ms 6596 KB Output is correct
4 Correct 4 ms 6588 KB Output is correct
5 Correct 14 ms 7528 KB Output is correct
6 Correct 13 ms 7500 KB Output is correct
7 Correct 12 ms 7372 KB Output is correct
8 Correct 15 ms 7500 KB Output is correct
9 Correct 14 ms 7508 KB Output is correct
10 Correct 12 ms 7368 KB Output is correct
11 Correct 13 ms 7372 KB Output is correct
12 Correct 13 ms 7312 KB Output is correct
13 Correct 11 ms 7244 KB Output is correct
14 Correct 13 ms 7336 KB Output is correct
15 Correct 13 ms 7244 KB Output is correct
16 Correct 15 ms 7500 KB Output is correct
17 Correct 13 ms 7372 KB Output is correct
18 Correct 10 ms 7368 KB Output is correct
19 Correct 13 ms 7372 KB Output is correct
20 Correct 12 ms 7388 KB Output is correct
21 Correct 11 ms 7372 KB Output is correct
22 Correct 12 ms 7372 KB Output is correct
23 Correct 11 ms 7376 KB Output is correct
24 Correct 14 ms 7372 KB Output is correct
25 Correct 12 ms 7372 KB Output is correct
26 Correct 12 ms 7244 KB Output is correct
27 Correct 13 ms 7372 KB Output is correct
28 Correct 12 ms 7372 KB Output is correct
29 Correct 12 ms 7492 KB Output is correct
30 Correct 14 ms 7756 KB Output is correct
31 Correct 13 ms 7756 KB Output is correct
32 Correct 15 ms 7452 KB Output is correct
33 Correct 741 ms 44648 KB Output is correct
34 Correct 614 ms 43108 KB Output is correct
35 Correct 695 ms 43248 KB Output is correct
36 Correct 568 ms 43068 KB Output is correct
37 Correct 430 ms 41176 KB Output is correct
38 Correct 383 ms 38268 KB Output is correct
39 Correct 446 ms 35240 KB Output is correct
40 Correct 399 ms 35140 KB Output is correct
41 Correct 302 ms 35140 KB Output is correct
42 Correct 416 ms 35228 KB Output is correct
43 Correct 406 ms 35372 KB Output is correct
44 Correct 660 ms 44400 KB Output is correct
45 Correct 475 ms 42308 KB Output is correct
46 Correct 298 ms 38364 KB Output is correct
47 Correct 487 ms 39052 KB Output is correct
48 Correct 369 ms 38212 KB Output is correct
49 Correct 304 ms 38124 KB Output is correct
50 Correct 401 ms 41400 KB Output is correct
51 Correct 284 ms 41368 KB Output is correct
52 Correct 486 ms 39584 KB Output is correct
53 Correct 355 ms 38844 KB Output is correct
54 Correct 298 ms 35400 KB Output is correct
55 Correct 428 ms 40516 KB Output is correct
56 Correct 415 ms 42468 KB Output is correct
57 Correct 405 ms 43936 KB Output is correct
58 Correct 556 ms 53172 KB Output is correct
59 Correct 552 ms 53188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6476 KB Output is correct
2 Correct 4 ms 6476 KB Output is correct
3 Correct 4 ms 6596 KB Output is correct
4 Correct 4 ms 6588 KB Output is correct
5 Correct 14 ms 7528 KB Output is correct
6 Correct 13 ms 7500 KB Output is correct
7 Correct 12 ms 7372 KB Output is correct
8 Correct 15 ms 7500 KB Output is correct
9 Correct 14 ms 7508 KB Output is correct
10 Correct 12 ms 7368 KB Output is correct
11 Correct 13 ms 7372 KB Output is correct
12 Correct 13 ms 7312 KB Output is correct
13 Correct 11 ms 7244 KB Output is correct
14 Correct 13 ms 7336 KB Output is correct
15 Correct 13 ms 7244 KB Output is correct
16 Correct 15 ms 7500 KB Output is correct
17 Correct 13 ms 7372 KB Output is correct
18 Correct 10 ms 7368 KB Output is correct
19 Correct 13 ms 7372 KB Output is correct
20 Correct 12 ms 7388 KB Output is correct
21 Correct 11 ms 7372 KB Output is correct
22 Correct 12 ms 7372 KB Output is correct
23 Correct 11 ms 7376 KB Output is correct
24 Correct 14 ms 7372 KB Output is correct
25 Correct 12 ms 7372 KB Output is correct
26 Correct 12 ms 7244 KB Output is correct
27 Correct 13 ms 7372 KB Output is correct
28 Correct 12 ms 7372 KB Output is correct
29 Correct 12 ms 7492 KB Output is correct
30 Correct 14 ms 7756 KB Output is correct
31 Correct 13 ms 7756 KB Output is correct
32 Correct 15 ms 7452 KB Output is correct
33 Correct 741 ms 44648 KB Output is correct
34 Correct 614 ms 43108 KB Output is correct
35 Correct 695 ms 43248 KB Output is correct
36 Correct 568 ms 43068 KB Output is correct
37 Correct 430 ms 41176 KB Output is correct
38 Correct 383 ms 38268 KB Output is correct
39 Correct 446 ms 35240 KB Output is correct
40 Correct 399 ms 35140 KB Output is correct
41 Correct 302 ms 35140 KB Output is correct
42 Correct 416 ms 35228 KB Output is correct
43 Correct 406 ms 35372 KB Output is correct
44 Correct 660 ms 44400 KB Output is correct
45 Correct 475 ms 42308 KB Output is correct
46 Correct 298 ms 38364 KB Output is correct
47 Correct 487 ms 39052 KB Output is correct
48 Correct 369 ms 38212 KB Output is correct
49 Correct 304 ms 38124 KB Output is correct
50 Correct 401 ms 41400 KB Output is correct
51 Correct 284 ms 41368 KB Output is correct
52 Correct 486 ms 39584 KB Output is correct
53 Correct 355 ms 38844 KB Output is correct
54 Correct 298 ms 35400 KB Output is correct
55 Correct 428 ms 40516 KB Output is correct
56 Correct 415 ms 42468 KB Output is correct
57 Correct 405 ms 43936 KB Output is correct
58 Correct 556 ms 53172 KB Output is correct
59 Correct 552 ms 53188 KB Output is correct
60 Incorrect 4 ms 6604 KB Output isn't correct
61 Halted 0 ms 0 KB -