当前位置:网站首页>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;
}
}
边栏推荐
- 免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据
- vs2013已阻止安装程序,需安装IE10
- Redis:关于列表List类型数据的操作命令
- 【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
- One brush 145 force deduction hot question-2 sum of two numbers (m)
- Host based intrusion system IDS
- New library online | cnopendata China bird watching record data
- [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)
- 汇编实例解析--实模式下屏幕显示
- [RT thread] NXP rt10xx device driver framework -- RTC construction and use
猜你喜欢
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
Talk about several methods of interface optimization
Bcvp developer community 2022 exclusive peripheral first bullet
New library online | cnopendata complete data of Chinese insurance institution outlets
国内如何购买Google Colab会员
How do large consumer enterprises make digital transformation?
One brush 149 force deduction hot question-10 regular expression matching (H)
Kotlin learning quick start (7) -- wonderful use of expansion
ANOVA example
聊聊接口优化的几个方法
随机推荐
新库上线 | CnOpenData中国观鸟记录数据
基于主机的入侵系统IDS
線程池:業務代碼最常用也最容易犯錯的組件
Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据
Golang单元测试、Mock测试以及基准测试
[JDBC] API parsing
SWM32系列教程4-端口映射及串口应用
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
Depth first search of graph
SSH连接远程主机等待时间过长的解决方法
One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
[combinatorics] recursive equation (characteristic equation and characteristic root | example of characteristic equation | root formula of monadic quadratic equation)
Apache service suspended asynchronous acceptex failed
Kotlin学习快速入门(7)——扩展的妙用
Kotlin learning quick start (7) -- wonderful use of expansion
Simple use of unity pen XR grab
kubernetes资源对象介绍及常用命令(三)
C语言字符串练习