26.CF1000F One Occurrence

博客介绍了如何解决一道竞赛编程题目CF1000F,该题要求在给定序列中支持查询区间内只出现一次的数字。博主提供了离线处理的思路,通过线段树维护每个数字的最新出现位置,并在更新时去除重复贡献。文章包含详细的题目解析、代码实现和解题思路,适合进阶的算法学习者参考。

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

26.CF1000F One Occurrence

个人Limitの线段树题单题解主目录:Limitの线段树题单 题解目录_HeartFireY的博客-CSDN博客

给定序列,要求支持查询区间内只出现一次的数字

可以离线,那么离线所有询问后按右端点排序,然后加入贡献回答即可。

洛谷传送门:CF1000F One Occurrence - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

CF传送门:F. One Occurrence (codeforces.com)

题目思路

给定序列,要求支持查询区间内只出现一次的数字。

这属于一类特别典型的问题:开桶维护最后一次出现位置,在更新时去重复贡献。

对于本题,我们首先对所有询问进行离线,然后按照右端点排序加入贡献计算答案:

  • 记录每个数字上次最后一次出现的位置
  • 只要对于上一次出现的位置维护一个区间最小值和最小值的数值,然后查询区间最小值是否位于区间内即可
  • 在每次加入右端点时判断是否之前出现过,如果出现过就清除上次加入的贡献(当当前点为右端点时,上次出现的位置一定只能作为左端点出现,因此需要消除其作为右端点时记录的前序信息)

当然,这题如果强制在线的话,可以用可持久化权值线段树保留历史版本进行查询。

Code

#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
using namespace std;

const int N = 4e6 + 10;

namespace SegTree{
    #define ls rt << 1
    #define rs rt << 1 | 1
    #define lson ls, l, mid
    #define rson rs, mid + 1, r

    struct Info{
        int val, pre;
        Info operator+ (Info x) {
            Info ans = {val, pre};
            if(pre > x.pre) ans.pre = x.pre, ans.val = x.val;
            return ans;
        }
    }tree[N];

    inline void push_up(int rt){
        tree[rt] = tree[ls] + tree[rs];
    }

    void build(int rt, int l, int r){
        tree[rt] = {0, INT_MAX};
        if(l == r) return;
        int mid = l + r >> 1;
        build(lson), build(rson);
    }

    void update(int rt, int l, int r, int pos, int val, int pre){
        if(l == r) return (void)(tree[rt] = Info{val, pre});
        int mid = l + r >> 1;
        if(mid >= pos) update(lson, pos, val, pre);
        else update(rson, pos, val, pre);
        push_up(rt);
    }


    Info query(int rt, int l, int r, int L, int R){
        if(l >= L && r <= R) return tree[rt];
        int mid = l + r >> 1; Info ans = Info{0, INT_MAX};
        if(mid >= L) ans = ans + query(lson, L, R);
        if(mid < R) ans = ans + query(rson, L, R);
        return ans;
    }

    #undef ls
    #undef rs
    #undef lson
    #undef rson
}

struct query{
    int l, id;
    const bool operator< (const query &x) const { return l < x.l; }
};

vector<query> q[N];

int a[N], ans[N], last[N];


inline void solve(){
    int n = 0; cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    SegTree::build(1, 1, n);
    int m = 0; cin >> m;
    for(int i = 1; i <= m; i++){
        int l, r; cin >> l >> r;
        q[r].emplace_back(query{l, i});
    }

    for(int i = 1; i <= n; i++){
        if(last[a[i]]){
            SegTree::update(1, 1, n, last[a[i]], 0, INT_MAX);
            SegTree::update(1, 1, n, i, a[i], last[a[i]]);
        }
        else SegTree::update(1, 1, n, i, a[i], 0);
        last[a[i]] = i;

        for(auto qu : q[i]){
            assert(qu.l <= i);
            auto res = SegTree::query(1, 1, n, qu.l, i);
            if(res.pre < qu.l) ans[qu.id] = res.val;
            else ans[qu.id] = 0; 
        }
    }

    for(int i = 1; i <= m; i++) cout << ans[i] << endl;
}

signed main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    int t = 1; // cin >> t;
    while(t--) solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeartFireY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值