博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Beta Round #13 E. Holes (分块)
阅读量:6514 次
发布时间:2019-06-24

本文共 3426 字,大约阅读时间需要 11 分钟。

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:

  • 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 【分析】数轴上n个点,没个点 i 上有一个值a[i],表示站在i点上下一步将跳向i+a[i]点,可能跳出区间[1,n]。然后两次操作,第一种是将某个a[i]改成x,另一种就是询问从x节点开始  需要多少步才能跳出去,最后一个经过的点是哪一个。  可以分块做。对于每个块,统计每个点需要多少步跳出此块,并且跳出此块后将跳向哪个点。然后就很简单了,详细看代码
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 1000000000#define met(a,b) memset(a,b,sizeof a)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long ll;using namespace std;const int N = 1e5+5;const int M = 4e2+50;int n,m;int l[N],r[N],belong[N];int cnt,num,x,v,ans;int a[N],tot[M],go[N],nxt[N];void init(){ num=sqrt(n); cnt=n/num; if(n%num)cnt++; for(int i=1;i<=n;i++){ belong[i]=(i-1)/num+1; } for(int i=1;i<=cnt;i++){ l[i]=(i-1)*num+1; r[i]=min(n,i*num); for(int j=r[i];j>=l[i];j--){ if(nxt[j]>r[i]){ tot[j]=1; go[j]=nxt[j]; } else { tot[j]=tot[nxt[j]]+1; go[j]=go[nxt[j]]; } } }}int main() { int op,ll,rr,x,y; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&a[i]),nxt[i]=min(n+1,a[i]+i); init(); while(m--){ scanf("%d",&op); if(!op){ scanf("%d%d",&x,&y); int b=belong[x]; nxt[x]=min(n+1,x+y); for(int j=r[b];j>=l[b];j--){ if(nxt[j]>r[b]){ tot[j]=1; go[j]=nxt[j]; } else { tot[j]=tot[nxt[j]]+1; go[j]=go[nxt[j]]; } } } else { scanf("%d",&x); int b,y; int ans1,ans2=0; while(x<=n){ ans2+=tot[x]; if(go[x]>n)y=x; x=go[x]; } while(y<=n){ ans1=y; y=nxt[y]; } printf("%d %d\n",ans1,ans2); } } return 0;}

 

转载于:https://www.cnblogs.com/jianrenfang/p/6674426.html

你可能感兴趣的文章
dSYM 文件分析工具
查看>>
R语言合并data.frame
查看>>
linux主机下的Vmware Workstation配置NAT设置 端口映射-Ubuntu为例
查看>>
unity physics joint
查看>>
TD的访问地址
查看>>
JAVA常见面试题之Forward和Redirect的区别
查看>>
tmpFile.renameTo(classFile) failed 错误
查看>>
【甘道夫】Apache Hadoop 2.5.0-cdh5.2.0 HDFS Quotas 配额控制
查看>>
一张图看懂normal,static,sealed,abstract 的 区别
查看>>
Task的使用
查看>>
grep和正则表达式
查看>>
s:iterator巧妙控制跳出循环
查看>>
移动互联网思维
查看>>
redis-手写redis切片和非切片连接池并注入springboot中
查看>>
Kosaraju算法详解
查看>>
Serv-U 的升级及数据备份和迁移【转】
查看>>
webstorm无法显示左边文件夹目录的解决方法
查看>>
Android数据保存之文件保存
查看>>
数字校园-云资源平台 2014.10.26-人人通共享空间
查看>>
使用IIS承载WCF服务
查看>>