6.离散化

离散化

离散化的本质,是映射,将间隔很大的点,映射到相邻的数组元素中。减少对空间的需求,也减少计算量。
离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。
通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小。
麻烦的点就是要建立映射关系

求区间和

假定有一个无限长的数轴,数轴上每个坐标上的数都是 0。
现在,我们首先进行 n 次操作,每次操作将某一位置 x 上的数加 c。
接下来,进行 m 次询问,每个询问包含两个整数 l 和 r,你需要求出在区间 [l,r] 之间的所有数的和。

输入格式
第一行包含两个整数 n 和 m。
接下来 n 行,每行包含两个整数 x 和 c。
再接下来 m 行,每行包含两个整数 l 和 r。

输出格式
共 m 行,每行输出一个询问中所求的区间内数字和。

数据范围
−10^9 ≤ x ≤ 10^9,
1 ≤ n,m ≤ 10^5,
−10^9 ≤ l ≤ r ≤ 10^9,
−10000 ≤ c ≤ 10000
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int,int> PII;
const int N = 300010;

int n, m;
int a[N], s[N];	//a[]存储映射后的数组(a[]中存储的就相当于所有映射前有的值,离散点),s[]存储前缀和数组

vector<int> alls;	//存储实际输入的下标,即映射前的数组
vector<PII> add, query;	//存储操作和查询的位置和所对应的值

//定义二分法查找函数
int find(int x) {
	int l = 1, r = alls.size();
	while (l < r) {
		int mid = (l + r) / 2;
		if (alls[mid] >= x)	r = mid;
		else l = mid + 1;
	}
	return r;
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		int x, c;
		cin >> x >> c;
		add.push_back({ x,c });

		alls.push_back(x);	//将下标放入向量中统一进行离散化
	}

	for (int i = 0; i < m; i++) {
		int l, r;
		cin >> 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 (auto 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];
	}

	for (auto item : query) {
		int l = find(item.first), r = find(item.second);
		cout << s[r] - s[l - 1] << endl;
	}
	return 0;
}

注释清晰版:

#include <iostream>
#include <algorithm>
#include <vector>
 
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 r + 1; //  因为要求前缀和,故下标从1开始方便,不用额外的再处理边界。
}
int main () {
    cin >> n >> m;
    for (int i = 0; i < n;++ i) {
        int x, c;
        cin >> x >> c;
        add.push_back({x, c});
 
        alls.push_back(x);//先把下标放入向量中 统一离散化 
    }
    for (int i = 0; i < m;++ i) {
        int l, r;
        cin >> 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());//去除重复元素
   // 1)erase( pos, n); 删除从pos开始的n个字符,例如erase( 0, 1),
   // 删除0位置的一个字符,即删除第一个字符
    //(2)erase( position); 
    //删除position处的一个字符(position是个string类型的迭代器)
    //(3)erase(first,last);删除从first到last之间的字符,
   // (first和last都是迭代器)last 不能是x.end()
    //unique 使用 必须要先过一遍sort排序。再者,unique函数返的返回值是
    //一个迭代器,它指向的是去重后容器中不重复序列的最后一个元素的
    //下一个元素。所以如果 想要得到不重复元素的个数就需要用返回值-开始地址。
    for ( auto item : add) { //先对添加里的元素映射 赋值 
        int x = find(item.first);//找到x的映射值 往原数组中加c 
        a[x] += item.second; // 处理插入
    }
    //for(auto a:b)中b为一个容器,效果是利用a遍历并获得b容器中的每一个值,
    //但是a无法影响到b容器中的元素。
    for (int i = 1; i <= alls.size(); ++i)
    {
        s[i] = s[i - 1] + a[i];//前缀和
    }
    for (auto item : query) {
            int l = find(item.first), r = find(item.second);
            cout << s[r] - s[l - 1] << endl;
    }//每个元素都对应一组{first, first}键值对(pair),
    //键值对中的第一个成员称为first,第二个成员称为second.
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值