Loading... ```cpp #include<bits/stdc++.h> using namespace std; const int N = 100; typedef pair<int, int> PII; int n, m; int arr[N], sum[N]; vector<int> alls; vector<PII> add, query; // 查找x在alls数组中的下标 int find(int x){ int l = 0, r = alls.size() - 1, mid; while(l < r){ mid = l + r >> 1; if(alls[mid] >= x) r = mid; else l = mid + 1; } return l + 1; } int main(){ cin >> n >> m; int x, c; for(int i = 0; i < n; i++){ cin >> x >> c; add.push_back({x, c}); alls.push_back(x); } int l, r; for(int i = 0; i < m; i++){ 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) arr[find(item.first)] += item.second; // 处理前缀和 for(int i = 1; i <= alls.size(); i++) sum[i] = sum[i - 1] + arr[i]; for(auto item : query) printf("%d\n", sum[find(item.second)] - sum[find(item.first) - 1]); return 0; } ``` Last modification:January 20, 2023 © Allow specification reprint Like 0 如果觉得我的文章对你有用,请随意赞赏
Comment here is closed