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

题意:给定n个弹簧和每个弹簧初始的弹力a[]。当球落在第i个位置。则球会被弹到i+a[i]的位置.

现在有2种操作:

1 a b:把第a个弹簧的弹力修改为b。

2 a:当球初始放入的位置为a时,需要弹几次才会弹出n。弹出前的最后一个位置是多少。 输出位置和次数。

思路:和BZOJ 2002一样。 然后记录一个最后弹出去的位置preidx。每次弹出当前块的时候记录当前的位置即可。然后最后再暴力模拟最后弹出去时所在的块的位置即可。

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
typedef long long int LL;
const int MAXN = + ;
int belong[MAXN], block, num, L[MAXN], R[MAXN];
int n, q;
int a[MAXN], cnt[MAXN], to[MAXN];
void build(){
block = (int)sqrt(n + 0.5);
num = n / block; if (n%block){ num++; }
for (int i = ; i <= num; i++){
L[i] = (i - )*block + ; R[i] = i*block;
}
R[num] = n;
for (int i = ; i <= n; i++){
belong[i] = ((i - ) / block) + ;
}
for (int i = num; i>=; i--){
for (int j = R[i]; j >= L[i]; j--){
if (j + a[j]>R[i]){
cnt[j] = ; to[j] = min(n + , j + a[j]);
}
else{
cnt[j] = cnt[j + a[j]] + ; to[j] = min(n + , to[j + a[j]]);
}
}
}
}
void modify(int pos, int val){
a[pos] = val;
for (int i = pos; i >= L[belong[pos]]; i--){
if (i + a[i]>R[belong[pos]]){
cnt[i] = ; to[i] = min(i + a[i], n + );
}
else{
cnt[i] = cnt[i + a[i]] + ; to[i] = min(to[i + a[i]], n + );
}
}
}
void query(int pos, int &ans, int &preidx){
ans = ;
for (int i = pos; i <= n; i = to[i]){
ans += cnt[i];
preidx = i; //记录最后弹出去前的位置
}
for (int i = preidx; i <= n; i = i + a[i]){//暴力模拟最后在哪个位置弹出去了
preidx = i;
}
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
while (~scanf("%d%d", &n, &q)){
for (int i = ; i <= n; i++){ scanf("%d", &a[i]); }
build(); int type, pos, v, ans, idx;
for (int i = ; i <= q; i++){
scanf("%d", &type);
if (type == ){
scanf("%d%d", &pos, &v); modify(pos, v);
}
else{
scanf("%d", &pos); query(pos, ans, idx); printf("%d %d\n", idx, ans);
}
}
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}

CodeForces 13E 分块的更多相关文章

  1. CodeForces 13E. Holes 分块处理

    正解是动态树,太难了,仅仅好分块处理水之.看了看status大概慢了一倍之多..     分块算法大体就是在找一个折衷点,使得查询和改动的时间复杂度都不算太高,均为o(sqrt(n)),所以总的时间复 ...

  2. (分块)Holes CodeForces - 13E

    题意 n(n≤105)个洞排成一条直线,第ii个洞有力量值ai,当一个球掉进洞ii时就会被立刻弹到i+ai,直到超出n.进行m(m≤105)次操作: ·修改第i个洞的力量值ai. ·在洞xx上放一个球 ...

  3. CodeForces - 13E(分块)

    Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for on ...

  4. codeforces 13E . Holes 分块

    题目链接 nextt数组表示这个位置的下一个位置. cnt数组表示这个位置 i 到nextt[i]可以弹几次. end[i] 表示在从 i 弹出去的情况下, 最后一个位置是哪里. 然后就看代码吧. # ...

  5. CodeForces 444C 分块

    题目链接:http://codeforces.com/problemset/problem/444/C 题意:给定一个长度为n的序列a[].起初a[i]=i,然后还有一个色度的序列b[],起初b[i] ...

  6. CodeForces 455D 分块

    题目链接:http://codeforces.com/problemset/problem/455/D 题意:给定一个长度为n的序列a[]. m次操作.共有两种操作 1 l r:将序列的a[l].a[ ...

  7. CodeForces 551E 分块

    题目链接:http://codeforces.com/problemset/problem/551/E 题意:给定一个长度为N的序列. 有2个操作 1 l r v:序列第l项到第r项加v(区间加), ...

  8. CodeForces 103D 分块处理

    题目链接:http://codeforces.com/problemset/problem/103/D 题意:给定一个长度为n的序列.然后q个询问.每个询问为(a,b),表示从序列第a项开始每b项的加 ...

  9. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块

    Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...

随机推荐

  1. OverWatch团队文档格式规范

    V1.0 最终修改于2016/10/19 概述 软件工程中,一份优雅的文档不仅能降低团队成员之间的沟通难度,而且能给之后的开发者提供一个非常有效的引导.本团队为了规范整个项目中文档的格式,便于统一管理 ...

  2. 树莓派系统介绍:DIetPi

    项目主页:http://fuzon.co.uk/phpbb/viewtopic.php?f=8&t=6 当前版本:V34(15年4月16日发布) DietPi是国外一个基于Raspbian的精 ...

  3. C# BlockCollection

    1.BlockCollection集合是一个拥有阻塞功能的集合,它就是完成了经典生产者消费者的算法功能. 它没有实现底层的存储结构,而是使用了IProducerConsumerCollection接口 ...

  4. DataTable数据检索的性能分析(转寒江独钓)

    我们知道在.NET平台上有很多种数据存储,检索解决方案-ADO.NET Entity Framework,ASP.NET Dynamic Data,XML, NHibernate,LINQ to SQ ...

  5. 项目里面的某个资源文件(比如plist、音频等)无法使用

    检查:Build Phases -> Copy Bundle Resources

  6. Git 简明教程

    其他Git资料: Git Community Book 中文版

  7. 【PHP升级】CentOS6.3编译安装 PHP5.4.38

    先前安装的PHP5.3.28(参考:CentOS6.3编译安装Nginx1.4.7 + MySQL5.5.25a + PHP5.3.28),现在准备升级PHP到5.4.38,有如下几个地方需要重新编译 ...

  8. Linux 执行文件查找命令 which 详解

    某个文件不知道放在哪里了,通常可以使用下面的一些命令来查找: which  查看可执行文件的位置 whereis 查看文件的位置 locate   配合数据库查看文件位置 find   实际搜寻硬盘查 ...

  9. HTTP API开发

    近期手上的安卓app需要用到自动更新的功能,想着怎么实现,看了下很多应用商店的sdk,觉得不太好,可能会收集用户隐私,于是想着自己实现一个http api后面自己可以实现自动更新,甚至广告推送的功能, ...

  10. js parseInt 显示0

    parseInt 有第二个参数, 就是进制参数 parseInt("08", 10);  //表示这个数字是十进制的就不会出错了.