题目描述

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 n nodes, each node i contains weight a[i], the initial value of a[i] is 0.  The root number of the tree is 1. Now you need to do the following operations:
1) Multiply all weight on the path from u to v by x
2) For all weight on the path from u to v, increasing x to them
3) For all weight on the path from u to v, change them to the bitwise NOT of them
4) Ask the sum of the weight on the path from u to v
The answer modulo 2^64.

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~~~

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 0 become 1, and those that are 1 become 0. For example:

NOT 0111 (decimal 7) = 1000 (decimal 8)
NOT 10101011 = 01010100

输入

The input contains multiple groups of data.
For each group of data, the first line contains a number of n, and the number of nodes.
The second line contains (n - 1) integers bi, which means that the father node of node (i +1) is bi.
The third line contains one integer m, which means the number of operations,
The next m lines contain the following four operations:
At first, we input one integer opt
1) If opt is 1, then input 3 integers, u, v, x, which means multiply all weight on the path from u to v by x
2) If opt is 2, then input 3 integers, u, v, x, which means for all weight on the path from u to v, increasing x to them
3) If opt is 3, then input 2 integers, u, v, which means for all weight on the path from u to v, change them to the bitwise NOT of them
4) If opt is 4, then input 2 integers, u, v, and ask the sum of the weights on the path from u to v

1 ≤ n, m, u, v ≤ 10^5
1 ≤ x < 2^64

输出

