CodeForces 13E Holes(分块处理)

Holes
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

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 ≤ 1051 ≤ 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 test(s)
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~n个洞,每个洞i都有一个对应的值v[i],当小球落入洞i中后将弹到第i+v[i]个洞里,直到弹出所有洞。一共m次操作,0 a b表示将洞a的值修改为b,1 a表示询问,要求输出将小球投入洞a后,小球最后弹入的洞和小球弹的次数。

每次修改操作都需要更新,如果暴力一定超时。所以考虑分块处理,将n个洞分成根号n个区块,每次只需要在洞a所在的区块中更新。cnt数组记录小球弹出所在区块需要的次数,last数组记录小球在所在区块最后弹入的洞,link数组记录小球从所在区块弹入其他区块时首先弹入的洞。这样,时间复杂度由O(N²)优化为O(NlogN)。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int n,m;
int v[100005];//v[i]:第i个洞的值
int cnt[100005];//cnt[i]:跳出第i个洞所在区块所需要的步数
int last[100005];//last[i]:跳出第i个洞所在区块时最后待过的洞
int link[100005];//link[i]:从i所在区块跳出后到达的洞
int block;//每个区块中洞的个数

void update(int x, int y)//从第x个洞跳到第y个洞
{
    if(y > n)//跳出去了
    {
        link[x] = n + 1;
        last[x] = x;
        cnt[x] = 1;
    }
    else if(x / block == y / block)//跳在同一个区块中
    {
        link[x] = link[y];
        last[x] = last[y];
        cnt[x] = cnt[y] + 1;
    }
    else//跳到其他区块
    {
        link[x] = y;
        last[x] = x;
        cnt[x] = 1;
    }
}

void Search(int x)
{
    int num,ans;
    ans = cnt[x];
    num = last[x];
    while(1)
    {
        x = link[x];
        if(x > n) break;
        num = last[x];
        ans += cnt[x];
    }
    printf("%d %d\n",num,ans);
}

int main()
{
    int choice,x,a,b;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        block = ceil(sqrt(n));//开根后进一法取整
        //cout << block << endl;
        for(int i = 1; i <= n; i++)
            scanf("%d",&v[i]);
        for(int i = n; i > 0; i--)//从后往前更新
            update(i,i + v[i]);
        while(m --)
        {
            scanf("%d",&choice);
            if(choice)
            {
                scanf("%d",&x);
                Search(x);
            }
            else
            {
                scanf("%d%d",&a,&b);
                v[a] = b;
                for(int i = a; i && i / block == a / block; i--)//更新所在区块即可
                    update(i,i + v[i]);
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章