Codechef ForbiddenSum
Mike likes to invent new functions. The latest one he has invented is called ForbiddenSum. Let's consider how it can be calculated:
You are given a set S of positive integers. The integers are not necessary distinct. ForbiddenSum of S equals to the minimal non-negative integer, that can't be returned by the algorithm described below:
- Choose a random subset S' of S(it can be empty as well);
- Calculate the sum of all the numbers of S' and assign it to the variable T;
- Return the value of the variable T.
I.e. if S = {1, 1, 3, 7}, the algorithm can return 0(S' = {}), 1(S' = {1}), 2(S' = {1, 1}), 3(S' = {3}), 4(S' = {1, 3}), 5(S' = {1, 1, 3}), but it can't return 6. So, ForbiddenSum of S equals to 6.
Inventing of new function is not the only Mike's hobby. Also, he likes to collect rare and unusual arrays. He is going to share with you one of them.
Formally, Mike gives you one array A, consisting of N positive integers. After that, he asks you M questions, two integers Li and Ri describe i'th question: What does ForbiddenSum of S={ALi, ALi+1, ..., ARi-1, ARi} equal to?
Input
The first line contains the only integer N. The second line contains N integers - the array A. A is 1-indexed.
The third line contains the only integer M. The following M lines contain two integer numbers 1 ≤ Li ≤ Ri ≤ N each.
Output
Output should consists of M lines. The i'th line should contains the answer to the i'th question.
Constraints
1 ≤ N, M ≤ 100,000
1 ≤ Ai ≤ 109
1 ≤ A1 + A2 + ... + AN ≤ 109
Example
Input:
5
1 2 4 9 10
5
1 1
1 2
1 3
1 4
1 5 Output:
2
4
8
8
8
Explanation
In the example test there are M=5 questions. We won't describe all of them, only two ones.
The first question
In the first test case S equals to {1}. The answer is 2, because we can recieve 1 in case the algorithm chooses S' = {1}. But there are no chances to receive 2.
The second question
In the first test case S equals to {1, 2}. The answer is 4, because we can recieve 1 in case the algorithm chooses S' = {1}, 2 in case the algorithm chooses S' = {2} and 3 in case the algorithm chooses S' = {1, 2}. But there are no chances to receive 4.
中文题面在这: Mandarin Chinese
有一个性质是我随便口胡的虽然不知道怎么证明但是却用它A了这个题。
那就是如果集合按数字大小排序之后,i是最小的满足a[1]+a[2]+...a[i]<a[i+1]-1,那么这个集合的forbidden sum为a[i+1]-1.
这就相当于表示不了a[i+1]-1这个数了。
而如果>=的话那么是可以接上的。
于是就可以暴力做啦,对于每次询问,设最大能表示的数为num。
一开始num都为0,然后每次查找区间内权值∈[1,num+1]的数的和,然后把num设为这个值。
当然如果这个值==num的话,说明无法扩展了,输出num+1然后去管下一个询问就好了。
这个的话用主席树比较好(当然你也可以用其他的)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define maxn 100005
using namespace std;
const int inf=<<;
struct node{
int lc,rc;
int s;
}nil[maxn*];
int n,m,ky,a[maxn],cnt=;
int rot[maxn];
int le,ri,S,T,ans,pre; int update(int u,int l,int r){
int ret=++cnt;
nil[ret]=nil[u];
nil[ret].s+=le;
if(l==r) return ret; int mid=l+r>>;
if(le<=mid) nil[ret].lc=update(nil[ret].lc,l,mid);
else nil[ret].rc=update(nil[ret].rc,mid+,r); return ret;
} int query(int x,int y,int l,int r){
if(l>=le&&r<=ri) return nil[y].s-nil[x].s;
int mid=l+r>>,an=;
if(le<=mid) an+=query(nil[x].lc,nil[y].lc,l,mid);
if(ri>mid) an+=query(nil[x].rc,nil[y].rc,mid+,r);
return an;
} inline void prework(){
rot[]=nil->lc=nil->rc=;
nil->s=; for(int i=;i<=n;i++){
le=a[i];
rot[i]=update(rot[i-],,1e9);
}
} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",a+i); prework(); scanf("%d",&m);
while(m--){
scanf("%d%d",&S,&T);
pre=-,ans=;
while(ans>pre){
pre=ans;
le=,ri=ans+;
ans=query(rot[S-],rot[T],,1e9);
} printf("%d\n",ans+);
} return ;
}
Codechef ForbiddenSum的更多相关文章
- 【CodeChef】ForbiddenSum
Portal --> CC ForbiddenSum Solution 场上想到了\(O(NM)\)的做法..然而并没有什么用 首先考虑比较简单的一个问题,给定一个数组\(A\),问这些数不能凑 ...
- [BZOJ4408&&BZOJ4299][FJOI2016 && Codechef]神秘数&&FRBSUM(主席树)
4299: Codechef FRBSUM Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 550 Solved: 351[Submit][Statu ...
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
- 【BZOJ4260】 Codechef REBXOR 可持久化Trie
看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...
- codechef 两题
前面做了这场比赛,感觉题目不错,放上来. A题目:对于数组A[],求A[U]&A[V]的最大值,因为数据弱,很多人直接排序再俩俩比较就过了. 其实这道题类似百度之星资格赛第三题XOR SUM, ...
- codechef January Challenge 2014 Sereja and Graph
题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...
- BZOJ3509: [CodeChef] COUNTARI
3509: [CodeChef] COUNTARI Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 339 Solved: 85[Submit][St ...
- CodeChef CBAL
题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...
- CodeChef FNCS
题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...
随机推荐
- 【COGS 14】 [网络流24题] 搭配飞行员 网络流板子题
用网络流水二分图的模型(存一下板子) #include <cstdio> #include <cstring> #include <algorithm> #defi ...
- BZOJ 1098: [POI2007]办公楼biu 链表
求补图连通块,用链表优化,势能O(n+m) #include<cstdio> #include<cstring> #include<iostream> #inclu ...
- Eclipse来push,fetch,rebase代码
如何与项目里的其他人一起合作项目,提交代码并更新呢?这里提出我比最近用到的两种工具:一种是Eclipse,另外一个是SourceTree.个人推荐从事Java开发的话,可以用Eclipse.当然,还有 ...
- 'express' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
新安装了express,但是当查看版本号输入: express -v 时出现如下错误: 网上查找了相关资料才发现express查看版本 的命令是 express -V (即V大写) 再次尝试: 发现同 ...
- Xamarin+vs2010部署错误:error MSB6004: 指定的任务可执行文件位置\sdk\\tools\zipalign.exe”无效
好不容易配好了Xamarin和vs2010,也搞好了GenyMotion的虚拟机配置,开始调试的时候又报出了这样的错误: error MSB6004: 指定的任务可执行文件位置"C:\Use ...
- HDFS的xshell及dfsadmin命令
一. DFS:distributied file system 是一种允许文件通过网络在多台主机上风向的文件系统,可让多机器上的多用户分享文件和存储空间 二.HDFS的shell **切记后面加的 / ...
- bzoj1861 书架 splay版
单点插入删除以及求前缀 #include<cstdio> #include<cstring> #include<algorithm> using namespace ...
- 【BZOJ】1571: [Usaco2009 Open]滑雪课Ski
[算法]动态规划 [题解]yy出了O(1w log 1w)的算法. 将雪坡排序预处理出g[i]表示能力值为i的最短时长雪坡. 这样就可以定义work(t,c)表示时长t能力c的最多滑雪数量,work( ...
- linux环境下的GUN make学习笔记(一)
第一章:概述 1.1:make概述 在linux环境下使用make工具能够比较容易的构建一个属于自己的工程,整个工程的编译只需要一个命令就可以完成编译.连接以至于最后的执行.不过我们需要投入一些时间去 ...
- 我的一次安装oracle的过程
1.在装oracle之前,先安装.net3.5 2.然后正常安装oracle,一直next 3.装完oracle后,安装plsql dev工具,打开工具,发现没有connect as,是需要进行一些配 ...