답안 #861726

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
861726 2023-10-16T19:30:15 Z dbence Election (BOI18_election) C++14
100 / 100
483 ms 58100 KB
#include <bits/stdc++.h>
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2
using namespace std;
const int N = 5e5 + 1;
int n, q;

string t;

struct Data {
    int pre = 0, suf = 0, sum = 0, ans = 0;

    Data operator + (const Data &data) const {
        return {max(pre, sum + data.pre), max(data.suf, data.sum + suf), sum + data.sum, max(max(sum + data.ans, ans + data.sum), pre + data.suf)};
    }
};

struct Node {
    int l, r;
    Data data;
} node[N << 2];

void merge(int id, int l, int r) {
    node[id].data = node[l].data + node[r].data;
}

void build(int id, int l, int r) {
    node[id].l = l;
    node[id].r = r;
    if (l == r) {
        node[id].data.sum = (t[l - 1] == 'T') - (t[l - 1] == 'C');
        node[id].data.pre = max(0, node[id].data.sum);
        node[id].data.suf = max(0, node[id].data.sum);
        node[id].data.ans = max(0, node[id].data.sum);
        return;
    }
    int q = l + r >> 1;
    build(l(id), l, q);
    build(r(id), q + 1, r);
    merge(id, l(id), r(id));
}

Data query(int id, int l, int r) {
    if (node[id].l == l && node[id].r == r) {
        return node[id].data;
    }
    int q = node[id].l + node[id].r >> 1;
    Data res;

    if (l <= q) {
        res = res + query(l(id), l, min(q, r));
    }
    if (r > q) {
        res = res + query(r(id), max(q + 1, l), r);
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> t >> q;
    build(0, 1, n);

    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        
        Data res = query(0, l, r);
        cout << res.ans << "\n";
    }
}

Compilation message

election.cpp: In function 'void build(int, int, int)':
election.cpp:37:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   37 |     int q = l + r >> 1;
      |             ~~^~~
election.cpp: In function 'Data query(int, int, int)':
election.cpp:47:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 |     int q = node[id].l + node[id].r >> 1;
      |             ~~~~~~~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 47448 KB Output is correct
2 Correct 9 ms 47432 KB Output is correct
3 Correct 9 ms 47452 KB Output is correct
4 Correct 8 ms 47452 KB Output is correct
5 Correct 8 ms 47256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 47448 KB Output is correct
2 Correct 9 ms 47432 KB Output is correct
3 Correct 9 ms 47452 KB Output is correct
4 Correct 8 ms 47452 KB Output is correct
5 Correct 8 ms 47256 KB Output is correct
6 Correct 49 ms 48672 KB Output is correct
7 Correct 47 ms 48532 KB Output is correct
8 Correct 46 ms 48720 KB Output is correct
9 Correct 54 ms 48660 KB Output is correct
10 Correct 47 ms 48464 KB Output is correct
11 Correct 50 ms 48852 KB Output is correct
12 Correct 48 ms 48720 KB Output is correct
13 Correct 60 ms 48720 KB Output is correct
14 Correct 49 ms 48724 KB Output is correct
15 Correct 49 ms 48580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 47448 KB Output is correct
2 Correct 9 ms 47432 KB Output is correct
3 Correct 9 ms 47452 KB Output is correct
4 Correct 8 ms 47452 KB Output is correct
5 Correct 8 ms 47256 KB Output is correct
6 Correct 49 ms 48672 KB Output is correct
7 Correct 47 ms 48532 KB Output is correct
8 Correct 46 ms 48720 KB Output is correct
9 Correct 54 ms 48660 KB Output is correct
10 Correct 47 ms 48464 KB Output is correct
11 Correct 50 ms 48852 KB Output is correct
12 Correct 48 ms 48720 KB Output is correct
13 Correct 60 ms 48720 KB Output is correct
14 Correct 49 ms 48724 KB Output is correct
15 Correct 49 ms 48580 KB Output is correct
16 Correct 436 ms 56972 KB Output is correct
17 Correct 354 ms 56804 KB Output is correct
18 Correct 388 ms 57220 KB Output is correct
19 Correct 337 ms 56612 KB Output is correct
20 Correct 448 ms 56000 KB Output is correct
21 Correct 483 ms 57860 KB Output is correct
22 Correct 477 ms 57676 KB Output is correct
23 Correct 444 ms 58100 KB Output is correct
24 Correct 441 ms 57724 KB Output is correct
25 Correct 431 ms 57212 KB Output is correct