一個可重複數字集合S的神祕數定義爲最小的不能被S的子集的和表示的正整數。例如S={1,1,1,4,13},
1 = 1
2 = 1+1
3 = 1+1+1
4 = 4
5 = 4+1
6 = 4+1+1
7 = 4+1+1+1
8沒法表示爲集合S的子集的和,故集合S的神祕數爲8。
現給定n個正整數a[1]..a[n],m個詢問,每次詢問給定一個區間[l,r](l<=r),求由a[l],a[l+1],…,a[r]所構成的可重複數字集合的神祕數。php
一個可重複數字集合S的神祕數定義爲最小的不能被S的子集的和表示的正整數。例如S={1,1,1,4,13},
1 = 1
2 = 1+1
3 = 1+1+1
4 = 4
5 = 4+1
6 = 4+1+1
7 = 4+1+1+1
8沒法表示爲集合S的子集的和,故集合S的神祕數爲8。
現給定n個正整數a[1]..a[n],m個詢問,每次詢問給定一個區間[l,r](l<=r),求由a[l],a[l+1],…,a[r]所構成的可重複數字集合的神祕數。php
第一行一個整數n,表示數字個數。
第二行n個整數,從1編號。
第三行一個整數m,表示詢問個數。
如下m行,每行一對整數l,r,表示一個詢問。ios
對於每一個詢問,輸出一行對應的答案。spa
對於100%的數據點,n,m <= 100000,∑a[i] <= 10^9blog
鳴謝yyh上傳ip
這道題挺好的思路。get
首先考慮在集合中已經選出$k$個數的時候,再加入第$k+1$個數的狀況。string
顯然有當$a_{k+1}>\sum ^{k}_{i=1} a_{k} +1$時,$ans=\sum ^{k}_{i=1} a_{k}+1$it
不然顯然這個這些數能組合出的範圍擴大$a_{k+1}$io
因此思路就是對於一個$ans$,求出$\sum ^{R}_{i=L} (a_{i}<ans) a_{i}$,若是這些數能組合到$ans$,那麼這個$ans$只能擴大,因此把$ans$擴大到$\sum ^{R}_{i=L} (a_{i}<ans) a_{i} +1$繼續作,不然獲得神祕數。ast
因此支持這樣作的仍是利用可持久化線段樹求出。
可是這樣的複雜度仍是比較暴力的,不過題目中說了$\sum a_{i}<10^{9}$因此複雜度最壞是 $O(MlogNlog10^{9})$
話說這題被xyx秒了....
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; inline int read() { int x=0,f=1; char ch=getchar(); while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} return x*f; } #define MAXN 100010 int N,M,a[MAXN]; namespace PrTree{ int root[MAXN],sum[MAXN*20],lson[MAXN*20],rson[MAXN*20],sz; inline void Insert(int l,int r,int &x,int last,int pos,int val) { x=++sz; lson[x]=lson[last],rson[x]=rson[last]; sum[x]=sum[last]+val; if (l==r) return; int mid=(l+r)>>1; if (pos<=mid) Insert(l,mid,lson[x],lson[last],pos,val); else Insert(mid+1,r,rson[x],rson[last],pos,val); } inline int Query(int l,int r,int L,int R,int x,int y) { if (L>R) return 0; if (L<=l && R>=r) return sum[y]-sum[x]; int mid=(l+r)>>1,re=0; if (L<=mid) re+=Query(l,mid,L,R,lson[x],lson[y]); if (R>mid) re+=Query(mid+1,r,L,R,rson[x],rson[y]); return re; } }using namespace PrTree; int ls[MAXN]; int main() { N=read(); for (int i=1; i<=N; i++) ls[i]=a[i]=read(); sort(ls+1,ls+N+1); int tot=unique(ls+1,ls+N+1)-ls-1; for (int i=1; i<=N; i++) a[i]=lower_bound(ls+1,ls+tot+1,a[i])-ls; for (int i=1; i<=N; i++) PrTree::Insert(1,tot,root[i],root[i-1],a[i],ls[a[i]]); M=read(); while (M--) { int l=read(),r=read(); int ans=1,up,pos; while (1) { pos=upper_bound(ls+1,ls+tot+1,ans)-ls-1; if (ans<=(up=PrTree::Query(1,tot,1,pos,root[l-1],root[r]))) ans=up+1; else break; } printf("%d\n",ans); } return 0; }