Submission #1152616

#TimeUsernameProblemLanguageResultExecution timeMemory
1152616YSH2020Sjeckanje (COCI21_sjeckanje)C++20
0 / 110
1 ms320 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int n; cin >> n;
  int q; cin >> q;
  int a[n]; for (int i = 0; i < n; i++ )cin >> a[i];
  int d[n]; for (int i = 0; i < n-1; i++) d[i] = a[i+1]-a[i];
  d[n-1] = 0;
  while (q--) {
    int s, e, c; cin >> s >> e >> c;
    if (s >= 2) d[s-2]+=c;
    d[e-1]-= c;
    int ans = 0;
    int cur = d[0];
    //two cases: take first, dont take first
    for (int i = 1; i < n-1; i++) {
      if (d[i]*d[i-1] > 0 or cur == 0) {
        //take it
        cur += d[i];
      }
      else {
        ans += abs(cur);
        cur = 0;
      }
    }
    ans += abs(cur);
    //case 2: dont take first
    int ans2 = 0;
    cur = 0;
    for (int i = 1; i < n-1; i++) {
      if (d[i]*d[i-1] > 0 or cur == 0) {
        cur += d[i];
      }
      else {
        ans2 += abs(cur);
        cur = 0;
      }
    }ans2 += abs(cur);
    cout << max(ans, ans2) << '\n';
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...