记录编号 |
270520 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[HAOI 2015]树上操作 |
最终得分 |
100 |
用户昵称 |
哒哒哒哒哒! |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
1.825 s |
提交时间 |
2016-06-14 21:35:05 |
内存使用 |
91.87 MiB |
显示代码纯文本
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
long long read(){
long long x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
const long long maxn=500001;
long long n,m,cnt,le,ri;
long long dis[maxn],pos[maxn],sec[maxn],dfn[maxn];
long long size[maxn],deep[maxn],fa[maxn],son[maxn],top[maxn];
long long dgcnt=0;
struct edge{
long long to,next;
}e[maxn*3];
long long tot,head[maxn];
long long num[maxn*4],lazy[maxn*4];
void add(long long u,long long v){
e[++tot].to=v;
e[tot].next=head[u];
head[u]=tot;
}
#define to e[i].to
void dfs1(int u){
size[u]=1;
for(int i=head[u];i;i=e[i].next){
//int to=e[i].to;
if(size[to]) continue;
deep[to]=deep[u]+1;
fa[to]=u;
dfs1(to);
size[u]+=size[to];
if(size[son[u]]<size[to]) son[u]=to;
}
}
void dfs2(long long u,long long tp){
top[u]=tp;
dfn[u]=++cnt;pos[cnt]=u;
if(son[u]) dfs2(son[u],tp);
for(long long i=head[u];i;i=e[i].next){
if(!dfn[to]) dfs2(to,to);
}
sec[u]=cnt;
}
void build(long long rt,long long l,long long r){
if(l==r){
num[rt]=dis[pos[l]];
return;
}
long long mid=(l+r)/2;
build(rt*2,l,mid);
build(rt*2+1,mid+1,r);
num[rt]=num[rt*2]+num[rt*2+1];
}
void updata(long long rt,long long l,long long r){
long long mid=(l+r)/2;
num[rt*2]+=lazy[rt]*(mid-l+1);
num[rt*2+1]+=lazy[rt]*(r-mid);
lazy[rt*2]+=lazy[rt];
lazy[rt*2+1]+=lazy[rt];
lazy[rt]=0;
}
void insert(long long rt,long long l,long long r,long long x){
if(le<=l&&ri>=r){
num[rt]+=x*(r-l+1);
lazy[rt]+=x;
return;
}
updata(rt,l,r);
long long mid=(l+r)/2;
if(le<=mid) insert(rt*2,l,mid,x);
if(ri>mid) insert(rt*2+1,mid+1,r,x);
num[rt]=num[rt*2]+num[rt*2+1];
}
long long query(long long rt,long long l,long long r){
if(le<=l&&ri>=r) return num[rt];
if(lazy[rt]) updata(rt,l,r);
long long ans=0;
long long mid=(l+r)/2;
if(le<=mid) ans+=query(rt*2,l,mid);
if(ri>mid) ans+=query(rt*2+1,mid+1,r);
return ans;
}
long long query1(long long l,long long r){
le=l,ri=r;
return query(1,1,n);
}
long long query(long long a,long long b){
long long ans=0;
while(top[a]!=top[b]){
if(deep[top[a]]<deep[top[b]]) swap(a,b);
ans+=query1(dfn[top[a]],dfn[a]);
a=fa[top[a]];
}
if(deep[a]>deep[b]) swap(a,b);
ans+=query1(dfn[a],dfn[b]);
return ans;
}
int main(){
freopen("haoi2015_t2.in","r",stdin);freopen("haoi2015_t2.out","w",stdout);
//int __size__=4<<20;
// char *__p__=(char*)malloc(__size__)+__size__;
// __asm__("movl %0, %%esp\n"::"r"(__p__));
n=read(),m=read();
for(long long i=1;i<=n;i++) dis[i]=read();
long long a,b,op;
for(long long i=1;i<n;i++){
a=read(),b=read();
add(a,b);add(b,a);
}
dfs1(1);
dfs2(1,1);
build(1,1,n);
while(m--){
op=read();
if(op==1){
a=read(),b=read();
le=dfn[a],ri=dfn[a];
insert(1,1,n,b);
}
else if(op==2){
a=read(),b=read();
le=dfn[a],ri=sec[a];
insert(1,1,n,b);
}
else{
a=read();
printf("%lld\n",query(1,a));
}
}
fclose(stdin);fclose(stdout);
return 0;
}