【BZOJ 4408】[FJOI2016] 神秘数

链接

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4408
神犇题解:http://www.cnblogs.com/ihopenot/p/6179880.html

题解

假如我们给定一个已经排好序的序列{ai}
考虑神秘数产生的原因:\({a_i} > 1 + \sum\limits_{j = 1}^{i – 1} {{a_j}}\)

考虑给定的询问,如果使用函数式线段树进行处理,那么已经排好序了
现在的问题就是如何找到最小的、符合上述要求的\({a_i}\)

这货不满足二分的性质,所以不能用二分
然后我就不会做了 QwQ
这时,神犇ihopenot说:“直接暴力模拟就可以了啊!”
具体来说:

假设当前已经能够凑出1~i中的所有的数
统计1~(i+1)这个区间内所有数的和
如果等于i则停止,否则继续该操作

我们来考虑一下为什么这样做复杂度是正确的:

假设当前能凑出1~i,使用的最大的数为a
那么合法的下一个数一定在a+1到i+1之间
换一句话来说:进行一次增值操作可以“收割”长度为i-a的区间
更进一步:使用的数最小为a+2,能凑出的数至少为i+a+2
更进两步:下一次至少可以收割长度为i的区间
更进三步:每次收割区间的长度会增加ai,而且ai > a(i-1)+1
更进四步:仔细想一想,这货比Fibonacci数列增长还要快,所以至多进行log(n)次

然后这题就用函数式线段树来暴力迭代就可以辣!
这™是我第四次被Fibonacci数列坑了 QwQ
ps:这货貌似是双倍经验,参见BZOJ_4299

Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;
 
const int N = 100000+9;
const int M = 3300000;
const int INF = 1e9;
 
int n,m,arr[N];
 
inline int read(){
    char c=getchar(); int ret=0,f=1;
    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;
}
 
namespace Chair_Tree{
    #define CT Chair_Tree
    int ch[M][2],sum[M],root[N];
    int ans_tmp,cnt;
     
    void insert(int p, int &w, int l, int r, int v) {
        w = ++cnt; sum[w] = sum[p] + v;
        memcpy(ch[w],ch[p],sizeof(ch[w]));
        if (l < r) {
            int mid = l + r + 1 >> 1;
            if (v < mid) insert(ch[p][0], ch[w][0], l, mid-1, v);
            else insert(ch[p][1],ch[w][1], mid, r, v);
        }   
    }
     
    inline void insert(int p, int v) {
        insert(root[p-1],root[p],1,INF,v);
    }
     
    void query(int p, int w, int l, int r, int v) {
        if (l == r) {
            ans_tmp += sum[w] - sum[p];
        } else {
            int mid = l + r + 1 >> 1;
            if (v < mid) query(ch[p][0],ch[w][0],l,mid-1,v);
            else {
                ans_tmp += sum[ch[w][0]] - sum[ch[p][0]];
                query(ch[p][1],ch[w][1],mid,r,v);
            }
        }
    }
     
    inline int query(int l, int r, int v) {
        ans_tmp = 0;
        query(root[l-1],root[r],1,INF,v);
        return ans_tmp;
    }
};
 
int main(){
    n = read();
    for (int i=1;i<=n;i++) { 
        arr[i] = read();
        CT::insert(i,arr[i]);
    }
         
    m = read();
    for (int j=1,l,r,cur,tmp;j<=m;j++) {
        l = read(); r = read(); cur = 0;
        while ((tmp = CT::query(l,r,cur+1)) > cur) {
            cur = tmp;
        }
        printf("%d\n",cur+1);
    }
    return 0;
}

