CodeForces - 13E(分块)

本文介绍了一种名为分块的技术,该技术将数组分为多个小块以减少查询和修改操作的时间复杂度。通过预处理每个块的数据,实现快速查找和更新。文章通过一个具体的程序实例解释了如何使用分块技术,并提供了AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }
View Code

 

参考博客:http://blog.youkuaiyun.com/zmx354/article/details/40582125

转载于:https://www.cnblogs.com/zmin/p/8476408.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值