焦作网络赛E-JiuYuanWantstoEat【树链剖分】【线段树】
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
题目来源
题意:
有一棵树 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【树链剖分】【线段树】的更多相关文章
- 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 ...
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树
[BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...
- BZOJ2243 (树链剖分+线段树)
Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- bzoj4034 (树链剖分+线段树)
Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...
- HDU4897 (树链剖分+线段树)
Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...
- 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 ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- HDU 2460 Network(双连通+树链剖分+线段树)
HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...
随机推荐
- vs2008 x64编译环境 忽略了 #ifdef WIN32
解决方法: 右键项目属性,在预处理器中添加WIN32即可 效果:
- 常用Javascript函数与原型功能收藏
// 重复字符串 String.prototype.repeat = function(n) { return new Array(n+1).join(this); } // 替换全部 String. ...
- js中的var
vars变量预解析 JavaScript中,你可以在函数的任何位置声明多个var语句,并且它们就好像是在函数顶部声明一样发挥作用,这种行为称为 hoisting(悬置/置顶解析/预解析).当你使用了一 ...
- iOS开发- UILabel 自己主动换行 及 高度自适应
主要是今天看到论坛有人问这个问题.帮忙解决之后, 顺便Mark下. 非常easy, 代码写的非常清楚. 直接上代码. UILabel *label = [[UILabel alloc] initWit ...
- 怎样设置easyui中datagrid行高
$('#face_table2').datagrid({ title: '信息', iconCls: 'icon-save', url: 'callro ...
- delphi中设置listview行高的方法
第一步.在form中放置一个ImageList: 第二步.将ListView的SmallImages设置为第一步中放置的ImageList: 第三部.将imageList的height设置成自己需要的 ...
- MathType编辑半直积符号的步骤
在数学中,特别是叫做群论的抽象代数领域中,半直积(semidirect product)是从其中一个是正规子群的两个子群形成一个群的特定方法.半直积是直积的推广.半直积是作为集合的笛卡尔积,但带有特定 ...
- 了解 Go 1.9 的类型别名
http://colobu.com/2017/06/26/learn-go-type-aliases/
- 0060 Spring MVC的数据类型转换--ConversionService--局部PropertyEditor--全局WebBindingInitializer
浏览器向服务器提交的数据,多是字符串形式,而有些时候,浏览器需要Date.Integer等类型的数据,这时候就需要数据类型的转换器 使用Spring的ConversionService及转换器接口 下 ...
- C#获取CPU处理器核心数量的方法_C#教程
https://yq.aliyun.com/ziliao/89096 摘要: 本文讲的是C#获取CPU处理器核心数量的方法_C#教程, 有几条不同的处理器信息,您可以获得有关的信息:物理处理器数量.核 ...