QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#581336 | #9373. Query on Tree | PlentyOfPenalty | WA | 75ms | 44756kb | C++20 | 4.4kb | 2024-09-22 12:06:52 | 2024-09-22 12:06:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+10;
const ll INF=1e18,FL=-1e17;
const int D=10;
struct node{
ll tg,mx,ad;
}tb[N<<2],td[N<<2];
int T,n,Q,u,v,op,K,sz[N];
int nw,pb[N],pd[N],b[N],d[N];
int f[D+5][N],l[D+5][N],r[D+5][N];
ll a[N],im[N],tmp;
vector<int>e[N];
void dfs(int x,int fa){
f[1][x]=fa;
sz[x]=1;
pd[x]=++nw;
d[nw]=x;
for(int i:e[x])if(i!=fa){
dfs(i,x);
}
}
queue<int>q;
void bfs(){
q.push(1);
while(!q.empty()){
u=q.front(),q.pop();
pb[u]=++nw;
b[nw]=u;
l[0][u]=r[0][u]=nw;
for(int i:e[u])if(i!=f[1][u])q.push(i);
}
}
void Upd(node*t,int x){
t[x].mx=max(t[x<<1].mx,t[x<<1|1].mx);
}
#define add(p,w) (t[p].tg+=w,t[p].mx+=w)
void Psd(node*t,int x){
if(t[x].tg){
add(x<<1,t[x].tg),add(x<<1|1,t[x].tg);
t[x].tg=0;
}
}
void Build(node*t,int x,int l,int r,int fl){
if(l==r){
if(fl){
t[x].mx=t[x].ad=im[d[l]];
t[x].tg=0;
}else{
t[x].ad=0;
t[x].mx=t[x].tg=a[b[l]];
}
return;
}
int mid=(l+r>>1);
Build(t,x<<1,l,mid,fl),Build(t,x<<1|1,mid+1,r,fl);
Upd(t,x);
}
void Ins(node*t,int x,int l,int r,int ql,int qr,ll v){
if(ql>qr||qr<l||r<ql)return;
if(ql<=l&&r<=qr)return(void)(add(x,v));
Psd(t,x);
int mid=(l+r>>1);
Ins(t,x<<1,l,mid,ql,qr,v),Ins(t,x<<1|1,mid+1,r,ql,qr,v);
Upd(t,x);
}
void Mod(node*t,int x,int l,int r,int id,ll v){
if(l==r)return(void)(t[x].ad=v,t[x].mx=t[x].ad+t[x].tg);
int mid=(l+r>>1);
Psd(t,x);
if(id<=mid)Mod(t,x<<1,l,mid,id,v);
else Mod(t,x<<1|1,mid+1,r,id,v);
Upd(t,x);
}
ll Que(node*t,int x,int l,int r,int ql,int qr){
if(ql>qr||qr<l||r<ql)return -INF;
if(ql<=l&&r<=qr)return t[x].mx;
Psd(t,x);
int mid=(l+r>>1);
return max(Que(t,x<<1,l,mid,ql,qr),Que(t,x<<1|1,mid+1,r,ql,qr));
}
ll Get(node*t,int x,int l,int r,int id){
if(!id)return 0;
if(l==r)return t[x].tg;
int mid=(l+r>>1);
Psd(t,x);
if(id<=mid)return Get(t,x<<1,l,mid,id);
return Get(t,x<<1|1,mid+1,r,id);
}
void Upd_node(int x){
if(!x)return;
Mod(td,1,1,n,pd[x],Que(tb,1,1,n,l[D][x],r[D][x]));
}
ll Upd_int(int l,int r,ll v){
if(l>r)return -INF;
int fa=f[D][b[l]];
Ins(tb,1,1,n,l,r,v);
Upd_node(fa);
return Get(td,1,1,n,pd[fa])+Que(tb,1,1,n,l,r);
}
ll Upd_dis(int x,int d,int dl,int dr,ll v){
int tl=l[d][x],tr=r[d][x];
ll ret=-INF;
if(dr<tl||tr<dl){
ret=max(ret,Upd_int(tl,tr,v));
}else{
ret=max(ret,Upd_int(tl,dl-1,v));
ret=max(ret,Upd_int(dr+1,tr,v));
}
if(d>1)return max(ret,Upd_dis(f[1][x],d-1,l[d-2][x],r[d-2][x],v));
else if(d)return max(ret,Upd_dis(f[1][x],d-1,-1,-1,v));
return ret;
}
ll Upd_dis(int x,int d,ll v){
if(d<0)return -INF;
ll ret=-INF;
ret=max(ret,Upd_int(l[d][x],r[d][x],v));
if(!f[1][x]){
for(int i=d-1;i>=0;--i){
ret=max(ret,Upd_int(l[i][x],r[i][x],v));
}
return ret;
}else if(d){
ret=max(ret,Upd_int(l[d-1][x],r[d-1][x],v));
ret=max(ret,Upd_dis(f[1][x],d-1,v));
}
return ret;
}
ll Upd_sub(int x,ll v){
ll ret=-INF;
for(int i=0;i<=D;++i){
ret=max(ret,Upd_int(l[i][x],r[i][x],v));
}
Ins(td,1,1,n,pd[x],pd[x]+sz[x]-1,v);
ret=max(ret,Que(td,1,1,n,pd[x],pd[x]+sz[x]-1));
return ret;
}
int main(){
cin.sync_with_stdio(0),cin.tie(0);
cin>>T;
while(T--){
cin>>n>>Q;
for(int i=1;i<=n;++i){
vector<int>().swap(e[i]);
im[i]=-INF;
for(int j=1;j<=D;++j){
f[j][i]=0;
l[j][i]=n+1,r[j][i]=0;
}
}
for(int i=1;i<=n;++i)cin>>a[i];
for(int i=1;i<n;++i){
cin>>u>>v;
e[u].push_back(v),e[v].push_back(u);
}
nw=0,dfs(1,0);
nw=0,bfs();
for(int i=1;i<=n;++i){
u=b[i];
f[0][u]=u;
for(int j=2;j<=D;++j)f[j][u]=f[j-1][f[1][u]];
for(int j=1;j<=D;++j){
v=f[j][u];
if(!v)break;
l[j][v]=min(l[j][v],i);
r[j][v]=max(r[j][v],i);
}
if(v=f[D][u])im[v]=max(im[v],a[u]);
}
Build(tb,1,1,n,0);
Build(td,1,1,n,1);
while(Q--){
cin>>op;
if(op==1){
cin>>u>>K>>v;
tmp=Upd_dis(u,K,0,0,v);
}else if(op==2){
cin>>u>>K>>v;
tmp=Upd_dis(u,K,v);
}else{
cin>>u>>v;
tmp=Upd_sub(u,v);
}
if(tmp<FL)cout<<"GG\n";
else cout<<tmp<<"\n";
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 44584kb
input:
1 5 5 1 2 1 3 2 1 2 2 3 2 4 4 5 2 2 1 0 1 2 1 3 3 4 -5 2 5 2 3 3 2 -1
output:
3 6 1 5 4
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 65ms
memory: 44756kb
input:
10000 3 9 42973045452542 34994498886390 -91733395797555 1 3 1 2 1 1 5 -71952967 3 1 -816873082 1 1 5 -842437577 2 3 7 254550528 3 3 -854082700 2 3 2 699808309 3 3 554885567 1 2 7 595565507 1 3 0 -318374053 3 2 -63158159333100 77264362049163 -99188430131116 1 2 3 2 2 2 4 -305866230 3 1 -549738403 3 5...
output:
GG 42972228579460 GG 42972483129988 -91734812202809 42973182938297 -91733557508933 GG -91733875882986 77264056182933 77263506444530 7065382376488 7065749360235 7066534912965 -85115611272570 -85114714781312 96492412785032 -20428913156111 -20428197540063 96491742171666 -14945310996805 96491180203461 -...
result:
ok 200000 lines
Test #3:
score: -100
Wrong Answer
time: 75ms
memory: 42744kb
input:
10000 4 32 -1057044448491 -93293078803919 -24212938548357 74427756948193 1 3 1 2 3 4 3 1 -82365883 1 2 9 -730670945 2 4 2 -618745828 2 1 2 774032949 3 3 6733210 3 3 -843683844 3 1 327410390 3 1 -865685600 1 4 6 -951367966 3 2 107763506 1 3 2 721870187 2 3 3 -530847597 2 2 1 451029291 3 2 231297873 3...
output:
74427674582310 GG 74427055836482 74427829869431 74427836602641 74426992918797 74427320329187 74426454643587 GG -93292817648557 -93292095778370 74425923795990 -1057589620769 -93291944298803 74425228504438 74425430401539 -93291936231808 74425906008467 GG -1058067327518 74424997886529 74424370598990 74...
result:
wrong answer 196th lines differ - expected: '92593553133044', found: '92593120115062'