Submission #646729

# Submission time Handle Problem Language Result Execution time Memory
646729 2022-09-30T13:10:27 Z bojackduy Sjeckanje (COCI21_sjeckanje) C++14
0 / 110
72 ms 188108 KB
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <string.h>
#include <functional>
#define task ""
#define size() size() * 1ll 
#define all(x) (x).begin(), (x).end()
#define allr(x, sz) (x) + 1, (x) + 1 + sz
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define MASK(x) (1LL << (x))
#define BIT(x,i) (((x) >> (i)) & 1)
#define numbit(x) __builtin_popcountll(x)

using namespace std;

const int N = 1e6 + 1;
const int M = 1e3 + 1;
const long long mod = 1e9 + 7;
const long long oo = 1e18 + 7;

typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;

template<class t>
bool mini(t &x,t y) {
    if (y < x) {
       x = y;
       return 1;
    }
return 0;
}
template<class t>
bool maxi(t &x,t y) {
    if (x < y) {
       x = y;
       return 1;
    }
return 0;
}
int n, m;
int a[N];
ll lazy[N];
struct node {
    ll maxlef, minlef, maxrig, minrig, ans;
    bool ok;
    node(const int &_x = 0) {
        maxlef = minlef = maxrig = minrig = _x;
        ans = 0;
        ok = 1;
    }
} st[4 * N];
node merge(node lef, node rig) {
    node res(0);
    ll cmax = max(lef.maxrig, rig.maxlef);
    ll cmin = min(lef.minrig, rig.minlef);

    res.maxlef = lef.maxlef;
    res.minlef = lef.minlef;

    res.maxrig = rig.maxrig;
    res.minrig = rig.minrig;

    if (cmax - cmin > lef.maxrig - lef.minrig + rig.maxlef - rig.minlef) {
        res.ans = lef.ans - lef.maxrig + lef.minrig + rig.ans - rig.maxlef + rig.minlef + cmax - cmin;
        if (lef.ok) {
            maxi(res.maxlef, cmax);
            mini(res.minlef, cmin);
        }
        if (rig.ok) {
            maxi(res.maxrig, cmax);
            mini(res.minrig, cmin);
        }
    } else {
        res.ans = lef.ans + rig.ans;
        res.ok = 0;
    }
return res;
}
void build(int id, int l, int r) {
    if (l == r) {
        st[id] = node(a[l]);
    return ;
    }
    int mid = l + r >> 1;
    if (l <= mid) build(id << 1, l, mid);
    if (mid + 1 <= r) build(id << 1 | 1, mid + 1, r);
    st[id] = merge(st[id << 1], st[id << 1 | 1]);
}
void add(int id, ll &val) {
    st[id].maxlef += val;
    st[id].minlef += val;
    st[id].maxrig += val;
    st[id].minrig += val;
    lazy[id] += val;
}
void down(int &id, int &l, int &r) {
    ll &x = lazy[id];
    add(id << 1, x);

    add(id << 1 | 1, x);
    x = 0;
}
void adj(int id, int l, int r, int u, int v, ll val) {
    if (r < u || v < l) return;
    if (u <= l && r <= v) {
        add(id, val);
    return;
    }
    int mid = l + r >> 1;
    down(id, l, r);
    adj(id << 1, l, mid, u, v, val);
    adj(id << 1 | 1, mid + 1, r, u, v, val);
    st[id] = merge(st[id << 1], st[id << 1 | 1]);
}
void solve(int test = -1) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    build(1, 1, n);
    while (m--) {
        int l, r;
        ll v;
        cin >> l >> r >> v;
        adj(1, 1, n, l, r, v);
        cout << st[1].ans << '\n';
    }
}

int32_t main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    // freopen(task".inp", "r", stdin);
    // freopen(task".out", "w", stdout);

    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++) {
        solve(i);
    }
return 0;
}
/*

*/

Compilation message

Main.cpp: In function 'void build(int, int, int)':
Main.cpp:90:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   90 |     int mid = l + r >> 1;
      |               ~~^~~
Main.cpp: In function 'void adj(int, int, int, int, int, ll)':
Main.cpp:115:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  115 |     int mid = l + r >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 72 ms 188108 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 72 ms 188108 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 72 ms 188108 KB Output isn't correct
2 Halted 0 ms 0 KB -