[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 ...
随机推荐
- C-概览
1.贝尔实验室的Dennis Ritchie在1972年开发了C语言,C来自于Ken Thompson的B语言.当时Ritchie正与 Thompson一起设计UNIX操作系统. 2.C是面向过程的编 ...
- MySQL 5.6 Reference Manual-14.3 InnoDB Transaction Model and Locking
14.3 InnoDB Transaction Model and Locking 14.3.1 InnoDB Lock Modes 14.3.2 InnoDB Record, Gap, and Ne ...
- 【Oracle】ORA-38171: Insufficient privileges for SQL management object operation
问题: 使用SQL PLAN MANAGEMENT的时候运行下面的存储过程报错. SYS@GOOD> conn scott/tiger Connected. SCOTT@GOOD> DEC ...
- SVN添加分支
1.打开版本库浏览视图 2.复制当前版本 3.输入复制的目的目录即可
- Windows平台上使用ANT编译Hadoop Eclipse Plugin
一.准备工作: 1.安装JDK 下载页面:http://www.oracle.com/technetwork/java/javase/downloads/index.html JDK6,JDK7都 ...
- 数据的图表统计highcharts
数据统计常用的图表一般是饼状图.柱状图.线状图,HighCharts可以很好的实现. HighCharts highcharts是基于jquery的一个功能强大的插件,使用时先导入jquery.js ...
- B/S架构的网站测试
一.功能测试 1.链接测试 链接是Web应用系统的一个主要特征,它是在页面之间切换和指导用户去一些不知道地址的页面的主要手段.链接测试可分为三个方面.首先,测试所有链接是否按指示的那样确实链接到了 ...
- JA document的练习
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【udacity】机器学习-波士顿房价预测小结
Evernote Export 机器学习的运行步骤 1.导入数据 没什么注意的,成功导入数据集就可以了,打印看下数据的标准格式就行 用个info和describe 2.分析数据 这里要详细分析数据的内 ...
- 训练1-L
n个人一起排队接水,第i个人需要ai的时间来接水. 1 <= n <= 1000 1 <= a,i<= 1000 同时只能有一个人接水,正在接水的人和没有接水的人都需要等待. ...