当前位置:网站首页>[14. Interval sum (discretization)]
[14. Interval sum (discretization)]
2022-07-01 14:33:00 【Little silly bird_ coding】
discretization
- Let's say there are 105 Number , The range of each number is 0~109, Some questions may use these value subscripts , But if we open a length of 109 Arrays are unrealistic . And that's where it comes in
mapping. - We need to map this sequence from 1 At the beginning
Continuous natural number, This process is called discretization .
nature
- The span of the whole range is very large , But very sparse , For example, there are 109 Number , But we only used 105 Number
step
- Going to the x Add a c, At this point, first find x The discretized value of k, And then again a[k] + c.
- It's also the same with prefix sum , seek [l, r] The sum of numbers between , First find l and r The value after discretization

Discretization needs to solve a problem
Premise a[ ] Is ordered .
- a[ ] Duplicate elements may appear in , It needs to be
duplicate removal - How to figure out x The discretized value of , It's usually used
Two points
Interval and
Suppose there is an infinite number axis , The number on each coordinate on the number axis is 0.
Now? , Let's start with n operations , Each operation will a certain position x Add... To the number on c.
Next , Conduct m Time to ask , Each query contains two integers l and r, You need to find the interval [l,r][l,r] The sum of all numbers between .
Input format
The first line contains two integers n and m.
Next n That's ok , Each line contains two integers x and c.
Next m That's ok , Each line contains two integers l and r.
Output format
common m That's ok , Each line outputs a number in the interval and .
Data range
−109 ≤ x ≤ 109
1 ≤ n, m ≤ 105
−109 ≤ l ≤ r ≤ 109
−10000 ≤ c ≤ 10000sample input :
3 3 1 2 3 6 7 5 1 3 4 6 7 8sample output :
8 0 5
Code
#include <iostream> #include <vector> #include <algorithm> using namespace std; using PII = pair<int, int>; // Because the input is a pair , So use pair To hold the //typedef pair<int, int> PII; const int N = 300010; int n, m; int a[N], s[N]; //a[N] Is the number to store ,s[N] Are prefixes and vector<int> alls; // All the discretized values we store vector<PII> add, query; // The first is the insert operation , The second is to ask // First read in all the numbers , Put the used subscript , discretization . int find(int x) // Please x The result of discretization { 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; // Map numbers to subscripts from 1 Start , Because the interval is found later [l, r] The sum of middle numbers uses the prefix and , Subscript from 1, Don't deal with boundary problems } int main() { cin >> n >> m; for (int i = 0; i < n; i ++) { int x, c; cin >> x >> c; add.push_back({ x, c}); // Put the number on add in alls.push_back(x); // Re count , Put it in the... To be discretized alls Array } 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); } // duplicate removal sort(alls.begin(), alls.end()); alls.erase(unique(alls.begin(), alls.end()), alls.end()); // Handle insertion for (auto item : add) { int x = find(item.first); a[x] += item.second; } // Preprocessing prefixes and for (int i = 1; i <= alls.size(); i ++) s[i] = s[i - 1] + a[i]; // Inquiry before processing for (auto item : query) { int l = find(item.first), r = find(item.second); cout << s[r] - s[l - 1] << endl; } return 0; }
unique Implementation of function
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] all a Non repeating number in return a.begin() + j; }
边栏推荐
- Websocket (simple experience version)
- Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
- 问题随记 —— Oracle 11g 卸载
- SWT / anr problem - how to open binder trace (bindertraces) when sending anr / SWT
- Generate random numbers (4-bit, 6-bit)
- 从零开发小程序和公众号【第三期】
- When the main process architecture game, to prevent calls everywhere to reduce coupling, how to open the interface to others to call?
- Research Report on the development trend and competitive strategy of the global CCTV robot industry
- Admire, Ali female program undercover more than 500 black production groups
- Sorting learning sorting
猜你喜欢

C 语言基础

建立自己的网站(14)

So programmers make so much money doing private work? It's really delicious

Sqlachemy common operations

【R语言数据科学】:机器学习常见评估指标

【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较

既不是研发顶尖高手,也不是销售大牛,为何偏偏获得 2 万 RMB 的首个涛思文化奖?

【IoT毕设.下】STM32+机智云AIoT+实验室安全监控系统

MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
![[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system](/img/b2/e8f81ecda6f5f4fc65501aaf9f13cf.gif)
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
随机推荐
问题随记 —— Oracle 11g 卸载
sqlilabs less-11~12
Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
Admire, Ali female program undercover more than 500 black production groups
2022-2-15 learning xiangniuke project - Section 4 business management
Research Report on the development trend and competitive strategy of the global ultrasonic scalpel system industry
Research Report on the development trend and competitive strategy of the global commercial glassware industry
Generate random numbers (4-bit, 6-bit)
使用 Lambda 函数URL + CloudFront 实现S3镜像回源
Research Report on the development trend and competitive strategy of the global facial wrinkle removal and beauty instrument industry
Leetcode(69)——x 的平方根
Après avoir été licencié pendant trois mois, l'entrevue s'est effondrée et l'état d'esprit a commencé à s'effondrer.
About the use of HTTP cache validation last modified and Etag
After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse
【R语言数据科学】:机器学习常见评估指标
日志中打印统计信息的方案
leetcode622. Design cycle queue (C language)
C 语言进阶
Research Report on the development trend and competitive strategy of the global diamond suspension industry
[dynamic programming] interval dp:p1005 matrix retrieval

