当前位置:网站首页>[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; }
边栏推荐
- SWT/ANR问题--如何捕获性能的trace
- Develop small programs and official account from zero [phase III]
- In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee
- WebSocket(简单体验版)
- 使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
- Build your own website (21)
- That hard-working student failed the college entrance examination... Don't panic! You have another chance to counter attack!
- sqlilabs less-8
- Research Report on the development trend and competitive strategy of the global electromagnetic flowmeter industry
- The integration of computing and Internet enables the transformation of the industry, and the mobile cloud lights up a new roadmap for the future of digital intelligence
猜你喜欢
随机推荐
2022. Let me take you from getting started to mastering jetpack architecture components - lifecycle
111. Minimum depth of binary tree
原来程序员搞私活这么赚钱?真的太香了
How will the surging tide of digitalization overturn the future?
SWT / anr problem - how to open binder trace (bindertraces) when sending anr / SWT
This paper introduces an implementation scheme to enhance the favorite transaction code management tool in SAP GUI
户外LED显示屏应该考虑哪些问题?
Research Report on the development trend and competitive strategy of the global high temperature label industry
Basic concepts of programming
Use the npoi package of net core 6 C to read excel Pictures in xlsx cells and stored to the specified server
Research Report on the development trend and competitive strategy of the global diamond suspension industry
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
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?
241. 为运算表达式设计优先级
sqlilabs less13
About the use of HTTP cache validation last modified and Etag
【R语言数据科学】:机器学习常见评估指标
sqlilabs less-8
sqlilabs less13
MySQL log










