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:

  • 0 a b
  • 1 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.

Example

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

第一次遇到分块,还好这题不太难懂,总算学会了。

分块就是把整个数组,分成若干个小块,用来减少查询与修改的次数。
一般都是分成sqrt(n)块,这样总时间复杂度就是o(m*logn)。

分块思路:
当需要求和的时候,需要记录跳到当前块的位置,在这个块里的总跳跃数。这个可以预处理。
当需要修改的时候,就直接按照预处理的做法暴力修改该块。

附ac代码:
 1 #include <algorithm>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <cstdlib>
6 using namespace std;
7 const int maxn = 1e5+10;
8 typedef long long LL;
9 int nexf[maxn];//下个块的第一个
10 int cnt[maxn];//一个块里面走多少步
11 int las[maxn];//这个块的最后一个
12 int nu[maxn];
13 int n,m;
14 int bk=sqrt(maxn);
15 void updat(int x,int y) //三种情况
16 {
17 if(y>n)//下一次跳跃超出n
18 {
19 nexf[x]=maxn;
20 cnt[x]=1;
21 las[x]=x;
22 }
23 else if(x/bk*bk==y/bk*bk)//下次跳跃仍在块内
24 {
25 nexf[x]=nexf[y];
26 cnt[x]=cnt[y]+1;
27 las[x]=las[y];
28 }
29 else//下次跳跃进入另一个块
30 {
31 nexf[x]=y;
32 cnt[x]=1;
33 las[x]=y;
34 }
35 }
36 int cal(int x)//计算所有的跳跃数
37 {
38 int ans=0,temp=0;
39 while(x!=maxn)
40 {
41 temp=las[x];
42 ans+=cnt[x];
43 x=nexf[x];
44 }
45 printf("%d %d\n",temp,ans);
46 }
47 int main()
48 {
49
50 scanf("%d%d",&n,&m);
51 for(int i=1;i<=n;++i)
52 scanf("%d",&nu[i]);
53 for(int i=n;i>=1;--i)
54 updat(i,i+nu[i]);
55 for(int i=0;i<m;++i)
56 {
57 int op,a,b;
58 scanf("%d",&op);
59 if(op)
60 {
61 scanf("%d",&a);
62 cal(a);
63 }
64 else//暴力修改
65 {
66 scanf("%d%d",&a,&b);
67 nu[a]=b;
68 int l=a/bk*bk;
69 for(int i=a;i>=l;--i)
70 updat(i,i+nu[i]);
71 }
72 }
73
74 return 0;
75 }

参考博客:http://blog.csdn.net/zmx354/article/details/40582125

CodeForces - 13E(分块)的更多相关文章

  1. CodeForces 13E 分块

    题目链接:http://codeforces.com/problemset/problem/13/E 题意:给定n个弹簧和每个弹簧初始的弹力a[].当球落在第i个位置.则球会被弹到i+a[i]的位置. ...

  2. CodeForces 13E. Holes 分块处理

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

  3. (分块)Holes CodeForces - 13E

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

  4. codeforces 13E . Holes 分块

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

  5. CodeForces 444C 分块

    题目链接:http://codeforces.com/problemset/problem/444/C 题意:给定一个长度为n的序列a[].起初a[i]=i,然后还有一个色度的序列b[],起初b[i] ...

  6. CodeForces 455D 分块

    题目链接:http://codeforces.com/problemset/problem/455/D 题意:给定一个长度为n的序列a[]. m次操作.共有两种操作 1 l r:将序列的a[l].a[ ...

  7. CodeForces 551E 分块

    题目链接:http://codeforces.com/problemset/problem/551/E 题意:给定一个长度为N的序列. 有2个操作 1 l r v:序列第l项到第r项加v(区间加), ...

  8. CodeForces 103D 分块处理

    题目链接:http://codeforces.com/problemset/problem/103/D 题意:给定一个长度为n的序列.然后q个询问.每个询问为(a,b),表示从序列第a项开始每b项的加 ...

  9. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块

    Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...

随机推荐

  1. SAP demo包 示例程序

    在SAP的这个开发类中SABAPDEMOS,存放了N多的demo程序 有空的时候,可以看看.

  2. ALV中的分隔条(SPLITTER_CONTROL)

    如上图,可以做成左右的分割,当然也可以做成上下的分割效果,在每个分割的容器内,显示各自的内容. 需要使用的class: cl_gui_splitter_container, cl_gui_custom ...

  3. java 记录数据持续变化时间

    1.需求:获取count为null和不为null的持续变化 [{count=0, time=0}, {count=10, time=1000}, {count=20, time=2000}, {cou ...

  4. .NET 项目中的单元测试

    .NET 项目中的单元测试 Intro "不会写单元测试的程序员不是合格的程序员,不写单元测试的程序员不是优秀的工程师." -- 一只想要成为一个优秀程序员的渣逼程序猿. 那么问题 ...

  5. 24V降压3.3V芯片,低压降线性稳压器

    PW6206系列是一款高精度,高输入电压,低静态电流,高速,低压降线性稳压器具有高纹波抑制.在VOUT=5V&VIN=7V时,输入电压高达40V,负载电流高达300mA,采用BCD工艺制造.P ...

  6. 我在华为OD的275天

    目录 0 - 时间线 1 - 为什么会去华为 OD 2 - 华为 OD 的工作内容 3 - OD 与华为自有员工的对比 4 - 那,到底要不要去华为 OD? 5 - 网传的 OD 转华为正编,真的假的 ...

  7. html新特性笔记

    HTML5知识总结 l  文档类型声明:<!DOCTYPE HTML> l  新绘制元素: Canvas:标签定义图形,比如图表和其他图像.该标签基于 JavaScript 的绘图 API ...

  8. 网络编程-I/O复用

    I/O模型 Unix下可用的I/O模型有五种: 阻塞式I/O 非阻塞式I/O I/O复用(select和poll.epoll) 信号驱动式I/O(SIGIO) 异步I/O(POSIX的aio_系列函数 ...

  9. 浅析Linux用户空间中的Mmap

    一.MMap基础概念 mmap是一种内存映射文件的方法,即将一个文件或者其它对象映射到进程的地址空间,实现文件磁盘地址和进程虚拟地址空间中一段虚拟地址的一一对映关系.实现这样的映射关系后,进程就可以采 ...

  10. 我教你如何解决 Docker 下载 mcr.microsoft.com 镜像慢的办法

    我教你如何解决 Docker 下载 mcr.microsoft.com 镜像慢的办法 一.介绍 最近,我在写有关使用 Jenkins 搭建企业级持续集成环境的文章,准备了四台服务器,企业级别嘛,一台就 ...