82 thoughts to “【BZOJ 4408】[FJOI2016] 神秘数”

  1. I know this if off topic but I’m looking into starting
    my own blog and was curious what all is needed to get set up?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very internet smart so I’m not 100% positive. Any
    recommendations or advice would be greatly appreciated.
    Appreciate it

  2. Do you mind if I quote a couple of your articles as long as I provide credit and
    sources back to your weblog? My website is in the very same
    niche as yours and my users would truly benefit
    from some of the information you present here. Please let me know if this alright with you.
    Thanks a lot!

  3. Do you mind if I quote a few of your articles as long as I provide
    credit and sources back to your site? My website is in the exact same area of
    interest as yours and my users would definitely benefit from some of the information you present here.
    Please let me know if this ok with you. Cheers!

  4. I’m not sure exactly why but this blog is loading extremely slow for
    me. Is anyone else having this problem or is it a problem on my end?
    I’ll check back later and see if the problem still exists.

  5. Have you ever considered writing an ebook or guest authoring on other websites?
    I have a blog based on the same topics you discuss and would really like to have you share some stories/information. I know my viewers would enjoy your work.
    If you’re even remotely interested, feel free to shoot me an e-mail.

  6. Hello there! Quick question that’s entirely off topic.
    Do you know how to make your site mobile friendly?
    My blog looks weird when viewing from my iphone4. I’m trying to find a theme or
    plugin that might be able to resolve this issue.

    If you have any recommendations, please share. Many thanks!

  7. Heya! I realize this is kind of off-topic but I needed to
    ask. Does operating a well-established website
    such as yours take a lot of work? I am brand new to operating
    a blog however I do write in my diary every day.

    I’d like to start a blog so I can easily share my experience and views online.

    Please let me know if you have any kind of recommendations or tips for brand new aspiring bloggers.
    Appreciate it!

  8. Hey there! Someone in my Facebook group shared this site with us so I came to look it over.
    I’m definitely enjoying the information. I’m book-marking and will be tweeting
    this to my followers! Wonderful blog and excellent design and style.

  9. Wonderful blog you have here but I was wondering if you knew of any
    forums that cover the same topics talked about here? I’d really like to be a part of community where I can get feed-back from other experienced individuals
    that share the same interest. If you have any recommendations, please let me know.
    Thanks!

  10. I’m curious to find out what blog system you’re working with?
    I’m having some small security issues with my latest blog and I would like
    to find something more secure. Do you have any suggestions?

  11. Today, while I was at work, my cousin stole my apple ipad
    and tested to see if it can survive a 25 foot drop, just so she can be a youtube
    sensation. My iPad is now broken and she has 83 views.

    I know this is totally off topic but I had to share it with
    someone!

  12. Howdy just wanted to give you a quick heads up.
    The text in your post seem to be running off the screen in Ie.
    I’m not sure if this is a formatting issue or something to do with web browser compatibility but I figured I’d
    post to let you know. The layout look great though! Hope you get the problem fixed
    soon. Thanks

  13. Hello, Neat post. There’s an issue along with your web site in web explorer, would check
    this? IE nonetheless is the marketplace leader and a good component of folks will miss your wonderful writing because of this problem.

  14. Hello, i believe that i saw you visited my blog so i came to return the desire?.I am trying to in finding
    things to improve my website!I assume its good enough
    to use a few of your ideas!!

  15. Hey there would you mind sharing which blog platform you’re using?
    I’m planning to start my own blog in the near future but I’m having a
    tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs
    and I’m looking for something completely unique.
    P.S Apologies for getting off-topic but I had to ask!

  16. We’re a group of volunteers and starting a new scheme in our community.
    Your web site offered us with valuable information to work on. You’ve done an impressive job
    and our whole community will be thankful to you.

  17. You could certainly see your expertise in the article you
    write. The sector hopes for even more passionate writers such as you who are not afraid to
    say how they believe. At all times follow your heart.

  18. I do not even know how I ended up right here, however I thought this submit was once good.
    I do not realize who you are however certainly you are going to a well-known blogger for those who aren’t
    already. Cheers!

  19. Write more, thats all I have to say. Literally, it seems as though
    you relied on the video to make your point.
    You definitely know what youre talking about, why throw away your
    intelligence on just posting videos to your site when you could be giving us something
    informative to read?

  20. Good day! I know this is somewhat off topic but I was wondering which blog platform are you
    using for this site? I’m getting sick and tired of WordPress because I’ve had
    problems with hackers and I’m looking at alternatives for another
    platform. I would be great if you could point me in the direction of a good platform.

  21. I know this if off topic but I’m looking into starting
    my own blog 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 internet smart so I’m not 100% sure.
    Any suggestions or advice would be greatly appreciated.
    Thanks

  22. You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand.
    It seems too complicated and extremely broad for me.
    I am looking forward for your next post, I will try to get the
    hang of it!

  23. Does your website have a contact page? I’m having trouble locating
    it but, I’d like to shoot you an e-mail. I’ve got some ideas for your blog you
    might be interested in hearing. Either way, great site and
    I look forward to seeing it expand over time.

  24. It’s a shame you don’t have a donate button! I’d certainly donate to this superb blog!
    I suppose for now i’ll settle for bookmarking and adding your RSS feed to my
    Google account. I look forward to fresh updates and will talk about this blog with my Facebook group.
    Chat soon!

  25. Great blog right here! Also your website a lot up fast! What host are you using?
    Can I am getting your associate hyperlink in your host? I desire
    my web site loaded up as fast as yours lol

  26. Good day! I know this is somewhat off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form?

    I’m using the same blog platform as yours and I’m having trouble finding
    one? Thanks a lot!

  27. Hi there everyone, it’s my first pay a visit at this web site, and piece of writing is truly fruitful designed for me, keep up
    posting these articles or reviews.

  28. It’s a pity you don’t have a donate button! I’d without a doubt
    donate to this excellent blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to
    my Google account. I look forward to brand new updates and will talk about this
    site with my Facebook group. Talk soon!

  29. My developer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the
    costs. But he’s tryiong none the less. I’ve been using Movable-type on various websites for about a
    year and am concerned about switching to another platform.

    I have heard great things about blogengine.net. Is there a way I can import all
    my wordpress content into it? Any help would be really
    appreciated!

  30. Hey there! I know this is kind of off topic but I was
    wondering which blog platform are you using for this
    site? I’m getting sick and tired of WordPress because I’ve had issues with hackers and I’m looking at options for
    another platform. I would be fantastic if you could point me in the direction of
    a good platform.

  31. You really make it seem so easy with your presentation but I find this matter to be actually something which I
    think I would never understand. It seems too complicated and extremely broad for me.
    I am looking forward for your next post, I’ll try to get the hang of it!

  32. That is really fascinating, You are a very professional blogger.

    I have joined your feed and stay up for searching for more of your magnificent post.
    Additionally, I have shared your website in my social networks

  33. Hey very cool website!! Man .. Beautiful .. Amazing .. I’ll bookmark your website and take the feeds also…I am happy to find numerous useful info here in the post, we need work out more techniques in this regard, thanks for sharing. . . . . .

  34. Do you mind if I quote a few of your posts as long as I provide credit and sources back to
    your site? My blog site is in the very same niche as yours and my visitors would truly benefit from a
    lot of the information you provide here. Please let me know if
    this okay with you. Many thanks!

  35. Its like you read my thoughts! You seem to know a lot about this, like you wrote the e book in it or something.

    I feel that you simply can do with some % to
    drive the message house a bit, however other than that, this is
    magnificent blog. An excellent read. I’ll certainly be back.

  36. Hey just wanted to give you a quick heads up. The words in your content
    seem to be running off the screen in Safari. I’m not sure
    if this is a formatting issue or something to do with internet browser compatibility but I figured I’d post to
    let you know. The design look great though!
    Hope you get the issue fixed soon. Many thanks

  37. wonderful points altogether, you simply gained a brand new reader. What would you recommend in regards to your post that you made some days ago? Any positive?

Leave a Reply

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