当前位置: 首页 > news >正文

题解:[HNOI2010] 弹飞绵羊

题目传送门

题意分析

其实用 LCT 可以很好维护,但是我们并不会 LCT。

因此考虑一些其他的数据结构来维护。发现并没有什么特别适合的数据结构,考虑分块。

\(\textit{step}_i\) 表示 \(i\) 跳出块的步数,\(\textit{to}_i\) 表示跳出到了哪里。

按照 \(\sqrt n\) 为块长分块。记 \(\operatorname{pos}(i),\operatorname{edgeL}(x),\operatorname{edgeR}(x)\) 分别表示 \(i\) 所在块编号,块 \(x\) 的左边界,块 \(x\) 的右边界。

预处理的时候,有:

  • \(i+k_i\leq\operatorname{edgeR}(\operatorname{pos}(i))\),则有 \(\textit{step}_i=\textit{step}_{i+k_i}+1,\textit{to}_i=\textit{to}_{i+k_i}\)
  • 否则有 \(\textit{step}_i=1,\textit{to}_i=i+k_i\)

更新的时候块内同理重构即可。

时间复杂度: \(\mathcal O\left((n+m)\sqrt n\right)\)

AC 代码

//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
constexpr const int N=2e5;
int n,size,k[N+1];
int pos(int x){return (x+size-1)/size;
}
int edgeL(int x){return (x-1)*size+1;
}
int edgeR(int x){return min(x*size,n);
}
int step[N+1],to[N+1];
void pre(){size=sqrt(n);for(int i=n;1<=i;i--){if(i+k[i]<=edgeR(pos(i))){step[i]=step[i+k[i]]+1;to[i]=to[i+k[i]];}else{step[i]=1;to[i]=i+k[i];}}
}
int query(int x){int ans=0;while(x<=n){ans+=step[x];x=to[x];}return ans;
}
void change(int x,int v){k[x]=v;for(int id=pos(x),i=edgeR(id);edgeL(id)<=i;i--){int x=i;step[i]=0;if(i+k[i]>edgeR(id)){to[i]=i+k[i];step[i]=1;}else{to[i]=to[i+k[i]];step[i]=step[i+k[i]]+1;}}
}
int main(){/*freopen("test.in","r",stdin);freopen("test.out","w",stdout);*/ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin>>n;for(int i=1;i<=n;i++){cin>>k[i];}pre();int m;cin>>m;while(m--){int op,i,j;cin>>op>>i;i++;if(op==1){cout<<query(i)<<'\n';}else{cin>>j;change(i,j);}}cout.flush();/*fclose(stdin);fclose(stdout);*/return 0;
}
http://www.sczhlp.com/news/8889/

相关文章:

  • c#属性(Property)的概念定义及使用详解
  • 火狐浏览器源码根目录下文件和目录说明
  • Ubuntu24.04多版本python安装
  • 基于算法配对的电子管特性图示仪
  • 为 Prometheus 告警规则增加 UI 管理能力
  • NotebookLM替代工具技术解析
  • 第二章 文本分类和初步训练(代码优化2)
  • 利用文档模式继承漏洞:EasyXDM 2.4.19 DOMXSS攻击分析
  • cf318a 题解
  • NOI2025终局之战摘银记
  • 【2025精选】AI大模型API统一接入平台 | 稳定直连ChatGPT/Claude/Deepseek API | 国内AI开发者首选API服务
  • [更新中] CCF CSP-J/S 2025 游记
  • JAVA - 注解
  • 文声图防御框架原理笔记:Interpret then Deactivate(ItD)
  • 思维导图神器 Xmind 2025:安装+激活全流程详解
  • 2025 -- 云智计划 -- 【CSP-S】模拟赛 #171819星航计划S模拟测试七月_总结+题解
  • shell脚本中echo和echo -e的区别
  • 在 Trae 中使用 RustFS MCP 来存储数据
  • WPF 使用 WNetUseConnection 连接 SMB 网络资源
  • Java-Stream流
  • 酵母双杂交实验:操作流程与关键细节
  • 从App Store高效获取iOS渗透测试所需的.ipa文件
  • 基于Java+Springboot+Vue开发的在线电影订票管理系统源码+运行步骤(课程设计/毕业设计)
  • RAG:让AI聊天不再张口就来
  • day19
  • spaCy v2.3发布:新增5种语言模型与性能优化
  • 2025年最新Java后端场景题+八股文合集(100w字面试题总结) - 详解
  • 8.10
  • Hadoop与虚拟机:Linux初识和VMware安装配置
  • 我是不是很有钱?