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.

Sample test(s)
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
题意:
给你n个编号1到N的洞(N<=1e5)。

每一个洞有个能量值po[i]。

当你在i号洞里放一个球时.这个球会弹到i+po[i]号洞内。

然后弹到i+po[i]+po[i+po[i]]。。

。。也就是说球每到一个洞i就会弹到i+po[i]号洞内。

如今有两种操作。

1.0 a b。把a号洞的po改成b
2.1 a。

询问当把球放到a号洞内时。它是从几号洞弹出界的。和它一共弹了几次。


思路:
假设题目没有改动操作。这题就会非常easy。

我们仅仅须要把每一个洞的出界点和弹跳数预处理出来就能够了。如今关键是怎么处理改动操作。假设还是依照上述预处理方式肯定时间复杂度下不来。

所以我们要想办法使每次改动操作更新尽量少的信息。于是能够想到分块处理。就是把整个序列分成sqrt(N)块。序列中每一个节点记录next[i]表示i结点要跳到下个块的位置。ed[i]表示放到i号洞时的出界位置。st[i]表示。

i跳到下个块须要的步数。这样预处理后每次改动操作仅仅会影响同块内且标号比当前小的位置的信息。

所以最多改动sqrt(n)个位置的信息。对于每次查询操作。因为结点指针仅仅会指向不同的块所以仅仅须要顺着指针统计一遍就好了。最多跳sqrt(n)次。

总时间发杂度O(M*sqrt(N))在能够接受的范围内。

具体见代码:
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
typedef long long ll;
int st[maxn],next[maxn],ed[maxn],po[maxn];
int block,n,m,ans,ansp;
void update(int x,int y)
{
if(y>n)
ed[x]=x,st[x]=1,next[x]=y;
else
{
ed[x]=ed[y];//ed[x]为从x处放球的终点
if(x/block==y/block)
next[x]=next[y],st[x]=st[y]+1;//next[i]表示i跳到另外的块的位置
else
next[x]=y,st[x]=1;
}
}
void qu(int x)
{
ans=0;
while(1)
{
ans+=st[x];
if(next[x]>n)
{
ansp=ed[x];
break;
}
x=next[x];
}
}
int main()
{
int i,cmd,a,b; while(~scanf("%d%d",&n,&m))
{
block=ceil(sqrt(1.0*n));
for(i=1;i<=n;i++)
scanf("%d",&po[i]);
for(i=n;i>=1;i--)
update(i,i+po[i]);
while(m--)
{
scanf("%d%d",&cmd,&a);
if(cmd)
{
qu(a);
printf("%d %d\n",ansp,ans);
}
else
{
scanf("%d",&po[a]);
b=(a/block)*block;
b=max(b,1);
for(i=a;i>=b;i--)
update(i,i+po[i]);
}
}
}
return 0;
}

codeforces 13EE. Holes(分块&amp;动态树)的更多相关文章

  1. CodeForces 13E. Holes 分块处理

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

  2. BZOJ 2002 Bounce 弹飞绵羊 (分块或动态树)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 13768  Solved: 6989[Subm ...

  3. codeforces 13E . Holes 分块

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

  4. 如何利用FineReport制作动态树报表

    在对数据字段进行分类管理时,利用动态树折叠数据是一个很好的方法,也就是点击数据前面的加号才展开对应下面的数据,如下图.那这样的效果在制作报表时该如何实现呢? 下面以报表工具FineReport为例介绍 ...

  5. 动态树之LCT(link-cut tree)讲解

    动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作.其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT( ...

  6. 【BZOJ-3589】动态树 树链剖分 + 线段树 + 线段覆盖(特殊的技巧)

    3589: 动态树 Time Limit: 30 Sec  Memory Limit: 1024 MBSubmit: 405  Solved: 137[Submit][Status][Discuss] ...

  7. BZOJ-2049 Cave洞穴勘测 动态树Link-Cut-Tree (并查集骗分TAT)

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 5833 Solved: 2666 [Submit] ...

  8. 学习笔记-动态树Link-Cut-Tree

    --少年你有梦想吗? --少年你听说过安利吗? 安利一个集训队讲解:http://wenku.baidu.com/view/75906f160b4e767f5acfcedb 关于动态树问题,有多种方法 ...

  9. BZOJ 3589 动态树(子树操作,链查询)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3589 题意:给出一棵有根树,两种操作:(1)以u为根的子树所有节点权值加上一个数字 ...

随机推荐

  1. HDU2586How far away? LCA

    去博客园看该题解 题意 给出一棵树,以及每条边的权值,给出一些询问,每个询问是2个节点,求每个询问对应的2个节点的距离. 算法 LCA_Tarjan 代码 #include <cstring&g ...

  2. maven启动tomcat访问报404(url中没有项目名)

    [INFO] Running war on http://localhost:8080/js_001(红色部分是项目名,要是没有的话是不能访问项目资源的) 但是我仍然不知道为什么有些maven项目却有 ...

  3. 堆排序算法(Java实现)

    将待排序的序列构造成一个大顶堆(从大到小排要构造成小顶堆).此时,整个序列的最大值就是堆顶的根节点,将他和末尾元素交换,然后将剩余的length-1个节点序列重新构造成新的堆.重复执行,便能得到一个有 ...

  4. js读取cookie,并利用encrypt和decrypt加密和解密方法

    以下为支持encrypt和decrypt加密和解密方法 eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a ...

  5. js(javaScript)的各种事件触发,以常见为主eg:onclick

    js的各种事件触发,以常见为主eg:onclick1.onclick,点击后触发事件 (1)<h1 onclick="this.innerHTML='谢谢!'">请点击 ...

  6. 爬虫之xpath用法

    导包用: from lxml import etree

  7. html-列表-3

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 2018WFU校赛B题

    我们在ACM的题目中已经了解了什么是ACM了,ACM还是很残酷的了(ಥ _ ಥ),那么现在你就要解决一个ACM最简单的题了,简单到省赛和区域赛都不会出这种简单的题.ls很强,即使每年都在ACM这个大坑 ...

  9. axios 进行类库封装

    ,,,,,, ,) { // 与后台约定的成功状态码 callback && callback(result) } else { // 错误处理,优先以自定义的的handle进行处理, ...

  10. tar 打包压缩

    tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...