# 【BZOJ-4408】神秘数 可持久化线段树

## 4408: [Fjoi 2016]神秘数

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 475  Solved: 287
[Submit][Status][Discuss]

## Description

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。

5
1 2 4 9 10
5
1 1
1 2
1 3
1 4
1 5

2
4
8
8
8

## Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
{
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()
{
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]]);

while (M--) {
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;
}


——It's a lonely path. Don't make it any lonelier than it has to be.
posted @ 2017-02-05 20:34  DaD3zZ  阅读(493)  评论(0编辑  收藏  举报