Submission #198121

# Submission time Handle Problem Language Result Execution time Memory
198121 2020-01-24T18:19:48 Z model_code Magic Tree (CEOI19_magictree) C++17
100 / 100
457 ms 43740 KB
#include <bits/stdc++.h>
#define VERBOSE 0
using namespace std;

#define int long long

struct node
{
    bool fruit = false;
    int day,where,weight;
    vector<int> children;
    set<pair<int,int> > harvest;
};

vector<node> tree;

int depth(int v)
{
    int ans = 0;
    for(int c : tree[v].children)
        ans = max(ans,depth(c)+1);
    return ans;
}

// merge solutions in tree[from] and tree[in]
// merge from smaller set into larger
// return index of smaller set
int join_sol(int in,int from)
{
    if(tree[from].harvest.size() > tree[in].harvest.size())
        swap(in,from);

    for(auto it = tree[from].harvest.begin(); it!=tree[from].harvest.end();it++)
    {
        int day = it->first, add = it->second;
        auto inptr = tree[in].harvest.lower_bound({day,-1});
        if(inptr == tree[in].harvest.end() || inptr->first != day)
        {
            tree[in].harvest.insert({day,add});
        }
        else //(inptr->first == day)
        {
            pair<int,int> tmp = {inptr->first, inptr->second};
            tmp.second += add;
            tree[in].harvest.erase(inptr);
            tree[in].harvest.insert(tmp);
        }
    }
    return in;
}

void solve(int v)
{

    tree[v].where = v;
    tree[v].harvest.insert({-1,0});

    for(int c : tree[v].children)
    {
        solve(c);
        tree[v].where = join_sol(tree[v].where,tree[c].where);
    }

    int w = tree[v].where;

    if(tree[v].fruit)
    {
        auto it = tree[w].harvest.lower_bound({tree[v].day,-1});

        if(it == tree[w].harvest.end())
            tree[w].harvest.insert({ tree[v].day, tree[v].weight });
        else if( it->first == tree[v].day)
        {
            pair<int,int> newval = {it->first,it->second+tree[v].weight};
            tree[w].harvest.erase(it);
            it = tree[w].harvest.insert(newval).first;
            
            int debt = tree[v].weight;
            while(debt)
            {
                auto rn = it;
                rn++;
                if(rn == tree[w].harvest.end()) break;
                pair<int,int> tmp = {rn->first,rn->second};
                tree[w].harvest.erase(rn);
                int take = min(debt, tmp.second);
                debt -= take;
                tmp.second -= take;
                if(tmp.second)
                    tree[w].harvest.insert(tmp);
            }
        }
        else
        {
            it = tree[w].harvest.insert({tree[v].day, tree[v].weight}).first;
            int debt = tree[v].weight;
            while(debt)
            {
                auto rn = it;
                rn++;
                if(rn == tree[w].harvest.end()) break;
                pair<int,int> tmp = {rn->first,rn->second};
                tree[w].harvest.erase(rn);
                int take = min(debt, tmp.second);
                debt -= take;
                tmp.second -= take;
                if(tmp.second)
                    tree[w].harvest.insert(tmp);
            }
        }
    }

}

main()
{
    int n,m,k;
    cin >> n >> m >> k;

    tree.resize(n);

    for(int i=1;i<n;++i)
    {
        int parent;
        cin >> parent;
        parent--;
        tree[parent].children.push_back(i);
    }

    for(int i=0;i<m;++i)
    {
        int pos;
        cin >> pos;
        pos--;
        tree[pos].fruit = true;
        cin >> tree[pos].day >> tree[pos].weight;
    }

    solve(0);
    int ans = 0;
    for(auto it = tree[ tree[0].where ].harvest.begin(); it != tree[ tree[0].where ].harvest.end(); it++)
        ans += it->second;
    cout << ans << "\n";

    if(VERBOSE)
    {
        cerr <<  "depth " << depth(0) << "\n";
    }
}

Compilation message

