Bzoj 4408: [Fjoi 2016] Mystery number can persist line tree, divine question

Source: Internet
Author: User

4408: [Fjoi 2016] Mystery number time limit:10 Sec Memory limit:128 MB
submit:177 solved:128
[Submit] [Status] [Discuss] Description

The mysterious number of a repeatable number set S is defined as the smallest positive integer that cannot be represented by a subset of S. For example, s={1,1,1,4,13},

1 = 1

2 =

3 = 1+1+1

4 = 4

5 = 4+1

6 = 4+1+1

7 = 4+1+1+1

8 cannot be represented as a subset of the set S and so the mystery number of the set S is 8.

Now given n positive integers a[1]. A[n],m a query, each time asking for a given interval [l,r] (l<=r), the mysterious number of a repeatable number set consisting of a[l],a[l+1],..., A[r].

Input

The first line is an integer n, which indicates the number of digits.
The second row of n integers, numbered from 1.
The third line, an integer m, indicates the number of queries.
The following m lines, each with a pair of integer l,r, represent a query.

Output

For each query, the output line corresponds to the answer.

Sample Input5
1 2 4) 9 10
5
1 1
1 2
1 3
1 4
1 5Sample Output2
4
8
8
8HINT

For 100% data points, n,m <= 100000,∑a[i] <= 10^9

Source

Acknowledgement Yyh Upload

Exercises

FJ God problem!!!

This problem is really psychedelic ...

I can't believe it!!! qaq!!! I've been thinking about mathematical methods ...

See the puzzle, unexpectedly is a durable line tree!!!!! (Orz the person)

All right, no more nonsense ...

Sort the sequence from small to large first.

Assuming that the current mystery number is ans, [1,ans-1] must be represented by the number of s in the set. Then, if a number A is currently added, it can be divided into two types of discussion.

1, if A<=ans, the interval can now be extended to: [1,ans+a-1], and then the mysterious number into ans+a.

2, if A>ans, the interval is empty, ans does not change.

Then ans starts from 1, each time seeking a number less than ans and get,ans into get+1.

This can be maintained with a durable line tree.

1#include <bits/stdc++.h>2 using namespacestd;3 #defineMAXN 1000104 intsum[ +*maxn],a[maxn],root[maxn],size;5 structnode6 {7     intLeft,right;8}tree[ +*MAXN];9 intRead ()Ten { One     ints=0, fh=1;CharCh=GetChar (); A      while(ch<'0'|| Ch>'9'){if(ch=='-') fh=-1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') {s=s*Ten+ (ch-'0'); ch=GetChar ();} -     returns*fh; the } - voidADD (intXint&y,intLintRintadd) - { -y=++SIZE; +sum[y]=sum[x]+add; -     if(L==R)return; +tree[y].left=tree[x].left;tree[y].right=Tree[x].right; A     intMid= (L+R)/2; at     if(add<=mid) ADD (tree[x].left,tree[y].left,l,mid,add); -     ElseADD (tree[x].right,tree[y].right,mid+1, R,add); - } - intQueryintXintYintLintRintk) - { -     if(L==R)returnsum[y]-Sum[x]; in     intMid= (L+R)/2; -     if(K<=mid)returnquery (tree[x].left,tree[y].left,l,mid,k); to     Else returnQuery (tree[x].right,tree[y].right,mid+1, r,k) +sum[tree[y].left]-Sum[tree[x].left]; + } - intMain () the { *     intN,tot,i,m,l,r,ans,Get; $n=read ();Panax Notoginsengtot=0; -      for(i=1; i<=n;i++) A[i]=read (), tot+=A[i]; theSize=0; +      for(i=1; i<=n;i++) Add (root[i-1],root[i],1, Tot,a[i]); Am=read (); the      for(i=1; i<=m;i++) +     { -L=read (); r=read (); $ans=1; $          while(1) -         { -             Get=query (root[l-1],root[r],1, Tot,ans); the             if(Get<ans) Break; -ans=Get+1;Wuyi         } theprintf"%d\n", ans); -     } Wu     return 0; -}

Bzoj 4408: [Fjoi 2016] Mystery number can persist line tree, divine question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.