QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#667928 | #9373. Query on Tree | lytqwq | WA | 43ms | 22432kb | C++14 | 4.9kb | 2024-10-23 09:48:48 | 2024-10-23 09:48:56 |
Judging History
answer
#include<bits/stdc++.h>
#define vectorwyx maze
namespace vectorwyx{
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define sml(x,y) (x=min(x,y))
#define big(x,y) (x=max(x,y))
#define ll long long
#define uint unsigned
#define ull unsigned long long
#define umap unordered_map
#define db double
#define fo(i,x,y) for(int i=(x);i<=(y);++i)
#define go(i,x,y) for(int i=(x);i>=(y);--i)
#define ptc putchar
#define gtc getchar
#define emp emplace
#define re return
#define co continue
#define brk break
#define HH (ptc('\n'))
#define bctz __builtin_ctz
#define bclz __builtin_clz
#define bppc __builtin_popcount
using namespace std;
ll seed=chrono::system_clock::now().time_since_epoch().count();
mt19937 rnd(seed);
inline int rm(int x,int y){return (int)(rnd()%(y-x+1ll)+x);}
inline int read(){signed ch=getchar();int x=0,f=1;while(!isdigit(ch)){if(ch==(int)('-'))f=-1;ch=getchar();}while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline ll llread(){signed ch=getchar();ll x=0,f=1;while(!isdigit(ch)){if(ch==(int)('-'))f=-1;ch=getchar();}while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
template<typename T> void out(T *a,int l,int r){fo(i,l,r) cout<<*(a+i)<<' ';puts("");}
const int N=2e5+5,K=9;
const ll inf=1e18;
vector<int> e[N];
int bfn[N],tb,bod[N],bL[N][K+2],bR[N][K+2],fa[N][K+2],dL[N],dR[N],td,n;
ll a[N];
void init(){
fo(i,1,K) fa[1][i]=0;
fo(i,1,n) e[i].clear(),fa[i][0]=i;
fo(i,1,n) fo(j,0,K) bL[i][j]=n+1,bR[i][j]=0;
tb=td=0;
}
void dfs1(int x){
for(int i:e[x]) if(i!=fa[x][1]){
fo(j,1,K) fa[i][j]=fa[x][j-1];
dfs1(i);
}
}
void bfs(){
queue<int> q;q.push(1);
while(!q.empty()){
int x=q.front();q.pop();
bfn[x]=++tb;bod[tb]=x;
fo(i,0,K) if(fa[x][i]) sml(bL[fa[x][i]][i],bfn[x]),big(bR[fa[x][i]][i],bfn[x]);
for(int i:e[x]) if(i!=fa[x][1]) q.push(i);
}
// cout<<"bfn:";out(bfn,1,n);
}
void dfs2(int x){
if(!bR[x][K]) re;
dL[x]=++td;
for(int i:e[x]) if(i!=fa[x][1]) dfs2(i);
dR[x]=td;
}
namespace Tree_b{
ll b[N];
void build(){fo(i,1,n) b[i]=a[i];}
void update(int l,int r,ll v){fo(i,l,r) b[i]+=v;}
ll ask(int l,int r){ll ret=-inf;fo(i,l,r) big(ret,b[i]);re ret;}
}
namespace Tree_d{
ll tag[N],mx[N];
void build(){
fo(i,1,n){
tag[i]=0,mx[i]=-inf;
if(bR[i][K]) fo(j,bL[i][K],bR[i][K]) big(mx[i],a[bod[j]]);
}
}
void update(int l,int r,ll v){fo(i,l,r) tag[i]+=v,mx[i]+=v;}
ll ask(int l,int r){ll ret=-inf;fo(i,l,r) big(ret,mx[i]);re ret;}
void clr_tag(int x){
Tree_b::update(bL[x][K],bR[x][K],tag[x]);
tag[x]=0;
}
void reset(int x){mx[x]=Tree_b::ask(bL[x][K],bR[x][K]);}
}
void gao(int l,int r,int v){
int x=bod[l];
assert(fa[x][K]==fa[bod[r]][K]);
if(!fa[x][K]){Tree_b::update(l,r,v);re;}
Tree_d::clr_tag(x);
Tree_b::update(l,r,v);
Tree_d::reset(x);
}
ll nbor(int x,int k,int v){
gao(bL[x][k],bR[x][k],v);
if(k==0) re Tree_b::ask(bfn[x],bfn[x]);
gao(bL[x][k-1],bR[x][k-1],v);
ll ret=max(Tree_b::ask(bL[x][k],bR[x][k]),Tree_b::ask(bL[x][k-1],bR[x][k-1]));
re max(ret,nbor(fa[x][1],k-1,v));
}
void solve(){
cin>>n;int q=read();init();
fo(i,1,n) a[i]=llread();
fo(i,2,n){
int x=read(),y=read();
e[x].pb(y),e[y].pb(x);
}
dfs1(1);
bfs();
td=0;dfs2(1);
Tree_b::build();Tree_d::build();
while(q--){
int o=read(),x=read();
if(o==3){
int v=read();
ll ans=0;
fo(i,0,K-1){
if(!bR[x][i]) break;
gao(bL[x][i],bR[x][i],v);
big(ans,Tree_b::ask(bL[x][i],bR[x][i]));
}
if(bR[x][K]) Tree_d::update(dL[x],dR[x],v),big(ans,Tree_d::ask(dL[x],dR[x]));
cout<<ans<<'\n';
co;
}
int k=read(),v=read();
if(o==1){
ll ans=0;
gao(bL[x][k],bR[x][k],v);big(ans,Tree_b::ask(bL[x][k],bR[x][k]));
fo(i,1,k){
if(!fa[x][i]) break;
int l=bL[fa[x][i]][k-i],r=bR[fa[x][i]][k-i];
if(i==k){gao(l,r,v);big(ans,Tree_b::ask(l,r));break;}
int L=bL[fa[x][i-1]][k-i-1],R=bR[fa[x][i-1]][k-i-1];
if(l<L) gao(l,L-1,v),big(ans,Tree_b::ask(l,L-1));
if(r>R) gao(r+1,R,v),big(ans,Tree_b::ask(r+1,R));
}
cout<<ans<<'\n';
co;
}
cout<<nbor(x,k,v)<<'\n';
}
}
signed main(){
int T=read();
while(T--) solve();
return 0;
}
}
/*
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
-------------------------------------------------
*/
signed main(){re vectorwyx::main();}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 22432kb
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: 43ms
memory: 22144kb
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:
0 42972228579460 0 2800055808 34992827930608 42972928387769 34994082624484 0 34993764250431 2888131657 77263812310760 7065382376488 3805451702 7066167929218 -61567338673593 -61566442182335 96492412785032 -20428913156111 0 96491742171666 0 2796454656 92592014656791 0 98377163227355 98376397205230 0 9...
result:
wrong answer 1st lines differ - expected: 'GG', found: '0'