[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 ...
随机推荐
- html中<frameset>标签,框架结构各窗口的父级菜单子级菜单关系
这个问题搞得我头大,并且在查过百度后各位大佬给出的解释我都不能理解,应该是我太白的原因,希望我写的能好理解. 下面文章窗口1.2.3,在代码里分别为chuangkou.chuangkou1.chuan ...
- javascript中五句话
1.弹出框 ,小括号中就是弹出的内容 alert("我是一个弹出框"); 2.控制台输出 小括号里面就是 控制台输出的东西 console.log("我是控制台输出的 ...
- hdu 3729 最大匹配
此题是我AC的HDU的201道题目.泪流满面啊! 字典序最大(最小)真是个烦人的东西. 学生i与其对应的分数区间的每个点连一条边.字典序最大,编号最大的学生开始匹配. HK无法AC啊,试了很久.我不会 ...
- CSS3 动画 思维导图
思维导图在新窗口打开浏览
- Verilog之openMSP430(1)
openMSP430_IO interrupt Verilog file: omsp_gpio.v //================================================ ...
- java真实面试题(2)
1,递归算法的实行过程,一般来说,可以分为()和()两个阶段,若一个问题的求解既可以用递归也可以用递推时,则往往用(),因为().贪婪法是一种()的算法. 答:递归算法分为递推和回归两个阶段,递推效率 ...
- charles抓https设置
1下载charles和破解包 2安装证书 打开charles的help->SSL Proxying->install charles root certificate 选择你要安装的列表里 ...
- day27-2 pandas模块
目录 pandas Series(了解) DataFrame 内置方法 处理缺失值 合并数据 取值 把表格传入excel文件中 把表格从excel中取出来 高级(了解) pandas 处理表格等文件/ ...
- 机器学习PAI快速入门
什么是机器学习? 机器学习(Machine Learning, ML)是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行为,以 ...
- 反射API提供的常用类和函数
ReflectionParameter 取回了函数或方法参数的相关信息. {//要自行检查函数的参数,首先创建一个 ReflectionFunction 或 ReflectionMethod 的 实例 ...