結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー ForestedForested
提出日時 2022-01-07 12:07:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,579 bytes
コンパイル時間 707 ms
コンパイル使用メモリ 76,108 KB
実行使用メモリ 5,292 KB
最終ジャッジ日時 2023-10-11 10:44:07
合計ジャッジ時間 9,074 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 275 ms
5,052 KB
testcase_02 AC 272 ms
5,060 KB
testcase_03 AC 279 ms
5,060 KB
testcase_04 AC 277 ms
5,000 KB
testcase_05 AC 274 ms
5,024 KB
testcase_06 AC 274 ms
5,040 KB
testcase_07 AC 277 ms
5,060 KB
testcase_08 AC 275 ms
4,996 KB
testcase_09 AC 276 ms
5,044 KB
testcase_10 AC 274 ms
5,124 KB
testcase_11 AC 262 ms
5,068 KB
testcase_12 AC 255 ms
5,124 KB
testcase_13 AC 259 ms
5,060 KB
testcase_14 AC 405 ms
5,292 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 RE -
testcase_23 AC 1 ms
4,352 KB
testcase_24 AC 1 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <iostream>

// ===== fenwick_tree.hpp =====
#ifndef FENWICK_TREE_HPP
#define FENWICK_TREE_HPP

#include <cassert>
#include <vector>

// ===== operations.hpp =====
#ifndef OPERATIONS_HPP
#define OPERATIONS_HPP

#include <limits>
#include <utility>

template <typename T>
struct Add {
    using Value = T;
    static Value id() {
        return T(0);
    }
    static Value op(const Value &lhs, const Value &rhs) {
        return lhs + rhs;
    }
    static Value inv(const Value &x) {
        return -x;
    }
};

template <typename T>
struct Mul {
    using Value = T;
    static Value id() {
        return Value(1);
    }
    static Value op(const Value &lhs, const Value &rhs) {
        return lhs * rhs;
    }
    static Value inv(const Value &x) {
        return Value(1) / x;
    }
};

template <typename T>
struct Min {
    using Value = T;
    static Value id() {
        return std::numeric_limits<T>::max();
    }
    static Value op(const Value &lhs, const Value &rhs) {
        return std::min(lhs, rhs);
    }
};

template <typename T>
struct Max {
    using Value = T;
    static Value id() {
        return std::numeric_limits<Value>::min();
    }
    static Value op(const Value &lhs, const Value &rhs) {
        return std::max(lhs, rhs);
    }
};

template <typename T>
struct Xor {
    using Value = T;
    static Value id() {
        return T(0);
    }
    static Value op(const Value &lhs, const Value &rhs) {
        return lhs ^ rhs;
    }
    static Value inv(const Value &x) {
        return x;
    }
};

template <typename Monoid>
struct Reversible {
    using Value = std::pair<typename Monoid::Value, typename Monoid::Value>;
    static Value id() {
        return Value(Monoid::id(), Monoid::id());
    }
    static Value op(const Value &v1, const Value &v2) {
        return Value(
            Monoid::op(v1.first, v2.first),
            Monoid::op(v2.second, v1.second));
    }
};

#endif
// ===== operations.hpp =====

template <typename CommutativeGroup>
class FenwickTree {
public:
    using Value = typename CommutativeGroup::Value;

private:
    std::vector<Value> data;

public:
    FenwickTree(std::size_t n) : data(n, CommutativeGroup::id()) {}

    void add(std::size_t idx, const Value &x) {
        assert(idx < data.size());
        for (; idx < data.size(); idx |= idx + 1) {
            data[idx] = CommutativeGroup::op(data[idx], x);
        }
    }

    Value sum(std::size_t r) const {
        assert(r <= data.size());
        Value ret = CommutativeGroup::id();
        for (; r > 0; r &= r - 1) {
            ret = CommutativeGroup::op(ret, data[r - 1]);
        }
        return ret;
    }

    Value sum(std::size_t l, std::size_t r) const {
        assert(l <= r && r <= data.size());
        return CommutativeGroup::op(sum(r), CommutativeGroup::inv(sum(l)));
    }
};

#endif
// ===== fenwick_tree.hpp =====

constexpr std::size_t N_MIN = 1;
constexpr std::size_t N_MAX = 200000;
constexpr std::size_t Q_MIN = 1;
constexpr std::size_t Q_MAX = 200000;

int main() {
    std::size_t n, q;
    std::cin >> n >> q;
    std::string s;
    std::cin >> s;
    
    assert(n >= N_MIN && n <= N_MAX);
    assert(q >= Q_MIN && q <= Q_MAX);
    assert(s.size() == n);
    for (char c : s) {
        assert(c == '(' || c == ')');
    }
    
    FenwickTree<Add<std::size_t>> fw(n - 1);
    for (std::size_t i = 0; i < n - 1; ++i) {
        if (s[i] == '(' && s[i + 1] == ')') {
            fw.add(i, 1);
        }
    }
    
    bool query2 = false;
    for (std::size_t qi = 0; qi < q; ++qi) {
        std::size_t type;
        std::cin >> type;
        if (type == 1) {
            std::size_t i;
            std::cin >> i;
            --i;
            assert(i < n);
            if (i != 0 && s[i - 1] == '(' && s[i] == ')') {
                fw.add(i - 1, - (std::size_t) 1);
            }
            if (i != n - 1 && s[i] == '(' && s[i + 1] == ')') {
                fw.add(i, - (std::size_t) 1);
            }
            s[i] ^= '(' ^ ')';
            if (i != 0 && s[i - 1] == '(' && s[i] == ')') {
                fw.add(i - 1, 1);
            }
            if (i != n - 1 && s[i] == '(' && s[i + 1] == ')') {
                fw.add(i, 1);
            }
        } else if (type == 2) {
            std::size_t l, r;
            std::cin >> l >> r;
            --l;
            assert(l < r && r <= n);
            std::size_t ans = fw.sum(l, r - 1);
            std::cout << ans << '\n';
            query2 = true;
        } else {
            assert(false);
        }
    }
    assert(query2);
}
0