Eliminates all except the first element from every consecutive group of equivalent elements from the range [first, last) and returns a past-the-end iterator for the new logical end of the range.
constint N = 3e5+10; vector<int> indexes; vector<pair<int, int>> adds, sums;
int a[N], S[N]; int n, m;
intfind(int x){ int l = 0, r = indexes.size() - 1; while (l < r) { int mid = l+r >> 1; if (indexes[mid] >= x) r = mid; else l = mid+1; } // !important: 因为使用前缀和,所以这里下标从1开始计算 return r+1; }
intmain(){ scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { int x, c; scanf("%d%d", &x, &c); adds.push_back({x, c}); indexes.push_back(x); } for (int i = 0; i < m; i++) { int l, r; scanf("%d%d", &l, &r); sums.push_back({l, r}); indexes.push_back(l); indexes.push_back(r); } // 去重 sort(indexes.begin(), indexes.end()); indexes.erase(unique(indexes.begin(), indexes.end()), indexes.end()); // 将所有加的操作应用到真正的数组上 for (auto add: adds) { int x = find(add.first); a[x] += add.second; } // 计算前缀和 for (int i = 1; i <= indexes.size(); i++) S[i] = S[i-1] + a[i]; // 处理区间和 for (auto sum: sums) { int l = find(sum.first), r = find(sum.second); printf("%d\n", S[r] - S[l-1]); } return0; }