답안 #476906

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
476906 2021-09-29T03:28:37 Z Lam_lai_cuoc_doi Feast (NOI19_feast) C++17
100 / 100
206 ms 118464 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <class T>
void read(T &x)
{
    x = 0;
    register int c;
    while ((c = getchar()) && (c > '9' || c < '0'))
        ;
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
}

constexpr bool typetest = 0;

constexpr int N = 3e5 + 5;
constexpr ll Inf = 1e17;

#define Sum(x, y) ((x == -Inf || y == -Inf) ? -Inf : (x + y))

struct node
{
    ll ans, pre, suf, sum;
    int l, r, rpre, lsuf;
    node(ll v = -Inf, int x = 0, int y = 0)
    {
        ans = pre = suf = sum = v;
        l = x;
        r = y;
        rpre = y;
        lsuf = x;
    }
    friend node Combine(const node &a, const node &b)
    {
        node ans(-Inf, 0, 0);
        ans.sum = Sum(a.sum, b.sum);
        ans.pre = a.pre;
        ans.rpre = a.rpre;
        ans.suf = b.suf;
        ans.lsuf = b.lsuf;

        if (a.ans > b.ans)
        {
            ans.ans = a.ans;
            ans.l = a.l;
            ans.r = a.r;
        }
        else
        {
            ans.ans = b.ans;
            ans.l = b.l;
            ans.r = b.r;
        }

        if (ans.ans < Sum(a.suf, b.pre))
        {
            ans.ans = Sum(a.suf, b.pre);
            ans.l = a.lsuf;
            ans.r = b.rpre;
        }

        if (ans.pre < Sum(a.sum, b.pre))
        {
            ans.pre = Sum(a.sum, b.pre);
            ans.rpre = b.rpre;
        }
        if (ans.suf < Sum(a.suf, b.sum))
        {
            ans.suf = Sum(a.suf, b.sum);
            ans.lsuf = a.lsuf;
        }

        return ans;
    }
};

struct SegmentTree
{
    vector<node> st, rv;
    vector<bool> lazy;
    int n;

    SegmentTree()
    {
    }

    void Assign(int n)
    {
        this->n = n;
        st.resize(n * 4 + 2);
        rv.resize(n * 4 + 2);
        lazy.assign(n * 4 + 2, 0);
    }

    void Build(int id, int l, int r, ll a[N])
    {
        if (l == r)
        {
            st[id] = node(a[l], l, l);
            rv[id] = node(-a[l], l, l);
            return;
        }
        Build(id << 1, l, (l + r) / 2, a);
        Build(id << 1 | 1, (l + r) / 2 + 1, r, a);
        st[id] = Combine(st[id << 1], st[id << 1 | 1]);
        rv[id] = Combine(rv[id << 1], rv[id << 1 | 1]);
    }

    void Build(ll a[N])
    {
        Build(1, 1, n, a);
    }

    void Push(int id)
    {
        if (lazy[id])
        {
            swap(st[id << 1], rv[id << 1]);
            lazy[id << 1] = !lazy[id << 1];

            swap(st[id << 1 | 1], rv[id << 1 | 1]);
            lazy[id << 1 | 1] = !lazy[id << 1 | 1];

            lazy[id] = 0;
        }
    }

    void Update(int id, int l, int r, const int &a, const int &b)
    {
        if (l > b || r < a)
            return;
        if (l >= a && r <= b)
        {
            swap(rv[id], st[id]);
            lazy[id] = !lazy[id];
            return;
        }
        Push(id);

        Update(id << 1, l, (l + r) / 2, a, b);
        Update(id << 1 | 1, (l + r) / 2 + 1, r, a, b);

        st[id] = Combine(st[id << 1], st[id << 1 | 1]);
        rv[id] = Combine(rv[id << 1], rv[id << 1 | 1]);
    }

    void Update(int l, int r)
    {
        Update(1, 1, n, l, r);
    }
} f;

int n, k;
ll a[N];

void Read()
{
    //freopen("test.INP", "r", stdin);
    //freopen("test.OUT", "w", stdout);
    cin >> n >> k;

    for (int i = 1; i <= n; ++i)
        cin >> a[i];
}

