树链剖分若不会的话可自行学习一下.

前两种操作是线性变换,模\(2^{64}\)可将线段树全部用unsigned long long 保存,另其自然溢出.

而取反操作比较不能直接处理,因为其模\(2^{64}\)的特殊性,可将其转化为线性变换.

显然

\[-x\equiv (2^{64}-1)*x (mod\ 2^{64})
\]

因为$$!x = (2^{64}-1) -x $$

所以

\[!x = (2^{64}-1) + (2^{64}-1)x
\]

#include<bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
#define Lson l,m,lson
#define Rson m+1,r,rson
typedef unsigned long long LL;
LL TTT = 0xffffffffffffffff;
using namespace std;
const int maxn =1e5+5;
struct Edge{
int to,next;
}E[2*maxn];
int n,head[maxn],tot;
int cnt,idx,size[maxn],fa[maxn],son[maxn],dep[maxn],top[maxn],id[maxn],rnk[maxn]; void init()
{
cnt=idx=tot=0;
memset(head,-1,sizeof(head));
dep[1]=0,fa[1]=1,size[0]=0;
memset(son,0,sizeof(son));
} void AddEdge(int u,int v)
{
E[tot] = (Edge){v,head[u]};
head[u]=tot++;
}
void dfs1(int u)
{
size[u]=1;
for(int i=head[u];~i;i=E[i].next){
int v=E[i].to;
if(v!=fa[u]){
fa[v]=u;
dep[v]=dep[u]+1;
dfs1(v);
size[u]+=size[v];
if(size[son[u]]<size[v]) son[u]=v;
}
}
} void dfs2(int u,int topu)
{
top[u]= topu;
id[u] = ++idx;
rnk[idx] = u;
if(!son[u]) return;
dfs2(son[u],top[u]);
for(int i=head[u];~i;i=E[i].next){
int v=E[i].to;
if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
}
} struct Node{
LL sum,add,b;
bool nt;
}tree[maxn<<2];
void pushup(int rt){
tree[rt].sum = tree[lson].sum + tree[rson].sum;
} void pushdown(int l,int r,int rt)
{
int m = (l+r)>>1;
if(tree[rt].add!=1){
tree[lson].sum *= tree[rt].add;
tree[rson].sum *= tree[rt].add;
tree[lson].b *= tree[rt].add;
tree[rson].b *= tree[rt].add;
tree[lson].add *= tree[rt].add;
tree[rson].add *= tree[rt].add;
tree[rt].add = 1;
}
if(tree[rt].b){
tree[lson].sum += (m-l+1)* tree[rt].b;
tree[rson].sum += (r-m) *tree[rt].b;
tree[lson].b += tree[rt].b;
tree[rson].b += tree[rt].b;
tree[rt].b= 0;
}
} void build(int l,int r,int rt)
{
tree[rt].add =1;
tree[rt].b =0;
tree[rt].nt = 0;
if(l==r){
tree[rt].sum = 0;
return;
}
int m = (l+r)>>1;
build(Lson);
build(Rson);
pushup(rt);
} void update(int L,int R,LL k,LL b,int l=1,int r=n,int rt=1)
{
if(L<=l && R>=r){
tree[rt].sum *= k;
tree[rt].add *= k;
tree[rt].b *= k;
tree[rt].sum += (r-l+1)*b;
tree[rt].b +=b;
tree[rt].nt = 0;
return;
}
pushdown(l,r,rt);
int m = (l+r)>>1;
if(L<=m) update(L,R,k,b,Lson);
if(R>m) update(L,R,k,b,Rson);
pushup(rt);
} LL query(int L,int R,int l=1,int r= n,int rt=1)
{
if(L<=l && R>=r){
return tree[rt].sum;
}
pushdown(l,r,rt);
LL ans=0;
int m = (l+r)>>1;
if(L<=m) ans += query(L,R,Lson);
if(R>m) ans += query(L,R,Rson);
return ans;
} void change(int u,int v,int op,LL val)
{
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
if(op==1){
update(id[top[u]],id[u],(LL)1,val);
}
else if(op==2){
update(id[top[u]],id[u],val,0);
}
else{
update(id[top[u]],id[u],TTT,TTT);
}
u = fa[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
if(op==1){
update(id[u],id[v],(LL)1,val);
}
else if(op==2){
update(id[u],id[v],val,0);
}
else{
update(id[u],id[v],TTT,TTT);
}
return ;
} LL Qsum(int u,int v)
{
LL ans=0;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
ans += query(id[top[u]],id[u]);
u = fa[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
ans += query(id[u],id[v]);
return ans;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int u,v,Q;
while(scanf("%d",&n)==1){
init();
for(int i=2;i<=n;++i){
scanf("%d",&u);
AddEdge(u,i);
AddEdge(i,u);
}
dfs1(1);
dfs2(1,1);
build(1,n,1);
scanf("%d",&Q);
int op;
LL tmp;
while(Q--){
scanf("%d",&op);
if(op==1){
scanf("%d %d %lld",&u, &v, &tmp);
change(u,v,2,tmp);
}
else if(op==2){
scanf("%d %d %llu",&u, &v,&tmp);
change(u,v,1,tmp);
}
else if(op==3){
scanf("%d %d",&u, &v);
change(u,v,3,0);
}
else{
scanf("%d %d",&u, &v);
printf("%llu\n",Qsum(u,v));
}
}
}
return 0;
}

ACM-ICPC 2018 焦作赛区网络预赛 E. Jiu Yuan Wants to Eat (树链剖分-线性变换线段树)的更多相关文章

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

  2. ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)

    There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...

  3. ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  4. ACM-ICPC 2018 焦作赛区网络预赛

    这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...

  5. ACM-ICPC 2018 焦作赛区网络预赛J题 Participate in E-sports

    Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...

  6. ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship

    There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry th ...

  7. ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  8. ACM-ICPC 2018 焦作赛区网络预赛 I题 Save the Room

    Bob is a sorcerer. He lives in a cuboid room which has a length of AA, a width of BB and a height of ...

  9. ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)

    Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring won ...

