QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#216549 | #7578. Salty Fish | SolitaryDream# | WA | 1604ms | 134260kb | C++17 | 4.6kb | 2023-10-15 19:44:22 | 2023-10-15 19:44:22 |
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;
#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 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(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));
}
}
// 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];
v.push_back({-1,0});
for(auto &[d,c]: v) d=min(d,mxd[x]);
sort(v.begin(),v.end());
if(!v.size()) return ;
ll e=0,w=0;
DOR(i,v.size()-1,1) {
// cerr << tid << ' ' << v[i].first << endl;
// cerr << " -- " << endl;
w=max(w,query(1,tid,1,pos[x]+v[i].first)-e);
// 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];
// FOR(j,l+1,r)
// assert(query(1,tid,1,j)<=query(1,tid,1,j-1));
// FOR(j,l,r) {
// modify(1,tid,1,j,j,max(query(1,tid,1,j)-e,w));
// }
int p=Find(1,tid,1,l,r,w+e);
if(!p) {
modify(1,tid,1,l,r,w);
} else {
// assert(query(1,tid,1,p)>=w+e);
// if(p+1<=r)
// assert(query(1,tid,1,p+1)<w+e);
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) << '\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: 7ms
memory: 44432kb
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: 1604ms
memory: 134260kb
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 16042 13746 19020 15666 19980 20779 17810 17699 12649 20100 23205 15486 12705 17586 17062 20333 16850 23416 18139 24214 16247 20967 19118 16291 21717 15722 21948 17362 22820 14603 21002 22629 15645 18097 16947 21236 21087 12993 10952 18209 12219 15477 22502 18583 15167 18621 25978 22388 16985 ...
result:
wrong answer 1st numbers differ - expected: '20180', found: '19856'