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] 尺树寸泓 解题报告 (线段树+中序遍历)的更多相关文章

  1. [LeetCode] 538. 把二叉搜索树转换为累加树 ☆(中序遍历变形)

    把二叉搜索树转换为累加树 描述 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. ...

  2. ACM: Hotel 解题报告 - 线段树-区间合并

    Hotel Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description The ...

  3. ACM: Billboard 解题报告-线段树

     Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descript ...

  4. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  5. [P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)

    题目链接:https://www.luogu.org/problemnew/show/P3097#sub 题目描述 Farmer John has recently purchased a new b ...

  6. ACM: 敌兵布阵 解题报告 -线段树

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Li ...

  7. ACM: Just a Hook 解题报告 -线段树

    E - Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   D ...

  8. [BZOJ1858] [SCOI2010] 序列操作 解题报告 (线段树)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1858 Description lxhgww最近收到了一个01序列,序列里面包含了n个数, ...

  9. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

随机推荐

  1. Fabric quickly

    环境很重要.环境很重要.环境很重要 # CentOS 7 $ setenforce 0 # 可以设置配置文件永久关闭 $ systemctl stop iptables.service $ syste ...

  2. lua队列实现

    Queue = {} function Queue.newquene() } end function Queue.push(queue, value) queue.count = queue.cou ...

  3. 将我们的parser转换成Monad

    还记得我们上一篇delegate类型的parser吗 ,在开始本篇之前,强烈建议你复习一下这个parser定义  public delegate Maybe<Tuple<T,string& ...

  4. MAVEN学习笔记之私服Nexus(2)

    MAVEN学习笔记之私服Nexus(2) 私有服务器搭建 Nexus www.snatype.org下载 snatype-work 是默认nexus存储nexus a:将bin添加到环境中 Admin ...

  5. QT线程使用收集示例

    关于多线程问题: Qt和Boost做跨平台的线程封装,OpenMP主要做并行计算,让不精通多线程的人也能高效地利用CPU的计算能力.个人倾向于用boost.thread, boost.mpi.   一 ...

  6. HashMap以及ConcurrentHashMap

    HashMap源码相关 HashMap实现原理及源码分析 总之就是这个博客,简直就是源码带逛,开心,最关键的是下面的图像 另外,自己的理解加上源码,总结如下 hash,原义散列,就是一对一: hash ...

  7. 提示 npm update check failed

    执行npm命令时出现以下提示 虽然不影响代码运行,但总觉得看了很碍事, 查找资料后发现是因为文件夹权限的问题, .config / configstore文件夹中包含一个文件:update-notif ...

  8. Oracle语句执行顺序

  9. nyoj26-孪生素数问题

    孪生素数问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 写一个程序,找出给出素数范围内的所有孪生素数的组数.一般来说,孪生素数就是指两个素数距离为2,近的不能再近的 ...

  10. P1828 香甜的黄油 Sweet Butter (spfa)

    题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...