zoukankan      html  css  js  c++  java
  • E. Holes(分块)

    题目链接:

    E. 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 ≤ 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
    output
    8 7
    8 5
    7 3

    题意:从第i个洞可以到达i+power[i],0操作:power[x]=y,1操作询问从x跳出去一共跳了多少步,最后一个洞的编号;
    思路:nex[i],num[i],last[i]表示从第i个洞跳到下一个块的洞的标号、跳的数目、和在次块的最后的洞的编号;
    AC代码:
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10;
    int n,m,power[maxn],nex[maxn],num[maxn],sq,last[maxn];
    inline void init()
    {
        sq=sqrt(n*1.0);
        for(int i=n;i>0;i--)
        {
            int ne=i+power[i];
            if(ne>n)num[i]=1,nex[i]=ne,last[i]=i;
            else
            {
                if(ne/sq==i/sq)num[i]=num[ne]+1,nex[i]=nex[ne],last[i]=last[ne];
                else num[i]=1,nex[i]=ne,last[i]=i;
            }
        }
    }
    inline void update(int x,int y)
    {
        power[x]=y;
        for(int i=x;i>0&&i/sq==x/sq;i--)
        {
            int ne=i+power[i];
            if(ne>n)num[i]=1,nex[i]=ne,last[i]=i;
            else
            {
                if(ne/sq==i/sq)num[i]=num[ne]+1,nex[i]=nex[ne],last[i]=last[ne];
                else num[i]=1,nex[i]=ne,last[i]=i;
            }
        }
    }
    inline void query(int x)
    {
        int p=x,ans=0,la=last[x];
        while(p<=n)
        {
            ans+=num[p];
            la=last[p];
            p=nex[p];
        }
        printf("%d %d
    ",la,ans);
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)scanf("%d",&power[i]);
        init();
        while(m--)
        {
            int op,x,y;
            scanf("%d",&op);
            if(op)scanf("%d",&x),query(x);
            else scanf("%d%d",&x,&y),update(x,y);
        }
        return 0;
    }
    

      

  • 相关阅读:
    写了10000条Airtest截图脚本总结出来的截图经验,赶紧收藏!
    自动化测试实操案例详解 | iOS应用篇
    Photoshop 2020特别版,内置多款实用插件,功能强大
    vue click.stop阻止点击事件继续传播
    CSS图标与文字对齐的两种方法
    为什么像王者荣耀这样的游戏Server不愿意使用微服务?
    13 张图解 Java 中的内存模型
    记住没:永远不要在 MySQL 中使用 UTF-8
    牛x!一个比传统数据库快 100-1000 倍的数据库!
    为什么我不建议你用去 “ ! = null " 做判空?
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/7878344.html
Copyright © 2011-2022 走看看