BZOJ 2002 - 弹飞绵羊 分块

传送门

 题意:

  询问每只绵羊通过序列跳出去的次数。

思路:

  分块,预处理每个块中每个位子跳出块的第一个位子就行。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}


/*-----------------------showtime----------------------*/


            const int maxn = 200009;

            int a[maxn],bel[maxn];
            int n,m,blo;

            int cnt[maxn],dp[maxn];
            void rebuild(int id){
                    int up = min(n, id*blo);
                    int down = (id-1)*blo+1;
                    for(int i=up; i>= down; i--){
                        if(a[i] + i > up) {cnt[i] = 1,dp[i] = a[i] + i;}
                        else {
                            int to = a[i] + i;
                            dp[i] = dp[to];
                            cnt[i] = cnt[to] + 1;
                        }
                    }
            }

            int query(int x){
                int res = 0;
                int t = x;
                while(t <= n){
                    res += cnt[t];
                    t = dp[t];
                }
                return res;
            }
int main(){
            scanf("%d", &n);
            for(int i=1; i<=n; i++) scanf("%d", &a[i]);
            scanf("%d", &m);
            blo = 10*sqrt(n);
            for(int i=1; i<=n; i++) bel[i] = (i-1)/blo + 1;
            for(int i=1; i<=bel[n]; i++) {
                rebuild(i);
            }
            for(int i=1; i<=m; i++){
                int op; scanf("%d", &op);
                if(op == 1){
                    int x;  scanf("%d", &x);
                    printf("%d\n", query(x+1));
                }
                else {
                    int x,y;
                    scanf("%d%d", &x, &y);
                    a[x+1] = y;
                    rebuild(bel[x+1]);
                }
            }
            return 0;
}

 

posted @ 2019-01-23 16:39  ckxkexing  阅读(149)  评论(0编辑  收藏  举报