mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-08 16:18:46 +00:00
802. 区间和
https://www.acwing.com/problem/content/submission/code_detail/10681087/
This commit is contained in:
parent
d14c69a1bf
commit
1aed9faffb
43
AcWing/802/802.cpp
Normal file
43
AcWing/802/802.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
#define endl '\n'
|
||||
|
||||
int n, m, x, c, l, r, data[300005], sum[300005];
|
||||
std::vector<int> nums;
|
||||
std::vector<std::pair<int, int>> adds, queries;
|
||||
|
||||
inline int find(int x) {
|
||||
auto pos = std::lower_bound(nums.begin(), nums.end(), x);
|
||||
return pos == nums.end() || *pos != x ? 0 : pos - nums.begin() + 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> x >> c;
|
||||
nums.push_back(x);
|
||||
adds.push_back(std::make_pair(x, c));
|
||||
}
|
||||
for (int i = 1; i <= m; i++) {
|
||||
cin >> l >> r;
|
||||
queries.push_back(std::make_pair(l, r));
|
||||
nums.push_back(l);
|
||||
nums.push_back(r);
|
||||
}
|
||||
std::sort(nums.begin(), nums.end());
|
||||
nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
|
||||
for (const auto& add : adds) {
|
||||
data[find(add.first)] += add.second;
|
||||
}
|
||||
for (int i = 1; i <= nums.size(); i++) {
|
||||
sum[i] = sum[i - 1] + data[i];
|
||||
}
|
||||
for (const auto& query : queries) {
|
||||
cout << sum[find(query.second)] - sum[find(query.first) - 1] << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user