随机推荐

  1. jquery合并表格中相同文本的相邻单元格

    <!DOCTYPE HTML> <html> <head>   <title>Example</title>   <meta char ...

  2. JavaScript的arguements

    ---恢复内容开始--- arguments 对象 在函数代码中,使用特殊对象 arguments,开发者无需明确指出参数名,就能访问它们. 例如,在函数 sayHi() 中,第一个参数是 messa ...

  3. hdu 3905(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3905 思路:dp[i][j]表示前i分钟,睡了j分钟收获的的最大价值,并记tmp_dp[i][j]为从 ...

  4. PagerAdapter 普通写法

    1,viewPagre的普通写法 public ImagePagerAdapter(Context context, List<Photo> imgList) { this.mContex ...

  5. iOS UIWebView 获取内容实际高度,关闭滚动效果

    本文转载至 http://my.oschina.net/Khiyuan/blog/341535   iOS UIWebView 获取内容实际高度,关闭滚动效果 近期做东西,将 UIWebView 嵌套 ...

  6. Linux Kernel 4.7版本发布

    导读 在经历了长达一周的惬意假日时光,大神Linus Torvalds宣布面向所有GNU/Linux操作系统发布Linux Kernel 4.7.Linux 4.7内核的研发历经2个多月,自今年5月2 ...

  7. SG函数入门

    sg[i]为0表示i节点先手必败. 首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数.例如mex{0,1,2,4}=3.mex{2 ...

  8. jenkins 配置 ssh插件

    一.安装SSH插件 系统管理->插件管理,在可选插件下,过滤SSH,找到publish over ssh插件,直接安装(我这里已经安装过了,在已安装选项下可以找到publish over ssh ...

  9. Windows配置MinGW环境变量

    先安装MinGW 1.添加3个系统变量(根据自己的实际路径) MinGW_INCLUDE_PATH MinGW_LIBRARY_PATH MinGW_PATH 2.将MinGW_PATH添加到Path ...

  10. Linux下安装谷歌访问助手,解压缩时出现中文乱码

    1.sudo apt-get install unar 安装unar 2.unar 谷歌访问助手chrome版本.zip   注意:使用 lsar 命令可以查看压缩文件内有那些文件: 例:lsar 谷 ...