a is an array of n positive integers, all of which are not greater than n.

You have to process q queries to this array. Each query is represented by two numbers p and k. Several operations are performed in each query; each operation changes p to p + ap + k. There operations are applied until p becomes greater than n. The answer to the query is the number of performed operations.

Input

The first line contains one integer n (1 ≤ n ≤ 100000).

The second line contains n integers — elements of a (1 ≤ ai ≤ n for each i from 1 to n).

The third line containts one integer q (1 ≤ q ≤ 100000).

Then q lines follow. Each line contains the values of p and k for corresponding query (1 ≤ p, k ≤ n).

Output

Print q integers, ith integer must be equal to the answer to ith query.

Example

input
3
1 1 1
3
1 1
2 1
3 1
output
2
1
1

Consider first example:

In first query after first operation p = 3, after second operation p = 5.

In next two queries p is greater than n after the first operation.

题意:

给你q次查询,每次会有两个数字p,k,问每次使p=p+a[p]+k,总共需要多少次会使p>n

题解:

存粹暴力必然超时,因此需要打个表,

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
const int INF=-0x3f3f3f3f;
int a[MAXN];
int dp[MAXN][510];
int main()
{
int n;
scanf("%d",&n);
for (int i = 1; i <=n ; ++i) {
scanf("%d",&a[i]);
}
for(int i=n;i>=1;i--) {//表示为p,由大->小,我们需要变化的次数增加
for (int j = 1; j <=500; ++j) {//表示为k的大小
if(i+a[i]+j>n) dp[i][j]=1;
else
dp[i][j]=dp[i+a[i]+j][j]+1;
}
}
int ans=0;
int m;
scanf("%d",&m);
int p,k;
while(m--)
{
ans=0;
scanf("%d%d",&p,&k);
if(k>400)
{
while(p<=n)
{
p=p+a[p]+k;
ans++;
}
printf("%d\n",ans);
}
else
printf("%d\n",dp[p][k]);
}
return 0;
}

  

CF797E. Array Queries的更多相关文章

  1. lightoj Again Array Queries

    1100 - Again Array Queries   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...

  2. Codeforces 797E - Array Queries

    E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...

  3. codeforces 797 E. Array Queries【dp,暴力】

    题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...

  4. AC日记——Array Queries codeforces 797e

    797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...

  5. Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~

                                                                                                        ...

  6. Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~

                                                                              1100 - Again Array Queries ...

  7. [Codeforces 863D]Yet Another Array Queries Problem

    Description You are given an array a of size n, and q queries to it. There are queries of two types: ...

  8. Yet Another Array Queries Problem CodeForces - 863D (暴力/思维)

    You are given an array a of size n, and q queries to it. There are queries of two types: 1 li ri — p ...

  9. Light oj 1100 - Again Array Queries (鸽巢原理+暴力)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1100 给你n个数,数的范围是1~1000,给你q个询问,每个询问问你l到r之间 ...

随机推荐

  1. Python开发环境Wing IDE如何检查Python集成

    在使用Wing IDE开始代码编辑之前,必须先确保Wing IDE已经成功地找到用户的Python安装位置(如果用户同时安装有多个版本,那么Wing IDE将有限选择最新版).要对这个进行检查,需要调 ...

  2. List之Sort使用

    void TestListSort(){ List<string> st = new List<string> (); st.Add ("abcd"); s ...

  3. linux服务器tomcat启动时,不能加载项目

    ---恢复内容开始--- 问题描述:tomcat启动时候,能够成功启动但是项目不能加载,查看catalina.out后出现以下的错误 due to a StackOverflowError. Poss ...

  4. maven下nutz与servlet报错org.nutz.mvc.NutFilter cannot be cast to javax.servlet.Filter

    使用maven搭建nutz时,加入servlet报错:org.nutz.mvc.NutFilter cannot be cast to javax.servlet.Filter 十二月 03, 201 ...

  5. eclipse 创建 user library 方法

    1.Window - Preferences - Java - Build Path - User Libraries 2.新建 UserLibraries 3. 4.重复上一步依次添加需要的jar文 ...

  6. Strut2_声明式异常处理

    Service 往外抛异常 public List<Category> list() throws SQLException{ Connection conn = DB.createCon ...

  7. 怎样学习webpack - 走心分享

    很多朋友可能都知道webpack,也见过webpack的代码,但是不明白里面一坨一坨的东西是什么意思,到底有什么用处!我们每个人学习一个新东西可能都会有这个过程,但是我个人觉得webpack可能是最混 ...

  8. Leetcode 78. Subsets (backtracking) 90 subset

    using prev class Solution { List<List<Integer>> res = new ArrayList<List<Integer&g ...

  9. Illegal access:this web application instance has been stopped already

    七月 23, 2014 2:34:35 下午 org.apache.catalina.loader.WebappClassLoader loadClass信息: Illegal access: thi ...

  10. 搭建FTP服务

    (一)FTP服务概述 FTP服务概述:名称.功能.特点.端口 VSFTP:very secure FTP  端口:21 服务安装#yum install vsftpd lftp -y   ##lftp ...