[BZOJ4408][Fjoi 2016]神秘数
[BZOJ4408][Fjoi 2016]神秘数
试题描述
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]所构成的可重复数字集合的神秘数。
输入
第二行n个整数,从1编号。
第三行一个整数m,表示询问个数。
以下m行,每行一对整数l,r,表示一个询问。
输出
输入示例
输出示例
数据规模及约定
对于100%的数据点,n,m <= 100000,∑a[i] <= 10^9
题解
一开始把神秘数 ans 设为 1,然后大小在 [0, ans] 范围中所有数显然都是可以用的,并且它们所能组成的最大数就是所有数之和 sum,于是 ans = sum + 1,重复以上动作。因为每次 ans 至少倍增,所以操作次数不超过 9·log210。
操作可以用线段树套 treap 实现,像这样:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 100010
#define maxnode 4093490
#define LL long long
struct Node {
int v, r;
LL sum;
Node() {}
Node(int _, int __): v(_), r(__) {}
} ns[maxnode];
int ToT, fa[maxnode], ch[2][maxnode];
inline void maintain(int o) {
ns[o].sum = ns[o].v;
for(int i = 0; i < 2; i++) if(ch[i][o])
ns[o].sum += ns[ch[i][o]].sum;
return ;
}
inline void rotate(int u) {
int y = fa[u], z = fa[y], l = 0, r = 1;
if(z) ch[ch[1][z]==y][z] = u;
if(ch[1][y] == u) swap(l, r);
fa[u] = z; fa[y] = u; fa[ch[r][u]] = y;
ch[l][y] = ch[r][u]; ch[r][u] = y;
maintain(y); maintain(u);
return ;
}
inline void insert(int& o, int v) {
if(!o) {
ns[o = ++ToT] = Node(v, rand());
return maintain(o);
}
bool d = v > ns[o].v;
insert(ch[d][o], v); fa[ch[d][o]] = o;
if(ns[ch[d][o]].r > ns[o].r) {
int t = ch[d][o];
rotate(t); o = t;
}
return maintain(o);
}
inline LL que(int& o, LL v) {
if(!o) return 0;
LL ls = ch[0][o] ? ns[ch[0][o]].sum : 0;
if(v >= ns[o].v) return ls + ns[o].v + que(ch[1][o], v);
return que(ch[0][o], v);
} int rt[maxn<<2], ql, qr;
LL V;
inline void update(int L, int R, int o) {
insert(rt[o], V);
if(L == R) return ;
int M = L + R >> 1, lc = o << 1, rc = lc | 1;
if(ql <= M) update(L, M, lc);
else update(M+1, R, rc);
return ;
}
inline LL query(int L, int R, int o) {
if(ql <= L && R <= qr) return que(rt[o], V);
int M = L + R >> 1, lc = o << 1, rc = lc | 1;
LL ans = 0;
if(ql <= M) ans += query(L, M, lc);
if(qr > M) ans += query(M+1, R, rc);
return ans;
} int main() {
int n = read();
for(int i = 1; i <= n; i++) ql = i, V = read(), update(1, n, 1); int q = read();
while(q--) {
ql = read(); qr = read();
LL ans = 0; V = ans + 1;
while(1) {
LL tmp = query(1, n, 1);
if(tmp == ans) break;
ans = tmp; V = ans + 1;
}
printf("%lld\n", ans + 1);
} return 0;
}
然后 T 飞。T_T
当然正解是主席树,代码还倍儿短。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <cstdlib>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *tail;
inline char Getchar() {
if(Head == tail) {
int l = fread(buffer, 1, BufferSize, stdin);
tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 100010
#define maxnode 4000010
int n, m, A[maxn], sum; int ToT, root[maxn], lc[maxnode], rc[maxnode], sumv[maxnode];
void update(int& y, int x, int L, int R, int p) {
sumv[y = ++ToT] = sumv[x] + p;
if(L == R) return ;
int M = L + R >> 1; lc[y] = lc[x]; rc[y] = rc[x];
if(p <= M) update(lc[y], lc[x], L, M, p);
else update(rc[y], rc[x], M+1, R, p);
return ;
}
int query(int y, int x, int L, int R, int p) {
if(L == R) return sumv[y] - sumv[x];
int M = L + R >> 1;
if(p <= M) return query(lc[y], lc[x], L, M, p);
return sumv[lc[y]] - sumv[lc[x]] + query(rc[y], rc[x], M+1, R, p);
} int main() {
n = read();
for(int i = 1; i <= n; i++) A[i] = read(), sum += A[i];
for(int i = 1; i <= n; i++) update(root[i], root[i-1], 1, sum, A[i]);
m = read();
while(m--) {
int ql = read(), qr = read(), ans = 1;
int v = query(root[qr], root[ql-1], 1, sum, ans);
while(v >= ans) ans = v + 1, v = query(root[qr], root[ql-1], 1, sum, ans);
printf("%d\n", ans);
} return 0;
}
[BZOJ4408][Fjoi 2016]神秘数的更多相关文章
- BZOJ4408: [Fjoi 2016]神秘数【主席树好题】
Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13}, 1 = 1 2 = 1+1 3 = 1+1+1 4 = 4 5 = ...
- bzoj4408 [Fjoi 2016]神秘数 & bzoj4299 Codechef FRBSUM 主席树+二分+贪心
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4299 https://lydsy.com/JudgeOnline/problem.php?id ...
- BZOJ4408 [Fjoi 2016]神秘数 【主席树】
题目链接 BZOJ4408 题解 假如我们已经求出一个集合所能凑出连续数的最大区间\([1,max]\),那么此时答案为\(max + 1\) 那么我们此时加入一个数\(x\),假若\(x > ...
- 【BZOJ4408】[Fjoi 2016]神秘数 主席树神题
[BZOJ4408][Fjoi 2016]神秘数 Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1 ...
- Bzoj 4408: [Fjoi 2016]神秘数 可持久化线段树,神题
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 128[Submit][Status ...
- BZOJ 4408: [Fjoi 2016]神秘数
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 464 Solved: 281[Submit][Status ...
- BZOJ 4408: [Fjoi 2016]神秘数 可持久化线段树
4408: [Fjoi 2016]神秘数 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 Description 一个可重复数字集 ...
- 4408: [Fjoi 2016]神秘数
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 452 Solved: 273 [Submit][Stat ...
- 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 = ...
随机推荐
- [转]Windows平台下Makefile学习笔记
Windows平台下Makefile学习笔记(一) 作者:朱金灿 来源:http://blog.csdn.net/clever101 决心学习Makefile,一方面是为了解决编译开源代码时需要跨编译 ...
- PyCharm3.0默认快捷键(翻译的)
PyCharm3.0默认快捷键(翻译的) PyCharm Default Keymap 1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt ...
- 使用gulp工具生成svgsprites
简介 SVG即可缩放矢量图形 (Scalable Vector Graphics)的简称, 是一种用来描述二维矢量图形的XML标记语言. SVG图形不依赖于分辨率, 因此图形不会因为放大而显示出明显的 ...
- iOS 关于字体根据不同屏幕尺寸等比适配的问题(zz)
http://www.jianshu.com/p/5815e81abb52 背景 去年的六月份开始了一个新的项目,此项目支持的设备从4S开始一直到6+,也就是说屏幕的尺寸最小的320x480 最大的1 ...
- Java 8新特性终极指南
目录结构 介绍 Java语言的新特性 2.1 Lambdas表达式与Functional接口 2.2 接口的默认与静态方法 2.3 方法引用 2.4 重复注解 2.5 更好的类型推测机制 2.6 扩展 ...
- 2.1 python使用MongoDB 示例代码
import pymongo client = pymongo.MongoClient('localhost', 27017) # MongoDB 客户端 walden = client['walde ...
- python基础-函数式编程
python基础-函数式编程 高阶函数:map , reduce ,filter,sorted 匿名函数: lambda 1.1函数式编程 面向过程编程:我们通过把大段代码拆成函数,通过一层一层 ...
- pyhton 学习
官方学习文档 https://docs.python.org/3/tutorial/
- java/python中的队列
Queue<TreeNode> que=new LinkedList<>(); 用linkedlist实现队列,offer,poll进出队列,peek对列顶部元素 python ...
- MFC的多线程操作
记得用MFC做了一个图像自动修复软件,当时没有多线程操作这一概念,由于图像修复算法比较复杂,因此,当执行图像修复时,程序就像卡死了似得而不能做其他操作.其实MFC对这种情况有一种很好地解决方案,那就是 ...