4408: [Fjoi 2016]神秘数/4299: Codechef FRBSUM

本文介绍了一种用于计算数字集合神秘数的高效算法,并通过具体代码实现了动态更新与区间查询的功能。该算法首先定义了神秘数的概念,即集合中无法由其子集的和表示的最小正整数,接着详细讲解了算法原理与实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

题目大意:一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数,给出a[],多次询问 a[l]a[l+1],a[r] 的神秘数

题解:设当前神秘数为 ans ,新加入 x
1. xans,则 ans=ans+x
2. x>ans ans 不变

对于每个询问,从 ans=1 开始,求出 get=aiansai ,更新答案

O(mlog21e9)

我的收获:强啊

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

#define INF 1e9 
#define M 100005
#define MX M*100
#define lson tl[x],tl[y],l,mid
#define rson tr[x],tr[y],mid+1,r

int n,m,cnt,a[M],root[M];
int tl[MX],tr[MX],sum[MX];

inline void node(int &x,int w,int v){x=++cnt,sum[x]=sum[w]+v,tl[x]=tl[w],tr[x]=tr[w];}

void insert(int x,int &y,int l,int r,int k)
{
    node(y,x,k);
    if(l==r) return ;
    int mid=(l+r)>>1;
    if(k<=mid) insert(lson,k);
    else insert(rson,k);
}

int query(int x,int y,int l,int r,int k)
{
    if(l==r) return sum[y]-sum[x];
    int mid=(l+r)>>1;
    if(k<=mid) return query(lson,k);
    return query(rson,k)+sum[tl[y]]-sum[tl[x]];
}

void work()
{
    while(m--){
        int l,r;scanf("%d%d",&l,&r);
        int ans=1;
        while(1){
            int get=query(root[l-1],root[r],1,INF,ans);
            if(get<ans) break;
            ans=get+1;
        }
        printf("%d\n",ans);
    }
}

void init()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) insert(root[i-1],root[i],1,INF,a[i]);
    scanf("%d",&m);
}

int main()
{
    init();
    work();
    return 0;
}
### 关于洛谷平台上线段树练习题目的推荐 #### 题目一:P3810 【模板】线段树 1 此题目作为入门级的线段树构建与基本操作实现,适合初学者掌握线段树的基础概念和简单应用。通过这道题可以熟悉如何在线段树上执行单点更新以及区间求和的操作[^2]。 ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; struct SegmentTree { int sum[maxn << 2]; void pushUp(int rt) {sum[rt] = sum[rt<<1] + sum[rt<<1|1];} void build(int l, int r, int rt){ if(l == r){ scanf("%d", &sum[rt]); return ; } int m = (l+r)>>1; build(lson); build(rson); pushUp(rt); } // Other functions like update and query can be implemented here. }; int main(){ SegmentTree tree; int n,m,opt,x,y,k; char str[2]; while(scanf("%d%d",&n,&m)!=EOF){ memset(tree.sum,0,sizeof(tree.sum)); tree.build(1,n,1); for(int i=0;i<m;++i){ getchar(); gets(str); sscanf(str,"%d %d %d",&opt,&x,&y); switch(opt){ case 1 : k=y-tree.a[x]; tree.update(x,k,1,n,1);break; case 2 : printf("%lld\n",tree.query(x,y,1,n,1)); break; } } } return 0; } ``` #### 题目二:P2791 幼儿园篮球题 该问题不仅考察了对线段树的理解程度,还涉及到更复杂的逻辑思考能力。在这个场景下,需要利用线段树来高效解决有关子树内节点权重总和的问题,在实际编程竞赛中具有较高的实用性价值[^1]。 #### 题目三:P4566 [FJOI2018]新型城市化 这是一个较为高级的应用实例,涉及动态开点线段树的知识点。对于想要深入研究据结构优化技巧的学习者来说是一个很好的挑战对象。这类题目有助于提高解决问题的能力,并加深对复杂算法设计模式的认识。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值