Description
In this paper, we give a binary tree with n nodes, each point has a point value wi, and define the force value of a node as the sum of the point values of all points in the subtree with this point as the root. Next, q-variables are divided into two kinds of query variables, which are the product module 109 + 7 of the force values of all nodes in a node tree, or the rotation variables, left / right rotation of a node, if the rotation does not Yes, please ignore this Cao work.
Data Constraint
n,q<=1050<wi<109+7
Solution
It is not difficult to find that a single left-handed / Youxuan Cao work will only change the force value of two nodes and the left-handed / Youxuan Cao work will not change the middle order traversal of the original binary tree. A line segment tree is used to maintain the product of the force value of the middle order traversal points in a certain interval, and the minimum and maximum value of the middle order traversal of the points in the node subtree can be maintained during the left-handed / Youxuan Cao work.
Time complexity O(q log n)
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i,j,l) for(int i=j;i<=l;++i)
#define fd(i,j,l) for(int i=j;i>=l;--i)
using namespace std;
typedef long long ll;
const ll N=32e4,mo=1e9+7,M=N<<2;
int fa[N],ls[N],rs[N],dfn[N],dy[N],be[N],en[N];
ll size[N],w[N];
int n,q,pp;
ll tr[M],ans;
inline int read()
{
int o=0; char ch=' ';
for(;ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar())o=o*10+ch-48;
return o;
}
void cor(int o,int l,int r,int op)
{
if(l==r){
tr[o]=size[dy[l]];
return;
}
int mid=l+r>>1;
if(op<=mid)cor(o<<1,l,mid,op);
else cor((o<<1)^1,mid+1,r,op);
tr[o]=tr[o<<1]*tr[(o<<1)^1]%mo;
}
void ask(int o,int l,int r,int le,int ri)
{
if(l==le&&r==ri){
ans=ans*tr[o]%mo;
return;
}
int mid=l+r>>1;
if(ri<=mid)ask(o<<1,l,mid,le,ri);
else if(le>mid)ask((o<<1)^1,mid+1,r,le,ri);
else{
ask(o<<1,l,mid,le,mid);
ask((o<<1)^1,mid+1,r,mid+1,ri);
}
}
void dg(int o)
{
if(ls[o])dg(ls[o]),be[o]=be[ls[o]],fa[ls[o]]=o;else be[o]=pp+1;
dfn[o]=++pp; dy[pp]=o; en[o]=pp;
if(rs[o])dg(rs[o]),en[o]=en[rs[o]],fa[rs[o]]=o;
size[o]=(size[ls[o]]+size[rs[o]]+w[o])%mo;
cor(1,1,n,dfn[o]);
}
inline void left_Rotate(int o)
{
if(rs[o]==0)return;
int ff=fa[o],yb=rs[o];
fa[yb]=ff; rs[o]=ls[yb]; ls[yb]=o;
if(ls[ff]==o)ls[ff]=yb;else rs[ff]=yb;
fa[o]=yb; fa[rs[o]]=o;
size[yb]=size[o];
size[o]=(size[ls[o]]+size[rs[o]]+w[o])%mo;
cor(1,1,n,dfn[yb]); cor(1,1,n,dfn[o]);
if(ls[o])be[o]=be[ls[o]];else be[o]=dfn[o];
if(rs[o])en[o]=en[rs[o]];else en[o]=dfn[o];
if(ls[yb])be[yb]=be[ls[yb]];else be[yb]=dfn[yb];
if(rs[yb])en[yb]=en[rs[yb]];else en[yb]=dfn[yb];
}
inline void right_Rotate(int o)
{
if(ls[o]==0)return;
int ff=fa[o],zb=ls[o];
fa[zb]=ff; ls[o]=rs[zb]; rs[zb]=o;
if(ls[ff]==o)ls[ff]=zb;else rs[ff]=zb;
fa[o]=zb; fa[ls[o]]=o;
size[zb]=size[o];
size[o]=(size[ls[o]]+size[rs[o]]+w[o])%mo;
cor(1,1,n,dfn[zb]); cor(1,1,n,dfn[o]);
if(ls[o])be[o]=be[ls[o]];else be[o]=dfn[o];
if(rs[o])en[o]=en[rs[o]];else en[o]=dfn[o];
if(ls[zb])be[zb]=be[ls[zb]];else be[zb]=dfn[zb];
if(rs[zb])en[zb]=en[rs[zb]];else en[zb]=dfn[zb];
}
int main()
{
cin>>n>>q;
fo(i,1,n){
w[i]=read(); ls[i]=read(); rs[i]=read();
if(ls[i])fa[ls[i]]=i;
if(rs[i])fa[rs[i]]=i;
}
fa[1]=0; dg(1);
fo(i,1,q){
int opt=read(),x=read();
if(opt==2){
ans=1;
ask(1,1,n,be[x],en[x]);
printf("%lld\n",ans);
}
if(opt==1)left_Rotate(x);
if(opt==0)right_Rotate(x);
}
}