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 nn nodes, each node ii contains weight a[i]a[i], the initial value of a[i]a[i] is 00. The root number of the tree is 11. Now you need to do the following operations:

1)1) Multiply all weight on the path from uu to vv by xx

2)2) For all weight on the path from uu to vv, increasing xx to them

3)3) For all weight on the path from uu to vv, change them to the bitwise NOT of them

4)4) Ask the sum of the weight on the path from uu to vv

The answer modulo 2^{64}264.

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\backsim\backsim\backsim∽∽∽

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 00 become 11, and those that are 11 become 00. 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 nn, and the number of nodes.

The second line contains (n - 1)(n−1) integers b_ibi​, which means that the father node of node (i +1)(i+1) is b_ibi​.

The third line contains one integer mm, which means the number of operations,

The next mm lines contain the following four operations:

At first, we input one integer opt

1)1) If opt is 11, then input 33 integers, u, v, xu,v,x, which means multiply all weight on the path from uu to vv by xx

2)2) If opt is 22, then input 33 integers, u, v, xu,v,x, which means for all weight on the path from uu to vv, increasing xx to them

3)3) If opt is 33, then input 22 integers, u, vu,v, which means for all weight on the path from uu to vv, change them to the bitwise NOT of them

4)4) If opt is 44, then input 22 integers, u, vu,v, and ask the sum of the weights on the path from uu to vv

1 \le n,m,u,v \le 10^51≤n,m,u,v≤105

1 \le x < 2^{64}1≤x<264

Output

For each operation 44, output the answer.

样例输入复制

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

样例输出复制

5
18446744073709551613
18446744073709551614
0

题目来源

ACM-ICPC 2018 焦作赛区网络预赛

题意:

有一棵树 4种类型的操作

1 u v x表示将u到v路径上的点的值乘以x

2 u v x表示将u到v路径上的点的值加x

3 u v 表示将u到v路径上的点的值取反

4 u v 表示查询u到v路径上所有点值之和

答案取模2^64

思路:

虽然操作乍一看就是线段树 但是和路径相关需要用的树链剖分了

124都是常见操作 只有3比较麻烦

应该要考虑到(-x)%(2^64) = (2^64-1)*x%(2^64)

-x = !x + 1

!x = (2^64-1)*x + (2^64-1) 就可以转换为乘一个数再加一个数了

因此线段树用三个数组维护 一个是sum存区间之和 add是加的lazy数组 mul是乘的lazy数组

由于答案取模2^64 比较特殊

用unsigned long long 位数刚好 溢出相当于取模

由于用到了dfs序 写的时候要注意标号的起始

 //#include"pch.h"

 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<cmath>
