Submission #369821

# Submission time Handle Problem Language Result Execution time Memory
369821 2021-02-22T13:54:58 Z Sorting Election (BOI18_election) C++17
100 / 100
570 ms 28096 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 3;

int n, q;
string s;

struct Segment_Tree{
    struct Node{
        int prefix, suffix, sum, ans;

        Node(){}
        Node(int x): prefix(max(x, 0)), suffix(max(x, 0)), ans(max(x, 0)), sum(x){}

        friend Node merge(Node l, Node r){
            Node ret;
            ret.sum = l.sum + r.sum;
            ret.prefix = max(l.prefix, r.prefix + l.sum);
            ret.suffix = max(r.suffix, l.suffix + r.sum);
            ret.ans = max(max(l.ans + r.sum, r.ans + l.sum), l.prefix + r.suffix);
            return ret;
        }
    };

    Node nd[4 * N];

    void init(int i = 0, int l = 0, int r = n - 1){
        if(l == r){
            nd[i] = Node((s[l] == 'C') ? -1 : 1);
            return;
        }

        int mid = (l + r) >> 1;
        init(2 * i + 1, l, mid);
        init(2 * i + 2, mid + 1, r);
        nd[i] = merge(nd[2 * i + 1], nd[2 * i + 2]);
    }

    Node query(int l2, int r2, int i = 0, int l = 0, int r = n - 1){
        if(l2 <= l && r <= r2) return nd[i];

        int mid = (l + r) >> 1;
        if(mid >= l2){
            if(mid + 1 <= r2)
                return merge(query(l2, r2, 2 * i + 1, l, mid), query(l2, r2, 2 * i + 2, mid + 1, r));
            return query(l2, r2, 2 * i + 1, l, mid);
        }
        return query(l2, r2, 2 * i + 2, mid + 1, r);
    }
} st;

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

    cin >> n >> s;
    st.init();
    cin >> q;
    for(int i = 0; i < q; ++i){
        int l, r;
        cin >> l >> r;
        --l, --r;
        Segment_Tree::Node ans = st.query(l, r);
        cout << ans.ans << "\n";
    }
}

Compilation message

election.cpp: In constructor 'Segment_Tree::Node::Node(int)':
election.cpp:12:34: warning: 'Segment_Tree::Node::ans' will be initialized after [-Wreorder]
   12 |         int prefix, suffix, sum, ans;
      |                                  ^~~
election.cpp:12:29: warning:   'int Segment_Tree::Node::sum' [-Wreorder]
   12 |         int prefix, suffix, sum, ans;
      |                             ^~~
election.cpp:15:9: warning:   when initialized here [-Wreorder]
   15 |         Node(int x): prefix(max(x, 0)), suffix(max(x, 0)), ans(max(x, 0)), sum(x){}
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 53 ms 5868 KB Output is correct
7 Correct 46 ms 5868 KB Output is correct
8 Correct 48 ms 5740 KB Output is correct
9 Correct 44 ms 5740 KB Output is correct
10 Correct 53 ms 5776 KB Output is correct
11 Correct 67 ms 5932 KB Output is correct
12 Correct 53 ms 5868 KB Output is correct
13 Correct 63 ms 5996 KB Output is correct
14 Correct 77 ms 5868 KB Output is correct
15 Correct 67 ms 5868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 53 ms 5868 KB Output is correct
7 Correct 46 ms 5868 KB Output is correct
8 Correct 48 ms 5740 KB Output is correct
9 Correct 44 ms 5740 KB Output is correct
10 Correct 53 ms 5776 KB Output is correct
11 Correct 67 ms 5932 KB Output is correct
12 Correct 53 ms 5868 KB Output is correct
13 Correct 63 ms 5996 KB Output is correct
14 Correct 77 ms 5868 KB Output is correct
15 Correct 67 ms 5868 KB Output is correct
16 Correct 515 ms 27168 KB Output is correct
17 Correct 476 ms 26624 KB Output is correct
18 Correct 502 ms 26912 KB Output is correct
19 Correct 370 ms 26368 KB Output is correct
20 Correct 558 ms 26192 KB Output is correct
21 Correct 511 ms 27980 KB Output is correct
22 Correct 565 ms 27776 KB Output is correct
23 Correct 570 ms 28096 KB Output is correct
24 Correct 511 ms 27804 KB Output is correct
25 Correct 543 ms 27272 KB Output is correct