当前位置:网站首页>The prefix and discretization
The prefix and discretization
2022-08-04 17:33:00 【Madness is free】
802. 区间和
假定有一个无限长的数轴,数轴上每个坐标上的数都是 0
.
现在,我们首先进行 n
次操作,每次操作将某一位置 x 上的数加 c
.
接下来,进行 m
次询问,每个询问包含两个整数 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
//lower_bound(beg,end,val);返回大于等于val的第一个元素的位置,如果
//The element was not found in the array or container,then returns the position if the element existed;
//upper_bound(beg,end,val):返回大于val的第一个元素的位置
//uniqueReturns the position of the last element without repeating elements
第一种解法:
//lower_bound(beg,end,val);返回大于等于val的第一个元素的位置,如果
//The element was not found in the array or container,then returns the position if the element existed;
//upper_bound(beg,end,val):返回大于val的第一个元素的位置
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int, int> PII;
vector<PII> add,query;
vector<int> all;
const int N = 3e5+10;
int a[N],s[N];
int main()
{
int n,m;
cin >> n >> m;
while(n--)
{
int x,c;
cin >> x >> c;
add.push_back({x,c}); //Save the incremental information of the coordinates
all.push_back(x); //Save the coordinates in all中
}
while (m -- )
{
int l,r;
cin >> l >> r;
query.push_back({l,r}); //Save the query information
all.push_back(l); //Record the information subscript of the query
all.push_back(r);
}
sort(all.begin(),all.end());
all.erase(unique(all.begin(),all.end())+1,all.end());//deduplicate the coordinates
//uniqueReturns the position of the last element without repeating elements
for(auto item:add)
{
int id=lower_bound(all.begin(),all.end(),item.first)-all.begin()+1;
a[id]+=item.second;
}
for(int i=1;i<=all.size()+1;++i)
s[i]=s[i-1]+a[i];
for(auto que:query)
{
int l=lower_bound(all.begin(),all.end(),que.first)-all.begin()+1;
int r=lower_bound(all.begin(),all.end(),que.second)-all.begin()+1;
cout << s[r]-s[l-1] << endl;
}
return 0;
}第二种解法:
/**
* 分析,We can use discretization to solve the prefix sum;
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int, int> PII;
const int N = 1e5+10;
int s[N]={0},a[N]={0};
vector<PII> add; //坐标的增量
vector<int> all,Unique; //allAll coordinates representing the increment of the coordinates;
//uniqueNon-repeating coordinates representing increments of coordinates;
int upper_equal(int val) //返回大于等于val的第一个坐标
{
int l=0,r=Unique.size(); //r应该设置为unique.size();
while(l<r)
{
int mid=(l+r)/2;
if(Unique[mid]>=val)
r=mid;
else
l=mid+1;
}
return l+1; //返回l+1,Because the subscript of the prefix sum is from1开始;
}
int main()
{
int n,m;
cin >> n >> m;
int x,c;
for(int i=1;i<=n;++i)
{
cin >> x >> c;
add.push_back({x,c});
all.push_back(x);
}
sort(all.begin(),all.end());
for(int i=0;i<all.size();++i)
{
if(i==0||all[i]!=all[i-1])
Unique.push_back(all[i]);
}
for(int i=0;i<add.size();++i)
{
int r=upper_equal(add[i].first);
a[r]+=add[i].second;
}
for(int i=1;i<=n;++i)
s[i]=s[i-1]+a[i];
while(m--)
{
int l,r;
cin >> l >> r;
l=upper_equal(l);
r=upper_equal(r+1);
/**不管r在不在unique数组中, 我们返回r+1的坐标,再减去1,
* to get the correct subscript
* */
cout << s[r-1]-s[l-1] << endl;
}
return 0;
}
边栏推荐
- R语言ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、使用ggarrange函数将表格数据和可视化图像组合起来(表格数据在可视化图像下方)
- C# Sqlite database construction and use skills
- CAS:385437-57-0,DSPE-PEG-Biotin,生物活性分子磷脂-聚乙二醇-生物素
- R语言缺失时间序列的填充及合并:补齐时间序列数据中所有缺失的时间索引、使用merge函数合并日期补齐之后的时间序列数据和另外一个时间序列数据(补齐左侧数据)
- JSP的Web监听器(Listener)
- arm交叉编译
- Understand Chisel language. 32. Chisel advanced hardware generator (1) - parameterization in Chisel
- 树莓派利用autofs自动挂载/卸载外部硬盘
- Cholesterol-PEG-DBCO,CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔科研试剂
- 《机器学习理论到应用》电子书免费下载
猜你喜欢

荣耀互联对外开放,赋能智能硬件合作伙伴,促进全场景生态产品融合
![【 Gazebo introductory tutorial] speak the second model library into robot modeling and visualization (editor) model](/img/db/44a1ac5338879c9e6edd933c28c0af.png)
【 Gazebo introductory tutorial] speak the second model library into robot modeling and visualization (editor) model

又一款高颜值 Redis 官方可视化工具,功能真心强大!

面试官:可以谈谈乐观锁和悲观锁吗

2022年五一数学建模C题讲解

ctfshow 萌新web1-21

CF86D Powerful array

Learning and Exploration-Introducing Baidu Statistics to the Website

Liunx删除乱码文件

【web自动化测试】Playwright快速入门,5分钟上手
随机推荐
DSPE-PEG-DBCO,DBCO-PEG-DSPE,磷脂-聚乙二醇-二苯并环辛炔科研实验用
】 【 LeetCode daily one problem - 540. The order of a single element of the array
mysqlbinlog 超过500g自动删除,保留7个,求大深给个版本
树莓派安装samba用来共享文件
【web自动化测试】Playwright快速入门,5分钟上手
Cholesterol-PEG-DBCO,CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔科研试剂
Thrift IDL示例文件
小程序笔记3
树莓派通过API向企业微信推送图文
消灭异步回调,还得是async-await
树莓派利用autofs自动挂载/卸载外部硬盘
JS中null与undefined的异同点
【LeetCode Daily Question】——374. Guess the size of the number
第一章 对象和封装
网络靶场监控系统的安全加固纪实(1)—SSL/TLS对日志数据加密传输
LeetCode 899. 有序队列
Boost库学习笔记(一)安装与配置
基于层次分析法的“内卷”指数分析
数字化金融企业的产品体系长啥样?
对象实例化之后一定会存放在堆内存中?