For each operation 4, 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
题意
给一棵n个节点的有根树,每个节点有权值,初始是0,m次操作
u v x:给u v路径上的点权值*x
u v x:给u v路径上的点权值+x
u v:给u v路径上的点权值取反
u v:询问u v路径上的权值和,对2^64取模 树链剖分:https://wenku.baidu.com/view/a088de01eff9aef8941e06c3.html 然后如果没有取反操作,线段树维护和sum,加法标记add和乘法标记mul即可
对于取反操作,因为是对2^64取模的,即x+(!x)=^-,所以x=(^-)-x,因此取反就变成乘法和加法了:!x=(-)*x+(-) (-1对于2^64取模后是(^-))
#include <bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int N=1e5+;
int n,m,tot,cnt;
int fa[N],last[N];
int son[N],deep[N],dfn[N],num[N],top[N];//重儿子 深度 dfs序 子树规模 所在重链的顶端节点
ull sum[N*],add[N*],mul[N*];
struct orz{
int v,nex;}e[N];
void init()
{
cnt=;
tot=;
memset(last,,sizeof(last));
memset(son,-,sizeof(son));
}
void Inses(int x,int y)
{
cnt++;
e[cnt].v=y;
e[cnt].nex=last[x];
last[x]=cnt;
}
void dfs1(int x,int d)
{
deep[x]=d;
num[x]=;
for (int i=last[x];i;i=e[i].nex)
{
int v=e[i].v;
dfs1(v,d+);
num[x]+=num[v];
if (son[x]==- || num[v]>num[son[x]]) son[x]=v;
}
}
void dfs2(int x,int sp)
{
top[x]=sp;
dfn[x]=++tot;
if (son[x]==-) return ;
dfs2(son[x],sp);
for (int i=last[x];i;i=e[i].nex)
{
int v=e[i].v;
if (v!=son[x]) dfs2(v,v);
}
}
void PushUp(int s)
{
sum[s]=sum[s<<]+sum[s<<|];
}
void PushDown(int s,int l,int r)
{
if (mul[s]!=)
{
mul[s<<]*=mul[s];
mul[s<<|]*=mul[s];
add[s<<]*=mul[s];
add[s<<|]*=mul[s];
sum[s<<]*=mul[s];
sum[s<<|]*=mul[s];
mul[s]=;
} if (add[s])
{
add[s<<]+=add[s];
add[s<<|]+=add[s];
int mid=(l+r)>>;
sum[s<<]+=(ull)(mid-l+)*add[s];
sum[s<<|]+=(ull)(r-mid)*add[s];
add[s]=;
}
} void build(int s,int l,int r)
{
sum[s]=add[s]=;
mul[s]=;
if (l==r) return ;
int m=(l+r)>>;
build(s<<,l,m); build(s<<|,m+,r);
PushUp(s);
}
void update(int s,int l,int r,int L,int R,ull val,int op)
{
//printf("s=%d,l=%d,r=%d,L=%d,R=%d\n",s,l,r,L,R);
if (L<=l&&r<=R)
{
if (l!=r) PushDown(s,l,r);
if (op==)
{
mul[s]*=val;
add[s]*=val;
sum[s]*=val;
}
else if (op==)
{
add[s]+=val;
sum[s]+=(ull)(r-l+)*val;
}
else
{
mul[s]*=val;
add[s]*=val;
add[s]+=val;
sum[s]=(ull)(r-l+)*val-sum[s];
}
return;
}
PushDown(s,l,r);
int mid=(l+r)>>;
if (L<=mid) update(s<<,l,mid,L,R,val,op);
if (R>mid) update(s<<|,mid+,r,L,R,val,op);
PushUp(s);
}
ull query(int s,int l,int r,int L,int R)
{
if (L<=l&&r<=R) return sum[s];
PushDown(s,l,r);
int mid=(l+r)>>;
ull ans=;
if (L<=mid) ans+=query(s<<,l,mid,L,R);
if (R>mid) ans+=query(s<<|,mid+,r,L,R);
PushUp(s);
return ans;
}
void solve(int op,int x, int y,ull val)
{
if (op==) val=-;
if (op<=)
{
while (top[x]!=top[y])
{
if (deep[top[x]]<deep[top[y]]) swap(x, y);
update(,,n,dfn[top[x]],dfn[x],val,op);
x=fa[top[x]];
}
if (deep[x]>deep[y]) swap(x,y);
update(,,n,dfn[x],dfn[y],val,op);
}
else
{
ull ans=;
while (top[x]!=top[y])
{
if (deep[top[x]]<deep[top[y]]) swap(x, y);
ans+=query(,,n,dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
if (deep[x]>deep[y]) swap(x,y);
ans+=query(,,n,dfn[x],dfn[y]);
printf("%llu\n",ans);
}
} int main()
{
while (scanf("%d",&n)!=EOF)
{
init();
for (int i=;i<=n;i++)
{
scanf("%d",&fa[i]);
Inses(fa[i],i);
}
dfs1(,);
dfs2(,);
build(,,n);
scanf("%d",&m);
int op,u,v; ull x;
while (m--)
{
scanf("%d",&op);
if (op== || op==) scanf("%d%d%llu",&u,&v,&x);
else scanf("%d%d",&u,&v);
solve(op,u,v,x);
}
}
return ;
}
 

2018焦作网络赛-E- Jiu Yuan Wants to Eat的更多相关文章

  1. 2018 ICPC 焦作网络赛 E.Jiu Yuan Wants to Eat

    题意:四个操作,区间加,区间每个数乘,区间的数变成 2^64-1-x,求区间和. 题解:2^64-1-x=(2^64-1)-x 因为模数为2^64,-x%2^64=-1*x%2^64 由负数取模的性质 ...

  2. 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 ...

  3. ACM-ICPC 2018 焦作赛区网络预赛 E. Jiu Yuan Wants to Eat (树链剖分-线性变换线段树)

    树链剖分若不会的话可自行学习一下. 前两种操作是线性变换,模\(2^{64}\)可将线段树全部用unsigned long long 保存,另其自然溢出. 而取反操作比较不能直接处理,因为其模\(2^ ...

  4. 2018焦作网络赛Mathematical Curse

    题意:开始有个数k,有个数组和几个运算符.遍历数组的过程中花费一个运算符和数组当前元素运算.运算符必须按顺序花费,并且最后要花费完.问得到最大结果. 用maxv[x][y]记录到第x个元素,用完了第y ...

  5. 2018焦作网络赛Give Candies

    一开始忽略了欧拉定理指数部分是modphi(n-1)没有memset,减法后面没加0:

  6. 2018焦作网络赛 - Poor God Water 一道水题的教训

    本题算是签到题,但由于赛中花费了过多的时间去滴吧格,造成了不必要的浪费以及智商掉线,所以有必要记录一下坑点 题意:方格从1到n,每一格mjl可以选择吃鱼/巧克力/鸡腿,求走到n格时满足 1.每三格不可 ...

  7. ACM-ICPC 2018 焦作网络赛

    题目顺序:A F G H I K L 做题链接 A. Magic Mirror 题意:判断 给出的 字符串 是否等于"jessie",需要判断大小写 题解:1.用stl库 tolo ...

  8. 2018 焦作网络赛 K Transport Ship ( 二进制优化 01 背包 )

    题目链接 题意 : 给出若干个物品的数量和单个的重量.问你能不能刚好组成总重 S 分析 : 由于物品过多.想到二进制优化 其实这篇博客就是存个二进制优化的写法 关于二进制优化的详情.百度一下有更多资料 ...

  9. 2018 焦作网络赛 G Give Candies ( 欧拉降幂 )

    题目链接 题意 : 给出 N 个糖果.老师按顺序给 1~N 编号的学生分配糖果.每个学生要么不分.要么最少分一个.且由于是按顺序发放.那么对于某个有分到糖果的编号为 i 的学生.则 1~(i-1) 这 ...

随机推荐

  1. dubbo服务调试管理实用命令

    公司如果分项目组开发的,各个项目组调用各项目组的接口,有时候需要在联调环境调试对方的接口,可以直接telnet到dubbo的服务通过命令查看已经布的接口和方法,并能直接invoke具体的方法,我们可以 ...

  2. linux 定时任务---给心爱的小姐姐发情书

    目录 1.计划任务基本概述 什么是crond? 为什么要用crond? 2.计划任务时间管理 crontab配置文件解析 crontab的时间编写规则 crontab命令选项 3.计划任务编写实践 使 ...

  3. Codeforces 1179D 树形DP 斜率优化

    题意:给你一颗树,你可以在树上添加一条边,问添加一条边之后的简单路径最多有多少条?简单路径是指路径中的点只没有重复. 思路:添加一条边之后,树变成了基环树.容易发现,以基环上的点为根的子树的点中的简单 ...

  4. 第二则java读取excel文件代码

    // 得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全 String savePath = this.getServletContext().ge ...

  5. 51单片机外部中断INT0实例(汇编程序)

    ;普中51开发板 ;单片机的P3.2(INT0)引脚与按键K3脚连接 ;用汇编语言实现:按一次K1外部中断INT0响应一次,LED显示值加1(十进制), ;前提是共阴数码LED第一位,需要设定,由P0 ...

  6. 怀旧浪潮来袭,小霸王游戏、windows95......曾经的经典哪些能戳中你的心怀?

    随着前两天上架的 Rewound 在 iPhone 上复刻了 iPod Classic为大家掀起一场怀旧浪潮,那么除了 Rewound还有什么经典?今天我们就来怀旧一下那些曾经的经典.80经典小霸王游 ...

  7. 推荐五个java基础学习网站,小白必备

    不知道去哪找java基础资料?推荐几个学习网站,小白必备 Java经过20多年的发展,仍然是世界上最受欢迎的编程语言之一,有无限多种方法使用Java.拥有庞大的客户群.并且java应用范围很广,基本只 ...

  8. Security Spring 配置

    <?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://w ...

  9. bind-dns服务器搭建

    环境:主服务器上IP为192.168.159.30 安装相关包bind dns服务器  bind-utils提供nslookup dig等命令 yum -y install bind bind-uti ...

  10. rocketmq单点部署

    下载地址:https://github.com/alibaba/RocketMQ 转载请注明来源:http://blog.csdn.net/loongshawn/article/details/510 ...