CodeForces 13E 分块
题目链接: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 分块的更多相关文章
- CodeForces 13E. Holes 分块处理
正解是动态树,太难了,仅仅好分块处理水之.看了看status大概慢了一倍之多.. 分块算法大体就是在找一个折衷点,使得查询和改动的时间复杂度都不算太高,均为o(sqrt(n)),所以总的时间复 ...
- (分块)Holes CodeForces - 13E
题意 n(n≤105)个洞排成一条直线,第ii个洞有力量值ai,当一个球掉进洞ii时就会被立刻弹到i+ai,直到超出n.进行m(m≤105)次操作: ·修改第i个洞的力量值ai. ·在洞xx上放一个球 ...
- 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 ...
- codeforces 13E . Holes 分块
题目链接 nextt数组表示这个位置的下一个位置. cnt数组表示这个位置 i 到nextt[i]可以弹几次. end[i] 表示在从 i 弹出去的情况下, 最后一个位置是哪里. 然后就看代码吧. # ...
- CodeForces 444C 分块
题目链接:http://codeforces.com/problemset/problem/444/C 题意:给定一个长度为n的序列a[].起初a[i]=i,然后还有一个色度的序列b[],起初b[i] ...
- CodeForces 455D 分块
题目链接:http://codeforces.com/problemset/problem/455/D 题意:给定一个长度为n的序列a[]. m次操作.共有两种操作 1 l r:将序列的a[l].a[ ...
- CodeForces 551E 分块
题目链接:http://codeforces.com/problemset/problem/551/E 题意:给定一个长度为N的序列. 有2个操作 1 l r v:序列第l项到第r项加v(区间加), ...
- CodeForces 103D 分块处理
题目链接:http://codeforces.com/problemset/problem/103/D 题意:给定一个长度为n的序列.然后q个询问.每个询问为(a,b),表示从序列第a项开始每b项的加 ...
- 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 ...
随机推荐
- How to stop pycharm show files in project in red color?
You can change the file color to whatever you want. File > Settings > Editor > Colors&F ...
- json使用
json是存储和交换文本信息的语法,类似 XML,比 XML 更小.更快,更易解析 json 可通过 JavaScript 进行解析 json 数据可使用 AJAX 进行传输 //读取var JSON ...
- Day1-python基础1
本次学习内容 Python介绍 发展史 版本选择 install 第一个程序hello world 字符编码及注释 变量 用户输入 表达式if...else 一.Python介绍 1)Python由来 ...
- JQ001-认识jQuery 和 JQ002-jQuery选择器
JQ001-认识jQuery jQuery环境配置:将jQuery.js文件引入到html页面中即可. 代码如下: <!DOCTYPE html> <html> <hea ...
- "Emgu.CV.CvInvoke”的类型初始值设定项引发异常 解决办法
系统win7 32位,只在这一台电脑上出现这种问题,已知VS编译是X86,在数台电脑上测试都正常. 后来把opencv的dll路径例如 E:\...\x86 加入到系统环境变量中就正常了. emgu ...
- fedora 24 使用扇贝网页版没有声音
(扇贝的官方答疑:https://www.shanbay.com/help/faq/no_voice/) 第4步下载MP3测试文件没办法使用: 似乎因为MP3格式的文件是的版权问题. 打算安装能处理M ...
- 多光谱图像数据库, Multispectral images databses
1. https://scien.stanford.edu/index.php/hyperspectral-image-data/ 2. http://www.cs.columbia.edu/CAVE ...
- Unable to load configuration. - Class: java.net.AbstractPlainSocketImpl
[Bug笔记]Unable to load configuration. - Class: java.net.AbstractPlainSocketImpl 标签: bugjartomcat服务器互联 ...
- Linux下ffmpeg的完整安装
最近在做一个企业项目, 期间需要将用户上传的视频转成flv格式或mp4格式并用flash插件在前端播放, 我决定采用ffmpeg (http://www.ffmpeg.org/ )实现. 当然以前也用 ...
- 子类可以有跟父类中同名的方法,但是会重写父类中的方法,甚至是root class中的方法
/* 子类可以重写父类中的方法,甚至是root class中的方法,比如NSObeject 的new方法,但是后提示警告如下 Method is expected to return an insta ...