bzoj2631: tree
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define mod 51061
#define maxn 100005
#define ll unsigned int
using namespace std; int n,q,fa[maxn],son[maxn][],size[maxn];
bool rev[maxn];
ll val[maxn],lazyc[maxn],lazyh[maxn],sum[maxn]; struct date{
int which(int x){
return son[fa[x]][]==x;
}
bool isroot(int x){
return son[fa[x]][]!=x&&son[fa[x]][]!=x;
}
void update(int x){
size[x]=size[son[x][]]+size[son[x][]]+;
sum[x]=val[x]%mod;
if (son[x][]) sum[x]=(sum[x]+sum[son[x][]])%mod;
if (son[x][]) sum[x]=(sum[x]+sum[son[x][]])%mod;
}
void jia(int x,ll y){
lazyh[x]=(lazyh[x]+y)%mod;
val[x]=(val[x]+y)%mod;
sum[x]=(sum[x]+size[x]*y%mod)%mod;
}
void cheng(int x,ll y){
if (lazyh[x]){
lazyh[x]=(lazyh[x]*y)%mod;
}
lazyc[x]=(lazyc[x]*y)%mod;
val[x]=(val[x]*y)%mod;
sum[x]=(sum[x]*y)%mod;
}
void pushdown(int x){
if (rev[x]){
rev[x]^=,swap(son[x][],son[x][]);
if (son[x][]) rev[son[x][]]^=;
if (son[x][]) rev[son[x][]]^=;
}
if (lazyc[x]!=){
if (son[x][]) cheng(son[x][],lazyc[x]);
if (son[x][]) cheng(son[x][],lazyc[x]);
lazyc[x]=;
}
if (lazyh[x]){
if (son[x][]) jia(son[x][],lazyh[x]);
if (son[x][]) jia(son[x][],lazyh[x]);
lazyh[x]=;
}
}
void relax(int x){
if (!isroot(x)) relax(fa[x]);
pushdown(x);
}
void rotata(int x){
int y=fa[x],d=which(x),dd=which(y);
if (!isroot(y)) son[fa[y]][dd]=x; fa[x]=fa[y];
fa[son[x][d^]]=y,son[y][d]=son[x][d^];
fa[y]=x,son[x][d^]=y;
update(y);
}
void splay(int x){
relax(x);
while (!isroot(x)){
if (isroot(fa[x])) rotata(x);
else if (which(x)==which(fa[x])) rotata(fa[x]),rotata(x);
else rotata(x),rotata(x);
}
update(x);
}
void access(int x){
for (int p=;x;x=fa[x]){
splay(x);
son[x][]=p;
update(x);
p=x;
}
}
void make_root(int x){
access(x);
splay(x);
rev[x]^=;
}
void link(int x,int y){
make_root(x),fa[x]=y;
}
void cut(int x,int y){
make_root(x);
access(y);
splay(y);
son[y][]=fa[x]=;
update(y);
}
void split(int x,int y){
make_root(x);
access(y);
splay(y);
}
void add(int x,int y,ll z){
split(x,y);
jia(y,z);
}
void multiply(int x,int y,ll z){
split(x,y);
cheng(y,z);
}
void query(int x,int y){
split(x,y);
printf("%u\n",sum[y]%mod);
}
}lct; int main(){
// freopen("tree.in","r",stdin);
// freopen("tree.out","w",stdout);
char st[];
int u,v,U,V;
ll UU,VV;
memset(size,,sizeof(size));
memset(fa,,sizeof(fa));
memset(son,,sizeof(son));
memset(rev,,sizeof(rev));
scanf("%d%d",&n,&q);
for (int i=;i<=n;i++){
val[i]=,sum[i]=,lazyc[i]=,lazyh[i]=,size[i]=;
}
for (int i=;i<n;i++){
scanf("%d%d",&u,&v);
lct.link(u,v);
}
while (q--){
scanf("%s%d%d",st+,&u,&v);
if (st[]=='+') scanf("%u",&UU),lct.add(u,v,UU);
else if (st[]=='-') scanf("%d%d",&U,&V),lct.cut(u,v),lct.link(U,V);
else if (st[]=='*') scanf("%u",&UU),lct.multiply(u,v,UU);
else lct.query(u,v);
}
return ;
}
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2631
做法:链乘链加:先传乘标记,并且把加标记乘上这个数,再传加标记;
看个变形就很显然了:(ax+b)*c=a*c*x+b*c,显然加标记也要乘上这个数。
bzoj2631: tree的更多相关文章
- BZOJ2631——tree
1.题目大意:bzoj1798的lct版本 2.分析:这个把线段树改成splay就好 #include <stack> #include <cstdio> #include & ...
- [BZOJ2631]tree 动态树lct
2631: tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 5171 Solved: 1754[Submit][Status][Discus ...
- [Link-Cut-Tree][BZOJ2631]Tree
题面 Description: 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\(v\)的路径上的点的 ...
- bzoj2631 tree LCT 区间修改,求和
tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 4962 Solved: 1697[Submit][Status][Discuss] Des ...
- bzoj2631: tree lct
要打mul和add的lct 50000+的mod用unsigned int好了TAT (坑爹没打pc('\n');(静态)调了好久,样例竟然只输出一个,orz,也不提示PE T_T) #include ...
- BZOJ2631 tree(伍一鸣) LCT 秘制标记
这个题一看就是裸地LCT嘛,但是我wa了好几遍,这秘制标记...... 注意事项:I.*对+有贡献 II.先下传*再下传+(因为我们已经维护了+,不能再让*对+产生贡献)III.维护+用到size # ...
- [bzoj2631]tree——lct
Brief Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: u v c:将u到v的路径上的点的权值都加上自然数c: u1 v1 u2 ...
- BZOJ2631 tree 【LCT】
题目 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的边( ...
- BZOJ2631: tree(LCT)
Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2 ...
随机推荐
- 布局 - layout
示例 <div id="cc" class="easyui-layout" style="width:600px;height:400px;&q ...
- pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat Windows7下pip安装包报错:Microso ...
- ZooKeeper 笔记(4) 实战应用之【消除单点故障】
关键节点的单点故障(Single Point of Failure)在大型的架构中,往往是致命的.比如:SOA架构中,服务注册中心(Server Register)统一调度所有服务,如果这个节点挂了, ...
- Bink Player
class CBIKMaterial { public: CBIKMaterial(); ~CBIKMaterial(); bool Init(const char *pFileName); void ...
- idea 生成代码中带参数final修饰
- ASP.NET 系列:单元测试之Log4Net
使用Log组件时,我们通常自定义ILogger接口,使用Log4Net等组件进行适配来定义不同的实现类.使用Log4Net日志组件时,为了即方便单元测试又能使用配置文件,我们通过Log4Net的ILo ...
- 项目分布式部署那些事(2):基于OCS(Memcached)的Session共享方案
在不久之前发布了一篇"项目分布式部署那些事(1):ONS消息队列.基于Redis的Session共享,开源共享",因为一些问题我们使用了阿里云的OCS,下面就来简单的介绍和分享下相 ...
- ArcGIS支持MongoDB数据源
ArcGIS支持MongoDB数据源 自从NoSQL推出之后,MongoDB就作为比较杰出的代表受到广大用户的推崇,当然,与之而来的大数据的讨论也非常激烈,GIS数据源向来都是以海量来计算,所以,GI ...
- 数据库表转javaBean
复制后修改部分代码 package com.study; import java.io.BufferedWriter; import java.io.File; import java.io.File ...
- 【转】Java 8十个lambda表达式案例
1. 实现Runnable线程案例 使用() -> {} 替代匿名类: //Before Java 8: new Thread(new Runnable() { @Override public ...