当前位置:网站首页>Y is always discrete and can't understand, how to solve it? Answer: read it several times
Y is always discrete and can't understand, how to solve it? Answer: read it several times
2022-07-03 17:19:00 【Liuyun maple】
y Always discrete and can't understand , How to solve ? Answer: read it several times
I saw y Total discrete and , It was supposed to collapse , But the barrage track : Look at it several times. . Do not deceive me , Here's a record , Also wrote more specific notes .
Challenge time , Suddenly I found that my hand speed increased a lot , The program is simple and happy
Interval and
Suppose there is an infinite number axis , The number on each coordinate on the number axis is 00.
Now? , Let's start with nn operations , Each operation will a certain position xx Add... To the number on cc.
Next , Conduct mm Time to ask , Each query contains two integers ll and rr, You need to find the interval [l,r][l,r] The sum of all numbers between .
Input format
The first line contains two integers nn and mm.
Next nn That's ok , Each line contains two integers xx and cc.
Next mm That's ok , Each line contains two integers ll and rr.
Output format
common mm That's ok , Each line outputs a number in the interval and .
Data range
−109≤x≤109−109≤x≤109,
1≤n,m≤1051≤n,m≤105,
−109≤l≤r≤109−109≤l≤r≤109,
−10000≤c≤10000
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int,int> pll;
vector<pll> adds,query;
vector<int> alls; // Store all the coordinates that need to be operated in
const int N = 300010;
int a[N],s[N];
// In fact, the discretization of large coordinates , Is the element value of the array, that is alls[i] It represents the value of the big target , Using discretization, the large scale can be corresponding to the small scale i, We use small coordinates to process . I think I've seen it several times , That's pretty clear .
// Use dichotomy to determine the number of small marks corresponding to the original big mark
int find(int x){
int l = 0, r = alls.size();
while(l < r){
int mid = l + r >> 1;
if(alls[mid] >= x) r = mid;
else l = mid + 1;
}
return r + 1;
}
int main(){
int n,m; cin >> n >> m;
for(int i = 0; i < n; i++) {
int x, c;
cin >> x >> c;
alls.push_back(x);
adds.push_back({
x,c});
}
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()); // duplicate removal
for(auto item: adds){
// The first step in implementing the requirements of the topic
int x = find(item.first); // Find the small coordinate corresponding to the large coordinate
a[x] += item.second;
}
// Prefixes and preprocessing
for(int i = 1; i <= alls.size(); i++) s[i] = s[i-1] + a[i];
// Perform the second step of the topic
for(auto item : query){
int l = find(item.first), r = find(item.second);
cout << s[r] - s[l-1] << endl;
}
}
边栏推荐
- 【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
- 企业级自定义表单引擎解决方案(十一)--表单规则引擎1
- Define a structure fraction to represent a fraction, which is used to represent fractions such as 2/3 and 5/6
- Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
- Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
- 跨境电商:外贸企业做海外社媒营销的优势
- Svn full backup svnadmin hotcopy
- 線程池:業務代碼最常用也最容易犯錯的組件
- TensorBoard快速入门(Pytorch使用TensorBoard)
- AcWing 3438. 数制转换
猜你喜欢

The largest matrix (H) in a brush 143 monotone stack 84 histogram

One brush 147-force deduction hot question-4 find the median of two positive arrays (H)

Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires

New features of C 10

大变局!全国房价,跌破万元大关

【JokerのZYNQ7020】DDS_ Compiler。

One brush 145 force deduction hot question-2 sum of two numbers (m)

Great changes! National housing prices fell below the 10000 yuan mark

Free data | new library online | cnopendata complete data of China's insurance intermediary outlets

TensorBoard快速入门(Pytorch使用TensorBoard)
随机推荐
Thread pool: the most common and error prone component of business code
简单配置PostFix服务器
人生还在迷茫?也许这些订阅号里有你需要的答案!
[combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
Great changes! National housing prices fell below the 10000 yuan mark
Simple configuration of postfix server
手把手带你入门 API 开发
Unity notes unityxr simple to use
Depth first search of graph
设计电商秒杀
One brush 144 force deduction hot question-1 sum of two numbers (E)
How to judge the region of an IP through C?
Squid service startup script
Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
Analysis of variance summary
跨境电商:外贸企业做海外社媒营销的优势
Apache service suspended asynchronous acceptex failed
[combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
A day's work list of an ordinary programmer
企业级自定义表单引擎解决方案(十一)--表单规则引擎1