Jiu Yuan Wants to Eat(树链剖分+线段树延迟标记)
Jiu Yuan Wants to Eat
https://nanti.jisuanke.com/t/31714
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
题目来源
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#define maxn 100005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef unsigned long long ull;
using namespace std; ull tree[maxn<<],lazya[maxn<<],lazym[maxn<<];
int n;
ull v[maxn],val[maxn];
int dep[maxn],fa[maxn],siz[maxn],son[maxn],id[maxn],top[maxn],cnt;
vector<ull>ve[maxn]; void pushup(int rt){
tree[rt]=tree[rt<<]+tree[rt<<|];
} void pushdown(int len,int rt){
if(lazya[rt]||lazym[rt]!=){
lazym[rt<<]*=lazym[rt];
lazym[rt<<|]*=lazym[rt];
lazya[rt<<]*=lazym[rt];
lazya[rt<<|]*=lazym[rt];
lazya[rt<<]+=lazya[rt];
lazya[rt<<|]+=lazya[rt];
tree[rt<<]*=lazym[rt];
tree[rt<<|]*=lazym[rt];
tree[rt<<]+=(len-len/)*lazya[rt];
tree[rt<<|]+=len/*lazya[rt];
lazya[rt]=;
lazym[rt]=;
}
} void build(int l,int r,int rt){
lazya[rt]=;
lazym[rt]=;
if(l==r){
tree[rt]=;
return;
}
int mid=(l+r)/;
build(lson);
build(rson);
pushup(rt);
} void mul(int L,int R,ull k,int l,int r,int rt){
if(L<=l&&R>=r){
pushdown(r-l+,rt);
tree[rt]*=k;
lazym[rt]*=k;
lazya[rt]*=k;
return;
}
int mid=(l+r)/;
pushdown(r-l+,rt);
if(L<=mid) mul(L,R,k,lson);
if(R>mid) mul(L,R,k,rson);
pushup(rt);
} void add(int L,int R,ull k,int l,int r,int rt){
if(L<=l&&R>=r){
pushdown(r-l+,rt);
tree[rt]+=(r-l+)*k;
lazya[rt]+=k;
return;
}
int mid=(l+r)/;
pushdown(r-l+,rt);
if(L<=mid) add(L,R,k,lson);
if(R>mid) add(L,R,k,rson);
pushup(rt);
} void Not(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
pushdown(r-l+,rt);
tree[rt]+=(r-l+);
tree[rt]=-tree[rt];
lazym[rt]=-lazym[rt];
lazya[rt]++;
lazya[rt]=-lazya[rt];
return;
}
int mid=(l+r)/;
pushdown(r-l+,rt);
if(L<=mid) Not(L,R,lson);
if(R>mid) Not(L,R,rson);
pushup(rt);
} ull query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return tree[rt];
}
int mid=(l+r)/;
pushdown(r-l+,rt);
ull ans=;
if(L<=mid) ans+=query(L,R,lson);
if(R>mid) ans+=query(L,R,rson);
pushup(rt);
return ans;
} void dfs1(int now,int f,int deep){
dep[now]=deep;
siz[now]=;
fa[now]=f;
int maxson=-;
for(int i=;i<ve[now].size();i++){
if(ve[now][i]==f) continue;
dfs1(ve[now][i],now,deep+);
siz[now]+=siz[ve[now][i]];
if(siz[ve[now][i]]>maxson){
maxson=siz[ve[now][i]];
son[now]=ve[now][i];
}
}
} void dfs2(int now,int topp){
id[now]=++cnt;
val[cnt]=v[now];
top[now]=topp;
if(!son[now]) return;
dfs2(son[now],topp);
for(int i=;i<ve[now].size();i++){
if(ve[now][i]==son[now]||ve[now][i]==fa[now]) continue;
dfs2(ve[now][i],ve[now][i]);
}
} ull qRange(int x,int y){
ull ans=;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
ans+=query(id[top[x]],id[x],,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
ans+=query(id[x],id[y],,n,);
return ans;
} void addRange(int x,int y,int k){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
add(id[top[x]],id[x],k,,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
add(id[x],id[y],k,,n,);
} void mulRange(int x,int y,int k){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
mul(id[top[x]],id[x],k,,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
mul(id[x],id[y],k,,n,);
} void notRange(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
Not(id[top[x]],id[x],,n,);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
Not(id[x],id[y],,n,);
} int main(){
int m,r;
while(~scanf("%d",&n)){
memset(v,,sizeof(v));
memset(val,,sizeof(val));
memset(dep,,sizeof(dep));
memset(fa,,sizeof(fa));
memset(siz,,sizeof(siz));
memset(son,,sizeof(son));
memset(id,,sizeof(id));
memset(top,,sizeof(top));
int pos,z,x,y;
for(int i=;i<=n;i++){
ve[i].clear();
}
for(int i=;i<=n;i++){
scanf("%d",&x);
ve[x].push_back(i);
ve[i].push_back(x);
}
cnt=;
dfs1(,,);
dfs2(,);
build(,n,);
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d %d %d",&pos,&x,&y);
if(pos==){
scanf("%d",&z);
mulRange(x,y,z);
}
else if(pos==){
scanf("%d",&z);
addRange(x,y,z);
}
else if(pos==){
notRange(x,y);
}
else{
printf("%llu\n",qRange(x,y));
}
}
}
}
Jiu Yuan Wants to Eat(树链剖分+线段树延迟标记)的更多相关文章
- 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 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...
随机推荐
- 为MyEclipse配置Spring的约束
1.Spring框架概述 Spring是一个Service层的框架,可以整合许多其它框架进行工作 Spring的主要技术是 IOC(DI) AOP IOC - 控制反转(依赖注入) AOP - 免息那 ...
- ubuntu 常用命令集锦
一.文件/文件夹管理 ls 列出当前目录文件(不包括隐含文件) ls -a 列出当前目录文件(包括隐含文件) ls -l 列出当前目录下文件的详细信息 cd .. 回当前目录的上一级目录 cd - 回 ...
- Jquery_artDialog对话框弹出
artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口l 自适应内容artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适 ...
- matlab下将图片序列转化为视频文件 && 将为视频文件转化图片序列
将图片序列转化为视频文件 程序如下: framesPath = 'E:\img\';%图像序列所在路径,同时要保证图像大小相同 videoName = 'Bolt.avi';%表示将要创建的视频文件的 ...
- javascript的防篡改对象之preventExtensions()方法
js在默认情况下,所有的对象都是可扩展的.这也是让很多开发人员头特疼的问题.因为在同一环境中,一不小心就会发生修改了不必要的对象,而自己却不知道. 在ECMAScript5可以解决这种问题了. pre ...
- 11g RAC 更改归档模式 ,归档文件存放在ASM 磁盘组(转载)
11g RAC 更改归档模式 ASM 本实验有两个节点tip01,tip02oracle_sid 分别是 tips1,tips2 1.在节点1 tip01上执行[root@tip01 ~]# su - ...
- sqoop操作之ORACLE导入到HIVE
导入表的所有字段 sqoop import --connect jdbc:oracle:thin:@192.168.1.107:1521:ORCL \ --username SCOTT --passw ...
- python3 string
字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hello World!' var2 ...
- (4/24) webpack3.x快速搭建本地服务和实现热更新
写在前面: (1)为了防止版本兼容问题,此处的webpack版本与之前的一致为:webpack@3.6.0.同时这里我们安装的webpack-dev-server版本是2.9.7版本. (2)之前已经 ...
- centos7防火墙使用方法
参考网站:https://blog.csdn.net/achang21/article/details/52538049 添加开放指定端口: [root@yao bin]# firewall-cmd ...