焦作网络赛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 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...
随机推荐
- 第二百九十节,MySQL数据库-MySQL命令行导出导入数据库,数据库备份还原
MySQL命令行导出导入数据库,数据库备份还原 MySQL命令行导出数据库:1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录如我输入的命令行:cd C:\Program ...
- UIView的几个枚举定义
UIView是iOS开发最主要的视图,非常多控件都是继承它,掌握当中的几个基本枚举定义,有利益理解视图的载入和參数差别. 一.UIViewAnimationCurve UIView的基本动画变化规律 ...
- 【Java面试题】59 Math.round(11.5)等於多少? Math.round(-11.5)等於多少?
Math类中提供了三个与取整有关的方法:ceil.floor.round,这些方法的作用与它们的英文名称的含义相对应,例如,ceil的英文意义是天花板,该方法就表示向上取整,Math.ceil(11. ...
- C# Serializable(转)
C# Serializable System.SerializableAttribute 串行化是指存储和获取磁盘文件.内存或其他地方中的对象.在串行化时,所有的实例数据都保存到存储介质上,在取消串行 ...
- 第四章 Spring.Net 如何管理您的类___对象的手动装配
前面我们知道了什么是对象,什么是对象工厂,什么是应用程序上下文.这一次我们来看一下对象的装配. Spring.Net 中有多种装配对象的方式,装配这个词可能比较学术化,我们可以理解为对象的创建. Sp ...
- 南京IT企业环境之最深心得体会
我是南京做嵌入式的. 之前搞过一年的PC平台Linux内核开发,Linux内核态的仅仅要不是非常复杂的BUG还是能修复的.一年的Linux用户态软件开发. 然后近期搞了两年ARM嵌入式开发. 做的CM ...
- swift - 将表格滚动条移动到底部
类似聊天那种效果,最新出现消息后,会自动滚动到表的底部,具体代码实现如下: 1,使用scrollToRow方法: let mySec = 1//最后一个分组的索引(0开始,如果没有分组则为0 let ...
- Python 流程控制:if
语法: if 判断条件1: # 如果判断条件1成立,就执行语句1 语句1... if 判断条件1: # 如果判断条件1成立,就执行语句1,否则执行语句2 语句1... else: 语句2... if ...
- osgEarth中的StringUtils头文件中有很多关于字符串的操作
- Centos下Nagios的安装与配置
一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报 ...