焦作网络赛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 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...
随机推荐
- Python 之 向上取整、向下取整以及四舍五入函数
import math f = 11.2 print math.ceil(f) #向上取整 print math.floor(f) #向下取整 print round(f) #四舍五入 #这三个函数的 ...
- 远程桌面能连接到服务器,但PING不通
解决方法:
- linux -- ubuntu 14.10开机出现错误“Error found when loading /root/.profile”解决
修改完root权限自动登录后,发现开机出现以下提示: Error found when loading /root/.profile stdin:is not a tty ………… 解决方法:在终端中 ...
- LINUX下CPU Load Average的一点研究
背景: 公司的某个系统工作在基于Linux的Cent OS下,一个host下同时连接了许多client, 最近某台Host总是显示CPU Load Average过高,我们单纯的以为是CPU的占用过高 ...
- java面试题------40个Java集合面试问题和答案
Java集合框架为Java编程语言的基础,也是Java面试中非常重要的一个知识点. 这里,我列出了一些关于Java集合的重要问题和答案. 1.Java集合框架是什么?说出一些集合框架的长处? 每种编程 ...
- 如何研究某个gene的ceRNA 网络
研究人员针对 PTEN 这个关键的抑癌基因,来探究调控该基因表达的ceRNA 网络: 分析策略: 1)预测能调控该基因的miRNAs 通过miRanda 软件预测和实验验证相结合的方式,挑选出 miR ...
- ros网址链接
安装教程:http://wiki.ros.org/cn/indigo/Installation robotics:http://www.rethinkrobotics.com/ 学习教程:http:/ ...
- SQLServer------Sql Server性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON
转载: http://www.cnblogs.com/xqhppt/p/4041799.html
- GIS-009-Cesium 使用
//加载ArcGIS 发布的地图服务MapServervar url='http://Jason:6080/arcgis/rest/services/SampleWorldCities/MapServ ...
- ActiveMQ内存配置和密码设置
1.配置内存 bin中activemq.bat 中的第一行 加上 : REM 配置内存 set ACTIVEMQ_OPTS=-Xms1G -Xmx1G 2.修改控制台密码 1.打开conf/jetty ...