#include<bits/stdc++.h> using namespace std; #define FastIOS ios::sync_with_stdio(false), cout.tie(0), cin.tie(0) #define int long long int n, m, i, j, k, t = 0; constint N = 1e6 + 10; typedefpair<int, int> PII; int a[N]; //存储坐标插入的值 int sum[N]; //存储数组a的前缀和 vector<int> alls; //存储(所有与插入和查询有关的)坐标 vector<PII> add, query; //存储插入和询问操作的数据
intfind(int x){ //返回的是输入的坐标的离散化下标 int l = 0, r = alls.size() - 1; while(l < r){ int mid = l + (r - l >> 1); if(alls[mid] >= x) r = mid; else l = mid + 1; } return r + 1; }
voidsolve() { cin >> n >> m; for(i = 0; i < n; i++){ int x, c; cin >> x >> c; add.push_back({x, c}); alls.push_back(x); }
for(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()); for(auto item : add){ int x = find(item.first); a[x] += item.second; } for(i = 1; i <= alls.size(); i++) sum[i] = sum[i - 1] + a[i]; for(auto item : query){ int l = find(item.first), r = find(item.second); cout << sum[r] - sum[l - 1] << '\n'; } }
signedmain() { FastIOS; solve(); return0; } // Thank U