[jzoj 5662] 尺树寸泓 解题报告 (线段树+中序遍历)
interlinkage:
https://jzoj.net/senior/#contest/show/2703/1
description:

solution:
- 发现$dfs$序不好维护
- 注意到这是一棵平衡树,左旋右旋中序遍历不会改变,且一个点的子树在中序遍历上也是一个连续的区间
- 每次旋转只改变两个点的力量值
- 在中序遍历上建线段树维护单点修改,区间乘积查询就好
code:
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll; const int N=2e5+;
const ll mo=1e9+;
int n,q,root,tim;
int t[N][],fa[N],id[N],dfn[N],size[N];
ll w[N],mul[N<<],sum[N];
inline ll read()
{
char ch=getchar();ll s=,f=;
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<='') {s=(s<<)+(s<<)+ch-'';ch=getchar();}
return s*f;
}
void up1(int o)
{
size[o]=size[t[o][]]+size[t[o][]]+;
sum[o]=(sum[t[o][]]+sum[t[o][]]+w[o])%mo;
}
void dfs(int x)
{
if (t[x][]) dfs(t[x][]);
dfn[++tim]=x;id[x]=tim;
if (t[x][]) dfs(t[x][]);
up1(x);
}
void up2(int o)
{
mul[o]=mul[o<<]*mul[o<<|]%mo;
}
void build(int o,int l,int r)
{
if (l==r)
{
mul[o]=sum[dfn[l]];
return;
}
int mid=l+r>>;
build(o<<,l,mid);
build(o<<|,mid+,r);
up2(o);
}
void rotate(int x,int op)
{
int y=fa[x],z=fa[y];
if (z) t[z][t[z][]==y]=x;
fa[x]=z;
t[y][op]=t[x][!op];fa[t[x][!op]]=y;
t[x][!op]=y;fa[y]=x;
up1(y);up1(x);
}
void ins(int o,int l,int r,int pos)
{
if (l==r)
{
mul[o]=sum[dfn[pos]];
return;
}
int mid=l+r>>;
if (pos<=mid) ins(o<<,l,mid,pos);
else ins(o<<|,mid+,r,pos);
up2(o);
}
ll query(int o,int l,int r,int x,int y)
{
if (l>=x&&r<=y) return mul[o];
int mid=l+r>>;
ll re=;
if (x<=mid) re=re*query(o<<,l,mid,x,y)%mo;
if (y>mid) re=re*query(o<<|,mid+,r,x,y)%mo;
return re;
}
int main()
{
freopen("splay.in","r",stdin);
freopen("splay.out","w",stdout);
n=read();q=read();
for (int i=;i<=n;i++)
{
w[i]=read();t[i][]=read();t[i][]=read();
if (t[i][]) fa[t[i][]]=i;
if (t[i][]) fa[t[i][]]=i;
}
for (int i=;i<=n;i++) if (!fa[i]) root=i;
dfs();
build(,,tim);
while (q--)
{
int op=read(),x=read();
if (op<=)
{
if (t[x][op]) x=t[x][op];
else continue;
rotate(x,op);
ins(,,tim,id[x]);
if (t[x][!op]) ins(,,tim,id[t[x][!op]]);
}
if (op==)
{
printf("%lld\n",query(,,tim,id[x]-size[t[x][]],id[x]+size[t[x][]]));
}
}
return ;
}
[jzoj 5662] 尺树寸泓 解题报告 (线段树+中序遍历)的更多相关文章
- [LeetCode] 538. 把二叉搜索树转换为累加树 ☆(中序遍历变形)
把二叉搜索树转换为累加树 描述 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. ...
- ACM: Hotel 解题报告 - 线段树-区间合并
Hotel Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description The ...
- ACM: Billboard 解题报告-线段树
Billboard Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descript ...
- ACM Minimum Inversion Number 解题报告 -线段树
C - Minimum Inversion Number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- [P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)
题目链接:https://www.luogu.org/problemnew/show/P3097#sub 题目描述 Farmer John has recently purchased a new b ...
- ACM: 敌兵布阵 解题报告 -线段树
敌兵布阵 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description Li ...
- ACM: Just a Hook 解题报告 -线段树
E - Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- [BZOJ1858] [SCOI2010] 序列操作 解题报告 (线段树)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1858 Description lxhgww最近收到了一个01序列,序列里面包含了n个数, ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
随机推荐
- Javascript Proxy对象 简介
Javascript Proxy对象 简介 Javascript Proxy对象 改变你操作对象的方式 Proxies 是Javasript对象的中间件 ...或者说至少是那种很早的版本. ES6 中 ...
- css3动画,点击圆形背景扩展整个页面效果
上次做项目的时候,要求点击链接,这个链接的圆形背景扩散充满整个页面,今天把这个效果整理一下,是简单的css3的动画特效,粘贴下面的代码看效果 <!DOCTYPE html> <htm ...
- Jquery插件:提示框
在实际项目中,很容易有这种需求:当某个操作成功或失败,需要给用户一个提示.当然最简单的做法是调用alert()方法弹窗.但alert()属于JavaScript中BOM部分,每个浏览器的样式不太一样, ...
- jQuery添加新的元素
append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元素之前插入内容 $(&quo ...
- Final 用法
可修饰:类及类的成员.成员变量.局部变量,不能修饰构造方法 final修饰的类不能被继承但可以继承其他的类 final修饰的类不能被重写和继承,子类修改的方法可以加继承 final修饰的局部变量和成员 ...
- TensorFlow+实战Google深度学习框架学习笔记(11)-----Mnist识别【采用滑动平均,双层神经网络】
模型:双层神经网络 [一层隐藏层.一层输出层]隐藏层输出用relu函数,输出层输出用softmax函数 过程: 设置参数 滑动平均的辅助函数 训练函数 x,y的占位,w1,b1,w2,b2的初始化 前 ...
- Linux 内核链表 list.h 的使用
Linux 内核链表 list.h 的使用 C 语言本身并不自带集合(Collection)工具,当我们需要把结构体(struct)实例串联起来时,就需要在结构体内声明指向下一实例的指针,构成所谓的& ...
- 漫谈 Google 的 Native Client 技术(一)---- 历史动力篇(Web 本地计算发展史)
转自:http://hzx5.blog.163.com/blog/static/40744388201172522313463/ 漫谈 Google 的 Native Client 技术(一)---- ...
- nyoj286-动物统计
动物统计 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 在美丽大兴安岭原始森林中存在数量繁多的物种,在勘察员带来的各种动物资料中有未统计数量的原始动物的名单.科学家想判 ...
- [luogu 1270] “访问”美术馆 (树形dp)
传送门 Description 经过数月的精心准备,Peer Brelstet,一个出了名的盗画者,准备开始他的下一个行动.艺术馆的结构,每条走廊要么分叉为两条走廊,要么通向一个展览室.Peer知道每 ...