【Codeforces 797E】Array Queries

相关链接

题目传送门:http://codeforces.com/contest/797/problem/E

解题报告

我们发现暴搜加上记忆化的复杂度是$O(n \sqrt{n})$的
于是暴搜就好了

Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 100009;

struct Query{int p,k,id;}q[N];
int n,m,stp[N],a[N],ans[N];
queue<int> vis;

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

inline bool cmp(const Query &A, const Query &B) {
	return A.k < B.k; 
}

inline void init() {
	for (;!vis.empty();vis.pop()) {
		stp[vis.front()] = 0;
	}
}

int DFS(int w, int k) {
	if (w > n) return 0;
	else if (stp[w] > 0) return stp[w];
	else return vis.push(w), stp[w] = DFS(w + a[w] + k, k) + 1;	
}

int main() {
	n = read(); for (int i=1;i<=n;i++) a[i] = read();
	m = read(); for (int i=1;i<=m;i++) q[i].p = read(), q[i].k = read(), q[i].id = i;
	sort(q+1, q+1+m, cmp);
	for (int i=1;i<=m;i++) {
		if (i > 1 && q[i].k != q[i-1].k) init();
		ans[q[i].id] = DFS(q[i].p, q[i].k);
	} 
	for (int i=1;i<=m;i++) printf("%d\n",ans[i]);
	return 0;
}

24 thoughts to “【Codeforces 797E】Array Queries”

  1. Howdy just wanted to give you a quick heads up and let
    you know a few of the images aren’t loading correctly. I’m
    not sure why but I think its a linking issue. I’ve tried it
    in two different internet browsers and both show the same outcome.

  2. With havin so much content do you ever run into
    any problems of plagorism or copyright infringement?
    My site has a lot of exclusive content I’ve either
    authored myself or outsourced but it appears
    a lot of it is popping it up all over the internet without my permission. Do you
    know any ways to help protect against content from being stolen?
    I’d really appreciate it.

  3. Someone necessarily assist to make severely posts
    I might state. That is the first time I frequented your
    website page and to this point? I amazed with the research you made to create
    this actual publish incredible. Excellent process!

  4. Thanks a bunch for sharing this with all people you actually know what
    you are speaking approximately! Bookmarked. Kindly also seek advice from my web
    site =). We may have a hyperlink trade arrangement between us

  5. I’m curious to find out what blog system you have been using?
    I’m having some minor security issues with my latest blog and I’d like
    to find something more secure. Do you have any recommendations?

  6. I know this if off topic but I’m looking into starting my own weblog and was curious what all is required to get set up? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web smart so I’m not 100 sure. Any tips or advice would be greatly appreciated. Cheers

  7. I have learn some excellent stuff here. Definitely price
    bookmarking for revisiting. I surprise how a lot effort you put to make such a fantastic informative web site.

  8. I have been browsing on-line greater than three hours
    as of late, but I by no means discovered any attention-grabbing article like yours.

    It is pretty worth sufficient for me. In my view, if all
    webmasters and bloggers made excellent content material as you probably did,
    the net shall be much more helpful than ever before.

  9. Pretty element of content. I simply stumbled upon your web
    site and in accession capital to say that I acquire in fact enjoyed account your
    weblog posts. Any way I’ll be subscribing to your feeds and even I fulfillment you get entry to constantly fast.

  10. You’ve made some good points there. I checked on the web to find out more about the
    issue and found most individuals will go along with your views
    on this website.

  11. I’m more than happy to uncover this page. I need to
    to thank you for your time for this particularly wonderful read!!
    I definitely enjoyed every bit of it and i also have you saved
    as a favorite to look at new stuff in your web site.

Leave a Reply

Your email address will not be published. Required fields are marked *