比赛 20241021 评测结果 AAAAAAAA
题目名称 大话西游 最终得分 100
用户昵称 ┭┮﹏┭┮ 运行时间 0.449 s
代码语言 C++ 内存使用 8.87 MiB
提交时间 2024-10-21 09:33:21
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 1e5+10;

ll read(){
	ll x = 0,f = 1;char c = getchar();
	for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
	for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
	return x * f;
}



int n,m;
vector<int>e[N];
int dfn[N],siz[N],dep[N],rnk[N],a[N],cnt;

struct segment{
	int mx[N<<2],mi[N<<2];
	void pushup(int p){mx[p] = max(mx[p<<1],mx[p<<1|1]),mi[p] = min(mi[p<<1],mi[p<<1|1]);}
	void build(int p,int l,int r){
		if(l == r)return mx[p] = mi[p] = a[rnk[l]],void();
		int mid = l + r >> 1;
		build(p<<1,l,mid),build(p<<1|1,mid+1,r);
		pushup(p);
	}
	void modify(int p,int l,int r,int x,int k){
		if(l == r)return mx[p] = mi[p] = k,void();
		int mid = l + r >> 1;
		if(x <= mid)modify(p<<1,l,mid,x,k);
		else modify(p<<1|1,mid+1,r,x,k);
		pushup(p);
	}
	ll ask(int p,int l,int r,int L,int R,bool op){
		if(L > r || R < l)return op ? 0 : 1e17;
		if(L <= l && r <= R)return op ? mx[p] : mi[p];
		int mid = l + r >> 1;
		return op ? max(ask(p<<1,l,mid,L,R,op),ask(p<<1|1,mid+1,r,L,R,op)) 
		: min(ask(p<<1,l,mid,L,R,op),ask(p<<1|1,mid+1,r,L,R,op));
	}
}t;


struct line{int x,y;}li[N];
void dfs(int x,int fa){
	dfn[x] = ++cnt,rnk[cnt] = x,siz[x] = 1;
	for(int y : e[x]){
		if(y == fa)continue;
		dep[y] = dep[x] + 1;
		dfs(y,x);
		siz[x] += siz[y];
	}
}
int main(){
	freopen("westward.in","r",stdin);
	freopen("westward.out","w",stdout);
	n = read(),m = read();
	for(int i = 1;i <= n;i++)a[i] = read();
	for(int i = 1;i < n;i++){
		int x = read(),y = read();
		li[i] = {x,y};
		e[x].pb(y),e[y].pb(x);
	}
	dep[1] = 1,dfs(1,0);
	t.build(1,1,n);
	for(int i = 1;i <= m;i++){
		char op[10];scanf("%s",op);
		if(op[0] == 'Q'){
			int x = read();
			x = dep[li[x].x] < dep[li[x].y] ? li[x].y : li[x].x;
			ll s1 = t.ask(1,1,n,dfn[x],dfn[x] + siz[x] - 1,1) * t.ask(1,1,n,dfn[x],dfn[x] + siz[x] - 1,0);
			ll s2 = max(t.ask(1,1,n,1,dfn[x] - 1,1),t.ask(1,1,n,dfn[x] + siz[x],n,1)) * 
			min(t.ask(1,1,n,1,dfn[x] - 1,0),t.ask(1,1,n,dfn[x] + siz[x],n,0));
			printf("%lld\n",s1 + s2);
		}
		else{
			int x = read(),z = read();
			t.modify(1,1,n,dfn[x],z);
		}
	}
	

	return 0;

}