#include<cstring>
#include<set>
#include<stack>
//#include<bits/stdc++.h> #define inf 18446744073709551615
using namespace std;
typedef unsigned long long LL; const int MAXN = 2e5 + ;
int siz[MAXN];//number of son
int top[MAXN];//top of the heavy link
int son[MAXN];//heavy son of the node
int dep[MAXN];//depth of the node
int faz[MAXN];//father of the node
int tid[MAXN];//ID -> DFSID
int rnk[MAXN];//DFSID -> ID
int head[MAXN], cnt, n, m, cntid;
LL sum[MAXN << ], add[MAXN << ], mul[MAXN << ];
struct edge {
int to;
int next;
}edg[MAXN]; void addedge(int u, int v)
{
edg[cnt].to = v;
edg[cnt].next = head[u];
head[u] = cnt++;
} void dfs1(int u, int father, int depth)
{
dep[u] = depth;
faz[u] = father;
siz[u] = ; for (int i = head[u]; i != -; i = edg[i].next) {
int v = edg[i].to;
if (v != faz[u]) {
dfs1(v, u, depth + );
siz[u] += siz[v];
if (son[u] == - || siz[v] > siz[son[u]]) {
son[u] = v;
}
}
}
} void dfs2(int u, int t)
{
top[u] = t;
tid[u] = cntid;
rnk[cntid] = u;
cntid++; if (son[u] == -) {
return;
}
dfs2(son[u], t);
for (int i = head[u]; i != -; i = edg[i].next) {
int v = edg[i].to;
if (v != son[u] && v != faz[u]) {
dfs2(v, v);
}
}
} void pushup(int rt)
{
sum[rt] = sum[rt << ] + sum[rt << | ];
} void pushdown(int rt, int l, int r)
{
add[rt << ] = add[rt << ] * mul[rt] + add[rt];
add[rt << | ] = add[rt << | ] * mul[rt] + add[rt];
mul[rt << ] = mul[rt << ] * mul[rt];
mul[rt << | ] = mul[rt << | ] * mul[rt];
int m = (l + r) / ;
sum[rt << ] = sum[rt << ] * mul[rt] + add[rt] * (m - l + );
sum[rt << | ] = sum[rt << | ] * mul[rt] + add[rt] * (r - m);
add[rt] = ;
mul[rt] = ;
} void build(int rt, int l, int r)
{
if (l == r) {
return;
}
int m = (l + r) / ;
build(rt << , l, m);
build(rt << | , m + , r);
pushup(rt);
} void update(int L, int R, LL c, int type, int l, int r, int rt)
{
if (L <= l && R >= r) {
if (type == ) {
sum[rt] = sum[rt] * c;
add[rt] = add[rt] * c;
mul[rt] = mul[rt] * c;
}
else if (type == ) {
sum[rt] = sum[rt] + (LL)c * (r - l + );
add[rt] += c;
}
else if (type == ) {
sum[rt] = sum[rt] * inf + inf * (r - l + );
add[rt] = add[rt] * inf + inf;
mul[rt] *= inf;
}
return;
}
pushdown(rt, l, r);
int m = (l + r) / ;
if (L <= m) {
update(L, R, c, type, l, m, rt << );
}
if (R > m) {
update(L, R, c, type, m + , r, rt << | );
}
pushup(rt);
} LL query(int L, int R, int l, int r, int rt)
{
if (L <= l && R >= r) {
return sum[rt];
}
int m = (l + r) / ;
LL res = ;
pushdown(rt, l, r);
if (L <= m) {
res += query(L, R, l, m, rt << );
}
if (R > m) {
res += query(L, R, m + , r, rt << | );
}
return res;
} LL query_path(int x, int y)
{
LL ans = ;
int fx = top[x], fy = top[y];
while (fx != fy) {
if (dep[fx] < dep[fy]) {
swap(fx, fy);
swap(x, y);
}
ans += query(tid[fx], tid[x], , n, );
x = faz[fx];
fx = top[x];
} ans += (dep[x] > dep[y])?query(tid[y], tid[x], , n, ):query(tid[x], tid[y], , n, );
return ans;
} void update_path(int x, int y, LL c, int type)
{
int fx = top[x], fy = top[y];
while (fx != fy) {
if (dep[fx] < dep[fy]) {
swap(fx, fy);
swap(x, y);
}
update(tid[fx], tid[x], c, type, , n, );
x = faz[fx];
fx = top[x];
}
if(dep[x] < dep[y]){
swap(x, y);
}
update(tid[y], tid[x], c, type, , n, );
} void init()
{
memset(head, -, sizeof(head));
memset(son, -, sizeof(son));
cnt = ;
cntid = ;
memset(add, , sizeof(add));
memset(mul, , sizeof(mul));
memset(sum, , sizeof(sum));
} int main()
{
while (scanf("%d", &n) != EOF) {
init();
for (int i = ; i < n; i++) {
int b;
scanf("%d", &b);
addedge(b, i + );
}
dfs1(, , );
dfs2(, );
build(, , n);
scanf("%d", &m);
for (int i = ; i < m; i++) {
int op, u, v;
LL x;
scanf("%d%d%d", &op, &u, &v);
if (op == || op == ) {
scanf("%lld", &x);
}
if (op == ) {
printf("%llu\n", query_path(u, v));
}
else {
if(op == ){
update_path(u, v, , op);
}
else {
update_path(u, v, x, op);
}
}
}
}
return ;
}

焦作网络赛E-JiuYuanWantstoEat【树链剖分】【线段树】的更多相关文章

  1. ACM-ICPC 2018 焦作赛区网络预赛 E Jiu Yuan Wants to Eat (树链剖分+线段树)

    题目链接:https://nanti.jisuanke.com/t/31714 题意:给你一棵树,初始全为0,有四种操作: 1.u-v乘x    2.u-v加x   3. u-v取反  4.询问u-v ...

  2. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  3. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  4. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  5. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  6. bzoj4034 (树链剖分+线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  7. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  8. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  9. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  10. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

随机推荐

  1. ADSI Edit 工具

    最近在弄.net的活动目录用到了工具ADSI Edit,网上找了点资料,一来自己记录下,二来分享给大家: 下载的压缩包里存在两个文件adsiedit.dll和adsiedit.msc 1.将adsie ...

  2. android 创建 xml文件

    android创建xml文件的方法. 要操作android的外部存储,所以要在AndroidManifest.xml文件中添加权限. <uses-permission android:name= ...

  3. Extracting and composing robust features with denosing autoencoders 论文

    这是一篇发表于2008年初的论文. 文章主要讲了利用 denosing autoencoder来学习 robust的中间特征..进上步,说明,利用这个方法,可以初始化神经网络的权值..这就相当于一种非 ...

  4. matlab中常用见的小知识点

    矩阵相关: 在matlab中,矩阵或向量是 column-major 表示形式.用 [] 来构建向量或矩阵, 用()来引用向量或矩阵中的元素:用:表示矩阵中的该index下的所以元素: matlab中 ...

  5. Socket网络编程精华篇

    几个和Socket编程紧密相关的概念: TCP/IP层次模型 当然这里我们只讨论重要的四层 01,应用层(Application):应用层是个很广泛的概念,有一些基本相同的系统级TCP/IP应用以及应 ...

  6. 一个简单的Golang实现的HTTP Proxy

    http://blog.csdn.net/michael__li/article/details/53941312

  7. mybatise 动态sql

    1. <if><choose> 动态sql 相当 <if> Java if  满足多个条件  <choose> <when>    java ...

  8. mongodb 安装(windows mongodb 安装)

    MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型.M ...

  9. HTML5开发之 -- 模态突出窗(bootstrap)

    最近在学习web端开发相关,bootstrap非常好用! 有个模态弹出窗的效果,在此记录下: 1.导入: <script src="libs/js/jquery-3.2.1.min.j ...

  10. Python 进阶(二)模块

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAW4AAABpCAIAAACVsl7UAAAgAElEQVR4nO2993vUxho2/P4r33XwSr