From 1aed9faffb75a9c145b9a06be08cbe8ba896899a Mon Sep 17 00:00:00 2001 From: Baoshuo Ren Date: Mon, 7 Feb 2022 22:06:00 +0800 Subject: [PATCH] =?UTF-8?q?802.=20=E5=8C=BA=E9=97=B4=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/10681087/ --- AcWing/802/802.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 AcWing/802/802.cpp diff --git a/AcWing/802/802.cpp b/AcWing/802/802.cpp new file mode 100644 index 00000000..130a4180 --- /dev/null +++ b/AcWing/802/802.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +using std::cin; +using std::cout; +#define endl '\n' + +int n, m, x, c, l, r, data[300005], sum[300005]; +std::vector nums; +std::vector> 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; +}