void Solve()
{
    f.Assign(n);
    f.Build(a);

    ll ans(0);

    while (k--)
    {
        if (f.st[1].ans < 0)
            break;
        ans += f.st[1].ans;
        f.Update(f.st[1].l, f.st[1].r);
    }

    cout << ans;
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t(1);
    if (typetest)
        cin >> t;
    for (int _ = 1; _ <= t; ++_)
    {
        //cout << "Case #" << _ << ": ";
        Read();
        Solve();
    }
    //cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}

Compilation message

feast.cpp: In function 'void read(T&)':
feast.cpp:12:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 |     register int c;
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 112068 KB Output is correct
2 Correct 102 ms 114140 KB Output is correct
3 Correct 95 ms 115268 KB Output is correct
4 Correct 95 ms 114464 KB Output is correct
5 Correct 93 ms 113948 KB Output is correct
6 Correct 91 ms 111984 KB Output is correct
7 Correct 93 ms 111876 KB Output is correct
8 Correct 109 ms 114268 KB Output is correct
9 Correct 121 ms 112204 KB Output is correct
10 Correct 97 ms 113348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 112644 KB Output is correct
2 Correct 93 ms 115232 KB Output is correct
3 Correct 88 ms 112552 KB Output is correct
4 Correct 152 ms 113516 KB Output is correct
5 Correct 98 ms 114820 KB Output is correct
6 Correct 89 ms 113680 KB Output is correct
7 Correct 181 ms 116636 KB Output is correct
8 Correct 99 ms 117544 KB Output is correct
9 Correct 92 ms 114500 KB Output is correct
10 Correct 133 ms 115828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 114188 KB Output is correct
2 Correct 101 ms 112824 KB Output is correct
3 Correct 97 ms 113876 KB Output is correct
4 Correct 97 ms 112728 KB Output is correct
5 Correct 112 ms 113732 KB Output is correct
6 Correct 99 ms 114584 KB Output is correct
7 Correct 113 ms 114972 KB Output is correct
8 Correct 101 ms 113988 KB Output is correct
9 Correct 101 ms 115376 KB Output is correct
10 Correct 105 ms 114888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 324 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 320 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 324 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 320 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 1100 KB Output is correct
22 Correct 1 ms 1100 KB Output is correct
23 Correct 1 ms 1108 KB Output is correct
24 Correct 1 ms 1100 KB Output is correct
25 Correct 1 ms 1124 KB Output is correct
26 Correct 1 ms 1100 KB Output is correct
27 Correct 1 ms 1104 KB Output is correct
28 Correct 1 ms 1100 KB Output is correct
29 Correct 1 ms 1112 KB Output is correct
30 Correct 2 ms 976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 112068 KB Output is correct
2 Correct 102 ms 114140 KB Output is correct
3 Correct 95 ms 115268 KB Output is correct
4 Correct 95 ms 114464 KB Output is correct
5 Correct 93 ms 113948 KB Output is correct
6 Correct 91 ms 111984 KB Output is correct
7 Correct 93 ms 111876 KB Output is correct
8 Correct 109 ms 114268 KB Output is correct
9 Correct 121 ms 112204 KB Output is correct
10 Correct 97 ms 113348 KB Output is correct
11 Correct 86 ms 112644 KB Output is correct
12 Correct 93 ms 115232 KB Output is correct
13 Correct 88 ms 112552 KB Output is correct
14 Correct 152 ms 113516 KB Output is correct
15 Correct 98 ms 114820 KB Output is correct
16 Correct 89 ms 113680 KB Output is correct
17 Correct 181 ms 116636 KB Output is correct
18 Correct 99 ms 117544 KB Output is correct
19 Correct 92 ms 114500 KB Output is correct
20 Correct 133 ms 115828 KB Output is correct
21 Correct 109 ms 114188 KB Output is correct
22 Correct 101 ms 112824 KB Output is correct
23 Correct 97 ms 113876 KB Output is correct
24 Correct 97 ms 112728 KB Output is correct
25 Correct 112 ms 113732 KB Output is correct
26 Correct 99 ms 114584 KB Output is correct
27 Correct 113 ms 114972 KB Output is correct
28 Correct 101 ms 113988 KB Output is correct
29 Correct 101 ms 115376 KB Output is correct
30 Correct 105 ms 114888 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 0 ms 332 KB Output is correct
36 Correct 0 ms 332 KB Output is correct
37 Correct 1 ms 320 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 0 ms 332 KB Output is correct
40 Correct 0 ms 324 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 324 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 0 ms 320 KB Output is correct
47 Correct 1 ms 320 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 1100 KB Output is correct
52 Correct 1 ms 1100 KB Output is correct
53 Correct 1 ms 1108 KB Output is correct
54 Correct 1 ms 1100 KB Output is correct
55 Correct 1 ms 1124 KB Output is correct
56 Correct 1 ms 1100 KB Output is correct
57 Correct 1 ms 1104 KB Output is correct
58 Correct 1 ms 1100 KB Output is correct
59 Correct 1 ms 1112 KB Output is correct
60 Correct 2 ms 976 KB Output is correct
61 Correct 180 ms 115580 KB Output is correct
62 Correct 206 ms 118464 KB Output is correct
63 Correct 128 ms 115152 KB Output is correct
64 Correct 172 ms 117828 KB Output is correct
65 Correct 158 ms 117444 KB Output is correct
66 Correct 175 ms 117116 KB Output is correct
67 Correct 139 ms 115416 KB Output is correct
68 Correct 104 ms 117700 KB Output is correct
69 Correct 99 ms 115396 KB Output is correct
70 Correct 98 ms 114756 KB Output is correct