Interval and ------ discretization

Discretization

Satisfies the nature of discretization: large value range, sparse number
a[ ] : { 1 , 3 , 100 , 200 , 500000000 } Mapping
subscript: 0 1 2 3 4
1. There may be repeated elements in a[] to deduplicate
2 .How to calculate the discretized value binary (this question is in order)

interval sum

Suppose there is an infinitely long number line, and the number on each coordinate on the number line is 0. Now, we first perform n operations, and each operation adds c to the number at a certain position x. Next, make m queries, each query contains two integers l and r, and you need to find the sum of all numbers in the interval [l,r]. Input format: The first line contains two integers n and m. Next n lines, each line contains two integers x and c. Next m lines, each line contains two integers l and r. Output format: a total of m lines, each line outputs a sum of numbers in the interval requested in the query. Data range −1e9≤x≤1e9, 1≤n, m≤1e5, −1e9≤l≤r≤1e9, −10000≤c≤10000
Input example:
3 3
1 2
3 6
7 5
1 3
4 6
7 8
Sample output:
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;
}

The unique() function replaces the previously repeated elements with the following elements, and is generally used after sorting (adjacent elements are removed), and the space behind is not deleted

#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 
}


Guess you like

Origin blog.csdn.net/weixin_52879528/article/details/122976645