magictree.cpp:115:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 1 ms 376 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 1 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 243 ms 26324 KB Output is correct
2 Correct 170 ms 26040 KB Output is correct
3 Correct 435 ms 43740 KB Output is correct
4 Correct 335 ms 26828 KB Output is correct
5 Correct 449 ms 28408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 632 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 243 ms 29332 KB Output is correct
5 Correct 290 ms 33272 KB Output is correct
6 Correct 249 ms 29296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 294 ms 22392 KB Output is correct
2 Correct 318 ms 21552 KB Output is correct
3 Correct 292 ms 24184 KB Output is correct
4 Correct 221 ms 23148 KB Output is correct
5 Correct 242 ms 29304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 1 ms 376 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 1 ms 376 KB Output is correct
10 Correct 287 ms 25252 KB Output is correct
11 Correct 282 ms 24248 KB Output is correct
12 Correct 262 ms 24312 KB Output is correct
13 Correct 189 ms 23148 KB Output is correct
14 Correct 216 ms 29428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 4216 KB Output is correct
2 Correct 138 ms 18812 KB Output is correct
3 Correct 144 ms 18936 KB Output is correct
4 Correct 138 ms 18828 KB Output is correct
5 Correct 63 ms 17720 KB Output is correct
6 Correct 143 ms 20396 KB Output is correct
7 Correct 114 ms 23160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 1 ms 376 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 1 ms 376 KB Output is correct
10 Correct 4 ms 632 KB Output is correct
11 Correct 4 ms 632 KB Output is correct
12 Correct 2 ms 632 KB Output is correct
13 Correct 243 ms 29332 KB Output is correct
14 Correct 290 ms 33272 KB Output is correct
15 Correct 249 ms 29296 KB Output is correct
16 Correct 287 ms 25252 KB Output is correct
17 Correct 282 ms 24248 KB Output is correct
18 Correct 262 ms 24312 KB Output is correct
19 Correct 189 ms 23148 KB Output is correct
20 Correct 216 ms 29428 KB Output is correct
21 Correct 50 ms 6624 KB Output is correct
22 Correct 242 ms 26616 KB Output is correct
23 Correct 267 ms 29816 KB Output is correct
24 Correct 417 ms 37528 KB Output is correct
25 Correct 286 ms 26928 KB Output is correct
26 Correct 356 ms 27768 KB Output is correct
27 Correct 292 ms 25892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 1 ms 376 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 1 ms 376 KB Output is correct
10 Correct 243 ms 26324 KB Output is correct
11 Correct 170 ms 26040 KB Output is correct
12 Correct 435 ms 43740 KB Output is correct
13 Correct 335 ms 26828 KB Output is correct
14 Correct 449 ms 28408 KB Output is correct
15 Correct 4 ms 632 KB Output is correct
16 Correct 4 ms 632 KB Output is correct
17 Correct 2 ms 632 KB Output is correct
18 Correct 243 ms 29332 KB Output is correct
19 Correct 290 ms 33272 KB Output is correct
20 Correct 249 ms 29296 KB Output is correct
21 Correct 294 ms 22392 KB Output is correct
22 Correct 318 ms 21552 KB Output is correct
23 Correct 292 ms 24184 KB Output is correct
24 Correct 221 ms 23148 KB Output is correct
25 Correct 242 ms 29304 KB Output is correct
26 Correct 287 ms 25252 KB Output is correct
27 Correct 282 ms 24248 KB Output is correct
28 Correct 262 ms 24312 KB Output is correct
29 Correct 189 ms 23148 KB Output is correct
30 Correct 216 ms 29428 KB Output is correct
31 Correct 26 ms 4216 KB Output is correct
32 Correct 138 ms 18812 KB Output is correct
33 Correct 144 ms 18936 KB Output is correct
34 Correct 138 ms 18828 KB Output is correct
35 Correct 63 ms 17720 KB Output is correct
36 Correct 143 ms 20396 KB Output is correct
37 Correct 114 ms 23160 KB Output is correct
38 Correct 50 ms 6624 KB Output is correct
39 Correct 242 ms 26616 KB Output is correct
40 Correct 267 ms 29816 KB Output is correct
41 Correct 417 ms 37528 KB Output is correct
42 Correct 286 ms 26928 KB Output is correct
43 Correct 356 ms 27768 KB Output is correct
44 Correct 292 ms 25892 KB Output is correct
45 Correct 73 ms 6876 KB Output is correct
46 Correct 281 ms 27268 KB Output is correct
47 Correct 308 ms 30588 KB Output is correct
48 Correct 457 ms 39928 KB Output is correct
49 Correct 334 ms 26960 KB Output is correct
50 Correct 454 ms 28024 KB Output is correct
51 Correct 369 ms 26072 KB Output is correct