首页 技术 正文
技术 2022年11月15日
0 收藏 653 点赞 3,681 浏览 1163 个字

传送门啦

非常神奇的分块大法。

每块分 √N 个元素 , 预处理出来:对于每个点,记录两个量:一个是它要弹几次才能出它所在的这个块,另外一个是它弹出这个块后到哪个点。

查询操作:一块一块跳过去 单次复杂度 O(√N)

修改操作:只需要把相应的块改一遍就好了 这个也是O(√N)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 200005;
const int maxm = 100005;inline int read(){
char ch = getchar();
int f = 1 , x = 0;
while(ch > '9' || ch < '0'){if(ch == '-')f = -1;ch =getchar();}
while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}
return x * f;
}int n,a[maxn],m,flag,x,y,ans;
int l[maxn],r[maxn],cnt,belong[maxn],to[maxn],step[maxn];void init(){
int t = sqrt(n);
if(n / t) cnt = n / t + 1;
else cnt = n / t;
for(int i=1;i<=cnt;i++){
l[i] = r[i-1] + 1;
r[i] = min(l[i] + t - 1 , n);
}
int x = 1;
for(int j=1;j<=n;j++){
belong[j] = x;
if(j == r[x] && x <= cnt) x++;
}
for(int i=n;i>=1;i--){
to[i] = i + a[i];
if(to[i] > r[belong[i]]) step[i] = 1;
else {
step[i] = step[to[i]] + 1;
to[i] = to[to[i]];
}
}
}int main(){
n = read();
for(int i=1;i<=n;i++) a[i] = read();
init();
m = read();
while(m--){
ans = 0;
flag = read();
if(flag == 1){
x = read(); x++;
while(x <= n){
ans += step[x];
x = to[x];
}
printf("%d\n",ans);
}
else {
x = read(); y = read();
x++;
a[x] = y;
for(int i=r[belong[x]];i>=l[belong[x]];i--){
to[i] = i + a[i];
if(to[i] > r[belong[i]]) step[i] = 1;
else {
step[i] = step[to[i]] + 1;
to[i] = to[to[i]];
}
}
}
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893