题目链接:https://nanti.jisuanke.com/t/31714

题意:给你一棵树,初始全为0,有四种操作:

1.u-v乘x    2.u-v加x   3. u-v取反  4.询问u-v的和

思路:

除去第三个操作就是很简单的树链剖分+线段树多重标记下放,所以我们只要考虑怎么维护第三个操作就好了,

由题目给的取反可知:!x =  (2^64-1) - x;   但是这样维护还是很麻烦,因为这道题是对2^64取模的,我们可以

尝试把这个式子转换成只有加法和乘法的,这样就可以将其和前面两个操作一起维护降低难度。

我们可以想到: (-x)%(2^64) = (2^64-1)*x%(2^64),那么式子就可以转换成:

!x = (2^64-1)*x + (2^64-1) 这样这个式子就只有乘法和加法操作,以及一个常数,然后带入多重标记维护就好了

数组要开 unsigned long long ,题目给的模数也很特殊,2^64, 因为unsigned long long 溢出的时候相当于对2^64

取模,那么我们就不需要取模操作了,直接运算就完事了。

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
#define ll unsigned long long
const ll inf = ;
const int M = 2e5+; vector<int>g[M];
int n,cnt1,cnt,a[M];
int son[M],fa[M],head[M],siz[M],top[M],dep[M],tid[M];
ll sum[M<<],add[M<<],mul[M<<]; void dfs1(int u,int faz,int deep){
dep[u] = deep;
fa[u] = faz;
siz[u] = ;
for(int i = ;i < g[u].size();i++){
int v = g[u][i];
//cout<<v<<" ";
if(v != fa[u]){
dfs1(v,u,deep+);
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] = ++cnt;
if(son[u] == -) return;
dfs2(son[u],t);
for(int i = ;i<g[u].size();i++){
int v = g[u][i];
if(v != son[u]&&v != fa[u])
dfs2(v,v);
}
} void pushup(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
} void pushdown(int m,int rt){
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];
sum[rt<<] = sum[rt<<]*mul[rt] + add[rt]*(m-(m>>));
sum[rt<<|] = sum[rt<<|]*mul[rt]+add[rt]*(m>>);
add[rt] = ; mul[rt] = ;
} void build(int l,int r,int rt){
add[rt] = ; mul[rt] = ;
if(l == r){
sum[rt] = ;
return;
}
mid;
build(lson); build(rson);
pushup(rt);
} void update(int L,int R,ll c,int v,int l,int r,int rt){
if(L <= l&&R >= r){
if(v == ){
sum[rt] = sum[rt]*c;
add[rt] = add[rt]*c;
mul[rt] = mul[rt]*c;
}
else if(v == ){
sum[rt] = sum[rt] + (ll)c*(r-l+);
add[rt] = add[rt]+c;
}
else if(v == ){
sum[rt] = inf*sum[rt] + (r-l+)*inf;
add[rt] = add[rt]*inf+inf;
mul[rt] *= inf;
}
return;
}
pushdown(r-l+,rt);
int m = (l + r) >> ;
if(L <= m) update(L,R,c,v,lson);
if(R > m) update(L,R,c,v,rson);
pushup(rt);
} ll query(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return sum[rt];
}
mid;
pushdown(r-l+,rt);
ll ret = ;
if(L <= m) ret += query(L,R,lson);
if(R > m) ret += query(L,R,rson);
return ret;
} void cover(int x,int y,ll c,int v){
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,v,,n,);
x = fa[fx];fx = top[x];
}
if(dep[x] < dep[y]) swap(x,y);
update(tid[y],tid[x],c,v,,n,);
} ll ask(int x,int y){ //求两结点路径上的权值和
int fx = top[x],fy = top[y];
ll ans = ;
while(fx != fy){
if(dep[fx] < dep[fy]) swap(fx,fy),swap(x,y);
ans += query(tid[fx],tid[x],,n,);
x = fa[fx]; fx = top[x];
}
ans += (dep[x] > dep[y])?query(tid[y],tid[x],,n,):query(tid[x],tid[y],,n,);
return ans;
} void init(){
cnt = cnt1 = ;
memset(son,-,sizeof(son));
dep[] = ; fa[] = ; siz[] = ;
} int main()
{
int x,q,op,l,r;
ll c;
while(scanf("%d",&n)!=EOF){
init();
// cout<<inf<<endl;
for(int i = ;i <= n;i ++){
scanf("%d",&x);
g[x].push_back(i);
}
dfs1(,,);
dfs2(,);
build(,n,);
scanf("%d",&q);
while(q--){
scanf("%d",&op);
if(op == ||op == ){
scanf("%d%d%llu",&l,&r,&c);
cover(l,r,c,op);
}
else if(op == ){
scanf("%d%d",&l,&r);
cover(l,r,,op);
}
else {
scanf("%d%d",&l,&r);
printf("%llu\n",ask(l,r));
}
}
for(int i = ;i <= n;i ++)
g[i].clear();
}
return ;
}

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

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

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

  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. 如何利用Skyline的TerraExplorer Pro 6.5提供的API接口实现矢量图层数据的动态投影

    Skyline 支持国内常见的地图投影坐标系,包括WGS84.Beijing54.西安80.2000坐标系等,也可以自定义坐标系,比如一些做过参数变换加密的坐标系等. <!DOCTYPE htm ...

  2. Spring Boot 之 Profile 使用

    Spring Boot 之 Profile 使用 一个应用为了在不同的环境下工作,常常会有不同的配置,代码逻辑处理.Spring Boot 对此提供了简便的支持. 关键词: @Profile.spri ...

  3. C#实现一张塔松叶

    前段时间,Insus.NET有实现一组字符串在输出时,靠左或靠右对齐.<输出的字符靠右对齐>http://www.cnblogs.com/insus/p/7953304.html 现在In ...

  4. WPF中, 启用添加到RichTextBox中的控件

    原文:WPF中, 启用添加到RichTextBox中的控件   WPF中, 启用添加到RichTextBox中的控件                                           ...

  5. Java并发——线程中断学习

    1. 使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一个标志,表示它已经被中断,并立即返 ...

  6. zuul简单使用

    zuul路由的几个配置参数1.静态路由 zuul: routes: myroute1: path: /mypath/** url: http://localhost:8080 (注意这里url要htt ...

  7. POJ1850&&1019&&1942

    这三道题都水的难以想象,所以就放在一起写 1850 题目大意:有一种一种序列排列方式(如同题目中给出的例子),然后给你一个序列,问你这个序列的排名 首先我们先判断无解的情况,这个就很简单了. 由于题目 ...

  8. Scala学习(四)---映射和元组

    映射和元组 摘要: 一个经典的程序员名言是:"如果只能有一种数据结构,那就用哈希表吧".哈希表或者更笼统地说映射,是最灵活多变的数据结构之一.映射是键/值对偶的集合.Scala有一个通用的叫法:元组, ...

  9. 小白学Docker之基础篇

    系列文章: 小白学Docker之基础篇 小白学Docker之Compose 小白学Docker之Swarm PS: 以下是个人作为新手小白学习docker的笔记总结 1. docker是什么 百科上的 ...

  10. 几何学观止(Riemann流形部分)

    上承这个页面,相较之前,增加了古典的曲线曲面论,这部分介绍得很扼要,Riemann流形介绍得也很快,花了仅仅30页就介绍到了Gauss-Bonnet公式.同时配上了提示完整的习题. 几何学观止-Rie ...