Submission #681353

# Submission time Handle Problem Language Result Execution time Memory
681353 2023-01-12T20:05:29 Z finn__ Election (BOI18_election) C++17
100 / 100
250 ms 28068 KB
#include <bits/stdc++.h>
using namespace std;

struct node
{
    int l, r, s, a;
};

inline node transition(node const &u, node const &v)
{
    return (node){
        .l = max(u.l, u.s + v.l),
        .r = max(v.r, v.s + u.r),
        .s = u.s + v.s,
        .a = max(u.l + v.r, max(u.a + v.s, v.a + u.s))};
}

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

    size_t n;
    string s;
    cin >> n >> s;

    vector<node> tree(2 * (1 << (32 - __builtin_clz(n))), {0, 0, 0, 0});

    for (size_t i = 0; i < n; i++)
    {
        if (s[i] == 'C')
            tree[tree.size() / 2 + i] = {0, 0, -1, 0};
        else
            tree[tree.size() / 2 + i] = {1, 1, 1, 1};
    }

    for (size_t i = tree.size() / 2 - 1; i; i--)
        tree[i] = transition(tree[2 * i], tree[2 * i + 1]);

    size_t q;
    cin >> q;

    for (size_t z = 0; z < q; z++)
    {
        size_t i, j;
        cin >> i >> j;
        i += tree.size() / 2 - 1, j += tree.size() / 2 - 1;
        node x = {0, 0, 0, 0}, y = {0, 0, 0, 0};

        while (i <= j)
        {
            if (i & 1)
                x = transition(x, tree[i++]);
            if (!(j & 1))
                y = transition(tree[j--], y);

            i >>= 1;
            j >>= 1;
        }

        x = transition(x, y);
        cout << x.a << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 412 KB Output is correct
6 Correct 27 ms 5748 KB Output is correct
7 Correct 27 ms 5708 KB Output is correct
8 Correct 29 ms 5736 KB Output is correct
9 Correct 23 ms 5708 KB Output is correct
10 Correct 28 ms 5708 KB Output is correct
11 Correct 30 ms 5928 KB Output is correct
12 Correct 27 ms 5900 KB Output is correct
13 Correct 28 ms 5900 KB Output is correct
14 Correct 27 ms 5808 KB Output is correct
15 Correct 32 ms 5696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 412 KB Output is correct
6 Correct 27 ms 5748 KB Output is correct
7 Correct 27 ms 5708 KB Output is correct
8 Correct 29 ms 5736 KB Output is correct
9 Correct 23 ms 5708 KB Output is correct
10 Correct 28 ms 5708 KB Output is correct
11 Correct 30 ms 5928 KB Output is correct
12 Correct 27 ms 5900 KB Output is correct
13 Correct 28 ms 5900 KB Output is correct
14 Correct 27 ms 5808 KB Output is correct
15 Correct 32 ms 5696 KB Output is correct
16 Correct 244 ms 26972 KB Output is correct
17 Correct 227 ms 26576 KB Output is correct
18 Correct 232 ms 26868 KB Output is correct
19 Correct 194 ms 26336 KB Output is correct
20 Correct 234 ms 25996 KB Output is correct
21 Correct 244 ms 27776 KB Output is correct
22 Correct 250 ms 27744 KB Output is correct
23 Correct 250 ms 28068 KB Output is correct
24 Correct 242 ms 27588 KB Output is correct
25 Correct 241 ms 27264 KB Output is correct