Submission #712733

# Submission time Handle Problem Language Result Execution time Memory
712733 2023-03-19T15:51:10 Z Johann Sjeckanje (COCI21_sjeckanje) C++14
55 / 110
2000 ms 5696 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
#define sz(x) (int)(x).size()

int k;
struct item
{
    int first, last;
    vvi cnt;
    item()
    {
        first = last = -1;
        cnt.assign(k, vi(k, 0));
    }
};

struct segtree
{
    int size;
    vector<item> arr;
    vi op;
    void create(int x, int c, int len)
    {
        arr[x].first = arr[x].last = c;
        for (int i = 0; i < sz(arr[x].cnt); ++i)
            for (int j = 0; j < sz(arr[x].cnt.front()); ++j)
                arr[x].cnt[i][j] = 0;
        if (c != -1)
            arr[x].cnt[c][c] = len - 1;
    }
    void combine(int x, item &a, item &b)
    {
        arr[x].first = a.first, arr[x].last = b.last;
        for (int i = 0; i < sz(arr[x].cnt); ++i)
            for (int j = 0; j < sz(arr[x].cnt.front()); ++j)
                arr[x].cnt[i][j] = a.cnt[i][j] + b.cnt[i][j];
        if (a.last != -1 && b.first != -1)
            ++arr[x].cnt[a.last][b.first];
    }
    void apply(int x, int len)
    {
        if (op[x] == -1)
            return;
        create(x, op[x], len);
        if (x < size)
            op[2 * x] = op[2 * x + 1] = op[x];
        op[x] = -1;
    }
    void init(string &s)
    {
        size = 1;
        while (size < sz(s))
            size *= 2;
        op.assign(2 * size, -1);
        arr.resize(2 * size);
        for (int i = 0; i < sz(s); ++i)
            create(i + size, s[i] - 'a', 1);
        for (int i = size - 1; i > 0; --i)
            combine(i, arr[2 * i], arr[2 * i + 1]);
    }
    void set(int l, int r, int c)
    {
        set(l, r, c, 1, 0, size);
    }
    void set(int l, int r, int c, int x, int lx, int rx)
    {
        apply(x, rx - lx);
        if (l <= lx && rx <= r)
        {
            op[x] = c;
            apply(x, rx - lx);
            return;
        }
        if (r <= lx || rx <= l)
            return;
        int m = (lx + rx) / 2;
        set(l, r, c, 2 * x, lx, m);
        set(l, r, c, 2 * x + 1, m, rx);
        combine(x, arr[2 * x], arr[2 * x + 1]);
    }
    int query(string &per)
    {
        vi pos(sz(per));
        for (int i = 0; i < sz(per); ++i)
            pos[per[i] - 'a'] = i;
        int ans = 1; // initial thing
        for (int i = 0; i < sz(arr[1].cnt); ++i)
            for (int j = 0; j < sz(arr[1].cnt.front()); ++j)
                if (pos[i] >= pos[j])
                    ans += arr[1].cnt[i][j];
        return ans;
    }
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N, Q;
    cin >> N >> Q;
    vi A(N);
    for (int i = 0; i < N; ++i)
        cin >> A[i];

    for (int q = 0; q < Q; ++q)
    {
        ll l, r, x;
        cin >> l >> r >> x;
        --l;
        for (int i = l; i < r; ++i)
            A[i] += x;

        vi dp(N, 0);
        dp[0] = 0;
        dp[1] = abs(A[0] - A[1]);
        for (int i = 2; i < N; ++i)
        {
            ll delta = abs(A[i] - A[i - 1]);
            if ((A[i - 2] <= A[i - 1] && A[i - 1] <= A[i]) || (A[i - 2] >= A[i - 1] && A[i - 1] >= A[i]))
                dp[i] = dp[i - 1] + delta;
            else
                dp[i] = max(dp[i - 1], dp[i - 2] + delta);
        }
        cout << dp.back() << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 36 ms 440 KB Output is correct
8 Correct 37 ms 464 KB Output is correct
9 Correct 34 ms 480 KB Output is correct
10 Correct 32 ms 472 KB Output is correct
11 Correct 34 ms 468 KB Output is correct
12 Correct 37 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 36 ms 440 KB Output is correct
8 Correct 37 ms 464 KB Output is correct
9 Correct 34 ms 480 KB Output is correct
10 Correct 32 ms 472 KB Output is correct
11 Correct 34 ms 468 KB Output is correct
12 Correct 37 ms 468 KB Output is correct
13 Execution timed out 2059 ms 5696 KB Time limit exceeded
14 Halted 0 ms 0 KB -