Codeforces13E - Holes
Description
\(n(n\leq10^5)\)个洞排成一条直线,第\(i\)个洞有力量值\(a_i\),当一个球掉进洞\(i\)时就会被立刻弹到\(i+a_i\),直到超出\(n\)。进行\(m(m\leq10^5)\)次操作:
- 修改第\(i\)个洞的力量值\(a_i\)。
- 在洞\(x\)上放一个球,问该球几次后被哪个洞弹飞出界。
Solution
将\(n\)个洞分成大小为\(\sqrt n\)的\(\sqrt n\)个块。
\(c[i]\)记录\(i\)要跳出所在的块需要多少次,\(nxt[i]\)记录跳出到哪个点。
修改时,从后到前重构该块内所有点的\(c[i]\)和\(nxt[i]\),其他块不受影响。
查询时,由\(i\)跳到\(nxt[i]\)并累加\(c[i]\),在即将出界\((nxt[i]>n)\)前一步一步跳来得知是哪个洞将它弹出界的。
时间复杂度\(O(m\sqrt n)\)。
Code
//Holes
#include <cstdio>
#include <cmath>
inline char gc()
{
static char now[1<<16],*S,*T;
if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;}
return *S++;
}
inline int read()
{
int x=0,f=1; char ch=gc();
while(ch<'0'||'9'<ch) {if(ch=='-') f=-1; ch=gc();}
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
int const N=1e5+10;
int n,m,n0;
int p[N],nxt[N],c[N];
void update(int t)
{
int fr=t*n0,to=fr+n0-1; if(to>n) to=n;
for(int i=to;i>=fr;i--)
if(i+p[i]>to) nxt[i]=i+p[i],c[i]=1;
else nxt[i]=nxt[i+p[i]],c[i]=1+c[i+p[i]];
}
int main()
{
n=read(),m=read(); n0=sqrt(n);
for(int i=1;i<=n;i++) p[i]=read(),nxt[i]=i,c[i]=0;
for(int t=0;t<=n/n0;t++) update(t);
for(int i=1;i<=m;i++)
{
int opt=read();
if(opt==0)
{
int x=read(),y=read();
p[x]=y; update(x/n0);
}
else
{
int res=0,pre;
for(int x=read();x<=n;x=nxt[x]) pre=x,res+=c[x];
while(pre+p[pre]<=n) pre+=p[pre];
printf("%d %d\n",pre,res);
}
}
return 0;
}
P.S.
Codeforces13E - Holes的更多相关文章
- Codeforces Beta Round #13 E. Holes 分块暴力
E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...
- codeforces 13EE. Holes(分块&动态树)
E. Holes time limit per test 1 second memory limit per test 64 megabytes input standard input output ...
- 寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)
一.问题提出 这是一个来自"answerOpenCV"(http://answers.opencv.org/question/200422/opencv-c-filling-hol ...
- Linux操作系统中文件结构stat中st_size的说明以及对于文件中洞(Holes)的理解
文件stat结构体中st_size成员 对于所有的文件类型,st_size成员对其中的普通文件.目录以及符号链接有实在的意义.其中,对于普通文件而言,st_size记录了该文件的实际大小:对于目录而言 ...
- CF13E Holes LCT
CF13E Holes LG传送门 双倍经验题,几乎同[HNOI2010]弹飞绵羊,LCT练手题,LG没有LCT题解于是发一波. 从当前点向目标点连边,构成一棵树,带修改就用LCT动态维护答案,由于不 ...
- Codeforces 797 F Mice and Holes
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test 1.5 ...
- E. Holes(分块)
题目链接: E. Holes time limit per test 1 second memory limit per test 64 megabytes input standard input ...
- Codeforces Beta Round #13 E. Holes (分块)
E. Holes time limit per test 1 second memory limit per test 64 megabytes input standard input output ...
- 2018/7/19 考试(tower,work,holes)
noip模拟赛,挺良心的题,考的贼烂(膜一下@来日方长大佬(sdfz rank1)) 不多说了,看题吧 1.tower 题面: 铁塔(tower.pas/c/cpp) 题目描述 Rainbow和Fre ...
随机推荐
- 如何通过命令或脚本方式在Windows上访问linux系统
很多情况下,我们需要在Windows上写脚本,创建计划任务程序,这个过程中可能需要访问linux系统,执行脚本或者上传下载文件.并且我们也不想在Windows上安装什么东西.那最好的办法就是使用put ...
- android dialog弹出的情况下监听返回键
view = LayoutInflater.from(getActivity()).inflate( R.layout.dialog_tips, null); title2 = (TextView) ...
- 用Markdown格式写一份前端简历
1. 基本信息 姓名:xxx 手机号码:1380000xxxx 学校:南昌大学 学历:大学本科/硕士/博士 工作经验:3年以上Web前端 电子邮件:xxx@outlook.com 2. 求职意向 工作 ...
- spring boot热部署
1.pom配置 参考:http://412887952-qq-com.iteye.com/blog/2300313 2.intellij配置 参考:http://blog.csdn.net/wjc47 ...
- dfs 与 dijkstra 总结
Dijkstra: //寻求加权图起始点到各个节点的最短路径 for i <- 1:n do distance[i] <- INF; distance[0] <- 0;//起始节点距 ...
- 基于 React + Webpack 的音乐相册项目(下)
上一篇我们完成了音乐相册里面的播放图片的功能,这一篇主要完成的是音乐相册里面的音乐播放器功能.最终让我们基于 React 的音乐相册图文并茂.有声有色. 我们主要从以下几个部分来展开: 数据准备 进度 ...
- ffmpeg命令行循环推流
用ffmpeg循环推一个文件到rtmp服务器.一般都是建议用-stream_loop选项.如: ffmpeg -threads -re -fflags +genpts -stream_loop - - ...
- srs2录制flv文件metadata不准确
测试环境:server:srs2client:librestreaming / yasea srs 配置 dvr录制24分钟flv文件. e:\flv $ ll total drwxr-xr-x Ad ...
- SpringMVC源码情操陶冶-DispatcherServlet简析(二)
承接前文SpringMVC源码情操陶冶-DispatcherServlet类简析(一),主要讲述初始化的操作,本文将简单介绍springmvc如何处理请求 DispatcherServlet#doDi ...
- Springboot security cas源码陶冶-FilterSecurityInterceptor
前言:用户登录信息校验成功后,都会获得当前用户所拥有的全部权限,所以对访问的路径当前用户有无权限则需要拦截验证一发 Spring security过滤器的执行顺序 首先我们需要验证为啥FilterSe ...