CodeForces - 13E

时间:2022-06-04 00:22:47

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
分块,对每次更新,维护每个块里面的每个元素的三个属性:下一个到达的位置,跳的次数,最后一次跳的位置。
 #include <cstdio>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> #define lid id<<1
#define rid id<<1|1
#define closein cin.tie(0)
#define scac(a) scanf("%c",&a)
#define scad(a) scanf("%d",&a)
#define print(a) printf("%d\n",a)
#define debug printf("hello world")
#define form(i,n,m) for(int i=n;i<m;i++)
#define mfor(i,n,m) for(int i=n;i>m;i--)
#define nfor(i,n,m) for(int i=n;i>=m;i--)
#define forn(i,n,m) for(int i=n;i<=m;i++)
#define scadd(a,b) scanf("%d%d",&a,&b)
#define memset0(a) memset(a,0,sizeof(a))
#define scaddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scadddd(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d) #define INF 0x3f3f3f3f
#define maxn 100005
typedef long long ll;
using namespace std;
//---------AC(^-^)AC---------\\ int n,m,blo;
int cnt[maxn],nxt[maxn],last[maxn],power[maxn]; void update(int i,int j)
{
if(j>n)
{
cnt[i]=;
last[i]=i;
nxt[i]=n+;
}
else if(i/blo==j/blo)
{
nxt[i]=nxt[j];
cnt[i]=cnt[j]+;
last[i]=last[j];
}
else
{
nxt[i]=j;
last[i]=i;
cnt[i]=;
}
}
void query(int x)
{
int num,ans;
ans=cnt[x];
num=last[x];
while(true)
{
x=nxt[x];
if(x>n) break;
num=last[x];
ans+=cnt[x];
}
printf("%d %d\n",num,ans);
} int main()
{
scadd(n,m);
blo=ceil(sqrt(n));
forn(i,,n) scad(power[i]);
nfor(i,n,) update(i,i+power[i]);
while(m--)
{
int op;
scad(op);
if(op==)
{
int x;
scad(x);
query(x);
}
else
{
int x,y;
scadd(x,y);
power[x]=y;
for(int i=x;i&&i/blo==x/blo;i--) update(i,i+power[i]); }
}
return ;
}

<-click here to see the code