QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#581319 | #9373. Query on Tree | PlentyOfPenalty | WA | 66ms | 46624kb | C++20 | 4.3kb | 2024-09-22 11:53:41 | 2024-09-22 11:53:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+10;
const ll INF=1e18;
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(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,0,0,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==-INF)cout<<"GG\n";
else cout<<tmp<<"\n";
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 44556kb
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: -100
Wrong Answer
time: 66ms
memory: 46624kb
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 42971666256906 -91735629075891 42972366065215 -91734374382015 GG -91734692756068 77264056182933 77263506444530 7065382376488 7065749360235 7066534912965 -85115611272570 -85114714781312 96492412785032 -20428754637121 -20428039021073 96491900690656 -14945152477815 96491338722451 -...
result:
wrong answer 4th lines differ - expected: '42972483129988', found: '42971666256906'