답안 #1100558

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100558 2024-10-14T08:59:15 Z vjudge1 Election (BOI18_election) C++17
100 / 100
1324 ms 42780 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int MAXN = 5e5 + 10;

char c[MAXN];

struct Segment {
    
    struct Node {
        int pf, sf, sum, ans;
        Node()
        {
            pf = sf = sum = ans = 0;
        }
    } node[MAXN << 2];

    Node merge(Node l, Node r)
    {
        Node res;
        res.pf = max(l.pf, l.sum + r.pf);
        res.sf = max(r.sf, r.sum + l.sf);
        res.sum = l.sum + r.sum;
        res.ans = max(max(l.ans + r.sum, r.ans + l.sum), l.pf + r.sf);
        return res;
    }

    void initialize(int id, int l, int r)
    {
        if (l + 1 == r)
        {
            if (c[l] == 'T')
                node[id].pf = node[id].sf = node[id].sum = node[id].ans = 1;
            else
            {
                node[id].pf = node[id].sf = node[id].ans = 0;
                node[id].sum = -1;
            }
            return;
        }
        int mid = (l + r) >> 1;
        initialize(id << 1 | 0, l, mid);
        initialize(id << 1 | 1, mid, r);
        node[id] = merge(node[id << 1 | 0], node[id << 1 | 1]);
    }

    Node get(int id, int L, int R, int l, int r)
    {
        if (L == l && R == r)
        {
            return node[id];
        }
        int mid = (L + R) >> 1;
        Node res;
        if (l < mid)
            res = merge(res, get(id << 1 | 0, L, mid, l, min(r, mid)));
        if (r > mid)
            res = merge(res, get(id << 1 | 1, mid, R, max(l, mid), r));
        return res;
    }

} Seg;

int main()
{
    int n, q;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> c[i];
    Seg.initialize(1, 1, n + 1);
    cin >> q;
    while (q--)
    {
        int l, r;
        cin >> l >> r;
        auto ans = Seg.get(1, 1, n + 1, l, r + 1);
        cout << ans.ans << endl;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 31568 KB Output is correct
2 Correct 10 ms 31568 KB Output is correct
3 Correct 9 ms 31568 KB Output is correct
4 Correct 10 ms 31568 KB Output is correct
5 Correct 9 ms 31568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 31568 KB Output is correct
2 Correct 10 ms 31568 KB Output is correct
3 Correct 9 ms 31568 KB Output is correct
4 Correct 10 ms 31568 KB Output is correct
5 Correct 9 ms 31568 KB Output is correct
6 Correct 167 ms 32072 KB Output is correct
7 Correct 157 ms 32072 KB Output is correct
8 Correct 156 ms 31832 KB Output is correct
9 Correct 156 ms 31824 KB Output is correct
10 Correct 159 ms 31928 KB Output is correct
11 Correct 158 ms 32332 KB Output is correct
12 Correct 169 ms 32088 KB Output is correct
13 Correct 163 ms 32132 KB Output is correct
14 Correct 159 ms 32072 KB Output is correct
15 Correct 164 ms 31892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 31568 KB Output is correct
2 Correct 10 ms 31568 KB Output is correct
3 Correct 9 ms 31568 KB Output is correct
4 Correct 10 ms 31568 KB Output is correct
5 Correct 9 ms 31568 KB Output is correct
6 Correct 167 ms 32072 KB Output is correct
7 Correct 157 ms 32072 KB Output is correct
8 Correct 156 ms 31832 KB Output is correct
9 Correct 156 ms 31824 KB Output is correct
10 Correct 159 ms 31928 KB Output is correct
11 Correct 158 ms 32332 KB Output is correct
12 Correct 169 ms 32088 KB Output is correct
13 Correct 163 ms 32132 KB Output is correct
14 Correct 159 ms 32072 KB Output is correct
15 Correct 164 ms 31892 KB Output is correct
16 Correct 1263 ms 41288 KB Output is correct
17 Correct 1143 ms 41424 KB Output is correct
18 Correct 1201 ms 41644 KB Output is correct
19 Correct 1093 ms 41052 KB Output is correct
20 Correct 1179 ms 40780 KB Output is correct
21 Correct 1259 ms 42576 KB Output is correct
22 Correct 1324 ms 42460 KB Output is correct
23 Correct 1263 ms 42780 KB Output is correct
24 Correct 1200 ms 42316 KB Output is correct
25 Correct 1235 ms 41804 KB Output is correct