Codeforces Beta Round #13 E. Holes (分块)
1 second
64 megabytes
standard input
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.
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.
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.
8 5
1 1 1 1 1 2 8 2
1 1
0 1 3
1 1
0 3 4
1 2
8 7
8 5
7 3
【分析】数轴上n个点,没个点 i 上有一个值a[i],表示站在i点上下一步将跳向i+a[i]点,可能跳出区间[1,n]。然后两次操作,第一种是将某个a[i]改成x,另一种就是询问从x节点开始
需要多少步才能跳出去,最后一个经过的点是哪一个。
可以分块做。对于每个块,统计每个点需要多少步跳出此块,并且跳出此块后将跳向哪个点。然后就很简单了,详细看代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#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|1
typedef long long ll;
using namespace std;
const int N = 1e5+;
const int M = 4e2+;
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=;i<=n;i++){
belong[i]=(i-)/num+;
}
for(int i=;i<=cnt;i++){
l[i]=(i-)*num+;
r[i]=min(n,i*num);
for(int j=r[i];j>=l[i];j--){
if(nxt[j]>r[i]){
tot[j]=;
go[j]=nxt[j];
}
else {
tot[j]=tot[nxt[j]]+;
go[j]=go[nxt[j]];
}
}
}
} int main() {
int op,ll,rr,x,y;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&a[i]),nxt[i]=min(n+,a[i]+i);
init();
while(m--){
scanf("%d",&op);
if(!op){
scanf("%d%d",&x,&y);
int b=belong[x];
nxt[x]=min(n+,x+y);
for(int j=r[b];j>=l[b];j--){
if(nxt[j]>r[b]){
tot[j]=;
go[j]=nxt[j];
}
else {
tot[j]=tot[nxt[j]]+;
go[j]=go[nxt[j]];
}
}
}
else {
scanf("%d",&x);
int b,y;
int ans1,ans2=;
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 ;
}
Codeforces Beta Round #13 E. Holes (分块)的更多相关文章
- Codeforces Beta Round #13 E. Holes 分块暴力
E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round 84 (Div. 2 Only)
layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
随机推荐
- java enum用法
基本用法 enum Day { SUNDAY, MONDAY, TUESDAY, WENDSDAY, THURSDAY, FRIDAY, SATURDAY; } 枚举是常量,所以应该用大写. 枚举是对 ...
- 【BZOJ5010】【FJOI2017】矩阵填数 [状压DP]
矩阵填数 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 给定一个 h*w 的矩阵,矩阵的行 ...
- 程序员你为什么这么累? - Controller规范
导读:程序员你为什么这么累? 接口定义:程序员你为什么这么累? - 接口定义 第一篇文章中,我贴了2段代码,第一个是原生态的,第2段是我指定了接口定义规范,使用AOP技术之后最终交付的代码,从15行到 ...
- css3动画总结
- Coursera在线学习---第十节.大规模机器学习(Large Scale Machine Learning)
一.如何学习大规模数据集? 在训练样本集很大的情况下,我们可以先取一小部分样本学习模型,比如m=1000,然后画出对应的学习曲线.如果根据学习曲线发现模型属于高偏差,则应在现有样本上继续调整模型,具体 ...
- Webmin LFD to LFI
Webmin < 1.290 / Usermin < 1.220 - Arbitrary File Disclosure (Perl) https://www.exploit-db.com ...
- perl6中函数参数(1)
sub F($number is copy){ $number++; say $number; } F(); #下面是错误的 sub F($number){ $number++; say $numbe ...
- 自动化测试===unittest和requests接口测试案例,测试快递查询api(二)
在原来基础上生成测试报告: 首先需要 HTMLTestRunner.py 的unittest生成报告文件 (源码,自动化测试===unittest配套的HTMLTestRunner.py生成html ...
- FC4-i386-SRPMS
[重点] http://archives.fedoraproject.org/pub/archive/fedora/linux/core/6/ http://archives.fedoraprojec ...
- JAVA常见的集合类
关系的介绍: Set(集):集合中的元素不按特定方式排序,并且没有重复对象.他的有些实现类能对集合中的对象按特定方式排序. List(列表):集合中的元素按索引位置排序,可以有重复对象,允许按照对象在 ...