分块水过。。。
将序列分成 n√ 块。
记录位置
i
跳到的下一个位置
每次更新都重新计算整块的 next 和 step ,查询时在块与块直接弹跳即可~。
时间复杂度: O(m∗n√)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
const int maxn = 200050, maxm = 100050;
const int eps = 1e-4;
#define REP(__i,__st,__ed) for(int __i = (__st); __i <= (__ed); __i++)
int n, m, size, k[maxn];
int next[maxn], step[maxn];
void init()
{
read(n), size = sqrt(n) + eps;
REP(i, 1, n) read(k[i]);
read(m);
}
void prework()
{
for(int i = n; i > 0; i--)
{
if(i + k[i] > n) next[i] = 0, step[i] = 1;
else
{
if((i + k[i] - 1)/size == (i - 1)/size)
next[i] = next[i + k[i]], step[i] = step[i + k[i]] + 1;
else
next[i] = i + k[i], step[i] = 1;
}
}
}
int count(int x)
{
int ret = 0;
while(x) ret += step[x], x = next[x];
return ret;
}
void update(int x,int v)
{
int blo = (x - 1)/size + 1;
int l = (blo - 1)*size, r = size*blo;
k[x] = v;
for(int i = r; i >= l; i--)
{
if(i + k[i] > n) next[i] = 0, step[i] = 1;
else
{
if((i + k[i] - 1)/size == (i - 1)/size)
next[i] = next[i + k[i]], step[i] = step[i + k[i]] + 1;
else
next[i] = i + k[i], step[i] = 1;
}
}
}
void solve()
{
int op, pos, val;
while(m--)
{
read(op), read(pos), ++pos;
if(op == 1)
write(count(pos)), puts("");
else
read(val), update(pos, val);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("2002.in","r",stdin);
freopen("2002.out","w",stdout);
#endif
init();
prework();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}