QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#291436 | #7563. Fun on Tree | skc | WA | 886ms | 42296kb | C++14 | 3.5kb | 2023-12-26 16:38:26 | 2023-12-26 16:38:26 |
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*dep[u];
up(ans,o1);
}
ans.c+=depw[tu];
cout<<ans.x<<' '<<ans.c<<'\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 15908kb
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: 17992kb
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: 20128kb
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: 0ms
memory: 17992kb
input:
2 0 1 1 1 3
output:
result:
ok 0 lines
Test #5:
score: -100
Wrong Answer
time: 886ms
memory: 42296kb
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:
120167 15999999996 120167 16999999998 120167 15999999996 120167 15999999996 120167 16999999998 120167 14999999998 120167 15999999998 120167 17999999996 120167 16999999996 120167 12999999996 120167 17999999996 120167 15999999998 120167 13999999998 120167 16999999998 120167 17999999998 120167 15999999...
result:
wrong answer 1st lines differ - expected: '119017 15000000000', found: '120167 15999999996'