比赛 20241021 评测结果 AAAAAAAA
题目名称 大话西游 最终得分 100
用户昵称 wdsjl 运行时间 0.395 s
代码语言 C++ 内存使用 6.95 MiB
提交时间 2024-10-21 10:45:39
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010; 
int num,n,q,g,Ev[maxn],fa[maxn],dfn[maxn],id[maxn],head[maxn],point[maxn],sz[maxn];

struct node{
	int to,pre,d;
}e[maxn*4];

struct Node{
	int l,r,mn,mx;
}tr[maxn*4];

void addeg(int from,int to,int d){
	e[++num].to=to;
	e[num].d=d;
	e[num].pre=head[from];
	head[from]=num;
}

void dfs(int father,int now){
    sz[now]=1;
    dfn[now]=++g;
    fa[now]=father;
    id[g]=now;
    for(int i=head[now];i;i=e[i].pre){
        int to=e[i].to;
        if(to==father)continue;
        point[e[i].d]=to;
        dfs(now,to);
        sz[now]+=sz[to];
    }
}

void pushup(int k){
    tr[k].mn=min(tr[k<<1].mn,tr[(k<<1)|1].mn);
    tr[k].mx=max(tr[k<<1].mx,tr[(k<<1)|1].mx);
}

void build(int l,int r,int k){
    tr[k].l=l;tr[k].r=r;
    if(tr[k].l==tr[k].r){tr[k].mn=tr[k].mx=Ev[id[l]];return;}
    int mid=(l+r)>>1;
    build(l,mid,k<<1);build(mid+1,r,(k<<1)|1);
    pushup(k);
}

int findmax(int l,int r,int k){
    if(tr[k].l==l&&tr[k].r==r)return tr[k].mx;
    int mid=(tr[k].l+tr[k].r)>>1;
    if(r<=mid)return findmax(l,r,k<<1);
    else if(l>mid)return findmax(l,r,(k<<1)|1);
    else return max(findmax(l,mid,k<<1),findmax(mid+1,r,(k<<1)|1));
}

int findmin(int l,int r,int k){
    if(tr[k].l==l&&tr[k].r==r)return tr[k].mn;
    int mid=(tr[k].l+tr[k].r)>>1;
    if(r<=mid)return findmin(l,r,k<<1);
    else if(l>mid)return findmin(l,r,(k<<1)|1);
    else return min(findmin(l,mid,k<<1),findmin(mid+1,r,(k<<1)|1));
}

void update(int pos,int v,int k){
    if(tr[k].l==tr[k].r){tr[k].mn=tr[k].mx=v;return;}
    int mid=(tr[k].l+tr[k].r)>>1;
    if(pos<=mid)update(pos,v,k<<1);
    if(pos>mid)update(pos,v,(k<<1)|1);
    pushup(k);
}

int main(){
    freopen("westward.in","r",stdin);
    freopen("westward.out","w",stdout);
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)scanf("%d",&Ev[i]);
    int x,y;
    for(int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        addeg(x,y,i);addeg(y,x,i);
    }
    dfs(0,1);
    build(1,n,1);
    char ch[10];
    for(int i=1;i<=q;i++){
        scanf("%s",&ch);
        int a,b;
        if(ch[0]=='C'){
            scanf("%d%d",&a,&b);
            update(dfn[a],b,1);
        }
        if(ch[0]=='Q'){
            scanf("%d",&a);
            int s=dfn[point[a]],t=s+sz[point[a]]-1;
            int max1,max2;int min1,min2;min1=min2=0x7fffffff;
            max1=findmax(s,t,1);min1=findmin(s,t,1);
            if(s!=1)max2=findmax(1,s-1,1),min2=findmin(1,s-1,1);
            if(t!=n) max2=max(max2,findmax(t+1,n,1)),min2=min(min2,findmin(t+1,n,1));
            printf("%lld\n",(long long)min1*(long long)max1+(long long)min2*(long long)max2);
        }
    }
    return 0;
}