https://www.acwing.com/problem/content/description/804/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
typedef pair<int,int>PII;
const int N = 300010;//n+2m个坐标数量,即最多用3*1e5次方
vector<PII>add,query;//存储操作
vector<int>alls;//离散化后的下标
int a[N];//存储值
int s[N];
int n,m;
int find(int x)//寻找下标离散化后的结果
{
int l=0,r=alls.size()-1;
while(l<r)
{
int mid=l+r>>1;
if(alls[mid]>=x)r=mid;
else l=mid+1;
}
return r+1;//下标从1开始
}
int main()
{
cin >> n >> m;
for(int i=0;i<n;i++)
{
int x,c;
cin >> x >> c;
add.push_back({x,c});
alls.push_back(x);
}
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());//unique删除重复元素,返回剩下元素的结尾位置
//这样便实现了离散化,映射成功
for(auto item:add)
{
int x=find(item.first);
a[x]+=item.second;
}
for(int i=1;i<=alls.size();i++)s[i]=s[i-1]+a[i];
for(auto item:query)
{
int l=find(item.first),r=find(item.second);
cout << s[l]-s[r-1] << endl;
}
}
标签:int,back,离散,alls,push,include,模板 From: https://www.cnblogs.com/lxl-233/p/17406335.html