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^64-1)*x%2^64 所以 2^64-1-x=2^64-1+(2^64-1)*x 所以第三个操作也就变成了区间乘 和区间加。 然后就是树剖加线段树多重标记。表示这是第一次写多重标记,整体凭感觉,细节看题解,树剖有点点遗忘,不过还好。今天看群里说邀请赛没什么价值,,细想一下那些题确实裸了点,的确网络赛难啊QAQ。今天好颓,王者玩了好久。
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define ls x<<1
#define rs x<<1|1
#define ull unsigned long long
#define _mp make_pair
#define ldb long double
using namespace std;
const int maxn=1e5+100;
const ull inf=(1<<64)-1;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}struct node
{
int l,r;
ull sum,add,mul;
}no[maxn<<2];
int siz[maxn],depth[maxn],dfn[maxn],id[maxn],son[maxn],top[maxn],fa[maxn];
int tot;
vector<int>g[maxn];
int n,m;
void dfs1(int x,int f)
{
depth[x]=depth[f]+1;
fa[x]=f;
siz[x]=1;
for(int i=0;i<(int)g[x].size();i++)
{
int v=g[x][i];
if(v==f)continue;
dfs1(v,x);
siz[x]+=siz[v];
if(son[x]==-1||siz[son[x]]<siz[v])son[x]=v;
}
}
void dfs2(int x,int f)
{
top[x]=f;
id[x]=++tot;
if(son[x]==-1)return;
dfs2(son[x],f);
for(int i=0;i<(int)g[x].size();i++)
{
if(g[x][i]==son[x]||g[x][i]==fa[x])continue;
dfs2(g[x][i],g[x][i]);
}
}
void push_up(int x)
{
no[x].sum=no[x<<1].sum+no[x<<1|1].sum;
}
void push_down(int x)
{
int len=no[x].r-no[x].l+1;
no[x<<1].add=no[x<<1].add*no[x].mul+no[x].add;
no[x<<1|1].add=no[x<<1|1].add*no[x].mul+no[x].add;
no[x<<1].mul=no[x<<1].mul*no[x].mul;
no[x<<1|1].mul=no[x<<1|1].mul*no[x].mul;
no[x<<1].sum=no[x<<1].sum*no[x].mul+(len-(len>>1))*no[x].add;
no[x<<1|1].sum=no[x<<1|1].sum*no[x].mul+((len>>1))*no[x].add;
no[x].add=0;no[x].mul=1;
}
void build(int x,int l,int r)
{
no[x].l=l;no[x].r=r;
no[x].add=0;no[x].mul=1;
no[x].sum=0;
if(l==r)return;
int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
}
ull query(int x,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
{
return no[x].sum;
}
ull res=0;
push_down(x);
int mid=(l+r)>>1;
if(L<=mid)res+=query(ls,l,mid,L,R);
if(R>mid) res+=query(rs,mid+1,r,L,R);
return res;
}
void update(int x,int l,int r,int L,int R,int type,ull v)
{
if(L<=l&&r<=R)
{
if(type==1)
{
no[x].sum=no[x].sum*v;
no[x].add=no[x].add*v;
no[x].mul=no[x].mul*v;
return;
}
if(type==2)
{
no[x].sum+=v*(r-l+1);
no[x].add+=v;
return;
}
if(type==3)
{
no[x].sum=no[x].sum*inf+inf*(r-l+1);
no[x].add=no[x].add*inf+inf;
no[x].mul=no[x].mul*inf;
return;
}
}
push_down(x);
int mid=(l+r)>>1;
if(L<=mid)update(ls,l,mid,L,R,type,v);
if(R>mid)update(rs,mid+1,r,L,R,type,v);
push_up(x);
}
void ask1(int x,int y,int type,ull val)
{
int xx=top[x],yy=top[y];
while(xx!=yy)
{
if(depth[xx]<depth[yy])swap(xx,yy),swap(x,y);
update(1,1,n,id[xx],id[x],type,val);
x=fa[xx];
xx=top[x];
}
if(depth[x]<depth[y])swap(x,y);
update(1,1,n,id[y],id[x],type,val);
}
ull ask2(int x,int y)
{
int xx=top[x],yy=top[y];
ull aa=0;
while(xx!=yy)
{
if(depth[xx]<depth[yy])swap(xx,yy),swap(x,y);
aa+=query(1,1,n,id[xx],id[x]);
x=fa[xx];
xx=top[x];
}
if(depth[x]<depth[y])swap(x,y);
aa+=query(1,1,n,id[y],id[x]);
return aa;
}
void init()
{
for(int i=1;i<=n;i++)g[i].clear();
memset(fa,0,sizeof(fa));
memset(son,-1,sizeof(son));
memset(depth,0,sizeof(depth));
tot=0;
}
int main()
{
while(~scanf("%d",&n))
{
init();
int tmp;
for(int i=2;i<=n;i++)
{
scanf("%d",&tmp);
g[tmp].pb(i);
}
dfs1(1,0);
dfs2(1,1);
build(1,1,n);
scanf("%d",&m);
int pp,qq,rr;
ull s;
for(int i=1;i<=m;i++)
{
scanf("%d",&tmp);
if(tmp==1||tmp==2)
{
scanf("%d%d%llu",&pp,&qq,&s);
ask1(pp,qq,tmp,s);
}
else if(tmp==3)
{
scanf("%d%d",&pp,&qq);
ask1(pp,qq,tmp,0);
}
else
{
scanf("%d%d",&pp,&qq);
cout<<ask2(pp,qq)<<"\n";
}
}
}
}
2018 ICPC 焦作网络赛 E.Jiu Yuan Wants to Eat的更多相关文章
- 【2018 ICPC焦作网络赛 K】Transport Ship(多重背包二进制优化)
There are N different kinds of transport ships on the port. The ith kind of ship can carry the weigh ...
- 【2018 ICPC焦作网络赛 G】Give Candies(费马小定理+快速幂取模)
There are N children in kindergarten. Miss Li bought them N candies. To make the process more intere ...
- 2018 ICPC 沈阳网络赛
2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...
- 2018 ICPC 徐州网络赛
2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...
- 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 ...
- ACM-ICPC 2018 焦作赛区网络预赛 E. Jiu Yuan Wants to Eat (树链剖分-线性变换线段树)
树链剖分若不会的话可自行学习一下. 前两种操作是线性变换,模\(2^{64}\)可将线段树全部用unsigned long long 保存,另其自然溢出. 而取反操作比较不能直接处理,因为其模\(2^ ...
- 2018 ICPC南京网络赛 L Magical Girl Haze 题解
大致题意: 给定一个n个点m条边的图,在可以把路径上至多k条边的权值变为0的情况下,求S到T的最短路. 数据规模: N≤100000,M≤200000,K≤10 建一个立体的图,有k层,每一层是一份原 ...
- 2018 icpc 青岛网络赛 J.Press the Button
Press the Button Time Limit: 1 Second Memory Limit: 131072 KB BaoBao and DreamGrid are playing ...
- 2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)
BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among ...
随机推荐
- docker技术之基本命令
我们使用基本命令之前,先来普及一下操作中使用的基本概念 镜像 image 容器 container 仓库 repository 镜像 Docker 镜像是一个特殊的文件系统,除了提供容器运 ...
- vue路由动态过渡效果
不多说,直接上代码 import Vue from 'vue' //引入vue import VueRouter from 'vue-router' //引入路由 Vue.use(VueRouter) ...
- 三、如何设置npm镜像
一.临时使用 npm --registry https://registry.npm.taobao.org install express 二.永久使用 npm config set registry ...
- java.lang包【Object类】
基本描述: (1)Object类位于java.lang包中,java.lang包包含着Java最基础和核心的类,在编译时会自动导入: (2)Object类是所有Java类的祖先.每个类都使用 Obje ...
- v-router几种定义方式
第一种 const router = new VueRouter({ routes: [{ path: '/newSongs', component: require('../views/NewSon ...
- js中的call、apply、bind
在js中每个函数都包含两个非继承而来的方法:call()和apply() call和apply的作用都是在特定的作用域中将函数绑定到另外一个对象上去运行,即可以用来重新定义函数的执行环境,两者仅在定义 ...
- ubuntu 完全卸载mysql
卸载 sudo apt-get --purge remove mysql-common -y sudo apt-get --purge remove mysql* -y sudo apt-get au ...
- delphi 中出现dataset not in edit or insert mode的问题
self.ADOQuery2.Edit;self.ADOQuery2.First;while not self.ADOQuery2.Eof dobeginself.ADOQuery2.FieldByN ...
- 启动docker容器 防火墙问题报错 ! -i docker0' failed: iptables: No chain/target/match by that name.
COMMAND_FAILED: '/sbin/iptables -t nat -A DOCKER -p tcp -d 0/0 --dport 8111 -j DNAT --to-destination ...
- Linux下4个查找命令which、whereis、locate、find的总结
(1)which [-a] cmdname1 cmdname2 ...... 作用:locate a command,从环境变量PATH中,定位/返回与指定名字相匹配的可执行文件所在的路径 ...