QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#553345 | #7563. Fun on Tree | ucup-team1231# | WA | 2454ms | 177636kb | C++17 | 7.0kb | 2024-09-08 12:03:13 | 2024-09-08 12:03:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SZ 234567
template<class T>
void cmax(T&a,T b) {if(a<b)a=b;}
int n,q,fa[SZ];
ll a[SZ],fe[SZ];
int sz[SZ],son[SZ],top[SZ],fc[SZ];
vector<int> fcc[SZ];
ll dd[SZ];
void dfs(int x) {
dd[x]=dd[fa[x]]+fe[x];
sz[x]=1;
for(auto s:fcc[x]) {
dfs(s);
sz[x]+=sz[s];
if(sz[s]>sz[son[x]]) son[x]=s;
}
// cerr<<x<<":"<<son[x]<<"+\n";
}
int D=0,fx[SZ],dw[SZ];
void dfs2(int x,int t) {
top[x]=t; fc[x]=++D; dw[t]=x;
if(son[x]) dfs2(son[x],t);
for(auto s:fcc[x]) if(s!=son[x]) dfs2(s,s);
fx[x]=D;
}
#define pb push_back
#define sz(t) int((t).size())
typedef pair<ll,int> pll;
#define fi first
#define se second
pll operator + (pll a,ll b) {
return pll(a.fi+b,a.se);
}
pll operator - (pll a,ll b) {
return pll(a.fi-b,a.se);
}
#define PS 4400000
pll pool1[PS+PS],*p1=pool1;
ll pool2[PS+PS],*p2=pool2;
struct Seg {
int M=262144;
bool built=0;
vector<pair<int,pll>> es;
int mi=1e9,mx=-1e9,N;
pll*seg;
ll*tg;
// vector<pll> seg;
// vector<ll> tg;
void reup() {
for(int i=M-1;i>=1;--i) seg[i]=max(seg[i+i],seg[i+i+1]);
}
void build() {
if(built) return;
built=1;
N=mx-mi+1;
if(N<=0) N=1;
M=1;
while(M<N+1) M<<=1;
seg=p1; p1+=M+M+1;
assert(p1<pool1+PS+PS);
tg=p2; p2+=M+M+1;
assert(p2<pool2+PS+PS);
// seg.resize(M+M+1);
// tg.resize(M+M+1);
for(int i=0;i<=M+M;++i) seg[i]=pll(-4e18,0);
for(auto e:es) cmax(seg[e.fi-mi+M],e.se);
reup();
}
void clear() {
for(int i=0;i<=M+M;++i) seg[i]=pll(-4e18,0),tg[i]=0;
}
void warmup(int x) {
mx=max(mx,x);
mi=min(mi,x);
}
void edt(int x,pll p) {
assert(!built);
es.pb(make_pair(x,p));
mx=max(mx,x);
mi=min(mi,x);
}
void edt_(int x,pll p) {
assert(built&&x>=mi&&x<=mx);
cmax(seg[x-mi+M],p);
}
pll qry(int x,int l,int r,int ql,int qr) {
if(ql>qr||r<ql||qr<l) return pll(-4e18,0);
if(ql<=l&&r<=qr) {
return seg[x]+tg[x];
}
int m=(l+r)>>1;
pll aa=max(qry(x+x,l,m,ql,qr),qry(x+x+1,m+1,r,ql,qr));
return aa+tg[x];
}
void add(int x,int l,int r,int ql,int qr,ll v,ll s=-1) {
if(ql>qr||r<ql||qr<l) return;
if(ql<=l&&r<=qr) {
tg[x]+=v;
if(s!=-1) seg[x].se=s;
return;
}
int m=(l+r)>>1;
add(x+x,l,m,ql,qr,v,s);
add(x+x+1,m+1,r,ql,qr,v,s);
seg[x]=max(seg[x+x]+tg[x+x],seg[x+x+1]+tg[x+x+1]);
}
pll qry(int l,int r) {
l=l-mi; r=r-mi;
if(l>r) return pll(-4e18,0);
build();
assert(l>=0&&r<N);
return qry(1,0,M-1,l,r);
}
pll qry() {
return qry(mi,mx);
}
void add(int l,int r,ll v,ll s=-1) {
l=l-mi; r=r-mi;
if(l>r) return;
build();
assert(l>=0&&r<N);
add(1,0,M-1,l,r,v,s);
}
}Sub,Top,sA,A[233333];//,B[233333];
pll sAc[SZ];//,sBc[SZ];
pll Asf[SZ];//,Bsf[SZ];
void edt(int x,int v) {
Sub.add(fc[x],fx[x],v);
// cerr<<D<<":"<<n<<" "<<fc[x]<<"~"<<fx[x]<<" "<<Top.mi<<"~"<<Top.mx<<"\n";
Top.add(fc[x],fx[x],v); // add v if your top is in this subtree
if(x!=top[x]) {
sA.add(fc[x],fc[dw[x]],v);
}
for(int t=x;t;t=fa[top[t]]) {
if(t==x) continue;
int f=fc[t];
ll dt;
dt=-sAc[f].fi;
A[f].add(fc[x],fx[x],v);
if(fc[x]<=f&&f<=fx[x])
Asf[f].fi+=v;
dt+=(sAc[f]=max(A[f].qry(),Asf[f])).fi;
sA.add(f,f,dt,sAc[f].se);
}
}
#define ed fx
int xx[SZ],yy[SZ],zz[SZ];
pll ans[SZ];
void edt2(int x,int v) {
// cerr<<D<<":"<<n<<" "<<fc[x]<<"~"<<fx[x]<<" "<<Top.mi<<"~"<<Top.mx<<"\n";
Top.add(fc[x],fx[x],v); // add v if your top is in this subtree
if(x!=top[x]) {
sA.add(fc[x],fc[dw[x]],v);
}
for(int t=x;t;t=fa[top[t]]) {
if(t==x) continue;
int f=fc[t];
ll dt;
dt=-sAc[f].fi;
A[f].add(fc[x],fx[x],v);
if(fc[x]<=f&&f<=fx[x])
Asf[f].fi+=v;
dt+=(sAc[f]=max(A[f].qry(),Asf[f])).fi;
sA.add(f,f,dt,sAc[f].se);
}
}
pll qry(int x) {
pll ans = Sub.qry(fc[x],ed[x])-dd[x];
ll td=0;
for(int t=x;t;t=fa[top[t]]) {
ll chainbuff = Top.qry(fc[top[t]],fc[top[t]]).fi;
int L=fc[top[t]],M=fc[t],R=fc[dw[t]];
// cerr<<t<<"->"<<top[t]<<" "<<fa[top[t]]<<"+\n";
// cerr<<sA.qry(1,1).fi<<" "<<dd[t]-dd[top[t]]<<" "<<chainbuff<<"+\n";
if(L<M) ans=max(ans,sA.qry(L,M-1)+dd[t]-dd[top[t]]+td+chainbuff);
td+=dd[t]-dd[fa[top[t]]];
}
return ans;
}
pll qry2(int x,pll ans) {
ll td=0;
for(int t=x;t;t=fa[top[t]]) {
ll chainbuff = Top.qry(fc[top[t]],fc[top[t]]).fi;
int L=fc[top[t]],M=fc[t],R=fc[dw[t]];
// cerr<<t<<"->"<<top[t]<<" "<<fa[top[t]]<<"+\n";
// cerr<<sA.qry(1,1).fi<<" "<<dd[t]-dd[top[t]]<<" "<<chainbuff<<"+\n";
if(M<R) ans=max(ans,sA.qry(M+1,R)-(dd[t]-dd[top[t]])+td+chainbuff);
td+=dd[t]-dd[fa[top[t]]];
}
return ans;
}
int main() {
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i) scanf("%lld",a+i),a[i]*=-1;
for(int i=2;i<=n;++i)
scanf("%d%lld",fa+i,fe+i),
fcc[fa[i]].pb(i);
dfs(1); dfs2(1,1);
for(int i=1;i<=n;++i) Top.edt(i,pll(0,-i));
for(int i=1;i<=n;++i) {
Sub.edt(fc[i],pll(dd[i]+a[i],-i));
for(int t=i;t;t=fa[top[t]]) {
ll B=dd[i]-dd[t],A=dd[t]-dd[top[t]];
if(i!=t)
::A[fc[t]].warmup(fc[i]);
}
}
for(int i=1;i<=n;++i) A[i].build();
for(int i=1;i<=n;++i) {
for(int t=i;t;t=fa[top[t]]) {
ll B=dd[i]-dd[t],A=dd[t]-dd[top[t]];
if(i!=t)
::A[fc[t]].edt_(fc[i],pll(B-A+a[i],-i));
else
Asf[fc[t]]=pll(B-A+a[i],-i);
}
}
for(int i=1;i<=n;++i) A[i].reup();
for(int i=1;i<=n;++i) {
sA.edt(fc[i],sAc[fc[i]]=max(A[fc[i]].qry(),Asf[fc[i]]));
}
Top.build();
sA.build();
for(int i=1;i<=n;++i) A[i].build();
for(int i=1;i<=q;++i) {
scanf("%d%d%d",xx+i,yy+i,zz+i);
int x=xx[i],y=yy[i],v=zz[i];
// scanf("%d%d%d",&x,&y,&v);
edt(y,-v);
auto q=qry(x);
ans[i]=q;
// printf("%d %lld\n",-q.se,q.fi);
}
Top.clear();
sA.clear();
for(int i=1;i<=n;++i) A[i].clear();
for(int i=1;i<=n;++i) {
for(int t=i;t;t=fa[top[t]]) {
ll B=dd[i]-dd[t],A=dd[t]-dd[top[t]];
if(i!=t)
::A[fc[t]].edt_(fc[i],pll(B+A+a[i],-i));
else
Asf[fc[t]]=pll(B+A+a[i],-i);
}
}
for(int i=1;i<=n;++i) A[i].reup();
for(int i=1;i<=n;++i) {
sA.edt_(fc[i],sAc[fc[i]]=max(A[fc[i]].qry(),Asf[fc[i]]));
}
for(int i=1;i<=n;++i) Top.edt_(i,pll(0,-i));
sA.reup(); Top.reup();
for(int i=1;i<=q;++i) {
int x=xx[i],y=yy[i],v=zz[i];
// scanf("%d%d%d",&x,&y,&v);
edt2(y,-v);
auto q=qry2(x,ans[i]);
printf("%d %lld\n",-q.se,q.fi);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 26520kb
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: 28548kb
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: 4ms
memory: 26420kb
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: 28456kb
input:
2 0 1 1 1 3
output:
result:
ok 0 lines
Test #5:
score: -100
Wrong Answer
time: 2454ms
memory: 177636kb
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:
wrong answer 24978th lines differ - expected: '120167 14000000000', found: '123161 14000000000'