题目链接:

E. Holes

time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output

Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for one person with following rules:

There are N holes located in a single row and numbered from left to right with numbers from 1 to N. Each hole has it's own power (hole number i has the power ai). If you throw a ball into hole i it will immediately jump to hole i + ai, then it will jump out of it and so on. If there is no hole with such number, the ball will just jump out of the row. On each of the M moves the player can perform one of two actions:

  • Set the power of the hole a to value b.
  • Throw a ball into the hole a and count the number of jumps of a ball before it jump out of the row and also write down the number of the hole from which it jumped out just before leaving the row.

Petya is not good at math, so, as you have already guessed, you are to perform all computations.

Input

The first line contains two integers N and M (1 ≤ N ≤ 105, 1 ≤ M ≤ 105) — the number of holes in a row and the number of moves. The second line contains N positive integers not exceeding N — initial values of holes power. The following M lines describe moves made by Petya. Each of these line can be one of the two types:

  • a b
  • a

Type 0 means that it is required to set the power of hole a to b, and type 1 means that it is required to throw a ball into the a-th hole. Numbers a and b are positive integers do not exceeding N.

Output

For each move of the type 1 output two space-separated numbers on a separate line — the number of the last hole the ball visited before leaving the row and the number of jumps it made.

Examples
input
8 5
1 1 1 1 1 2 8 2
1 1
0 1 3
1 1
0 3 4
1 2
output
8 7
8 5
7 3 题意:从第i个洞可以到达i+power[i],0操作:power[x]=y,1操作询问从x跳出去一共跳了多少步,最后一个洞的编号;
思路:nex[i],num[i],last[i]表示从第i个洞跳到下一个块的洞的标号、跳的数目、和在次块的最后的洞的编号;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n,m,power[maxn],nex[maxn],num[maxn],sq,last[maxn];
inline void init()
{
sq=sqrt(n*1.0);
for(int i=n;i>0;i--)
{
int ne=i+power[i];
if(ne>n)num[i]=1,nex[i]=ne,last[i]=i;
else
{
if(ne/sq==i/sq)num[i]=num[ne]+1,nex[i]=nex[ne],last[i]=last[ne];
else num[i]=1,nex[i]=ne,last[i]=i;
}
}
}
inline void update(int x,int y)
{
power[x]=y;
for(int i=x;i>0&&i/sq==x/sq;i--)
{
int ne=i+power[i];
if(ne>n)num[i]=1,nex[i]=ne,last[i]=i;
else
{
if(ne/sq==i/sq)num[i]=num[ne]+1,nex[i]=nex[ne],last[i]=last[ne];
else num[i]=1,nex[i]=ne,last[i]=i;
}
}
}
inline void query(int x)
{
int p=x,ans=0,la=last[x];
while(p<=n)
{
ans+=num[p];
la=last[p];
p=nex[p];
}
printf("%d %d\n",la,ans);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&power[i]);
init();
while(m--)
{
int op,x,y;
scanf("%d",&op);
if(op)scanf("%d",&x),query(x);
else scanf("%d%d",&x,&y),update(x,y);
}
return 0;
}

  

E. Holes(分块)的更多相关文章

  1. Codeforces Beta Round #13 E. Holes 分块暴力

    E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...

  2. codeforces 13EE. Holes(分块&amp;动态树)

    E. Holes time limit per test 1 second memory limit per test 64 megabytes input standard input output ...

  3. CF 13E. Holes 分块数组

    题目:点这 跟这题BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊  一模一样 分析: 分块数组入门题. 具体的可以学习这篇博文以及做国家集训队2008 - 苏煜<对块状链表的一 ...

  4. CodeForces 13E. Holes 分块处理

    正解是动态树,太难了,仅仅好分块处理水之.看了看status大概慢了一倍之多..     分块算法大体就是在找一个折衷点,使得查询和改动的时间复杂度都不算太高,均为o(sqrt(n)),所以总的时间复 ...

  5. codeforces 13E . Holes 分块

    题目链接 nextt数组表示这个位置的下一个位置. cnt数组表示这个位置 i 到nextt[i]可以弹几次. end[i] 表示在从 i 弹出去的情况下, 最后一个位置是哪里. 然后就看代码吧. # ...

  6. Codeforces Beta Round #13 E. Holes (分块)

    E. Holes time limit per test 1 second memory limit per test 64 megabytes input standard input output ...

  7. (分块)Holes CodeForces - 13E

    题意 n(n≤105)个洞排成一条直线,第ii个洞有力量值ai,当一个球掉进洞ii时就会被立刻弹到i+ai,直到超出n.进行m(m≤105)次操作: ·修改第i个洞的力量值ai. ·在洞xx上放一个球 ...

  8. CF13E Holes LCT

    CF13E Holes LG传送门 双倍经验题,几乎同[HNOI2010]弹飞绵羊,LCT练手题,LG没有LCT题解于是发一波. 从当前点向目标点连边,构成一棵树,带修改就用LCT动态维护答案,由于不 ...

  9. CF 13E Holes

    Holes 题意:现在有一排洞,每个洞有一个弹力,能弹到ai之后的洞,球会弹到这个排的外面,现在有2个操作,0 a b 将第a个洞的弹力设为b, 1 a 将球放入第a个洞,求输出进洞的次数 和 弹出这 ...

随机推荐

  1. PHP爬取知乎日报图片显示不了问题

    在爬取知乎日报的内容时,虽然能拿到图片的地址,在窗口中也能打开图片,但是在前端却显示不了,报403错误. 经查证,这是因为知乎对图片做了防盗链处理,其中一个解决方法是添加meta标签: <met ...

  2. gTest&gMock learning

    在C++中,编写服务后的一种测试方式是使用google的gTest和gMock结合 之前写py,测试方式是将服务挂起,使用工具模拟请求发包,check resp,这样的缺点在于不方便,即使存下了所有的 ...

  3. 机器学习 delay learning

    计蒜之道总决赛考了机器学习,大多数人都不会所以现场学,然后我看了一些之后放弃了..采取了人力分析的办法,最后果然被学习能力碾压.. 不过机器学习看起来是很有趣的,也听别人说了很多,和别人聊了一些,如果 ...

  4. mac 下如何建立vue-cli项目

    command+space : terminal //检测是否存在node node -v //检测是否带有npm安装管理工具 npm -v //具备进行下一步 //在documnets下建立vue文 ...

  5. Struts2框架学习第三章——Struts2基础

    本章要点 —  Struts 1框架的基本知识 — 使用Struts 1框架开发Web应用 —  WebWork框架的基本知识 — 使用WebWork框架开发Web应用 — 在Eclipse中整合To ...

  6. Oracle 冷备份详解【实战案例】

    Oracle 冷备份详解 --准备工作 select * from v$database; select file_name from dba_data_files; create tablespac ...

  7. python中enumerate()函数用法

    python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...

  8. Android数据库框架-----ORMLite关联表的使用

    上一篇已经对ORMLite框架做了简单的介绍:Android数据库框架-----ORMLite 的基本用法~~本篇将介绍项目可能会使用到的一些用法,也为我们的使用ORMLite框架总结出一个较合理的用 ...

  9. 玲珑oj 1117 线段树+离线+离散化,laz大法

    1117 - RE:从零开始的异世界生活 Time Limit:1s Memory Limit:256MByte Submissions:438Solved:68 DESCRIPTION 486到了异 ...

  10. ansible入门五

    一.Ansible条件测试 在ansible中还可以进行条件测试.如果需要根据变量.facts或此前任务的执行结果来做为某task执行与否的前提时要用到条件测试. 1. when语句 在task后添加 ...