焦作网络赛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 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...
随机推荐
- bcm56150_i2c驱动分析
本文主要关注bsp中,关于smbus(系统管理总线,是i2c的子集)的配置过程,了解如如何配置i2c寄存器.所有发送的数据都会写在FIFO中,使能之后就发送出去.接收数据就从接收寄存器中读取.读取和发 ...
- C++ 模板类友元之输出流操作符重载
几个关键点: 需要前置声明!--奇怪的是别人告诉我也可以不需要,但我这里不行! 友元函数的函数名后面的<>,必须要有. #include <stdio.h> #include ...
- javax.naming.NoInitialContextException错误的解决方案
今天,学习用了一下nutz框架,写了一个HelloWorld的小程序,在用jndi配置数据源时,写了一个测试类,并在main方法中调用了jndi获得数据库连接,但是报错了,错误信息如下: javax. ...
- dm8127-内存分配
在前天一直完车辆捕获算法和车牌识别算法之后,算法移植告一段落,五月份以来,总算有点欣慰了,可是cmos采集视频有点问题,主要是前端采集不是我接手,嵌入式部门的小宋和小李负责,据说是20多万没了图纸,防 ...
- CentOS下的一些基础问题解答
1. 在/etc/passwd中某一行信息为“Linux01:x:505:505:/home/linux12:/bin/bash”,由此可知哪些信息? 用户名为linux01,需要密码登陆,用户ID为 ...
- R语言中两个数组(或向量)的外积怎样计算
所谓数组(或向量)a和b的外积,指的是a的每个元素和b的每个元素搭配在一起相乘得到的新元素.当然运算规则也可自己定义.外积运算符为 %o%(注意:百分号中间的字母是小写的字母o).比如: > a ...
- java web - 为什么要使用spring struts
1.软件里有很多优秀的框架,有一种类型的框架,它的特点是建立在一个现有技术的基础上,提供和现有技术一样业务功能的技术框架,这个新的技术框架比原技术更加易用,更加健壮同时功能更加强大,例如:jQuery ...
- SharePoint 2010用“localhost”方式访问网站,File not found问题处理方式
场景:本地服务器上,用“localhost”方式访问网站:在某网站集(Site Collection)下的子网站(Sub Site)中,点击网站权限菜单(Site permissions)等关于调用L ...
- MVC Razor与javascript混编(js中嵌入razor)
其中的关键是输出js上的纯文本内容,让浏览器解析为其中的js代码 <script> BUI.use('common/main',function(){ var conf ...
- select2 选择框插件
<select id="selBusi_type"><select> //初始化业务类型下拉 var initBusiTypeSel = function( ...