Submission #1107175

# Submission time Handle Problem Language Result Execution time Memory
1107175 2024-10-31T19:22:37 Z farica Election (BOI18_election) C++14
100 / 100
1216 ms 52724 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int MAX_N = 5e5;
const int MOD = 1e9 + 7;
const int INF = 1e9;

struct Node {
    int L, R, A, S;
};

string s;

Node segm[4*MAX_N];

Node comb(Node a, Node b) {
    Node c;
    c.S = a.S + b.S;
    c.L = max(a.L, a.S + b.L);
    c.R = max(b.R, b.S + a.R);
    c.A = max(max(a.S + b.A, b.S + a.A), a.L + b.R);
    return c;
}

void build(int pos, int l, int r) {
    if(l==r) {
        if(s[l] == 'C') {
            segm[pos].S = -1;
            segm[pos].L = segm[pos].R = segm[pos].A = 0;
        } else {
            segm[pos].S = segm[pos].R = segm[pos].L = segm[pos].A = 1;
        }
        return;
    }
    int m = (l+r)/2;
    build(2*pos+1, l, m);
    build(2*pos+2, m+1, r);
    segm[pos] = comb(segm[2*pos+1], segm[2*pos+2]);
}

Node query(int pos, int l, int r, int L, int R) {
    if(l > R or r < L) {
        Node tmp;
        tmp.A = tmp.L = tmp.R = tmp.S = 0;
        return tmp;
    } else if(l >= L && r <= R) return segm[pos];
    int m = (l+r)/2;
    return comb(query(2*pos+1, l, m, L, R), query(2*pos+2, m+1, r, L, R));
}

void solve() {
    int n, q;
    cin >> n >> s >> q;
    build(1, 0, n-1);
    while(q--) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        Node ans = query(1, 0, n-1, l, r);
        cout << ans.A << endl;
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t = 1;
    while(t--) solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2640 KB Output is correct
2 Correct 5 ms 2640 KB Output is correct
3 Correct 4 ms 2524 KB Output is correct
4 Correct 5 ms 2640 KB Output is correct
5 Correct 5 ms 2808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2640 KB Output is correct
2 Correct 5 ms 2640 KB Output is correct
3 Correct 4 ms 2524 KB Output is correct
4 Correct 5 ms 2640 KB Output is correct
5 Correct 5 ms 2808 KB Output is correct
6 Correct 146 ms 14240 KB Output is correct
7 Correct 161 ms 14004 KB Output is correct
8 Correct 138 ms 14004 KB Output is correct
9 Correct 139 ms 13976 KB Output is correct
10 Correct 142 ms 14152 KB Output is correct
11 Correct 161 ms 14152 KB Output is correct
12 Correct 151 ms 14260 KB Output is correct
13 Correct 137 ms 14152 KB Output is correct
14 Correct 154 ms 14164 KB Output is correct
15 Correct 162 ms 14268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2640 KB Output is correct
2 Correct 5 ms 2640 KB Output is correct
3 Correct 4 ms 2524 KB Output is correct
4 Correct 5 ms 2640 KB Output is correct
5 Correct 5 ms 2808 KB Output is correct
6 Correct 146 ms 14240 KB Output is correct
7 Correct 161 ms 14004 KB Output is correct
8 Correct 138 ms 14004 KB Output is correct
9 Correct 139 ms 13976 KB Output is correct
10 Correct 142 ms 14152 KB Output is correct
11 Correct 161 ms 14152 KB Output is correct
12 Correct 151 ms 14260 KB Output is correct
13 Correct 137 ms 14152 KB Output is correct
14 Correct 154 ms 14164 KB Output is correct
15 Correct 162 ms 14268 KB Output is correct
16 Correct 1183 ms 51676 KB Output is correct
17 Correct 1110 ms 51236 KB Output is correct
18 Correct 1158 ms 51560 KB Output is correct
19 Correct 1039 ms 51100 KB Output is correct
20 Correct 1197 ms 50972 KB Output is correct
21 Correct 1147 ms 52520 KB Output is correct
22 Correct 1165 ms 52580 KB Output is correct
23 Correct 1143 ms 52724 KB Output is correct
24 Correct 1203 ms 52484 KB Output is correct
25 Correct 1216 ms 52024 KB Output is correct