Files
python/TangDou/AcWing/Discretization/802_3_STL.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

54 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 300010;
int a[N], al;
int b[N], s[N]; // 假定有一个无限长的数轴,数轴上每个坐标上的数都是 0
PII q[N], p[N];
int ql, pl;
int n, m;
int main() {
cin >> n >> m;
while (n--) {
int x, c;
cin >> x >> c;
p[pl++] = {x, c};
a[al++] = x;
}
int l, r;
while (m--) {
cin >> l >> r;
q[ql++] = {l, r};
a[al++] = l, a[al++] = r;
}
// ① 排序+去重
sort(a, a + al);
// ② 使用STL的去重函数去重,不用手写的去重,原因:只排序一次,去重一次,不像是二分需要重复使用,性能差别不大,但代码就短的多
al = unique(a, a + al) - a;
// 处理一下某个x上加c的事情
for (int i = 0; i < pl; i++) {
int x = lower_bound(a,a+ al, p[i].first)-a + 1; // 下标从0开始,需要加1个偏移量
b[x] += p[i].second;
}
// 一维前缀和
for (int i = 1; i < N; i++) s[i] = s[i - 1] + b[i];
// 处理询问(前缀和应用)
for (int i = 0; i < ql; i++) {
// 根据原来的位置值,计算出映射后的位置值
l = lower_bound(a, a+ al, q[i].first)-a + 1;
r = lower_bound(a, a+ al, q[i].second)-a + 1;
// 利用一维前缀和计算区间和
printf("%d\n", s[r] - s[l - 1]);
}
return 0;
}