AtCoder Beginner Contest 460
G
顶点颜色翻转查询
Vertex Flip Query
AtCoder
⌀ 2838
原题

顶点颜色翻转查询AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

There is a tree with $N$ vertices numbered $1$ to $N$. The $i$-th edge connects vertices $a_i$ and $b_i$. Each vertex $i$ has a weight $W_i$ and a color $C_i \in \lbrace 0,1 \rbrace$. Process $Q$ queries. There are three types of queries as follows.

给定一棵有 $N$ 个顶点的树,顶点编号为 $1$ 到 $N$。第 $i$ 条边连接顶点 $a_i$ 和 $b_i$。每个顶点 $i$ 有一个权值 $W_i$ 和一种颜色 $C_i \in \lbrace 0,1 \rbrace$。 请处理 $Q$ 个查询,查询分为以下三种类型:AI (CI翻译)

  • 1 v: Change $C_v$ to $1 - C_v$ for vertex $v$.
  • 2 v x: Change $W_v$ to $W_v + x$ for vertex $v$.
  • 3 v: Let $c$ be the color of vertex $v$. Output the sum of weights of all vertices reachable from vertex $v$ by traveling only through vertices of color $c$ (including vertex $v$ itself).
  • 1 v:将顶点 $v$ 的颜色 $C_v$ 改为 $1 - C_v$。
  • 2 v x:将顶点 $v$ 的权值 $W_v$ 增加 $x$。
  • 3 v:令 $c$ 为顶点 $v$ 的颜色。输出仅经过颜色为 $c$ 的顶点,从顶点 $v$ 可以到达的所有顶点的权值之和(包括顶点 $v$ 本身)。
AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \leq N \leq 3 \times 10^5$
  • $1 \leq Q \leq 2 \times 10^5$
  • $1 \leq W_i \leq 10^9$
  • $C_i \in \lbrace 0,1 \rbrace$
  • $1 \leq a_i \lt b_i \leq N$
  • The input graph is a tree.
  • $1 \leq v \leq N$
  • $1 \leq x \leq 10^9$
  • All input values are integers.
  • $1 \leq N \leq 3 \times 10^5$
  • $1 \leq Q \leq 2 \times 10^5$
  • $1 \leq W_i \leq 10^9$
  • $C_i \in \lbrace 0,1 \rbrace$
  • $1 \leq a_i \lt b_i \leq N$
  • 输入的图是一棵树。
  • $1 \leq v \leq N$
  • $1 \leq x \leq 10^9$
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format, where $\mathrm{query}_i$ denotes the $i$-th query:

输入从标准输入给出,格式如下,其中 $\mathrm{query}_i$ 表示第 $i$ 个查询:AI (CI翻译)

N Q
W_1 W_2 \dots W_N
C_1 C_2 \dots C_N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

N Q
W_1 W_2 \dots W_N
C_1 C_2 \dots C_N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
AI (CI翻译)

Each query is given in one of the following formats:

每个查询的格式为以下之一:AI (CI翻译)

1 v

1 v
AI (CI翻译)

2 v x

2 v x
AI (CI翻译)

3 v

3 v
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Let $m$ be the number of type-$3$ queries given. Output $m$ lines. The $i$-th line should contain the answer for the $i$-th type-$3$ query.

设给出的 3 型查询共有 $m$ 个。输出 $m$ 行,第 $i$ 行包含第 $i$ 个 3 型查询的答案。AI (CI翻译)

样例

Input #1
5 9
1 10 100 1000 10000
0 0 0 0 0
1 2
2 3
3 4
2 5
3 1
1 2
3 1
3 2
3 3
1 3
1 2
2 1 1
3 5
Output #1
11111
1
10
1100
10012
For example, for the first query, the set of vertices reachable from vertex $1$ by traveling only through vertices of color $0$ is $\lbrace 1,2,3,4,5 \rbrace$.
Input #2
10 25
1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000
0 1 0 0 0 1 0 0 0 0
1 2
1 3
2 4
4 5
4 6
2 7
5 8
8 9
6 10
3 4
1 8
2 6 100000
3 3
1 3
1 7
1 7
1 7
3 8
2 1 1
2 6 100000
2 8 10000000
1 5
2 5 10000
1 9
2 7 1000000
1 7
2 10 1000000000
1 9
1 6
1 9
1 1
3 7
2 5 10000
3 6
Output #2
110011000
101
10000000
2000000
2000301000
题目信息
题号
abc460_g
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。