ACM-ICPC 2018 焦作赛区网络预赛 E Jiu Yuan Wants to Eat【树链剖分】

Jiu Yuan Wants to Eat
Time Limit: 3000 MS Memory Limit: 65536 K

Problem Description

You ye Jiu yuan is the daughter of the Great GOD Emancipator. And when she becomes an adult, she will be queen of Tusikur, so she wanted to travel the world while she was still young. In a country, she found a small pub called Whitehouse. Just as she was about to go in for a drink, the boss Carola appeared. And ask her to solve this problem or she will not be allowed to enter the pub. The problem description is as follows:
There is a tree with nnn nodes, each node i contains weight a[i], the initial value of a[i] is 0. The root number of the tree is 1. Now you need to do the following operations:

  1. Multiply all weight on the path from u to v by x
  2. For all weight on the path from u to v, increasing x to them
  3. For all weight on the path from u to v, change them to the bitwise NOT of them
  4. Ask the sum of the weight on the path from u to v

The answer modulo 2 64 2^{64} .

Jiu Yuan is a clever girl, but she was not good at algorithm, so she hopes that you can help her solve this problem. Ding∽∽∽
The bitwise NOT is a unary operation that performs logical negation on each bit, forming the ones’ complement of the given binary value. Bits that are 0 become 1, and those that are 1 become 0. For example:

NOT 0111 (decimal 7) = 1000 (decimal 8)

NOT 10101011 = 01010100

Input

The input contains multiple groups of data.
For each group of data, the first line contains a number of n, and the number of nodes.
The second line contains (n−1) integers bi, which means that the father node of node (i+1) is bi.
The third line contains one integer m, which means the number of operations,
The next m lines contain the following four operations:
At first, we input one integer opt

  1. If opt is 1, then input 3 integers, u,v,x, which means multiply all weight on the path from u to v by x
  2. If opt is 2, then input 3 integers, u,v,x, which means for all weight on the path from u to v, increasing x to them
  3. If opt is 3, then input 2 integers, u,v, which means for all weight on the path from u to v, change them to the bitwise NOT of them
  4. If opt is 4, then input 2 integers, u,v, and ask the sum of the weights on the path from u to v

1 n , m , u , v 1 0 5 , 1 x < 2 64 1≤n,m,u,v≤10^5,1≤x<2^{64}

Output

For each operation 4, output the answer.

Examples

Input
7
1 1 1 2 2 4
5
2 5 6 1
1 1 6 2
4 5 6
3 5 2
4 2 2
2
1
4
3 1 2
4 1 2
3 1 1
4 1 1

Output
5
18446744073709551613
18446744073709551614
0

【题目链接】 Jiu Yuan Wants to Eat
【题意】
给定一棵以1为根的树,存在路径加,路径乘,路径的节点权值取反,查询路径和等四个操作。
【思路】
路径加、路径乘、路径查询是最基本的树链剖分操作,但是这里还涉及到路径取反操作。11000 取反为00111,相当于(11111-11000)=00111即将原来的值先乘上-1,再加 2 64 1 2^{64}-1 ,相当于减1。每次膜 2 64 2^{64} ,利用unsigned long long 自然溢出即可

这样就可以把取反操作转化为加法和乘法操作。

对于路径加和乘法的计算,可以使用两个数组add, mul来线段树维护,线段树上每个节点的值为 t r e e [ ] m u l + a d d tree[]*mul+add

修改时:
+ v a l a d d + v a l +val,则add+val
v a l a d d v a l m u l v a l *val,则add*val,mul*val

我是代码

发布了259 篇原创文章 · 获赞 100 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/my_sunshine26/article/details/82780574