Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct SegmentAdder {
- vector<int> diffArray;
- int initArrayLen;
- vector<int> findDiffsArray(const vector<int>& arr) {
- int n = arr.size();
- vector<int> diffs(n - 1);
- for (int i = 0; i < n - 1; i++) {
- diffs[i] = arr[i + 1] - arr[i];
- }
- return diffs;
- }
- SegmentAdder(vector<int> b) {
- initArrayLen = b.size();
- b.insert(b.begin(), 0); // add leading zero
- diffArray = findDiffsArray(b);
- }
- vector<int> findPrefixSums(const vector<int>& a) {
- int n = a.size();
- vector<int> prefixSums(n + 1, 0);
- for (int i = 0; i < n; i++) {
- prefixSums[i + 1] = prefixSums[i] + a[i];
- }
- return prefixSums;
- }
- void addOnSegment(int left, int right, int d) { // [left, right) += d
- diffArray[left] += d;
- if (right < initArrayLen) {
- diffArray[right] -= d;
- }
- }
- vector<int> getArray() {
- vector<int> finalB = findPrefixSums(diffArray);
- finalB.erase(finalB.begin()); // delete leading zero
- return finalB;
- }
- };
- int main() {
- int n;
- cin >> n;
- vector<int> b(n);
- for (int &val : b) {
- cin >> val;
- }
- SegmentAdder myAdder(b);
- int queriesNumber;
- cin >> queriesNumber;
- for (int i = 0; i < queriesNumber; i++) {
- int l, r;
- int d;
- cin >> l >> r >> d;
- l--;
- myAdder.addOnSegment(l, r, d);
- }
- vector<int> finalB = myAdder.getArray();
- for (int val : finalB) {
- cout << val << ' ';
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement