HAOI2015 樹上操作

2022-04-29 22:33:17 字數 2552 閱讀 3418

有一棵點數為 n 的樹,以點 1 為根,且樹點有邊權。然後有 m 個操作,分為三種:操作 1 :把某個節點 x 的點權增加 a 。操作 2 :把某個節點 x 為根的子樹中所有點的點權都增加 a 。操作 3 :詢問某個節點 x 到根的路徑中所有點的點權和。

輸入格式:

第一行包含兩個整數 n, m 。表示點數和運算元。接下來一行 n 個整數,表示樹中節點的初始權值。接下來 n-1 行每行兩個正整數 from, to , 表示該樹中存在一條邊 (from, to) 。再接下來 m 行,每行分別表示一次操作。其中第乙個數表示該操作的種類( 1-3 ) ,之後接這個操作的引數( x 或者 x a ) 。

輸出格式:

對於每個詢問操作,輸出該詢問的答案。答案之間用換行隔開。

輸入樣例#1: 複製

5 5

1 2 3 4 5

1 21 4

2 32 5

3 31 2 1

3 52 1 2

3 3

輸出樣例#1: 複製

6

913

對於 100% 的資料, n,m<=100000 ,且所有輸入資料的絕對值都不

會超過 10^6 。

比洛谷板子還簡單。。。不解釋了,應該都會吧

#include#include

#include

#include

#include

#define ll long long

using

namespace

std;

struct

nodee[

500001

];ll head[

500001],dep[500001],sum[500001],a[500001

];ll tot,num,n,m,lazy[

500001],fa[500001],l[500001

];ll ch[

500001],top[500001],size[500001],son[500001

];void build(int root,int l,int

r)

int mid=(l+r)>>1

; build(root

<<1

,l,mid);

build(root

<<1|1,mid+1

,r);

sum[root]=sum[root<<1]+sum[root<<1|1

];

return;}

void push(int root,int l,int

r)void update(int root,int left,int right,int l,int

r,ll k)

if(left>r||rightreturn

;

int mid=(left+right)>>1

;

if(lazy[root])push(root,left,right);

if(mid>=l)update(root<<1

,left,mid,l,r,k);

if(mid1|1,mid+1

,right,l,r,k);

sum[root]=sum[root<<1|1]+sum[root<<1

];

return;}

ll query(

int root,int left,int right,int l,int

r)void dfs1(int

x)

}return;}

void dfs2(int x,int

t)

return;}

ll cal(

int x,int

y) maxx+=query(1,1

,tot,l[fx],l[x]);

x=fa[fx];

fx=top[x];

}if(l[x]>l[y])swap(x,y);

maxx+=query(1,1

,tot,l[x],l[y]);

return

maxx;

}ll read()

while(ch>='

0'&&ch<='

9')x=x*10+ch-'

0',ch=getchar();

return x*w;

}void add(int

from,int

to)int

main()

for(int i=1;i)

dep[

1]=1

; fa[

1]=1

; dfs1(1);

dfs2(

1,1);

build(

1,1,n);

for(int i=1;i<=m;i++)

if(qwq==2

)

if(qwq==3

)

}return0;

}

HAOI2015 樹上操作

題目描述 有一棵點數為 n 的樹,以點 1 為根,且樹點有邊權。然後有 m 個操作,分為三種 操作 1 把某個節點 x 的點權增加 a 操作 2 把某個節點 x 為根的子樹中所有點的點權都增加 a 操作 3 詢問某個節點 x 到根的路徑中所有點的點權和。輸入格式 第一行包含兩個整數 n,m 表示點數...

HAOI2015 樹上操作

嘟嘟嘟 樹剖自然可解,就是一道板子題,而且這道題還只問到根節點的距離是多少,而不是樹上任意兩點距離,就更方便了。1 include2 include3 include4 include5 include6 include7 include8 include9 include10 include11 ...

HAOI2015 樹上操作

題目 bzoj4034 洛谷p3178。題目大意 有一棵點數為 n 的樹,以點 1 為根,且樹有點權。然後有 m 個操作,分為三種 操作 1 把某個節點 x 的點權增加 a 操作 2 把某個節點 x 為根的子樹中所有點的點權都增加 a 操作 3 詢問某個節點 x 到根的路徑中所有點的點權和。現在要你...