【算法基础】1.8离散化(区间和:离散化+前缀和)

当数据范围的跨度很大,但是数据很稀疏时,可以使用离散化。

离散化

如何离散

在这里插入图片描述

数据范围很大,但是并不是每个数字都会出现,就可以将原始数据按照顺序映射到一个小的数据范围。

确定映射方式

按照顺序,一对一的方式,比如:
原数据:1, 3, 7, 8, 12, 1000, 99999
就可以映射成:0,1,2,3,4,5,6
其中1->0, 3->1, 7->2, 8->3, 12->4, 1000->5, 99999->6。

如果原始数据有重复,需要排序去重。

如何确定一个数字排在哪个位置呢?可以使用二分。

例题——区间和⭐⭐⭐⭐⭐(离散化+前缀和)

假定有一个无限长的数轴,数轴上每个坐标上的数都是 0。

现在,我们首先进行 n 次操作,每次操作将某一位置 x 上的数加 c。

接下来,进行 m 次询问,每个询问包含两个整数 l 和 r,你需要求出在区间 [l,r] 之间的所有数的和。

在这里插入图片描述

思路

数据范围不大,但是数据的值域很大!——考虑离散化

Q:具体怎么离散化?
A:将各个下标映射到 从 0 开始的有序下标。

Q:怎么确定有序下标的范围?
A:用列表存储所有出现的下标,然后去重 + 排序,最后列表的 size 就是映射之后的下标个数。

Q:如何确定每个初始下标映射之后的下标呢?
A:使用二分查找,找到下标列表中第一个 >= x 的元素下标,那就是原始下标 x 映射之后的新下标。

之后使用前缀和来处理这个问题就好了。

代码

Java

import java.util.*;
import java.util.stream.Collectors;

public class Main {
    final static int N = 300010;
    static int n, m;
    static int[] a = new int[N], s = new int[N];

    // alls存储所有的下标
    static List<Integer> alls = new ArrayList<>();
    // add存储所有的加操作,query存储所有的查询
    static List<Pair<Integer, Integer>> add = new ArrayList<>(), query = new ArrayList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(), m = scanner.nextInt();
        for (int i = 0; i < n; ++i) {
            int x = scanner.nextInt(), c = scanner.nextInt();
            add.add(new Pair<>(x, c));
            alls.add(x);
        }
        for (int i = 0; i < m; ++i) {
            int l = scanner.nextInt(), r = scanner.nextInt();
            query.add(new Pair<>(l, r));
            alls.add(l);
            alls.add(r);
        }

        alls = alls.stream().distinct().collect(Collectors.toList());   // 对列表进行去重
        Collections.sort(alls);     // 对所有下标进行排序

        // 处理所有加操作
        for (Pair<Integer, Integer> item: add) a[find(item.x)] += item.y;
        // 求前缀和数组
        for (int i = 0; i < alls.size(); ++i) {
            s[i + 1] = s[i] + a[i];
        }
        for (Pair<Integer, Integer> item: query) {
            int l = find(item.x), r = find(item.y);
            System.out.println(s[r + 1] - s[l]);
        }
    }

    // 找到第一个>=x的下标
    static int find(int x) {
        int l = 0, r = alls.size() - 1;
        while (l < r) {
            int mid = l + r >> 1;
            if (alls.get(mid) >= x) r = mid;
            else l = mid + 1;
        }
        return l;
    }
}

class Pair<T, E> {
    T x;
    E y;

    public Pair(T x, E y) {
        this.x = x;
        this.y = y;
    }
}

Cpp

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;

const int N = 300010;

int n, m;
int a[N], s[N];

vector<int> alls;
vector<PII> add, query;

int find(int x) {
    int l = 0, r = alls.size() - 1;
    while (l < r) {
        int mid = l + r >> 1;
        if (alls[mid] >= x) r = mid;
        else l = mid + 1;
    }
    return l;
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i ++ ) {
        int x, c;
        scanf("%d%d", &x, &c);
        add.push_back({x, c});
        alls.push_back(x);
    }
    
    for (int i = 0; i < m; i ++ ) {
        int l, r;
        scanf("%d%d", &l, &r);
        query.push_back({l, r});
        alls.push_back(l);
        alls.push_back(r);
    }
    
    // 去重
    sort(alls.begin(), alls.end());
    alls.erase(unique(alls.begin(), alls.end()), alls.end());
    
    // 处理插入
    for (PII item: add) {
        int x = find(item.first);
        a[x] += item.second;
    }
    
    // 预处理前缀和
    for (int i = 1; i <= alls.size(); i ++ ) s[i] = s[i - 1] + a[i - 1];
    
    // 处理询问
    for (PII item: query) {
        int l = find(item.first), r = find(item.second);
        printf("%d\n", s[r + 1] - s[l]);
    }
    return 0;
}

可能有读者会有疑问,为什么需要二分呢?直接排序好按顺序分配下标就好了呀。

这里是因为有 add 操作的存在,在操作 add 时,需要根据原数据直接得到其映射的下标位置。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wei *

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

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

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

打赏作者

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

抵扣说明:

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

余额充值