http://www.elijahqi.win/2018/03/02/codeforces-13e-holes/
题意翻译
有N个洞,每个洞有相应的弹力,能把这个球弹到i+power[i] 位置。当球的位置>N时即视为被弹出
输入: 第一行两个正整数N,M,下面N个数代表初始的power[i] 之后M行分别代表M个操作 共有两种操作: 0 a b 把a位置的弹力改成b 1 a 在a处放一个球,输出球被弹出前共被弹了多少次,最后一次落在哪个洞。
输出:对于每个操作2,输出两个题目中要求的值(空格隔开)
1<=N<=100000 1<=M<=100000
题目描述
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 N
N holes located in a single row and numbered from left to right with numbers from 1 to
N N
N . Each hole has it’s own power (hole number
i i
i has the power
ai a_{i}
a
i
). If you throw a ball into hole
i i
i it will immediately jump to hole
i+ai i+a_{i}
i+a
i
, 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 M
M moves the player can perform one of two actions:
Set the power of the hole
a a
a to value
b b
b .
Throw a ball into the hole
a a
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.
输入输出格式
输入格式:
The first line contains two integers
N N
N and
M M
M (
1<=N<=105 1<=N<=10^{5}
1<=N<=10
5
,
1<=M<=105 1<=M<=10^{5}
1<=M<=10
5
) — the number of holes in a row and the number of moves. The second line contains
N N
N positive integers not exceeding
N N
N — initial values of holes power. The following
M M
M lines describe moves made by Petya. Each of these line can be one of the two types:
0 0
0
a a
a
b b
b
1 1
1
a a
a
Type
0 0
0 means that it is required to set the power of hole
a a
a to
b b
b , and type
1 1
1 means that it is required to throw a ball into the
a a
a -th hole. Numbers
a a
a and
b b
b are positive integers do not exceeding
N N
N .
输出格式:
For each move of the type
1 1
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.
输入输出样例
输入样例#1: 复制
8 5
1 1 1 1 1 2 8 2
1 1
0 1 3
1 1
0 3 4
1 2
输出样例#1: 复制
8 7
8 5
7 3
非常的弹飞绵羊 啊 注意一点 这个要求输出最后的位置要考虑跳出去是直接跳出块 还是需要在块里再跳几下出去才可以找到最后的位置
设nxt[i]表示i号节点跳出块后会在第几号节点 step[i]表示这个节点跳出块要几步 然后每次暴力sqrt(n)求解即可
#include<cmath>
#include<cstdio>
#include<algorithm>
#define N 110000
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=gc();
return x*f;
}
int nxt[N],step[N],n,m,aa[N],nn;
inline void rebuild(int x,int s){
int l=(x-1)/nn*nn+1,r=min(n,l+nn-1);aa[x]=s;
for (int i=x;i>=l;--i){
if (i+aa[i]>r) nxt[i]=i+aa[i],step[i]=1;
else nxt[i]=nxt[i+aa[i]],step[i]=step[i+aa[i]]+1;
}
}
inline void qr(int x){
int tmp=0,last=0;
while(x<=n){
if (nxt[x]>n) last=x;
tmp+=step[x];x=nxt[x];
}while(last+aa[last]<=n) last=last+aa[last];
printf("%d %d\n",last,tmp);
}
int main(){
// freopen("cf13e.in","r",stdin);
n=read();m=read();nn=sqrt(n);
for (int i=1;i<=n;++i) aa[i]=read();
for (int i=n;i;--i) rebuild(i,aa[i]);
while(m--){
int op=read(),a=read();
if (!op) {int b=read();rebuild(a,b);}else qr(a);
//for (int i=1;i<=n;++i) printf("%d %d\n",nxt[i],step[i]);
//puts("sd");
}
return 0;
}