QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#585394#9373. Query on TreeDisplace_WA 75ms94016kbC++177.9kb2024-09-23 20:37:362024-09-23 20:37:37

Judging History

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

  • [2024-09-23 20:37:37]
  • 评测
  • 测评结果:WA
  • 用时:75ms
  • 内存:94016kb
  • [2024-09-23 20:37:36]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dou;
typedef pair<int,int> pii;
#define fi first
#define se second
#define mapa make_pair
typedef long double ld;
typedef unsigned long long ull;
template <typename T>inline void read(T &x){
	x=0;char c=getchar();bool f=0;
	for(;c<'0'||c>'9';c=getchar()) f|=(c=='-');
	for(;c>='0'&&c<='9';c=getchar())
	x=(x<<1)+(x<<3)+(c^48);
	x=(f?-x:x);
}
const int N=3e5;
const ll inf=1e16;
int Test, n, m;
ll a[N];
vector<int> e[N];
int rt;
namespace TREE{
	int f[N][11];
	vector<int> d[N][11];
	inline void dfs(int x, int fa){
		f[x][1]=fa;
		for(auto y:e[x]) if(y^fa) dfs(y, x);
	}
	inline void init(){
		for(int i=1; i<=rt; ++i) for(int j=0; j<=10; ++j) d[i][j].clear();
		dfs(rt, 0);
		for(int i=1; i<=n; ++i){
			int x=i;
			for(int j=0; j<=10; ++j){
				d[x][j].emplace_back(i);
				f[i][j]=x;
				x=f[x][1];
			}
		}
	}
};
namespace DS1{
	int que[N], hh, tt;
	ll tr[N<<2], tag[N<<2];
	int bfn[N], seq[N];
	int lb[N][11], rb[N][11];
	inline void build(int p, int l, int r){
		tag[p]=0;
		if(l==r){
			tr[p]=a[seq[l]];
			return ;
		}
		int mid=(l+r)>>1;
		build(p<<1, l, mid); build(p<<1|1, mid+1, r);
		tr[p]=max(tr[p<<1], tr[p<<1|1]);
	}
	inline void push_down(int p){
		if(tag[p]!=0){
			tr[p<<1]+=tag[p]; tag[p<<1]+=tag[p];
			tr[p<<1|1]+=tag[p]; tag[p<<1|1]+=tag[p];
			tag[p]=0;
		}
	}
	inline void add(int p, int l, int r, int L, int R, ll v){
		if(L>R) return ;
		if(L<=l&&r<=R) {
			tag[p]+=v; tr[p]+=v;
			return ;
		}
		int mid=(l+r)>>1;
		push_down(p);
		if(L<=mid) add(p<<1, l, mid, L, R, v);
		if(R>mid) add(p<<1|1, mid+1, r, L, R, v);
		tr[p]=max(tr[p<<1], tr[p<<1|1]);
	}
	inline ll get(int p, int l, int r, int L, int R){
		if(L>R) return -inf;
		if(L<=l&&r<=R) return tr[p];
		int mid=(l+r)>>1;
		push_down(p);
		ll ret=-inf;
		if(L<=mid) ret=max(ret, get(p<<1, l, mid, L, R));
		if(R>mid) ret=max(ret, get(p<<1|1, mid+1, r, L, R));
		return ret;
	}
	inline void init(){
		for(int i=1; i<=rt; ++i) bfn[i]=0;
		que[hh=tt=1]=rt;
		while(hh<=tt){
			int x=que[hh]; bfn[x]=hh; seq[hh]=x; ++hh;
			for(auto y:e[x]) if(!bfn[y]) que[++tt]=y;
		}
		for(int i=1; i<=rt; ++i){
			for(int j=0; j<=10; ++j) if(TREE::d[i][j].size()){
				lb[i][j]=rt+1; rb[i][j]=0;
				for(auto t:(TREE::d[i][j])){
					lb[i][j]=min(lb[i][j], bfn[t]);
					rb[i][j]=max(rb[i][j], bfn[t]);
				}
			}
		}
		build(1, 1, rt);
	}
};
namespace DS2{
	int dfn[N], out[N], seq[N], timer;
	inline void dfs(int x, int fa){
		dfn[x]=++timer; seq[timer]=x;
		for(auto y:e[x]) if(y^fa){
			dfs(y, x);
		}
		out[x]=timer;
	}
	ll tr[N<<2], tag[N<<2];
	inline void build(int p, int l, int r){
		if(l==r){
			int cur=seq[l]; tr[p]=-inf;
			for(auto t:(TREE::d[cur][10])){
				tr[p]=max(tr[p], a[t]);
			} 
			return ;
		}
		int mid=(l+r)>>1;
		build(p<<1, l, mid); build(p<<1|1, mid+1, r);
		tr[p]=max(tr[p<<1], tr[p<<1|1]);
	}
	inline void push_down(int p){
		if(tag[p]!=0){
			tr[p<<1]+=tag[p]; tag[p<<1]+=tag[p];
			tr[p<<1|1]+=tag[p]; tag[p<<1|1]+=tag[p];
			tag[p]=0;
		}
	}
	inline void set(int p, int l, int r, int x, ll v){
		tag[p]=0;
		if(l==r) {
			tr[p]=v;
			return ;
		}
		int mid=(l+r)>>1;
		if(x<=mid) set(p<<1, l, mid, x, v);
		else set(p<<1|1, mid+1, r, x, v);
		tr[p]=max(tr[p<<1], tr[p<<1|1]);
	}
	inline void add(int p, int l, int r, int L, int R, ll v){
		if(L>R) return ;
		if(L<=l&&r<=R) {
			tag[p]+=v; tr[p]+=v;
			return ;
		}
		int mid=(l+r)>>1;
		push_down(p);
		if(L<=mid) add(p<<1, l, mid, L, R, v);
		if(R>mid) add(p<<1|1, mid+1, r, L, R, v);
		tr[p]=max(tr[p<<1], tr[p<<1|1]);
	}
	inline ll get(int p, int l, int r, int L, int R){
		if(L>R) return -inf;
		if(L<=l&&r<=R) return tr[p];
		int mid=(l+r)>>1;
		ll ret=-inf;
		if(L<=mid) ret=max(ret, get(p<<1, l, mid, L, R));
		if(R>mid) ret=max(ret, get(p<<1|1, mid+1, r, L, R));
		return ret;
	}
	ll tr2[N<<2], tag2[N<<2];
	inline void build2(int p, int l, int r){
		tr2[p]=tag2[p]=0;
		if(l==r) return ;
		int mid=(l+r)>>1;
		build2(p<<1, l, mid); build2(p<<1|1, mid+1, r);
	}
	inline void push_down2(int p){
		if(tag2[p]!=0){
			tr2[p<<1]+=tag2[p]; tag2[p<<1]+=tag2[p];
			tr2[p<<1|1]+=tag2[p]; tag2[p<<1|1]+=tag2[p];
			tag2[p]=0;
		}
	}
	inline void add2(int p, int l, int r, int L, int R, ll v){
		if(L>R) return ;
		if(L<=l&&r<=R){
			tr2[p]+=v; tag2[p]+=v;
			return ;
		}
		int mid=(l+r)>>1;
		push_down2(p);
		if(L<=mid) add2(p<<1, l, mid, L, R, v);
		if(R>mid) add2(p<<1|1, mid+1, r, L, R, v);
	}
	inline void set2(int p, int l, int r, int x, ll v){
		if(l==r){
			tr2[p]=v;
			return ;
		}
		int mid=(l+r)>>1;
		push_down2(p);
		if(x<=mid) set2(p<<1, l, mid, x, v);
		else set2(p<<1|1, mid+1, r, x, v);
	}
	inline ll get2(int p, int l, int r, int x){
		if(l==r) return tr2[p];
		int mid=(l+r)>>1;
		push_down2(p);
		if(x<=mid) return get2(p<<1, l, mid, x);
		else return get2(p<<1|1, mid+1, r, x);
	}
	inline void init(){
		timer=0;
		dfs(rt, 0);
		build(1, 1, rt);
		build2(1, 1, rt);
	}
};
inline void clr(){
	for(int i=1; i<=n+10; ++i) {
		e[i].clear();
	}
}
inline void mdff(int x, int k, int v){
	if(TREE::d[x][k].empty()) return ;
	int p=TREE::d[x][k][0], fp=TREE::f[p][10];
	ll tag=DS2::get2(1, 1, rt, DS2::dfn[fp]);
	DS2::set2(1, 1, rt, DS2::dfn[fp], 0);
	if(tag!=0) DS1::add(1, 1, rt, DS1::lb[fp][10], DS1::rb[fp][10], tag);
	DS1::add(1, 1, rt, DS1::lb[x][k], DS1::rb[x][k], v);
	ll curmx=DS1::get(1, 1, rt, DS1::lb[fp][10], DS1::rb[fp][10]);
	DS2::set(1, 1, rt, DS2::dfn[fp], curmx);
}
inline void mdf(int x, int k, int v){
	while(k>=0){
		mdff(x, k, v);
		if(k-1>=0) mdff(x, k-1, v);
		--k; x=TREE::f[x][1];
	}
}
inline ll gett(int x, int k){
	if(TREE::d[x][k].empty()) return -inf;
	int p=TREE::d[x][k][0], fp=TREE::f[p][10];
	ll tag=DS2::get2(1, 1, rt, DS2::dfn[fp]);
	return tag+DS1::get(1, 1, rt, DS1::lb[x][k], DS1::rb[x][k]);
}
inline ll get(int x, int k){
	ll ret=-inf;
	while(k>=0){
		ret=max(ret, gett(x, k));
		if(k-1>=0) ret=max(ret, gett(x, k-1));
		--k; x=TREE::f[x][1];
	}
	return ret;
}
inline ll get2(int x, int k){
	ll ret=-inf;
	int del=0;
	do{
		if(!TREE::d[x][k].empty()){
			if(del&&k-1>=0){
				int p=TREE::d[x][k][0], fp=TREE::f[p][10];
				ll tag=DS2::get2(1, 1, rt, DS2::dfn[fp]);
				int llb=DS1::lb[del][k-1], rrb=DS1::rb[del][k-1];
				ret=max(ret, tag+max(DS1::get(1, 1, rt, DS1::lb[fp][10], llb-1), DS1::get(1, 1, rt, rrb+1, DS1::rb[fp][10])));
			}
			else{
				int p=TREE::d[x][k][0], fp=TREE::f[p][10];
				ll tag=DS2::get2(1, 1, rt, DS2::dfn[fp]);
				ret=max(ret, tag+DS1::get(1, 1, rt, DS1::lb[fp][10], DS1::rb[fp][10]));
			}
		}
		del=x; --k; x=TREE::f[x][1];
	}while(k>=0);
	return ret;
}
inline void print(ll x){
	if(x==-inf) printf("GG\n");
	else printf("%lld\n", x);
}
int main(){
	// freopen("D:\\nya\\acm\\C\\test.in","r",stdin);
	// freopen("D:\\nya\\acm\\C\\test.out","w",stdout);
	read(Test);
	while(Test--){
		read(n); read(m);
		clr();
		for(int i=1; i<=n; ++i) read(a[i]);
		for(int i=1, x, y; i<n; ++i){
			read(x); read(y); e[x].emplace_back(y); e[y].emplace_back(x);
		}
		e[n+1].emplace_back(1);
		a[n+1]=-inf;
		for(int i=n+2; i<=n+10; ++i) e[i].emplace_back(i-1), a[i]=-inf;
		rt=n+10;
		TREE::init();
		DS1::init();
		DS2::init();
		int op, x, k, v;
		while(m--){
			read(op); read(x);
			if(op==1){
				read(k); read(v);
				mdf(x, k, v);
				if(k) mdf(x, k-1, -v);
				print(get2(x, k));
			}
			else if(op==2){
				read(k); read(v);
				mdf(x, k, v);
				print(get(x, k));
			}
			else {
				read(v);
				for(int t=0; t<=10; ++t){
					mdff(x, t, v);
				}
				DS2::add(1, 1, rt, DS2::dfn[x]+1, DS2::out[x], v);
				DS2::add2(1, 1, rt, DS2::dfn[x]+1, DS2::out[x], v);
				ll ans=DS2::get(1, 1, rt, DS2::dfn[x]+1, DS2::out[x]);
				for(int t=0; t<=10; ++t) ans=max(ans, gett(x, t));
				print(ans);
			}
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 92964kb

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: 75ms
memory: 94016kb

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
34994636372145
77264056182933
77263506444530
7065382376488
7065749360235
7066534912965
-85115611272570
-85114714781312
96492412785032
-20428913156111
-20428197540063
96491742171666
-14945310996805
96491180203461
-2...

result:

wrong answer 9th lines differ - expected: '-91733875882986', found: '34994636372145'