Codeforces Beta Round #13 E. Holes 分块暴力

E. Holes

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/13/problem/E

Description

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.

Sample Input

8 5
1 1 1 1 1 2 8 2
1 1
0 1 3
1 1
0 3 4
1 2

Sample Output

8 7
8 5
7 3

HINT

题意

有n个弹簧,每次扔个球,这个球可以弹到i+power[i]的位置

然后有两种操作

将第i个位置的弹簧的弹力改为b

将球扔到第i个位置,问这个球最后弹到了哪儿,这个球弹了几次?

题解:

分块暴力,块内暴力修改就好了,块外就直接指过去

暴力搞一搞就好了,这道题和hnoi2010弹飞绵羊这道题基本上是一样的

代码

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000010
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

int power[maxn];
int block;
int cnt[maxn];
int end[maxn];
int next[maxn];
int n,m;
void update(int i,int j)
{
    if(j>n)
    {
        next[i]=n+1;
        cnt[i]=1;
        end[i]=i;
    }
    else
    {
        if(i/block==j/block)
        {
            cnt[i]=cnt[j]+1;
            end[i]=end[j];
            next[i]=next[j];
        }
        else
        {
            next[i]=j;
            end[i]=i;
            cnt[i]=1;
        }
    }
}
void solve(int x)
{
    int c=cnt[x],e=end[x];
    while(1)
    {
        x=next[x];
        if(x>n)
            break;
        c+=cnt[x];
        e=end[x];
    }
    printf("%d %d
",e,c);
}
int main()
{
    n=read(),m=read();
    block=ceil(sqrt(n*1.0));
    for(int i=1;i<=n;i++)
        power[i]=read();
    for(int i=n;i>=1;i--)
        update(i,i+power[i]);
    for(int i=0;i<m;i++)
    {
        int q=read();
        if(q)
        {
            int v=read();
            solve(v);
        }
        else
        {
            int a=read(),b=read();
            update(a,a+b);
            for(int i=a-1;i>=a/block*block;i--)
                update(i,i+power[i]);
            power[a]=b;
        }
    }
}
原文地址:https://www.cnblogs.com/qscqesze/p/4596018.html