CodeForces 797E:配列クエリのセグメント化された処理

ポータル

タイトル説明

aは長さnの正の整数シーケンスであり、各項目の値はnを超えません。
これでq個のクエリグループができました。クエリの各セットには、2つのパラメータpとkが含まれています。操作が繰り返されます:pをp + ap + kに変更します。この操作は、pがnより大きくなるまで続きます。この質問に対する答えは、操作の数です。

分析

n ^ 2の複雑さで結果を前処理できますが、DP前処理には明らかに開いていないN * Nスペースが必要です。どうすれ
よいですか?sqrt(n)の範囲よりも大きいkの範囲をセグメント化できます。基本的に1回のジャンプで十分であり、sqrt(n)の範囲よりも小さいため、暴力を振るうことができます。前処理することができます。

コード

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 100010;
int a[N];
int f[N][500];
int n,q;

int main(){
    
    
    scanf("%d",&n);
    for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
    int maxv = (int)(sqrt(n) + 0.5);
    for(int i = n;i;i--)
        for(int j = 1;j <= maxv;j++)
            if(i + a[i] + j > n) f[i][j] = 1;
            else f[i][j] = f[i + a[i] + j][j] + 1;
    scanf("%d",&q);
    while(q--){
    
    
        int x,y;
        scanf("%d%d",&x,&y);
        if(y <= maxv) printf("%d\n",f[x][y]);
        else{
    
    
            int ans = 0;
            while(x <= n){
    
    
                x = x + a[x] + y;
                ans++;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}


/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


おすすめ

転載: blog.csdn.net/tlyzxc/article/details/112883223