AcWing 802. 区间和->离散化
原题链接
简单
作者:
YMYS
,
2025-04-05 17:36:52
· 河南
,
所有人可见
,
阅读 12
#include<bits/stdc++.h>
#define int long long
#define err cout << "error" << endl
using namespace std;
typedef pair<int,int> PII;
const int N = 3e5 +10;
int n,m;
int a[N];
int b[N];
vector<int> alls;
vector<PII> add, qy;
int find(int k){
int l = 0, r = alls.size()-1;
while (l<r)
{
int mid = l+r>>1;
if(alls[mid]>=k) r = mid;
else l = mid+1;
}
return r+1;
}
signed main()
{
#ifdef ABC
freopen("D:/daily_Coding/VScode-C&C++-Coding/in.in", "r", stdin);
freopen("D:/daily_Coding/VScode-C&C++-Coding/out.out", "w", stdout);
#endif
std::ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
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;
alls.push_back(l), alls.push_back(r);
qy.push_back({l,r});
}
sort(alls.begin(), alls.end());
alls.erase(unique(alls.begin(), alls.end()), alls.end());
for(auto xx : add){
int x = find(xx.first);
a[x] += xx.second;
}
for(int i=1;i<=alls.size();i++){
b[i] = b[i-1] + a[i];
}
for(auto xx : qy){
int l = find(xx.first), r = find(xx.second);
cout << b[r] - b[l-1] << endl;
}
return 0;
}