QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#291437 | #7563. Fun on Tree | skc | WA | 1148ms | 42328kb | C++14 | 3.5kb | 2023-12-26 16:44:44 | 2023-12-26 16:44:45 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
constexpr int N=200000,sN=1<<19;
typedef long long ll;
inline void up(ll &x,ll y){if(y>x) x=y;}
int n,a[N+10],fa[N+10],fw[N+10],dep[N+10],sz[N+10],h[N+10],dfn[N+10],clk,dfninv[N+10],c[N+10];
ll depw[N+10];
struct _{
int x;
ll c;
bool operator<(const _ &ob)const{
if(c!=ob.c) return c<ob.c;
return x>ob.x;
}
};
inline void up(_ &x,_ y){if(x<y) x=y;}
struct EDGE{
int to,next;
}e[N+10];
int head[N+10],etop;
void adde(int u,int v){
e[etop].to=v;
e[etop].next=head[u];
head[u]=etop++;
}
void dfs(int x){
sz[x]=1;
int i;
for(i=head[x];~i;i=e[i].next){
dep[e[i].to]=dep[x]+1;
depw[e[i].to]=depw[x]+fw[e[i].to];
dfs(e[i].to);
sz[x]+=sz[e[i].to];
if(sz[e[i].to]>sz[h[x]]) h[x]=e[i].to;
}
}
void dfs1(int x){
dfn[x]=++clk;
if(!h[x]) return;
c[h[x]]=c[x],dfs1(h[x]);
int i;
for(i=head[x];~i;i=e[i].next){
if(e[i].to==h[x]) continue;
c[e[i].to]=e[i].to;
dfs1(e[i].to);
}
}
struct seg{
_ max_[sN+10];
ll tag[sN+10];
void pushup(int rt){max_[rt]=max(max_[rt<<1],max_[rt<<1|1]);}
template<typename Tp>
void build(int l,int r,int rt,Tp f){
if(l==r) return max_[rt]=f(dfninv[l]),void();
int m=l+r>>1;
build(l,m,rt<<1,f);
build(m+1,r,rt<<1|1,f);
pushup(rt);
}
void puttag(int rt,ll c){max_[rt].c+=c,tag[rt]+=c;}
void pushdown(int rt){
if(tag[rt]){
puttag(rt<<1,tag[rt]);
puttag(rt<<1|1,tag[rt]);
tag[rt]=0;
}
}
void update(int pos,_ c,int l,int r,int rt){
if(l==r) return max_[rt]=c,void();
int m=l+r>>1;
pushdown(rt);
if(pos<=m) update(pos,c,l,m,rt<<1);
else update(pos,c,m+1,r,rt<<1|1);
pushup(rt);
}
void update(int L,int R,ll c,int l,int r,int rt){
if(L<=l&&r<=R) return puttag(rt,c);
int m=l+r>>1;
pushdown(rt);
if(m>=L) update(L,R,c,l,m,rt<<1);
if(m<R) update(L,R,c,m+1,r,rt<<1|1);
pushup(rt);
}
_ query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R) return max_[rt];
int m=l+r>>1;
pushdown(rt);
_ rv={0,(ll)-1e18};
if(m>=L) up(rv,query(L,R,l,m,rt<<1));
if(m<R) up(rv,query(L,R,m+1,r,rt<<1|1));
return rv;
}
}s1,s2;
_ t[N+10];
_ getval1(int x){
int L,R=dfn[x]+sz[x]-1;
if(h[x]) L=dfn[x]+sz[h[x]]+1;
else L=dfn[x]+1;
_ tmp={0,(ll)-1e18};
if(L<=R) tmp=s1.query(L,R,1,n,1);
up(tmp,{x,depw[x]-a[x]});
tmp.c-=2*depw[x];
return tmp;
}
int main(){
memset(head,255,sizeof(head));
ios::sync_with_stdio(0);
cin.tie(0);
int q;
cin>>n>>q;
int i;
for(i=1;i<=n;++i) cin>>a[i];
for(i=2;i<=n;++i){
cin>>fa[i]>>fw[i];
adde(fa[i],i);
}
dep[1]=1,dfs(1);
c[1]=1,dfs1(1);
for(i=1;i<=n;++i) dfninv[dfn[i]]=i;
s1.build(1,n,1,[&](int x){return (_){x,depw[x]-a[x]};});
for(i=1;i<=n;++i) t[i]=getval1(i);
s2.build(1,n,1,[&](int x){return t[x];});
for(i=1;i<=q;++i){
int u,v,c;
cin>>u>>v>>c;
s1.update(dfn[v],dfn[v]+sz[v]-1,-c,1,n,1);
s2.update(dfn[v],dfn[v]+sz[v]-1,-c,1,n,1);
while(1){
v=::c[v];
if(v==1) break;
v=fa[v];
_ o=getval1(v);
s2.update(dfn[v],o,1,n,1);
}
int tu=u;
_ ans=s1.query(dfn[u],dfn[u]+sz[u]-1,1,n,1);
ans.c-=2*depw[u];
while(1){
int t=::c[u];
if(t!=u){
_ o=s2.query(dfn[t],dfn[u]-1,1,n,1);
up(ans,o);
}
if(t==1) break;
u=fa[t];
int L=dfn[u],R=dfn[u]+sz[u]-1;
int L1=dfn[t],R1=dfn[t]+sz[t]-1;
_ o1=s1.query(L,L1-1,1,n,1);
_ o2={0,(ll)-1e18};
if(R1<R) o2=s1.query(R1+1,R,1,n,1);
up(o1,o2);
o1.c-=2*depw[u];
up(ans,o1);
}
ans.c+=depw[tu];
cout<<ans.x<<' '<<ans.c<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 15968kb
input:
6 6 1 1 4 5 1 4 1 5 2 0 3 2 4 1 5 6 3 2 -100000 1 2 100000 1 1 0 2 2 66 3 1 5 4 4 -3
output:
6 100005 6 10 6 10 1 4 1 -1 1 1
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 18004kb
input:
5 6 -10 0 2 -4 8 1 7 1 1 2 2 2 -2 1 1 100 2 1 -100 1 1 0 4 3 10 2 5 3 5 2 2
output:
4 -87 1 17 4 13 1 19 1 17 1 15
result:
ok 6 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 19980kb
input:
6 3 0 0 0 0 0 0 1 10 1 10 1 -100 4 10 4 11 1 1 0 4 1 0 1 4 1000
output:
2 10 6 11 2 10
result:
ok 3 lines
Test #4:
score: 0
Accepted
time: 2ms
memory: 16016kb
input:
2 0 1 1 1 3
output:
result:
ok 0 lines
Test #5:
score: 0
Accepted
time: 844ms
memory: 42248kb
input:
200000 200000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...
output:
119017 15000000000 120167 17000000000 119017 15000000000 119017 15000000000 120167 17000000000 120167 15000000000 120167 16000000000 119017 17000000000 119017 16000000000 119017 12000000000 119017 17000000000 120167 16000000000 120167 14000000000 120167 17000000000 120167 18000000000 120167 16000000...
result:
ok 200000 lines
Test #6:
score: 0
Accepted
time: 1148ms
memory: 42236kb
input:
200000 200000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...
output:
169355 88000000000 171273 95000000000 171273 100000000000 169355 88000000000 169355 93000000000 169355 97000000000 169355 93000000000 171273 78000000000 171273 86000000000 169355 90000000000 169355 84000000000 169355 80000000000 169355 78000000000 171273 84000000000 169355 89000000000 171273 8400000...
result:
ok 200000 lines
Test #7:
score: 0
Accepted
time: 814ms
memory: 42260kb
input:
200000 200000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...
output:
60406 17000000000 163359 19000000000 163359 18000000000 163359 17000000000 163359 18000000000 60406 17000000000 163359 16000000000 60406 16000000000 60406 18000000000 163359 17000000000 163359 16000000000 163359 15000000000 163359 18000000000 163359 16000000000 60406 13000000000 163359 15000000000 6...
result:
ok 200000 lines
Test #8:
score: 0
Accepted
time: 1073ms
memory: 42236kb
input:
200000 200000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...
output:
158239 95000000000 170228 90000000000 170228 91000000000 158239 97000000000 158239 90000000000 170228 97000000000 170228 95000000000 158239 89000000000 170228 84000000000 158239 97000000000 158239 88000000000 170228 81000000000 158239 89000000000 170228 84000000000 170228 84000000000 158239 83000000...
result:
ok 200000 lines
Test #9:
score: -100
Wrong Answer
time: 829ms
memory: 42328kb
input:
200000 200000 -999999197 -999999547 -999999355 -999999799 -999999360 -999999720 -999999365 -999999419 -999999958 -999999574 -999999169 -999999422 -999999695 -999999971 -999999820 -999999822 -999999107 -999999420 -999999519 -999999970 -999999601 -999999521 -999999134 -999999638 -999999338 -999999570 ...
output:
2841 999999254 109445 999999369 58340 999999315 117049 999999782 118472 999999076 24148 999999131 47844 999999290 113056 999999668 46680 999999228 109091 999999634 68365 999999666 156304 999999674 39689 999999729 103343 999999322 123506 999999661 189258 999999210 142816 999999890 88985 999999626 120...
result:
wrong answer 3691st lines differ - expected: '110392 -1000000095', found: '441 -999999112'