QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#216586#7578. Salty FishSolitaryDream#AC ✓1528ms134204kbC++174.6kb2023-10-15 20:12:582023-10-15 20:12:58

Judging History

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

  • [2023-10-15 20:12:58]
  • 评测
  • 测评结果:AC
  • 用时:1528ms
  • 内存:134204kb
  • [2023-10-15 20:12:58]
  • 提交

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;
#define int long long 
const int N=6e5+50;
#define ls (p<<1)
#define rs (p<<1|1)
#define lson L,mid,ls
#define rson mid+1,R,rs
ll add[N<<2],col[N<<2],mx[N<<2];
void build(int L,int R,int p) {
    add[p]=mx[p]=0;
    col[p]=-1;
    if(L==R) return ;
    int mid=L+R>>1;
    build(lson);
    build(rson);
}
void upd(int p,ll c) {
    mx[p]=col[p]=c;
    add[p]=0;
}
void down(int p) {
    if(col[p]==-1 && !add[p]) return ;
    if(col[p]!=-1) {
        upd(ls,col[p]+add[p]);
        upd(rs,col[p]+add[p]);
    } else {
        mx[ls]+=add[p];
        add[ls]+=add[p];
        mx[rs]+=add[p];
        add[rs]+=add[p];
    }
    col[p]=-1;
    add[p]=0;
}
void up(int p) {
    mx[p]=max(mx[ls],mx[rs]);
}
void update(int L,int R,int p,int l,int r,ll v) {
    if(L==l && R==r) {
        add[p]+=v;
        mx[p]+=v;
        return ;
    }
    down(p);
    int mid=L+R>>1;
    if(r<=mid) update(lson,l,r,v);
    else if(l>mid) update(rson,l,r,v);
    else update(lson,l,mid,v),update(rson,mid+1,r,v);
    up(p);
}
void modify(int L,int R,int p,int l,int r,ll v) {
    if(L==l && R==r) {
        upd(p,v);
        return ;
    }
    down(p);
    int mid=L+R>>1;
    if(r<=mid) modify(lson,l,r,v);
    else if(l>mid) modify(rson,l,r,v);
    else modify(lson,l,mid,v),modify(rson,mid+1,r,v);
    up(p);
}
ll query(int L,int R,int p,int l,int r) {
    if(L==l && R==r) return mx[p];
    down(p);
    int mid=L+R>>1;
    if(r<=mid) return query(lson,l,r);
    else if(l>mid) return query(rson,l,r);
    else return max(query(lson,l,mid),query(rson,mid+1,r));
}
int Find(int L,int R,int p,int l,int r,ll v) {
    // if(v==251870270)
    //     cout<<"GG"<<endl;
    if(mx[p]<v) return 0;
    if(L==R) return L;
    down(p);
    int mid=L+R>>1;
    if(L==l && R==r) {
        if(mx[rs]>=v) return Find(rson,mid+1,r,v);
        else return Find(lson,l,mid,v);
    } else {
        if(r<=mid) return Find(lson,l,r,v);
        else if(l>mid) return Find(rson,l,r,v);
        else {
            int o=Find(rson,mid+1,r,v);
            if(o) return o;
            else return Find(lson,l,mid,v);
        }
    }
}
vector<int> E[N];
vector<pair<int,int>> G[N];
int a[N];
int dep[N],mxd[N];
int son[N];
int n,m,tid;
int pos[N];
void dfs(int x,int fr) {
    dep[x]=dep[fr]+1;
    mxd[x]=0;
    son[x]=0;
    for(auto y: E[x]) {
        dfs(y,x);
        if(mxd[y]+1>mxd[x]) {
            mxd[x]=mxd[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);
    // cerr << x << endl;
    for(auto y: E[x]) if(y!=son[x]) {
        FOR(i,-1,mxd[y]) {
            update(1,tid,1,pos[x]+1+i,pos[x]+1+i,query(1,tid,1,pos[y]+i,pos[y]+i));
        }
    }
    // cerr << x << endl;
    modify(1,tid,1,pos[x]-1,pos[x]-1,query(1,tid,1,pos[x],pos[x])+a[x]);

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

    auto o=v;
    ll e=0,w=0;
    DOR(i,v.size()-1,1) {
        w=o[i].second=max(w,query(1,tid,1,pos[x]+v[i].first,pos[x]+v[i].first)-e);
        e+=v[i].second;
    }
    e=0,w=0;
    DOR(i,v.size()-1,1) {
        w=o[i].second;
        e+=v[i].second;
        int l=v[i-1].first,r=v[i].first-1;
        if(l>r) continue;
        l+=pos[x];
        r+=pos[x];
        // cerr << tid << ' ' << r+1 << ' ' << pos[x]+mxd[x] << endl;
        w=max(w,query(1,tid,1,r+1,pos[x]+mxd[x]));
        // cerr << " -- " << endl;
        int p=Find(1,tid,1,l,r,w+e);
        if(!p) {
            modify(1,tid,1,l,r,w);
        } else {
            if(p<r) modify(1,tid,1,p+1,r,w);
            update(1,tid,1,l,p,-e);
        }
    }
}
int T;
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});
    }
    // if(T!=8) return ;

    dfs(1,0);
    tid=0;
    ++tid;
    rdfs(1);

    build(1,tid,1);
    sol(1);
    cout << query(1,tid,1,pos[1]-1,pos[1]-1) << '\n';
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    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: 44692kb

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: 0
Accepted
time: 1528ms
memory: 134204kb

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:

20180
17083
14650
19924
15814
20189
20894
18175
18478
13758
20217
23680
15562
12882
18046
18132
20548
17000
23734
18740
24814
16728
20979
19727
16450
21717
15739
22081
17803
23024
14820
21503
23497
15804
18097
17197
21236
21286
14250
11632
18335
12317
16313
22840
18583
15245
19331
25978
22388
17091
...

result:

ok 1003 numbers