답안 #524799

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
524799 2022-02-10T04:49:08 Z Evang Sjeckanje (COCI21_sjeckanje) C++17
110 / 110
359 ms 27436 KB
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;

#ifdef _DEBUG
#define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
#define dout(x)
#endif

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define uid(a,b) uniform_int_distribution<int>(a,b)(rng)

#define ins insert
#define ssize(x) (int((x).size()))
#define bs(args...) binary_search(args)
#define lb(args...) lower_bound(args)
#define ub(args...) upper_bound(args)
#define all(x) (x).begin(),(x).end()
#define mp(a, b) make_pair(a, b)
#define mt(args...) make_tuple(args)
#define pb(x) push_back(x)
#define eb(args...) emplace_back(args)
#define ff first
#define ss second
#define die exit(0)

template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp>
using pq = std::priority_queue<T, vc<T>, Comp>;
template<typename T>
using maxpq = pq<T, less<T>>;
template<typename T>
using minpq = pq<T, greater<T>>;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vc<int>;
using vll = vc<ll>;
using vpi = vc<pi>;
using vpll = vc<pll>;
using str = string;

constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int inf = 0x3f3f3f3f;
constexpr ll llinf = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------


const int N = 8e5+5;
int n, n2, q, m[N];
ll x[N], a[N], b[N], c[N], d[N];

int sign(ll x){
    if(x==0)
        return 0;
    if(x<0)
        return -1;
    return 1;
}

void pull(int i){
    a[i] = max({b[2*i]+c[2*i+1], b[2*i]+a[2*i+1], a[2*i]+c[2*i+1]});
    b[i] = max({b[2*i]+d[2*i+1], b[2*i]+b[2*i+1], a[2*i]+d[2*i+1]});
    c[i] = max({d[2*i]+c[2*i+1], d[2*i]+a[2*i+1], c[2*i]+c[2*i+1]});
    d[i] = max({d[2*i]+b[2*i+1], c[2*i]+d[2*i+1], d[2*i]+d[2*i+1]});

    if(m[i]+1<n+n2 && sign(x[m[i]]) != sign(x[m[i]+1]))
//    if(x[m[i]]>0&&x[m[i]+1]<0 || x[m[i]]<0&&x[m[i]+1]>0)
        return;

    a[i] = max(a[i], a[2*i]+a[2*i+1]);
    b[i] = max(b[i], a[2*i]+b[2*i+1]);
    c[i] = max(c[i], c[2*i]+a[2*i+1]);
    d[i] = max(d[i], c[2*i]+b[2*i+1]);
}

void evan(int i, int l, int r){
    if(i>=n)
        return;
    m[i] = (l+r)/2;
    evan(2*i, l, m[i]);
    evan(2*i+1, m[i]+1, r);
}

void upd(int i, int v){
    x[i] += v;
    a[i] = abs(x[i]);
    for(i /= 2; i > 0; i /= 2)
        pull(i);
}

void upd(int l, int r, int v){
    l += n-1;
    r += n-1;
    if(l>n)
        upd(l-1, -v);
    if(r<n+n2)
        upd(r, v);
}

signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cout << fixed; clog << fixed; clog << unitbuf;
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#else
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
#endif

    cin >> n2 >> q;
    if(n2==1){
        while(q--)
            cout << 0 << el;
        die;
    }

    --n2;
    n = 1;
    while(n<n2)
        n *= 2;
    evan(1, n, 2*n-1);

    for(int i = n; i <= n+n2; ++i)
        cin >> x[i];
    for(int i = n; i < n+n2; ++i) {
        x[i] -= x[i + 1];
        a[i] = abs(x[i]);
        b[i] = c[i] = -inf;
    }
    for(int i = n+n2; i < 2*n; ++i){
        x[i] = 0;
        a[i] = 0;
        b[i] = c[i] = -inf;
    }

    for(int i = n-1; i > 0; --i)
        pull(i);

    while(q--){
        int l, r, v;
        cin >> l >> r >> v;
        upd(l, r, v);
        cout << max({a[1], b[1], c[1], d[1]}) << el;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 3 ms 692 KB Output is correct
8 Correct 4 ms 716 KB Output is correct
9 Correct 3 ms 716 KB Output is correct
10 Correct 3 ms 716 KB Output is correct
11 Correct 3 ms 716 KB Output is correct
12 Correct 3 ms 708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 3 ms 692 KB Output is correct
8 Correct 4 ms 716 KB Output is correct
9 Correct 3 ms 716 KB Output is correct
10 Correct 3 ms 716 KB Output is correct
11 Correct 3 ms 716 KB Output is correct
12 Correct 3 ms 708 KB Output is correct
13 Correct 350 ms 26960 KB Output is correct
14 Correct 352 ms 26920 KB Output is correct
15 Correct 359 ms 26988 KB Output is correct
16 Correct 347 ms 26776 KB Output is correct
17 Correct 343 ms 26792 KB Output is correct
18 Correct 298 ms 27436 KB Output is correct