QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#627371#9373. Query on Tree275307894a#WA 58ms28568kbC++143.5kb2024-10-10 15:43:192024-10-10 15:43:20

Judging History

你现在查看的是最新测评结果

  • [2024-10-10 15:43:20]
  • 评测
  • 测评结果:WA
  • 用时:58ms
  • 内存:28568kb
  • [2024-10-10 15:43:19]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=5e5+5,M=N*4+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const ll INF=1e18+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#ifdef LOCAL
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
	#else 
	#define gdb(...) void()
	#endif
}using namespace Debug;
int n,q;
ll A[N];
namespace DS{
	#define ls v<<1
	#define rs v<<1|1
	ll f[M],g[M];
	void clr(int l=1,int r=n,int v=1){
		f[v]=g[v]=0;if(l==r) return;
		int m=l+r>>1;clr(l,m,ls);clr(m+1,r,rs);
	}
	void up(int v){f[v]=max(f[ls],f[rs])+g[v];}
	void Pf(int v,ll w){g[v]+=w;f[v]+=w;}
	ll add(int x,int y,ll z,int l=1,int r=n,int v=1){
		if(x<=l&&r<=y) return Pf(v,z),f[v];int m=l+r>>1;
		return max(x<=m?add(x,y,z,l,m,ls):-INF,y>m?add(x,y,z,m+1,r,rs):-INF)+g[v];
	}
	#undef ls
	#undef rs
}
vector<int> S[N];
int id[N],ih;
int l[N][12],r[N][12];
int d[N],fa[N];
void tag(int x,int La,int d){
	queue<pii> q;
	q.emplace(x,d);
	while(!q.empty()){
		auto [x,d]=q.front();q.pop();
		if(!id[x]) id[x]=++ih,DS::add(id[x],id[x],A[x]),gdb(x,id[x]);
		if(d) for(int i:S[x]) if(i^fa[x]) q.emplace(i,d-1);
	}
}
void make(int x,int La){
	fa[x]=La;d[x]=d[La]+1;
	for(int i:S[x]) if(i^La) make(i,x);
	if(La) S[x].erase(find(all(S[x]),La));
}
void dfs(int x,int La){
	tag(x,La,10);
	for(int i:S[x]) if(i^La) dfs(i,x);
	Me(l[x],0x3f);Me(r[x],-0x3f); 
	l[x][0]=r[x][0]=id[x];
	for(int i:S[x]) if(i^La){
		for(int j=0;j<=11;j++){
			l[x][min(j+1,11)]=min(l[x][min(j+1,11)],l[i][j]);
			r[x][min(j+1,11)]=max(r[x][min(j+1,11)],r[i][j]);
		}
	}
}
void Solve(){
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++) S[i].clear(),id[i]=0;
	DS::clr();
	for(int i=1;i<=n;i++) scanf("%lld",&A[i]);
	for(int i=1;i<n;i++){
		int x,y;scanf("%d%d",&x,&y);
		S[x].push_back(y);S[y].push_back(x);
	}
	ih=0;
	make(1,0);
	dfs(1,0);
	gdb(DS::f[1],DS::f[2],DS::f[3],DS::f[4],DS::f[5]);
	while(q--){
		int op,x;
		scanf("%d%d",&op,&x);
		if(op==1||op==2){
			int k,v;scanf("%d%d",&k,&v);
			ll mx=-INF;
			if(op==1){
				int L=n+1,R=0;
				for(int i=k;~i&&x;i--){
					if(L>R) mx=max(mx,DS::add(l[x][i],r[x][i],v));
					else mx=max(mx,max(DS::add(l[x][i],L-1,v),DS::add(R+1,r[x][i],v)));
					if(i>=2) L=l[x][i-2],R=r[x][i-2];
					else L=n+1,R=0;
					x=fa[x];
				}
			}else{
				for(int i=k;~i;i--){
					mx=max(mx,DS::add(l[x][i],r[x][i],v));
					if(x^1) mx=max(mx,DS::add(l[x][i-1],r[x][i-1],v)),x=fa[x];
					gdb(i,mx,v,l[x][i],r[x][i]);
				}
			}
			if(mx<=-INF) puts("GG");
			else printf("%lld\n",mx);
		}else{
			int v;scanf("%d",&v);
			ll mx=-INF;
			for(int i=0;i<=11;i++) if(l[x][i]<=r[x][i]) mx=max(mx,DS::add(l[x][i],r[x][i],v));
			if(mx<=-INF) puts("GG");
			else printf("%lld\n",mx);
		}
	}
}
int main(){
	int t=1;
	scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 28568kb

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: 58ms
memory: 28544kb

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:

wrong answer 241st lines differ - expected: 'GG', found: '-999978547089710894'