답안 #611784

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
611784 2022-07-29T07:29:02 Z tengiz05 Election (BOI18_election) C++17
100 / 100
1250 ms 53036 KB
#include <bits/stdc++.h>

using i64 = long long;
using namespace std;

constexpr int inf = 1E9;

struct Info {
    int x;
    Info(int x = -inf) : x(x) {}
};
Info operator+(const Info &a, const Info &b) {
    return Info(max(a.x, b.x));
}
struct Tag {
    int x;
    Tag(int x = 0) : x(x) {}
};
void apply(Tag &a, const Tag &b) {
    a.x += b.x;
}
void apply(Info &a, const Tag &b) {
    a.x += b.x;
}
template<class Info, class Tag,
    class Merge = std::plus<Info>>
struct LazySegmentTree {
    const int n;
    const Merge merge;
    std::vector<Info> info;
    std::vector<Tag> tag;
    LazySegmentTree(int n) : n(n), merge(Merge()), info(4 << std::__lg(n)), tag(4 << std::__lg(n)) {}
    LazySegmentTree(std::vector<Info> init) : LazySegmentTree(init.size()) {
        std::function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) {
                info[p] = init[l];
                return;
            }
            int m = (l + r) / 2;
            build(2 * p, l, m);
            build(2 * p + 1, m, r);
            pull(p);
        };
        build(1, 0, n);
    }
    void pull(int p) {
        info[p] = merge(info[2 * p], info[2 * p + 1]);
    }
    void apply(int p, const Tag &v) {
        ::apply(info[p], v);
        ::apply(tag[p], v);
    }
    void push(int p) {
        apply(2 * p, tag[p]);
        apply(2 * p + 1, tag[p]);
        tag[p] = Tag();
    }
    void modify(int p, int l, int r, int x, Info v) {
        if (r - l == 1) {
            info[p] = v;
            return;
        }
        int m = (l + r) / 2;
        push(p);
        if (x < m) {
            modify(2 * p, l, m, x, v);
        } else {
            modify(2 * p + 1, m, r, x, v);
        }
        pull(p);
    }
    void modify(int x, Info v) {
        modify(1, 0, n, x, v);
    }
    Info rangeQuery(int p, int l, int r, int x, int y) {
        if (l >= y || r <= x) {
            return Info();
        }
        if (l >= x && r <= y) {
            return info[p];
        }
        int m = (l + r) / 2;
        push(p);
        return merge(rangeQuery(2 * p, l, m, x, y), rangeQuery(2 * p + 1, m, r, x, y));
    }
    Info rangeQuery(int l, int r) {
        return rangeQuery(1, 0, n, l, r);
    }
    void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
        if (l >= y || r <= x) {
            return;
        }
        if (l >= x && r <= y) {
            apply(p, v);
            return;
        }
        int m = (l + r) / 2;
        push(p);
        rangeApply(2 * p, l, m, x, y, v);
        rangeApply(2 * p + 1, m, r, x, y, v);
        pull(p);
    }
    void rangeApply(int l, int r, const Tag &v) {
        return rangeApply(1, 0, n, l, r, v);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    
    string S;
    cin >> S;
    
    int q;
    cin >> q;
    
    vector<vector<pair<int, int>>> Q(n);
    vector<int> ans(q);
    
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        l--;
        r--;
        Q[l].emplace_back(r, i);
    }
    
    stack<int> s;
    
    LazySegmentTree<Info, Tag> res(n), f(n);
    
    for (int i = 0; i < n; i++) {
        res.modify(i, Info(0));
        f.modify(i, Info(0));
    }
    
    for (int i = n - 1; i >= 0; i--) {
        if (S[i] == 'T') {
            res.rangeApply(i, n, 1);
            s.push(i);
        } else {
            if (!s.empty()) {
                res.rangeApply(s.top(), n, -1);
                f.rangeApply(s.top(), n, -1);
                s.pop();
            }
            f.rangeApply(i, n, 1);
        }
        for (auto [r, id] : Q[i]) {
            ans[id] = res.rangeQuery(r, r + 1).x + (f.rangeQuery(i, r + 1).x - f.rangeQuery(r, r + 1).x);
        }
    }
    
    for (int i = 0; i < q; i++) {
        cout << ans[i] << "\n";
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 129 ms 9036 KB Output is correct
7 Correct 117 ms 8524 KB Output is correct
8 Correct 138 ms 8608 KB Output is correct
9 Correct 141 ms 8932 KB Output is correct
10 Correct 149 ms 8980 KB Output is correct
11 Correct 133 ms 9156 KB Output is correct
12 Correct 130 ms 9140 KB Output is correct
13 Correct 130 ms 9204 KB Output is correct
14 Correct 135 ms 9128 KB Output is correct
15 Correct 122 ms 9124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 129 ms 9036 KB Output is correct
7 Correct 117 ms 8524 KB Output is correct
8 Correct 138 ms 8608 KB Output is correct
9 Correct 141 ms 8932 KB Output is correct
10 Correct 149 ms 8980 KB Output is correct
11 Correct 133 ms 9156 KB Output is correct
12 Correct 130 ms 9140 KB Output is correct
13 Correct 130 ms 9204 KB Output is correct
14 Correct 135 ms 9128 KB Output is correct
15 Correct 122 ms 9124 KB Output is correct
16 Correct 1217 ms 51148 KB Output is correct
17 Correct 1014 ms 47204 KB Output is correct
18 Correct 1130 ms 48216 KB Output is correct
19 Correct 1042 ms 49860 KB Output is correct
20 Correct 1180 ms 50244 KB Output is correct
21 Correct 1250 ms 52476 KB Output is correct
22 Correct 1171 ms 51960 KB Output is correct
23 Correct 1213 ms 53036 KB Output is correct
24 Correct 1161 ms 52124 KB Output is correct
25 Correct 1136 ms 51336 KB Output is correct