题目链接

BZOJ4408

题解

假如我们已经求出一个集合所能凑出连续数的最大区间\([1,max]\),那么此时答案为\(max + 1\)

那么我们此时加入一个数\(x\),假若\(x > max + 1\),显然对答案没有影响

但是假若\(x \le max + 1\),显然最大区间变为\([1,max + x]\),答案变为\(max + x + 1\)

那么我们就能得出这题的解法了

将区间内的数排序,一开始\(ans = 0\),然后逐一将数加入集合之中, 一但出现\(x > max + 1\)的情况,由于是有序的,后面的数也无法更新答案,此时答案就是最优的

但是暴力排序枚举显然不行,我们可以用主席树优化

每求出一个新的区间\([1,max]\)后,\([1,max + 1]\)内的数都可以参与贡献,那么此时新的区间为\([1,\sum a_i]\),其中\(a_i \le max + 1\)

当\(max\)不变时算法结束

显然\(max\)是成倍增长的,所以复杂度为\(O(nlog^2(\sum a_i))\)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 100005,maxm = 7000005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int ls[maxm],rs[maxm],sum[maxm],rt[maxn],cnt;
int n,m,a[maxn],M;
void modify(int& u,int pre,int l,int r,int pos){
u = ++cnt;
sum[u] = sum[pre] + pos; ls[u] = ls[pre]; rs[u] = rs[pre];
if (l == r) return;
int mid = l + r >> 1;
if (mid >= pos) modify(ls[u],ls[pre],l,mid,pos);
else modify(rs[u],rs[pre],mid + 1,r,pos);
}
int query(int u,int v,int l,int r,int L,int R){
if (l >= L && r <= R) return sum[u] - sum[v];
int mid = l + r >> 1;
if (mid >= R) return query(ls[u],ls[v],l,mid,L,R);
if (mid < L) return query(rs[u],rs[v],mid + 1,r,L,R);
return query(ls[u],ls[v],l,mid,L,R) + query(rs[u],rs[v],mid + 1,r,L,R);
}
int main(){
n = read();
REP(i,n) a[i] = read(),M = max(M,a[i]);
m = read();
for (int i = 1; i <= n; i++)
modify(rt[i],rt[i - 1],1,M,a[i]);
int l,r,ans,s;
while (m--){
l = read(); r = read(); ans = 0;
while (true){
s = query(rt[r],rt[l - 1],1,M,1,ans + 1);
if (s <= ans) break;
ans = s;
}
printf("%d\n",ans + 1);
}
return 0;
}

BZOJ4408 [Fjoi 2016]神秘数 【主席树】的更多相关文章

  1. BZOJ4408&4299[Fjoi 2016]神秘数——主席树

    题目描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 1 2 = 1+1 3 = 1+1+1 4 = 4 5 = 4+1 6 = ...

  2. 【bzoj4408】[Fjoi 2016]神秘数 主席树

    题目描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1+13 = 1+1+14 = 45 = 4+16 = 4+1+1 ...

  3. BZOJ 4408: [Fjoi 2016]神秘数 [主席树]

    传送门 题意: 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},8无法表示为集合S的子集的和,故集合S的神秘数为8.现给定n个正整数a[1]. ...

  4. BZOJ 4408: [Fjoi 2016]神秘数 主席树 + 神题

    Code: #include<bits/stdc++.h> #define lson ls[x] #define mid ((l+r)>>1) #define rson rs[ ...

  5. [BZOJ4408][Fjoi 2016]神秘数

    [BZOJ4408][Fjoi 2016]神秘数 试题描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1+13 = 1 ...

  6. 【BZOJ4408】[Fjoi 2016]神秘数 主席树神题

    [BZOJ4408][Fjoi 2016]神秘数 Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1 ...

  7. BZOJ4408: [Fjoi 2016]神秘数【主席树好题】

    Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13}, 1 = 1 2 = 1+1 3 = 1+1+1 4 = 4 5 = ...

  8. bzoj4408 [Fjoi 2016]神秘数 & bzoj4299 Codechef FRBSUM 主席树+二分+贪心

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4299 https://lydsy.com/JudgeOnline/problem.php?id ...

  9. Bzoj 4408: [Fjoi 2016]神秘数 可持久化线段树,神题

    4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 177  Solved: 128[Submit][Status ...

随机推荐

  1. Django——多网页网站及网页互联

    在helloapp文件夹下添加名为templates的文件夹(此文件夹名称是固定的),并在其下添加html文件,文件内容根据自己网页想呈现的内容而定 在views文件内添加新的函数 在urls文件内添 ...

  2. 11-Dockerfile构建镜像

    用 Dockerfile 创建上节的 ubuntu-with-vi,其内容则为: FROM ubuntu RUN apt-get update && apt-get install v ...

  3. Animator & Timeline

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Pla ...

  4. Centos7.2部署saltstack

    原文发表于cu:2016-06-23 参考文档: Saltstack安装文档:https://repo.saltstack.com/#rhel saltstack的安装与简单配置,应用. 一.环境 S ...

  5. 【python 2.7】python读取json数据存入MySQL

    同上一篇,只是适配 CentOS+ python 2.7 #python 2.7 # -*- coding:utf-8 -*- __author__ = 'BH8ANK' import json im ...

  6. [转载]文件系统缓存dirty_ratio与dirty_background_ra

    原文地址:文件系统缓存dirty_ratio与dirty_background_ratio两个参数区别作者:vincent 这两天在调优数据库性能的过程中需要降低操作系统文件Cache对数据库性能的影 ...

  7. JPA error org.hibernate.AnnotationException: No identifier specified for entity

    错误:org.hibernate.AnnotationException: No identifier specified for entity 原因:JPA所使用的Entity需要标注@Id,在引用 ...

  8. Python:字符串操作总结

    所有标准的序列操作(索引.分片.乘法.判断成员资格.求长度.取最小值最大值)对字符串同样适用,且字符串是不可变的. 一.字符串格式化 转换说明符 [注]: 这些项的顺序至关重要 (1)%字符:标记转换 ...

  9. CodeForces 479C Exams 贪心

    题目: C. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  10. HDU 5464 Clarke and problem 动态规划

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5464 Clarke and problem  Accepts: 130  Submissions: ...