题目链接: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. P4099 [HEOI2013]SAO

    P4099 [HEOI2013]SAO 贼板子有意思的一个题---我()竟然没看题解 有一张连成树的有向图,球拓扑序数量. 树形dp,设\(f[i][j]\)表示\(i\)在子树中\(i\)拓扑序上排 ...

  2. PyCharm Tips 常用操作帮助

    以下内容转自 http://www.2cto.com/os/201410/341542.html --------------------------------------------------- ...

  3. 网站每日PV/IP统计/总带宽/URL统计脚本分享(依据网站访问日志)

    在平时的运维工作中,我们运维人员需要清楚自己网站每天的总访问量.总带宽.ip统计和url统计等.虽然网站已经在服务商那里做了CDN加速,所以网站流量压力都在前方CDN层了像每日PV,带宽,ip统计等数 ...

  4. Daily Scrumming* 2015.12.20(Day 12)

    一.团队scrum meeting照片 二.成员工作总结 姓名 任务ID 迁入记录 江昊 任务1090 https://github.com/buaaclubs-team/temp-front/com ...

  5. Linux内核分析——进程的切换和系统的一般执行过程

    进程的切换和系统的一般执行过程 一.进程切换的关键代码switch_to分析 (一)进程调度与进程调度的时机分析 1.不同类型的进程有不同的调度需求 第一种分类: (1)I/O-bound:频繁进行I ...

  6. 软件项目第一次sprint评论

    软件项目第一次sprint评论 组名:天线宝宝四人组                                   姓名:高长志 1. 组名:9-652组 项目:炸弹人游戏 对于炸弹人游戏,首先 ...

  7. ajax多级菜单栏

    1.jsp 首先ajax查询数据 <script type="text/javascript"> function targetlist() { $.ajax({ ur ...

  8. nginx+tpmcat+redis实现session共享

    nginx+tpmcat+redis实现session共享 版本:nginx nginx-1.8.0.tar.gztomcat apache-tomcat-7.0.78.tar.gzredis  re ...

  9. 小学四则运算APP 第三阶段冲刺-第一天

    团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第三次冲刺阶段时间:12.12~12.19 本次发布的是音乐播放功能,可以根据用户需求一边播放音乐一边做题,也拥有暂停播放音乐的功能,增强 ...

  10. 数学战神app(小学生四则运算app)开发需求及进度

    项目名字:“数学战神” 开发环境:Android eclipse 团队名称:战神联盟 团队成员:陈思明,许家豪,王宏财,吴旭涛 在之前的四则运算APP中添加更多的实用功能,并在各种平台推广宣传. 预加 ...