Submission #1098594

# Submission time Handle Problem Language Result Execution time Memory
1098594 2024-10-09T16:03:01 Z duytuandao21 Sjeckanje (COCI21_sjeckanje) C++17
110 / 110
383 ms 29520 KB
#include<bits/stdc++.h>
using namespace std;
 
const int N = 2e6 + 7;
const int inf = 1e9 + 7;
const long long infll = 1e18 + 7;
 
int n, q;
long long a[N], b[N];
long long seg[4 * N][2][2];
 
void update(int id, int l, int r, int pos, int v) {
    if (r < pos ||  l > pos) return;
    if (l == r) {
        if (l == pos) b[l] += v;
        seg[id][1][1] = abs(b[l]);
        seg[id][0][0] = 0;
        return;
    }
    int mid = (l + r) / 2;
    update(id * 2, l, mid, pos, v);
    update(id * 2 + 1, mid + 1, r, pos, v);
    int x = id * 2, y = id * 2 + 1;
    //merge two segment;
    seg[id][0][0] = max(seg[x][0][0] + seg[y][0][0], max(seg[x][0][1] + seg[y][0][0], seg[x][0][0] + seg[y][1][0]));
    seg[id][1][0] = max(seg[x][1][0] + seg[y][0][0], max(seg[x][1][1] + seg[y][0][0], seg[x][1][0] + seg[y][1][0]));
    seg[id][0][1] = max(seg[x][0][0] + seg[y][0][1], max(seg[x][0][1] + seg[y][0][1], seg[x][0][0] + seg[y][1][1]));
    seg[id][1][1] = max(seg[x][1][0] + seg[y][0][1], max(seg[x][1][1] + seg[y][0][1], seg[x][1][0] + seg[y][1][1]));
    if ((b[mid] < 0 && b[mid + 1] < 0) || (b[mid] > 0 && b[mid + 1] > 0)) {
        seg[id][0][0] = max(seg[id][0][0], seg[x][0][1] + seg[y][1][0]);
        seg[id][1][0] = max(seg[id][0][0], seg[x][1][1] + seg[y][1][0]);
        seg[id][0][1] = max(seg[id][0][0], seg[x][0][1] + seg[y][1][1]);
        seg[id][1][1] = max(seg[id][0][0], seg[x][1][1] + seg[y][1][1]);
    }
}
int main() 
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i < n; i++) {
        update(1, 1, n - 1, i, a[i] - a[i + 1]);
    }
    while (q--) {
        int l, r;
        long long v; 
        cin >> l >> r >> v;
        update(1, 1, n - 1, l - 1, -v);
        update(1, 1, n - 1, r, v);
        cout << seg[1][1][1];
        if (q > 0) cout << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 5 ms 868 KB Output is correct
8 Correct 5 ms 856 KB Output is correct
9 Correct 4 ms 732 KB Output is correct
10 Correct 4 ms 860 KB Output is correct
11 Correct 5 ms 860 KB Output is correct
12 Correct 4 ms 908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 5 ms 868 KB Output is correct
8 Correct 5 ms 856 KB Output is correct
9 Correct 4 ms 732 KB Output is correct
10 Correct 4 ms 860 KB Output is correct
11 Correct 5 ms 860 KB Output is correct
12 Correct 4 ms 908 KB Output is correct
13 Correct 383 ms 29012 KB Output is correct
14 Correct 375 ms 29008 KB Output is correct
15 Correct 373 ms 29068 KB Output is correct
16 Correct 327 ms 28976 KB Output is correct
17 Correct 328 ms 28752 KB Output is correct
18 Correct 279 ms 29520 KB Output is correct