QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#585610#9373. Query on TreeDisplace_WA 135ms92828kbC++177.9kb2024-09-23 21:28:492024-09-23 21:28:49

Judging History

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

  • [2024-09-23 21:28:49]
  • 评测
  • 测评结果:WA
  • 用时:135ms
  • 内存:92828kb
  • [2024-09-23 21:28:49]
  • 提交

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, inf2=1e15;
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(), f[i][j]=0;
		dfs(rt, 0);
		for(int i=1; i<=rt; ++i){
			int x=i;
			for(int j=0; j<=10&&x; ++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) {
				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){
		tag[p]=0;
		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){
		if(l==r) {
			tr[p]=v;
			return ;
		}
		push_down(p);
		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;
		push_down(p);
		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+20; ++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;
	while(k>=0){
		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[x][k], llb-1), DS1::get(1, 1, rt, rrb+1, DS1::rb[x][k])));
			}
			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[x][k], DS1::rb[x][k]));
			}
		}
		del=x; --k; x=TREE::f[x][1];
	}
	return ret;
}
inline void print(ll x){
	if(x<-inf2) 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+20; ++i) e[i].emplace_back(i-1), a[i]=-inf;
		rt=n+20;
		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-1>=0) 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;
}

详细

Test #1:

score: 100
Accepted
time: 8ms
memory: 89872kb

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: 135ms
memory: 92828kb

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: 135ms
memory: 90180kb

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 234th lines differ - expected: '-78585193822704', found: '97687177437076'