QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#216475 | #7578. Salty Fish | SolitaryDream# | WA | 1362ms | 115728kb | C++17 | 4.4kb | 2023-10-15 18:52:43 | 2023-10-15 18:52:43 |
Judging History
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;
#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,int 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 x) {
if(L==R) return mx[p];
down(p);
int mid=L+R>>1;
if(x<=mid) return query(lson,x);
else return query(rson,x);
}
int Find(int L,int R,int p,int l,int r,ll v) {
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,l,mid,v);
else return Find(lson,mid+1,r,v);
} else {
if(r<=mid) return Find(lson,l,r,v);
else if(l>mid) return Find(rson,l,r,v);
else {
int p=Find(rson,mid+1,r,v);
if(p) return p;
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));
}
}
// cerr << x << endl;
modify(1,tid,1,pos[x]-1,pos[x]-1,query(1,tid,1,pos[x])+a[x]);
auto &v=G[x];
if(!v.size()) return ;
// cerr << x << endl;
v.push_back({-1,0});
for(auto &[d,c]: v) d=min(d,mxd[x]);
sort(v.begin(),v.end());
ll e=0,w=0;
DOR(i,v.size()-1,1) {
// cerr << tid << ' ' << v[i].first << endl;
// cerr << " -- " << endl;
w=max(w,e+query(1,tid,1,pos[x]+v[i].first));
// cerr << " --- " << endl;
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];
int p=Find(1,tid,1,l,r,w+e);
// cerr << p << endl;
if(!p) {
// cerr << l << ' ' << r << endl;
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);
}
// cerr << " --- " << endl;
}
}
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);
build(1,tid,1);
sol(1);
cout << query(1,tid,1,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: 42632kb
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
Wrong Answer
time: 1362ms
memory: 115728kb
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:
28846 35492 9498 41711 3150 40301 38026 35200 9387 31437 37384 43842 20979 10951 26874 15733 40019 10724 33729 38316 45820 33869 40833 30974 21383 37214 30664 38998 31864 32872 25463 42702 40940 11931 12439 34063 31600 40586 29385 16287 23277 14989 2337 35393 34367 19402 37662 46138 23528 38987 2440...
result:
wrong answer 1st numbers differ - expected: '20180', found: '28846'