Luo Valley P3203 [HNOI2010] ricocheted sheep (block)

Portal

Solution to a problem late at night

This question should be a positive solution with dynamic tree, but I like this chicken dish is certainly not learned ah

Mang block to consider it because of the complexity of this question block is O (m√n) is perfectly acceptable

First block normal operation, the entire block is divided into columns √n like, around each recording interval, and each of which belongs to one location.

Then for each location i, determined to [i]: i position through continuous jumping out of the position of the block after this. step [i]: i out of the position of this minimum number of blocks needed.

Then for each query x, through constant iteration x = to [x], cumulative step [x], you can find the answer in the time complexity of O (√n) is.

For each modify the x position, we can adjust the violence of entire x where all locations and to step, where the time complexity is O (√n), while the other position because we have to consider in the calculation of step and only within the range in which the position of the block, without affecting all modifications.

===================== ===================== dividing line

Think for a moment, if not modified, then the O (1) query method should anyone can write it, but if you add modification operations on the basis of this method, then modify the time complexity is O (n) of the , put the whole question, the time complexity becomes O (mn) up.

Here spend the block, although the time complexity of the query becomes O (√n), but the same modification time complexity is reduced to O (√n), then the solution to the problem is to reduce the overall complexity of O ( m√n), the equivalent of us by block, shared equally the complexity of the query and modify, which is the core purpose of the block where the thought of it.

+++++++++++++++++++ Kankan Code ++++++++++++++++++++++

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 200010
using namespace std;
int n,m,t;
int a[MAXN],L[MAXN],R[MAXN],pos[MAXN],step[MAXN],to[MAXN];

int read(){
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}

int main(){
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    t=sqrt(n);
    for(int i=1;i<=t;i++) L[i]=(i-1)*t+1,R[i]=i*t;
    if(R[t]<n) t++,L[t]=R[t-1]+1,R[t]=n;
    for(int i=1;i<=t;i++) for(int j=L[i];j<=R[i];j++) pos[j]=i;
    for(int i=n;i>=1;i--){
        to[i]=i+a[i];step[i]=1;
        if(to[i]<=R[pos[i]]) step[i]+=step[to[i]],to[i]=to[to[i]];
    }
    m=read();
    int opt,x,y;
    while(m--){
        opt=read();x=read();x++;
        if(opt==1){
            int ans=0,now=x;
            while(now<=n) ans+=step[now],now=to[now];
            printf("%d\n",ans);
        }
        else if(opt==2){
            y=read();a[x]=y;
            for(int i=x;i>=L[pos[x]];i--){
                to[i]=i+a[i];step[i]=1;
                if(to[i]<R[pos[i]]) step[i]+=step[to[i]],to[i]=to[to[i]];
            }
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/BakaCirno/p/11717500.html