bzoj 2631: tree link-cut-tree
题目:
Description
一棵n个点的树,每个点的初始权值为1。对于这棵树有q个操作,每个操作为以下四种操作之一:
+ u v c:将u到v的路径上的点的权值都加上自然数c;
- u1 v1 u2 v2:将树中原有的边(u1,v1)删除,加入一条新边(u2,v2),保证操作完之后仍然是一棵树;
* u v c:将u到v的路径上的点的权值都乘上自然数c;
/ u v:询问u到v的路径上的点的权值和,求出答案对于51061的余数。Input
第一行两个整数n,q
接下来n-1行每行两个正整数u,v,描述这棵树
接下来q行,每行描述一个操作
Output
对于每个/对应的答案输出一行
题解
模板题
用了15mins打完,调了半小时
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const ll mod = 51061;
const ll maxn = 210010;
struct Node{
Node *ch[2],*fa;
ll mul,add,w,sum,tag;
ll siz;
void update();
void pushdown();
void set();
}*null;
Node mem[maxn],*it;
inline void init(){
it = mem;null = it++;
null->ch[0] = null->ch[1] = null->fa = null;
null->mul = null->add = null->w = null->sum = 0;
null->tag = null->siz = 0;
}
inline Node* newNode(ll w){
Node *p = it++;p->ch[0] = p->ch[1] = p->fa = null;
p->mul = 1;p->siz = 1;
p->add = p->tag = 0;p->w = p->sum = w;return p;
}
inline void Node::update(){
if(this == null) return;
sum = (ch[0]->sum + ch[1]->sum + w) % mod;
siz = (ch[0]->siz + ch[1]->siz + 1);
}
inline void Node::pushdown(){
if(this == null) return;
if(mul != 1){
if(ch[0] != null){
ch[0]->add = ch[0]->add*mul % mod;
ch[0]->mul = ch[0]->mul*mul % mod;
ch[0]->w = ch[0]->w*mul % mod;
ch[0]->sum = ch[0]->sum*mul % mod;
}
if(ch[1] != null){
ch[1]->add = ch[1]->add*mul % mod;
ch[1]->mul = ch[1]->mul*mul % mod;
ch[1]->w = ch[1]->w*mul % mod;
ch[1]->sum = ch[1]->sum*mul % mod;
}
mul = 1;
}
if(add != 0){
if(ch[0] != null){
ch[0]->add = (ch[0]->add + add) % mod;
ch[0]->sum = (ch[0]->sum + add*ch[0]->siz) % mod;
ch[0]->w = (ch[0]->w + add) % mod;
}
if(ch[1] != null){
ch[1]->add = (ch[1]->add + add) % mod;
ch[1]->sum = (ch[1]->sum + add*ch[1]->siz) % mod;
ch[1]->w = (ch[1]->w + add) % mod;
}
add = 0;
}
if(tag != 0){
if(ch[0] != null) ch[0]->tag ^= 1;
if(ch[1] != null) ch[1]->tag ^= 1;
swap(ch[0],ch[1]);tag = 0;
}
}
inline void rotate(Node *p,Node *x){
ll k = p == x->ch[1];
Node *y = p->ch[k^1],*z = x->fa;
if(z->ch[0] == x) z->ch[0] = p;
if(z->ch[1] == x) z->ch[1] = p;
if(y != null) y->fa = x;
p->fa = z;p->ch[k^1] = x;
x->fa = p;x->ch[k] = y;
x->update();p->update();
}
inline bool isroot(Node *p){
return (p == null) || (p->fa->ch[0] != p && p->fa->ch[1] != p);
}
inline void Splay(Node *p){
p->pushdown();
while(!isroot(p)){
Node *x = p->fa,*y = x->fa;
y->pushdown();x->pushdown();p->pushdown();
if(isroot(x)) rotate(p,x);
else if((p == x->ch[0])^(x == y->ch[0])) rotate(p,x),rotate(p,y);
else rotate(x,y),rotate(p,x);
}p->update();
}
inline Node* Access(Node *x){
for(Node *y = null;x != null;y = x,x = x->fa)
Splay(x),x->ch[1] = y,x->update();
return x;
}
inline void makeRoot(Node *x){
Access(x);Splay(x);x->tag ^= 1;
}
inline void link(Node *x,Node *y){
makeRoot(x);x->fa = y;
}
inline void cut(Node *x,Node *y){
makeRoot(x);Access(y);Splay(y);
y->ch[0] = y->ch[0]->fa = null;
y->update();
}
inline void inc(Node *x,Node *y,ll w){
makeRoot(x);Access(y);Splay(y);
y->add += w;y->add %= mod;
y->sum += w*y->siz;
y->sum %= mod;
y->w += w;y->w %= mod;
}
inline void mul(Node *x,Node *y,ll w){
makeRoot(x);Access(y);Splay(y);
y->add *= w;y->add %= mod;
y->mul *= w;y->mul %= mod;
y->sum *= w;y->sum %= mod;
y->w *= w;y->w %= mod;
}
inline ll query(Node *x,Node *y){
makeRoot(x);Access(y);Splay(y);
return y->sum;
}
int main(){
init();
ll n,q;read(n);read(q);
for(ll i=1;i<=n;++i) newNode(1);
for(ll i=1,u,v;i<n;++i){
read(u);read(v);
link(mem+u,mem+v);
}
char ch;
ll u,v,x;
while(q--){
while(ch = getchar(),ch<'!');
if(ch == '+'){
read(u);read(v);read(x);
inc(mem+u,mem+v,x);
}else if(ch == '-'){
read(u);read(v);
cut(mem+u,mem+v);
read(u);read(v);
link(mem+u,mem+v);
}else if(ch == '*'){
read(u);read(v);read(x);
mul(mem+u,mem+v,x);
}else if(ch == '/'){
read(u);read(v);
printf("%lld\n",query(mem+u,mem+v));
}
}
getchar();getchar();
return 0;
}
bzoj 2631: tree link-cut-tree的更多相关文章
- bzoj 3282: Tree (Link Cut Tree)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282 题面: 3282: Tree Time Limit: 30 Sec Memory L ...
- 【BZOJ 3282】Tree Link Cut Tree模板题
知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,, 现在才知道这些,,,真心弱,,, #include<cstdio ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
随机推荐
- EF之POCO应用系列2——复杂类型
在.NET开发中,EF4以前的版本以及LINQ TO SQL都不支持complex数据类型,EF4终于支持complex类型的数据了,这意味着微软的EF框架朝领域驱动方面又迈了一大步. 复杂的数据类型 ...
- Eclipse 查看第三方jar包文件源代码解决方法
1.打开第三方依赖包,源文件的快捷键:ctrl + mouseClick 2.由于我们下载的第三方jar 包,如Spring等相关的依赖包时,并没有附加下载相应的源文件,所以经常出现如图的这种问题. ...
- 九度OJ 1204:农夫、羊、菜和狼的故事 (遍历、BFS)
时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:744 解决:502 题目描述: 有一个农夫带一只羊.一筐菜和一只狼过河. 果没有农夫看管,则狼要吃羊,羊要吃菜. 但是船很小,只够农夫带一样 ...
- DNN优势
- Java语言平台
J2SE(Java 2 Platform Standard Edition) 标准版 开发普通桌面和商务应用程序提供的解决方案,该技术体系是下面两者的基础,可以完成一些桌面应用程序的开发 J2ME(J ...
- saltStack 证书管理
SaltStack 使用 SSL 签证的方式进行安全认证,我们可以在 Master 端看到 Minion 端的整数签证请求 1.查看当前证书签证情况 [root@SaltStack-Master ~] ...
- BZOJ1791: [Ioi2008]Island 岛屿
BZOJ1791: [Ioi2008]Island 岛屿 Description 你将要游览一个有N个岛屿的公园. 从每一个岛i出发,只建造一座桥. 桥的长度以Li表示. 公园内总共有N座桥. 尽管每 ...
- Jquery遍历table并拿到每个单元格里的值
$('#table_id tr').each(function(i){ alert(i); if(i!=0){ //td内的文本 var v0 = $(this).children('td').eq( ...
- 网络端口的作用及分类(转发:http://blog.csdn.net/dream_1996/article/details/73481201)
一.什么是端口? 在开始讲什么是端口(port)之前,我们先来聊一聊什么是 port 呢?常常在网络上听说『我的主机开了多少的 port ,会不会被入侵呀!?』或者是说『开那个 port 会比较安全? ...
- 第6条:在单次切片操作内,不要同时指定start、end和stride
核心知识点: 1.使用负步进可以反转取值字符串及ASCII. 2.stride最好不要与start和end用在一起,会降低代码可读性. 除了基本的切片操作之外,python还提供了somelist[s ...