BZOJ 2002 Bounce 弹飞绵羊 (分块或动态树)

2002: [Hnoi2010]Bounce 弹飞绵羊

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 13768  Solved: 6989
[Submit][Status][Discuss]

Description

某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏更有趣,Lostmonkey可以修改某个弹力装置的弹力系数,任何时候弹力系数均为正整数。

Input

第一行包含一个整数n,表示地上有n个装置,装置的编号从0到n-1,接下来一行有n个正整数,依次为那n个装置的初始弹力系数。第三行有一个正整数m,接下来m行每行至少有两个数i、j,若i=1,你要输出从j出发被弹几次后被弹飞,若i=2则还会再输入一个正整数k,表示第j个弹力装置的系数被修改成k。对于20%的数据n,m<=10000,对于100%的数据n<=200000,m<=100000

Output

对于每个i=1的情况,你都要输出一个需要的步数,占一行。

Sample Input

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

Sample Output

2
3

HINT

 

Source

 

析:把这 n 个数进行分块,每次维护一块,当修改时,也是只是修改一块,查询是是块数。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200000 + 100;
const int mod = 1000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r > 0 && r <= n && c > 0 && c <= m;
}

int l[maxn], pos[maxn], val[maxn], r[maxn];
int a[maxn], belong[maxn];

int solve(int x){
  int ans = 0;
  while(pos[x]){
    ans += val[x];
    x = pos[x];
  }
  return ans + pos[x];
}

int main(){
  scanf("%d", &n);
  for(int i = 0; i < n; ++i)  scanf("%d", a+i);
  int block = sqrt(n);
  int len = block;
  if(n % len)  ++block;
  for(int i = 0; i < block; ++i)  l[i] = i * len, r[i] = l[i] + len - 1;
  r[block-1] = n-1;  // do not loss
  for(int i = 0; i < n; ++i)  belong[i] = i / len;
  for(int i = n-1; i >= 0; --i)
    if(i + a[i] > r[belong[i]])  val[i] = 1, pos[i] = i + a[i];
    else  val[i] = val[i+a[i]] + 1, pos[i] = pos[i+a[i]];
  scanf("%d", &m);
  while(m--){
    int op, u, v;
    scanf("%d %d", &op, &u);
    if(op == 1)  printf("%d
", solve(u));
    else{
      scanf("%d", &v);
      a[u] = v;
      for(int i = u; i >= l[belong[u]]; --i)
        if(i + a[i] > r[belong[i]])  val[i] = 1, pos[i] = i + a[i];
        else val[i] = val[i+a[i]] + 1, pos[i] = pos[i+a[i]];
    }
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/7515991.html