先考虑一下一个集合怎么用 \(O(n)\) 时间求出来,然后用主席树推广到一个序列就可以了。大致思想就是考虑一个数的权值和它前面的数的和的关系。

#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
int n, a[100005], cnt, b[100005], m, uu, vv, rot[100005], tot;
struct Node{
int l, r, sum;
}nd[5000005];
int build(int l, int r){
int re=++tot;
if(l==r) ;
else{
int mid=(l+r)>>1;
if(l<=mid) nd[re].l = build(l, mid);
if(mid<r) nd[re].r = build(mid+1, r);
}
return re;
}
int update(int pre, int l, int r, int x){
int re=++tot;
nd[re] = nd[pre];
nd[re].sum += b[x];
if(l==r) ;
else{
int mid=(l+r)>>1;
if(x<=mid) nd[re].l = update(nd[pre].l, l, mid, x);
if(mid<x) nd[re].r = update(nd[pre].r, mid+1, r, x);
}
return re;
}
int query(int pre, int now, int l, int r, int x, int y){
if(b[l]>=x && b[r]<=y)
return nd[now].sum-nd[pre].sum;
else if(l==r)
return 0;
else{
int mid=(l+r)>>1;
int re=0;
if(x<=b[mid]) re += query(nd[pre].l, nd[now].l, l, mid, x, y);
if(b[mid]<y) re += query(nd[pre].r, nd[now].r, mid+1, r, x, y);
return re;
}
}
int getAns(int uu, int vv){
int sum=0, lst=0;
while(true){
int tmp=query(rot[uu-1], rot[vv], 1, cnt, lst+1, sum+1);
if(!tmp)
return sum+1;
lst = sum + 1;
sum += tmp;
}
}
int main(){
cin>>n;
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b+1, b+1+n);
cnt = unique(b+1, b+1+n) - b - 1;
for(int i=1; i<=n; i++)
a[i] = lower_bound(b+1, b+1+cnt, a[i]) - b;
rot[0] = build(1, cnt);
for(int i=1; i<=n; i++)
rot[i] = update(rot[i-1], 1, cnt, a[i]);
cin>>m;
while(m--){
scanf("%d %d", &uu, &vv);
printf("%d\n", getAns(uu, vv));
}
return 0;
}

loj2174 「FJOI2016」神秘数的更多相关文章

  1. 「FJOI2016」神秘数 解题报告

    「FJOI2016」神秘数 这题不sb,我挺sb的... 我连不带区间的都不会哇 考虑给你一个整数集,如何求这个神秘数 这有点像一个01背包,复杂度和值域有关.但是你发现01背包可以求出更多的东西,就 ...

  2. @loj - 2174@ 「FJOI2016」神秘数

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 一个可重复数字集合 S 的神秘数定义为最小的不能被 S 的子集的 ...

  3. 【LOJ】#2174. 「FJOI2016」神秘数

    题解 这道题的结论很显然= = 就是暴力求的话,把一个区间的数排一下序,如果当前这个数大于前面所有数的前缀和+1,那么前缀和+1即我们所求的答案 那么我们设置一个当前答案(初始为1),在主席树上求出来 ...

  4. 5255 -- 【FJOI2016】神秘数

    5255 -- [FJOI2016]神秘数 Description 一个可重复数字集合\(S\) 的神秘数定义为最小的不能被 \(S\) 的子集的和表示的正整数.例如: \(S = {1,1,1,4, ...

  5. LibreOJ2095 - 「CQOI2015」选数

    Portal Description 给出\(n,k,L,R(\leq10^9)\),求从\([L,R]\)中选出\(n\)个可相同有顺序的数使得其gcd为\(k\)的方案数. Solution 记\ ...

  6. 「CQOI2015」选数

    「CQOI2015」选数 题目描述 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都 ...

  7. 【LOJ】#3094. 「BJOI2019」删数

    LOJ#3094. 「BJOI2019」删数 之前做atcoder做到过这个结论结果我忘了... em,就是\([1,n]\)之间每个数\(i\),然后\([i - cnt[i] + 1,i]\)可以 ...

  8. 「BZOJ3505」[CQOI2014] 数三角形

    「BZOJ3505」[CQOI2014] 数三角形 这道题直接求不好做,考虑容斥,首先选出3个点不考虑是否合法的方案数为$C_{(n+1)*(m+1)}^{3}$,然后减去三点一线的个数就好了.显然不 ...

  9. [BZOJ4408&&BZOJ4299][FJOI2016 && Codechef]神秘数&&FRBSUM(主席树)

    4299: Codechef FRBSUM Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 550  Solved: 351[Submit][Statu ...

随机推荐

  1. System Center Configuration Manager 2016 必要条件准备篇(Part4)

    步骤4.重新启动Configuration Manager主服务器 注意:在Configuration Manager服务器(CM01)上以本地管理员身份执行以下操作 打开管理命令提示符并发出以下 ...

  2. Eclipse Push出现rejected - non-fast-forward错误

    在 Push到服务器时有时会出现 rejected - non-fast-forward 错误,这是由于pull的代码而远端发生改变,此时再提交之前你需要将远端的改变合并到本地上 参考:https:/ ...

  3. PHP : url中出现乱码问题

    例子: 在html中,将数据传到url中 当我点击“提交回复”后,跳转页面中将显示: 我们获取这个参数: 但是由于传过来的参数是中文,url会进行自动的解析成二进制的代码,那我们后台接受到的数据是解析 ...

  4. Metasploitable渗透测试实战——生成木马

    攻击机:kali 目标机:windows 1.生成木马  wincap发送至本机 2.进入msf  (命令:msfconsole)启动监听 3.当目标点击test.exe(可伪装)时,触发后门,实现入 ...

  5. 使用Excel消费C4C的OData service

    步骤比较简单, 打开Excel的标签Data->From Other Sources->From OData Data Feed: 输入如下url: https://.c4c.saphyb ...

  6. Leetcode 46 47 Permutation, 77 combination

    Permutation class Solution { List<List<Integer>> res = new ArrayList<List<Integer& ...

  7. UESTC 757 棋盘

    虽然是水题,但是还是很interesting的.(大概就是我最晚出这个题了... 博弈感觉就是靠yy能力啊.这题是对称性. 最后的必败态是白色格子对称的,一旦对称形成,对手怎么选,跟随就好,对手无法摆 ...

  8. GNOME keyring [(null)] 的密码:

    在ubuntu下执行svn checkout命令时,总是报下面的错误: GNOME keyring [(null)] 的密码:svn: 方法 OPTIONS 失败于 “http://xxxxxxxx/ ...

  9. 正定矩阵(Positive-definite Matrix)

    原文链接 正定矩阵是自共轭矩阵的一种.正定矩阵类似复数中的正实数.定义:对于对称矩阵M,当且仅当存在任意向量x,都有 若上式大于等于零,则称M为半正定矩阵.正定矩阵记为M>0.也被称为正定二次型 ...

  10. js 金额小写转换为大写

    <script> jQuery(document).ready(function () { //当金额文本框失去焦点时,自动将数字转化为大写填充到 大写的文本框中 $("#Amo ...