제출 #417033

#제출 시각아이디문제언어결과실행 시간메모리
417033tqbfjotldWorst Reporter 4 (JOI21_worst_reporter4)C++14
79 / 100
373 ms62824 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

int h[200005];
int c[200005];
vector<int> adjl[200005];

///pos, increase
set<pair<int,int> > func(int node){
    set<pair<int,int> > res1;
    for (auto x : adjl[node]){
        auto res = func(x);
        if (res.size()>res1.size()) swap(res,res1);
        for (auto y : res){
            auto it = res1.lower_bound({y.first,-1});
            if (it!=res1.end() && (*it).first==y.first){
                int t = (*it).second;
                res1.erase(it);
                res1.insert({y.first,y.second+t});
            }
            else{
                res1.insert({y.first,y.second});
            }
        }
    }
    pair<int,int> to_insert = {h[node]+1,c[node]};
    int cursum = 0;
    auto it = res1.lower_bound({h[node]+1,-1});
    if (it!=res1.end() && (*it).first==to_insert.first){
        int t = (*it).second;
        res1.erase(it);
        res1.insert({to_insert.first,to_insert.second+t});
    }
    else{
        res1.insert(to_insert);
    }
    it = res1.lower_bound(to_insert);
    bool done = false;
    while (it!=res1.begin()){
        it--;
        cursum += (*it).second;
        int curpos = (*it).first;
        it = res1.erase(it);
        if (cursum >= c[node]){
            int rem = cursum-c[node];
            if (rem!=0){
                res1.insert({curpos,rem});
            }
            done = true;
            break;
        }
    }
    if (!done){
        if (cursum!=0){
            if ((!res1.empty()) && (*res1.begin()).first==0){
                cursum += (*res1.begin()).second;
                res1.erase(res1.begin());
                res1.insert({0,cursum});
            }
            else
                res1.insert({0,cursum});
        }
    }
    else{
        if ((!res1.empty()) && (*res1.begin()).first==0){
            int t = (*res1.begin()).second;
            res1.erase(res1.begin());
            res1.insert({0,c[node]+t});
        }
        else
            res1.insert({0,c[node]});
    }
    /*printf("node %lld, ret ",node);
    for (auto x : res1){
        printf("(%lld,%lld) ",x);
    }
    printf("\n");*/
    return res1;
}

main(){
    int n;
    scanf("%lld",&n);
    for (int x = 1; x<=n; x++){
        int t;
        scanf("%lld%lld%lld",&t,&h[x],&c[x]);
        if (x!=1){
            adjl[t].push_back(x);
        }
    }
    auto res = func(1);
    if (res.empty()){
        printf("0");
    }
    else if ((*res.begin()).first!=0){
        printf("0");
    }
    else{
        printf("%lld",(*res.begin()).second);
    }
}

컴파일 시 표준 에러 (stderr) 메시지

worst_reporter2.cpp:82:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   82 | main(){
      | ^~~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |     scanf("%lld",&n);
      |     ~~~~~^~~~~~~~~~~
worst_reporter2.cpp:87:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         scanf("%lld%lld%lld",&t,&h[x],&c[x]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...