当前位置:网站首页>区间和------离散化
区间和------离散化
2022-07-06 09:25:00 【是小张张呀 zsy】
离散化
满足离散化的性质:值域大,数稀疏
a[ ] : { 1 , 3 , 100 , 200 , 500000000 } 映射
下标:0 1 2 3 4
1.a[]中可能有重复的元素 去重
2.如何计算出离散化的值 二分(此题有序)
区间和
假定有一个无限长的数轴,数轴上每个坐标上的数都是 0。现在,我们首先进行 n 次操作,每次操作将某一位置 x上的数加 c。接下来,进行 m 次询问,每个询问包含两个整数 l 和 r,你需要求出在区间 [l,r] 之间的所有数的和。输入格式:第一行包含两个整数 n 和 m。接下来 n 行,每行包含两个整数 x 和 c。再接下来 m 行,每行包含两个整数 l 和 r。输出格式:共 m 行,每行输出一个询问中所求的区间内数字和。数据范围−1e9≤x≤1e9,1≤n,m≤1e5,−1e9≤l≤r≤1e9,−10000≤c≤10000
输入样例:
3 3
1 2
3 6
7 5
1 3
4 6
7 8
输出样例:
8
0
5
#include<bits/stdc++.h>
using namespace std;
int n,m,l,r;
int const N=3e5+10; //数组里面存x,l,c对应离散化的值,个数最多3*1e5
int a[N],s[N];
vector<int> lisan; //存入所有离散化的值
typedef pair<int,int> PII;
vector<PII> addxc,xunwenlr; //插入x c; 插入询问l r;
int find(int x) //求离散化的结果
{
int l=0,r=lisan.size()-1;
while(l<r)
{
int mid=l+r >> 1;
if(lisan[mid]>=x) r=mid;
else l=mid+1;
}
return r+1; //坐标加1,映射到从1开始;
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
int x,c;
cin>>x>>c;
lisan.push_back(x);
addxc.push_back({
x,c});
}
for(int i=0;i<m;i++)
{
cin>>l>>r;
xunwenlr.push_back({
l,r});
lisan.push_back(l);
lisan.push_back(r);
}
sort(lisan.begin(),lisan.end());
lisan.erase(unique(lisan.begin(),lisan.end()),lisan.end()); //去重,插入的x l r 去掉相同的
for(auto item:addxc) //插入处理
{
int xx=find(item.first); //xx离散化所对应的下标
a[xx]+=item.second;
}
for(int i=1;i<=lisan.size();i++) //前缀和
s[i]=s[i-1]+a[i];
for(auto item:xunwenlr) //处理询问
{
l=find(item.first); //离散化所对应的下标
r=find(item.second);
cout<<s[r]-s[l-1]<<endl;
}
return 0;
}
//上面去重unique函数和erase函数 原代码
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];
Return a.begin()+j;
}
unique()函数,后面的元素替换的前面重复的元素,一般排序后使用(去除的是相邻元素),后面的空间并没有删除掉
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a[]={
2,3,4,4,6};
sort(a,a+5); //一般在使用unique之前都需要进行排序;
unique(a,a+5); //使用unique函数对数组进行去重
for(int i=0;i<5;i++)
{
cout<<a[i]<<” “; // 2 3 4 6 6
}
cout<<"不重复数列的长度:"<<unique(a,a+5)-a<<endl; //不重复序列的长度:4
}
边栏推荐
- Research Report on printed circuit board (PCB) connector industry - market status analysis and development prospect forecast
- 学习记录:TIM—电容按键检测
- ucorelab4
- Learning record: USART serial communication
- MySQL transactions
- What is "test paper test" in software testing requirements analysis
- STM32 learning record: play with keys to control buzzer and led
- LeetCode#19. Delete the penultimate node of the linked list
- Want to change jobs? Do you know the seven skills you need to master in the interview software test
- JS --- JS function and scope (II)
猜你喜欢
STM32如何使用STLINK下载程序:点亮LED跑马灯(库版本)
ucore lab 6
JS --- detailed explanation of JS facing objects (VI)
What are the commonly used SQL statements in software testing?
学习记录:理解 SysTick系统定时器,编写延时函数
ucore lab 2
Matlab example: two expressions of step function
Learning record: Tim - capacitive key detection
How to build a nail robot that can automatically reply
Leetcode notes - dynamic planning -day6
随机推荐
动态规划前路径问题优化方式
Market trend report, technical innovation and market forecast of lip care products in China and Indonesia
通俗地理解什么是编程语言
JS --- all basic knowledge of JS (I)
Research Report on market supply and demand and strategy of China's land incineration plant industry
动态规划前路径问题
UCORE lab5 user process management experiment report
学习记录:如何进行PWM 输出
Stm32 dossiers d'apprentissage: saisie des applications
Future trend and planning of software testing industry
入门C语言基础问答
基于485总线的评分系统
Introduction to safety testing
Es6--- two methods of capturing promise status as failed
Learning record: Tim - Basic timer
ucore lab 2
学习记录:使用STM32外部输入中断
Brief introduction to libevent
STM32 learning record: play with keys to control buzzer and led
Jupyter installation and use tutorial