Submission #242391

#TimeUsernameProblemLanguageResultExecution timeMemory
242391tqbfjotldMagic Tree (CEOI19_magictree)C++14
100 / 100
191 ms29048 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
bool fruit[100005];
int day[100005];
int val[100005];
vector<int> adjl[100005];
int n,m,k;

map<int,int> func(int node){
    map<int,int> ans;
    if (adjl[node].size()>=1){
        ans = func(adjl[node][0]);
    }
    for (int x = 1; x<adjl[node].size(); x++){
        int ch = adjl[node][x];
        auto res = func(ch);
        if (res.size()>ans.size()){
            swap(res,ans);
        }
        for (auto item : res){
            ans[item.first] += item.second;
        }
    }
    if (fruit[node]){
        ans[day[node]] += val[node];
        int t = val[node];
        auto it = ans.upper_bound(day[node]);
        while (t>0&& it!=ans.end()){
            if ((*it).second<=t){
                t -= (*it).second;
                it = ans.erase(it);
            }
            else{
                ans[(*it).first] -= t;
                break;
            }
        }
    }
    return ans;
}

main(){
    scanf("%lld%lld%lld",&n,&m,&k);
    for (int x = 1; x<n; x++){
        int t;
        scanf("%lld",&t);
        t--;
        adjl[t].push_back(x);
    }
    for (int x = 0; x<m; x++){
        int a,b;
        long long c;
        scanf("%lld%lld%lld",&a,&b,&c);
        a--;
        fruit[a] = true;
        day[a] = b;
        val[a] = c;
    }
    auto res = func(0);
    long long ans = 0;
    for (auto x : res){
        ans += x.second;
    }
    printf("%lld",ans);
}

Compilation message (stderr)

magictree.cpp: In function 'std::map<long long int, long long int> func(long long int)':
magictree.cpp:15:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int x = 1; x<adjl[node].size(); x++){
                     ~^~~~~~~~~~~~~~~~~~
magictree.cpp: At global scope:
magictree.cpp:43:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
magictree.cpp: In function 'int main()':
magictree.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld%lld%lld",&n,&m,&k);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",&t);
         ~~~~~^~~~~~~~~~~
magictree.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld%lld%lld",&a,&b,&c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...