当前位置:网站首页>【14. 区间和(离散化)】
【14. 区间和(离散化)】
2022-07-01 14:28:00 【小呆鸟_coding】
离散化
- 假设一共有105个数,每个数的值域是0~109,有些题目可能会使用这些值下标来做,但是我们如果开一个长度是109数组是不现实的。这时候就需要用到
映射
。 - 我们需要把这个序列映射到从1开始的
连续自然数
,这个过程就叫离散化。
性质
- 整个值域的跨度很大,但是非常稀疏,例如一共有109个数,但是我们只用到了105个数
步骤
- 打算在x的值上加上一个c,此时首先找到x的离散化后的值k,然后再a[k] + c.
- 求前缀和也是,求[l, r]之间的数的和,首先找到 l 和 r离散化后的值
离散化需要解决的来个问题
前提a[ ]是有序的。
- a[ ]中可能出现重复元素,需要对它进行
去重
- 如何算出x 的离散化后的值,一般用
二分
区间和
假定有一个无限长的数轴,数轴上每个坐标上的数都是 0。
现在,我们首先进行 n次操作,每次操作将某一位置 x 上的数加 c。
接下来,进行 m 次询问,每个询问包含两个整数 l和 r,你需要求出在区间 [l,r][l,r] 之间的所有数的和。
输入格式
第一行包含两个整数 n 和 m。
接下来 n 行,每行包含两个整数 x 和 c。
再接下来 m 行,每行包含两个整数 l 和 r。
输出格式
共 m 行,每行输出一个询问中所求的区间内数字和。
数据范围
−109 ≤ x ≤ 109
1 ≤ n, m ≤ 105
−109 ≤ l ≤ r ≤ 109
−10000 ≤ c ≤ 10000输入样例:
3 3 1 2 3 6 7 5 1 3 4 6 7 8
输出样例:
8 0 5
代码
#include <iostream> #include <vector> #include <algorithm> using namespace std; using PII = pair<int, int>; //因为输入是一对,所以用pair来存放 //typedef pair<int, int> PII; const int N = 300010; int n, m; int a[N], s[N]; //a[N]是要存放的数,s[N]是前缀和 vector<int> alls; //我们存的所有离散化的值 vector<PII> add, query; //第一个是插入操作,第二个是询问 //首先把所有的数读进来,把用到的下标,离散化。 int find(int x) //求一下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开始,因为后面求区间[l, r]中数的和用到前缀和,下标从1,不用处理边界问题 } int main() { cin >> n >> m; for (int i = 0; i < n; i ++) { int x, c; cin >> x >> c; add.push_back({ x, c}); //将数放在add中 alls.push_back(x); //再将数,放到待离散化后的alls数组中 } 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; }
unique函数的实现
vector<int>:: iterator unique(vector<int> &a) { int j = 0; for (int i = 0; i < a.size(); i ++) { if(!i || a[i] != a[i - 1]); a[j ++] = a[i]; } //a[0] ~ a[j - 1]所有a中不重复的数 return a.begin() + j; }
边栏推荐
- [IOT design. Part I] stm32+ smart cloud aiot+ laboratory security monitoring system
- C 语言基础
- TDengine 连接器上线 Google Data Studio 应用商店
- 2022 PMP project management examination agile knowledge points (6)
- Research Report on the development trend and competitive strategy of the global traditional computer industry
- Play with mongodb - build a mongodb cluster
- This paper introduces an implementation scheme to enhance the favorite transaction code management tool in SAP GUI
- What class loading mechanisms does the JVM have?
- C language ordering management system
- sqlilabs less13
猜你喜欢
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
[IOT design. Part I] stm32+ smart cloud aiot+ laboratory security monitoring system
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
One of the data Lake series | you must love to read the history of minimalist data platforms, from data warehouse, data lake to Lake warehouse
TexStudio使用教程
sqlilabs less-11~12
QT learning management system
leetcode622. Design cycle queue (C language)
如何看待国企纷纷卸载微软Office改用金山WPS?
111. Minimum depth of binary tree
随机推荐
问题随记 —— Oracle 11g 卸载
Research Report on the development trend and competitive strategy of the global high temperature label industry
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
Research Report on the development trend and competitive strategy of the global display filter industry
"National defense seven sons" funding soared, with Tsinghua reaching 36.2 billion yuan, ranking second with 10.1 billion yuan. The 2022 budget of national colleges and universities was made public
2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words
2022-2-15 learning the imitation Niuke project - post in Section 2
算网融合赋能行业转型,移动云点亮数智未来新路标
Research Report on the development trend and competitive strategy of the global navigation simulator industry
【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较
Phpcms realizes the direct Alipay payment function of orders
sqlilabs less13
Summary of leetcode's dynamic programming 5
2022-2-15 learning the imitation Niuke project - Section 3 post details
Vnctf2022 open web gocalc0
奔涌而来的数字化浪潮,将怎样颠覆未来?
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
那个很努力的学生,高考失败了……别慌!你还有一次逆袭机会!
当主程架构游戏的时候,防止到处调用减少耦合性,怎么开放接口给其他人调用呢?
111. Minimum depth of binary tree