ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

E - Holes

2020-02-27 18:02:24  阅读:365  来源: 互联网

标签:ball Holes number blo hole include row


题目链接:http://codeforces.com/problemset/problem/13/E

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:

  • a b
  • 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
Output8 7
8 5
7 3
思路:

x[i]代表在i位置跳出所属的块需要的步数
y[i]代表在i位置跳出所属的块到达的位置

看代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
#include<map>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define sc1(a) scanf("%lld",&a)
#define pf1(a) printf("%lld\n",a)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const LL INF=1e18;
const ull base=2333;
const int maxn=1e5+50;
const int maxm=1e3+50;
const int maxv=1e6+5;
const int mod=1e9+7;
const int ba=3e5;
LL N,M;
LL a[maxn];
LL block;
LL x[maxn],y[maxn];
LL blo[maxn];
/**
x[i]代表在i位置跳出所属的块需要的步数
y[i]代表在i位置跳出所属的块到达的位置
*/
void Build()
{
    for(int i=N;i>=1;i--)
    {
        x[i]=(i+a[i])>min(N,block*blo[i])?1:x[i+a[i]]+1;
        y[i]=(i+a[i])>min(N,block*blo[i])?i+a[i]:y[i+a[i]];
    }
}
void Update(LL u,LL v)
{
    a[u]=v;
    for(int i=u;i>(blo[u]-1)*block;i--) //后面的没有影响
    {
        x[i]=(i+a[i])>min(N,block*blo[i])?1:x[i+a[i]]+1;
        y[i]=(i+a[i])>min(N,block*blo[i])?i+a[i]:y[i+a[i]];
    }
}
void Query(LL u)
{
    LL ans1=0,ans2=0;
    while(u<=N)
    {
        if(y[u]>N) ans1=u;
        ans2+=x[u];//
        u=y[u];
    }
    while(ans1+a[ans1]<=N) ans1=ans1+a[ans1];//最后一个跳出去的位置
    printf("%lld %lld\n",ans1,ans2);
}
int main()
{
    sc1(N);sc1(M);
    block=sqrt(N);
    for(int i=1;i<=N;i++)
    {
        sc1(a[i]);
        blo[i]=(i-1)/block+1;
    }
    Build();
    LL opt,u,v;
    while(M--)
    {
        sc1(opt);
        if(opt==0)
        {
            sc1(u);sc1(v);
            Update(u,v);
        }
        else
        {
            sc1(u);//查询u
            Query(u);
        }
    }
    return 0;
}
/**

*/

 

标签:ball,Holes,number,blo,hole,include,row
来源: https://www.cnblogs.com/caijiaming/p/12373394.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有