CodeForces 797E: Array Queries segmented processing

Portal

Title description

a is a positive integer sequence of length n, and the value of each item does not exceed n.
Now there are q groups of queries. Each set of queries contains two parameters p and k. An operation is repeated: change p into p + ap + k. This operation will continue until p is greater than n. The answer to this query is the number of operations.

analysis

We can preprocess the result through the complexity of n^2, but the DP preprocessing requires N * N space, which is obviously not open. What should
we do? We can segment the range of k, which is larger than the range of sqrt(n). You can go to violence, because basically one jump is enough, and we can preprocess it if it is less than the range of sqrt(n)

Code

#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 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


Guess you like

Origin blog.csdn.net/tlyzxc/article/details/112883223