QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#216555#7578. Salty FishSolitaryDream#RE 0ms39736kbC++172.0kb2023-10-15 19:47:292023-10-15 19:47:29

Judging History

你现在查看的是最新测评结果

  • [2023-10-15 19:47:29]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:39736kb
  • [2023-10-15 19:47:29]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,t) for(int i=(s),_t=(t); i<=_t; ++i)
#define DOR(i,s,t) for(int i=(s),_t=(t); i>=_t; --i)
typedef long long ll;
const int N=6e5+50;

vector<int> E[N];
vector<pair<int,int>> G[N];
int a[N];
int dep[N],mx[N];
int son[N];
int n,m,tid;
int pos[N];
ll f[N];
void dfs(int x,int fr) {
    dep[x]=dep[fr]+1;
    mx[x]=0;
    son[x]=0;
    for(auto y: E[x]) {
        dfs(y,x);
        if(mx[y]+1>mx[x]) {
            mx[x]=mx[y]+1;
            son[x]=y;
        }
    }
}
void rdfs(int x) {
    pos[x]=++tid;
    if(son[x]) rdfs(son[x]);
    for(auto y: E[x]) if(y!=son[x]) {
        ++tid;
        rdfs(y);
    }
}
void sol(int x) {
    for(auto y: E[x]) sol(y);
    for(auto y: E[x]) if(y!=son[x]) {
        FOR(i,-1,mx[y]) {
            f[pos[x]+1+i]+=f[pos[y]+i];
        }
    }
    f[pos[x]-1]=f[pos[x]]+a[x];

    auto &v=G[x];
    v.push_back({-1,0});
    for(auto &[d,c]: v) d=min(d,mx[x]);
    sort(v.begin(),v.end());

    if(!v.size()) return ;

    ll e=0,w=0;
    DOR(i,v.size()-1,1) {
        w=max(w,-e+f[pos[x]+v[i].first]);
        e+=v[i].second;
        FOR(j,v[i-1].first,v[i].first-2) {
            if(f[pos[x]+j]<f[pos[x]+j+1]) {
                cerr << "Failed !!" << endl;
                assert(0);
            }
        }
        FOR(j,v[i-1].first,v[i].first-1) {
            f[pos[x]+j]=max(f[pos[x]+j]-e,w);
        }
    }
}
void solve() {
    cin >> n >> m;
    FOR(i,1,n) E[i].clear(),G[i].clear();
    FOR(i,2,n) {
        int x;
        cin >> x;
        E[x].push_back(i);
    }
    FOR(i,1,n) cin >> a[i];
    FOR(i,1,m) {
        int x,d,c;
        cin >> x >> d >> c;
        G[x].push_back({d,c});
    }
    dfs(1,0);
    tid=0;
    ++tid;
    rdfs(1);
    FOR(i,1,tid) f[i]=0;
    sol(1);
    cout << f[pos[1]-1] << '\n';
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while(T--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 39736kb

input:

1
6 3
1 1 2 2 3
2 5 4 3 3 2
2 1 3
3 1 7
1 2 4

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: -100
Runtime Error

input:

1003
100 100
1 2 3 4 5 6 7 8 9 10 6 1 2 4 1 11 17 14 17 2 13 8 8 5 11 7 18 6 2 10 23 11 13 3 9 1 33 20 3 9 32 35 11 41 42 29 33 45 21 35 9 36 12 54 19 24 57 31 32 5 3 10 46 15 46 48 20 44 5 41 67 7 18 30 27 6 29 69 57 75 62 74 18 64 17 21 38 60 79 69 54 90 83 83 31 96 31 93 53
152 498 653 559 287 38...

output:

19856

result: