# BZOJ-2002(分块)

Description

Input

Output

Sample Input

4
1 2 1 1
3
1 1
2 1 1
1 1


Sample Output

2
3


#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <random>
#include <time.h>
using namespace std;
#define int long long
#define ls root<<1
#define rs root<<1|1
const int maxn = 2e5 + 7;
const int inf = 0x3f3f3f3f;
std::mt19937 rnd(233);
int belong[maxn], posl[maxn], posr[maxn], len;
int st[maxn], posnext[maxn], a[maxn];
inline int ask(int x)
{
int ans = 0;
while(x){
ans += st[x];
x = posnext[x];
}
return ans;
}
signed main()
{
#ifndef ONLINE_JUDGE
//freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
#endif
int n;
scanf("%lld", &n);
len =(int) sqrt(n);
for (int i = 1;i<=n;i++){
scanf("%lld", a + i);
belong[i] = (i - 1) / len + 1;
}
int cnt = n / len + (n % len ? 1 : 0);
for (int i = 1; i <= cnt;i++){
posl[i] = (i - 1) * len + 1;
posr[i] = i * len;
}
posr[cnt] = n;
for (int i = n; i > 0;i--){
if(i+a[i]>n)
st[i] = 1;
else{
if(belong[i]==belong[i+a[i]]){
st[i] = st[i+a[i]]+1;
posnext[i] = posnext[i + a[i]];
}
else{
st[i] = 1;
posnext[i] = i + a[i];
}
}
}
int m;
scanf("%lld", &m);
while(m--){
int op, x, y;
scanf("%lld%lld", &op, &x);
x++;
if(op==1){
}
else{
scanf("%lld", &y);
a[x] = y;
for (int i = x; i >= posl[belong[x]];i--){
if(i+a[i]>n)
st[i] = 1, posnext[i] = 0;
else{
if(belong[i]==belong[i+a[i]]){
posnext[i] = posnext[i + a[i]];
st[i] = st[i + a[i]] + 1;
}
else{
posnext[i] = i + a[i];
st[i] = 1;
}
}
}
}
}
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客