CodeForces - 13E

本文详细解析了一款名为「洞穴」的游戏算法。游戏中,玩家在一个包含N个洞穴的行中进行操作,每个洞穴都有其特定的力量值。玩家可以更改洞穴的力量值或投入球体并追踪球体的跳跃路径。文章通过分块技术,介绍了如何高效地更新和查询洞穴状态,确保了游戏的流畅运行。

摘要生成于 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.

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
分块,对每次更新,维护每个块里面的每个元素的三个属性:下一个到达的位置,跳的次数,最后一次跳的位置。
 1 #include <cstdio>
 2 #include <stack>
 3 #include <cmath>
 4 #include <queue>
 5 #include <string>
 6 #include <queue>
 7 #include <cstring>
 8 #include <iostream>
 9 #include <algorithm>
10 
11 #define lid id<<1
12 #define rid id<<1|1
13 #define closein cin.tie(0)
14 #define scac(a) scanf("%c",&a)
15 #define scad(a) scanf("%d",&a)
16 #define print(a) printf("%d\n",a)
17 #define debug printf("hello world")
18 #define form(i,n,m) for(int i=n;i<m;i++)
19 #define mfor(i,n,m) for(int i=n;i>m;i--)
20 #define nfor(i,n,m) for(int i=n;i>=m;i--)
21 #define forn(i,n,m) for(int i=n;i<=m;i++)
22 #define scadd(a,b) scanf("%d%d",&a,&b)
23 #define memset0(a) memset(a,0,sizeof(a))
24 #define scaddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
25 #define scadddd(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
26 
27 #define INF 0x3f3f3f3f
28 #define maxn 100005
29 typedef long long ll;
30 using namespace std;
31 //---------AC(^-^)AC---------\\
32 
33 int n,m,blo;
34 int cnt[maxn],nxt[maxn],last[maxn],power[maxn];
35 
36 void update(int i,int j)
37 {
38     if(j>n)
39     {
40         cnt[i]=1;
41         last[i]=i;
42         nxt[i]=n+1;
43     }
44     else if(i/blo==j/blo)
45     {
46         nxt[i]=nxt[j];
47         cnt[i]=cnt[j]+1;
48         last[i]=last[j];
49     }
50     else
51     {
52         nxt[i]=j;
53         last[i]=i;
54         cnt[i]=1;
55     }
56 }
57 void query(int x)
58 {
59     int num,ans;
60     ans=cnt[x];
61     num=last[x];
62     while(true)
63     {
64         x=nxt[x];
65         if(x>n) break;
66         num=last[x];
67         ans+=cnt[x];
68     }
69     printf("%d %d\n",num,ans);
70 }
71 
72 int main()
73 {
74     scadd(n,m);
75     blo=ceil(sqrt(n));
76     forn(i,1,n) scad(power[i]);
77     nfor(i,n,1) update(i,i+power[i]);
78     while(m--)
79     {
80         int op;
81         scad(op);
82         if(op==1)
83         {
84             int x;
85             scad(x);
86             query(x);
87         }
88         else
89         {
90             int x,y;
91             scadd(x,y);
92             power[x]=y;
93             for(int i=x;i&&i/blo==x/blo;i--)    update(i,i+power[i]);
94 
95         }
96     }
97     return 0;
98 }
<-click here to see the code

转载于:https://www.cnblogs.com/mile-star/p/10478563.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值