当前位置:网站首页>[hnoi2010] flying sheep
[hnoi2010] flying sheep
2022-06-26 13:58:00 【__ LazyCat__】
Sequence tree building
link :[P3203 HNOI2010] Flying sheep - Luogu | New ecology of computer science education (luogu.com.cn)
The question : Given sequence a , from 0 Start to n-1, Each time from i The departure can be adjusted to i+a[i] , Repeated inquiries and modifications , Ask how many times you can jump to greater than or equal to n The location of , The revision will a[x] Change to y .
Answer key : Use the whole sequence LCT Build up trees , Move all positions one position back , The root node is n+1 . You can add edges every time you modify the broken edges . Remember to put... First when asking n+1 Reset to the root node with the smallest depth , Because the root has changed when connecting edges or short edges . And then x Put it in splay The root of the ( Not the smallest depth ), Finding the size of the left subtree is the answer , Represents how many x To the parent node of the root .
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=2e5+5;
int a[maxn],n,m,op,x,y,z;
int f[maxn],c[maxn][2],sz[maxn],st[maxn];
bool r[maxn];
#define lc c[x][0]
#define rc c[x][1]
bool nroot(int x)
{
return c[f[x]][0]==x||c[f[x]][1]==x;
}
void pushup(int x)
{
sz[x]=sz[lc]+sz[rc]+1;
}
void reverse(int x)
{
swap(lc,rc); r[x]^=1;
}
void pushdown(int x)
{
if(r[x])
{
if(lc)reverse(lc);
if(rc)reverse(rc);
r[x]=0;
}
}
void rotate(int x)
{
int y=f[x],z=f[y],k=c[y][1]==x,w=c[x][!k];
if(nroot(y))c[z][c[z][1]==y]=x;
c[x][!k]=y,c[y][k]=w;
if(w)f[w]=y; f[y]=x,f[x]=z;
pushup(y);
}
void splay(int x)
{
int y=x,z=0;
st[++z]=y;
while(nroot(y))st[++z]=y=f[y];
while(z)pushdown(st[z--]);
while(nroot(x))
{
y=f[x],z=f[y];
if(nroot(y))
{
rotate((c[y][0]==x)^(c[z][0]==y)?x:y);
}
rotate(x);
}
pushup(x);
}
void access(int x)
{
for(int y=0;x;x=f[y=x])
{
splay(x),rc=y,pushup(x);
}
}
void makeroot(int x)
{
access(x),splay(x),reverse(x);
}
int findroot(int x)
{
access(x),splay(x);
while(lc)pushdown(x),x=lc;
splay(x); return x;
}
void split(int x,int y)
{
makeroot(x),access(y),splay(y);
}
void link(int x,int y)
{
makeroot(x),f[x]=y;
}
void cut(int x,int y)
{
split(x,y),f[x]=c[y][0]=0;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(i+a[i]<=n)link(i,i+a[i]);
else link(i,n+1);
}
cin>>m;
for(int i=1;i<=m;i++)
{
cin>>op;
if(op==1)
{
cin>>x; x++; makeroot(n+1),access(x),splay(x);
cout<<sz[lc]<<"\n";
}
else
{
cin>>x>>y; x++;
if(x+a[x]<=n)cut(x,x+a[x]);
else cut(x,n+1);
if(x+y<=n)link(x,x+y);
else link(x,n+1);
a[x]=y;
}
}
return 0;
}
边栏推荐
- HW蓝队溯源流程详细整理
- Is it safe to open a securities account? Is there any danger
- The most critical elements of team management
- VTK 圆柱体的生成与渲染
- Es common grammar I
- Luogu p4145 seven minutes of God created questions 2 / Huashen travels around the world
- NVM installation tutorial
- ICML 2022 | limo: a new method for rapid generation of targeted molecules
- Exercises under insect STL string
- 去某东面试遇到并发编程问题:如何安全地中断一个正在运行的线程
猜你喜欢

Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached

windows版MySQL软件的安装与卸载

8.Ribbon负载均衡服务调用

Generation and rendering of VTK cylinder

Variable declaration of typescript

李航老师新作《机器学习方法》上市了!附购买链接

Installation and uninstallation of MySQL software for windows

免费的机器学习数据集网站(6300+数据集)

古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資

Create your own cross domain proxy server
随机推荐
33. Use rgbd camera for target detection and depth information output
D中不用GC
Es sauvegarde et restauration des données par instantané
hands-on-data-analysis 第三单元 模型搭建和评估
Ubuntu installation and configuration PostgreSQL (18.04)
Wechat applet magic bug - choose to replace the token instead of clearing the token, wx Getstoragesync will take the old token value instead of the new token value
Detailed practical sharing, two hours of funny videos after work, earning more than 7000 a month
Installation and uninstallation of MySQL software for windows
How to check if a text field is empty or not in swift
虫子 运算符重载的一个好玩的
爱可可AI前沿推介(6.26)
虫子 类和对象 中
Hands on data analysis unit 3 model building and evaluation
AGCO AI frontier promotion (6.26)
Wechat applet SetData dynamic variable value sorting
Calculate the distance between two points (2D, 3D)
A must for programmers, an artifact utools that can improve your work efficiency n times
虫子 内存管理 下 内存注意点
ES基於Snapshot(快照)的數據備份和還原
证券开户安全吗,有没有什么危险啊