結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー ForestedForested
提出日時 2021-12-07 21:21:29
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 6,160 bytes
コンパイル時間 1,703 ms
コンパイル使用メモリ 128,916 KB
実行使用メモリ 14,760 KB
最終ジャッジ日時 2023-09-22 08:28:28
合計ジャッジ時間 6,461 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
4,376 KB
testcase_01 AC 58 ms
4,376 KB
testcase_02 AC 56 ms
4,380 KB
testcase_03 AC 45 ms
4,380 KB
testcase_04 AC 61 ms
4,380 KB
testcase_05 AC 60 ms
7,076 KB
testcase_06 AC 37 ms
4,376 KB
testcase_07 AC 5 ms
5,140 KB
testcase_08 AC 113 ms
12,132 KB
testcase_09 AC 14 ms
6,536 KB
testcase_10 AC 51 ms
10,580 KB
testcase_11 AC 45 ms
7,888 KB
testcase_12 AC 65 ms
7,068 KB
testcase_13 AC 108 ms
12,072 KB
testcase_14 AC 46 ms
9,820 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 113 ms
12,144 KB
testcase_17 AC 113 ms
12,136 KB
testcase_18 AC 116 ms
12,144 KB
testcase_19 AC 114 ms
12,204 KB
testcase_20 AC 115 ms
12,148 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 87 ms
10,644 KB
testcase_24 AC 89 ms
12,328 KB
testcase_25 AC 110 ms
12,316 KB
testcase_26 AC 83 ms
14,704 KB
testcase_27 AC 66 ms
14,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (n); ++i)
#define REP3(i, m, n) for (i32 i = (m); i < (n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER(i, n) for (i32 i = (n) - 1; i >= 0; --i)
#define ALL(x) begin(x), end(x)

using namespace std;

using u32 = unsigned int;
using u64 = unsigned long long;
using u128 = __uint128_t;
using i32 = signed int;
using i64 = signed long long;
using i128 = __int128_t;

template <typename T>
using Vec = vector<T>;

template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

[[maybe_unused]] constexpr i32 inf = 1000000100;
[[maybe_unused]] constexpr i64 inf64 = 3000000000000000100;

struct SetIO {
    SetIO() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout << fixed << setprecision(10);
    }
} set_io;

// ===== new_library/data_structure/segment_tree.hpp =====
#ifndef SEGMENT_TREE_HPP
#define SEGMENT_TREE_HPP

#include <vector>
#include <cassert>
#include <utility>

template <typename Op>
class SegmentTree {
public:
    using Value = typename Op::Value;
    
private:
    std::size_t old_length;
    std::size_t length;
    std::vector<Value> node;
    
    static std::size_t ceil2(std::size_t n) {
        if (n == 0) return 0;
        return (std::size_t) 1 << (64 - __builtin_clzl(n));
    }
    
public:
    SegmentTree(std::size_t n) : old_length(n), length(ceil2(old_length)), node(length << 1, Op::id()) {}
    
    SegmentTree(const std::vector<Value> &v) : old_length(v.size()), length(ceil2(old_length)), node(length << 1, Op::id()) {
        for (std::size_t i = 0; i < old_length; ++i) {
            node[i + length] = v[i];
        }
        for (std::size_t i = length - 1; i > 0; --i) {
            node[i] = Op::op(node[i << 1], node[i << 1 | 1]);
        }
    }
    
    const Value &operator[](std::size_t idx) const {
        assert(idx < old_length);
        return node[idx + length];
    }
    
    void update(std::size_t idx, Value val) {
        assert(idx < old_length);
        idx += length;
        node[idx] = std::move(val);
        while (idx != 1) {
            idx >>= 1;
            node[idx] = Op::op(node[idx << 1], node[idx << 1 | 1]);
        }
    }
    
    Value prod(std::size_t l, std::size_t r) const {
        assert(l <= r && r <= old_length);
        Value prodl = Op::id();
        Value prodr = Op::id();
        l += length;
        r += length;
        while (l != r) {
            if (l & 1) {
                prodl = Op::op(prodl, node[l++]);
            }
            if (r & 1) {
                prodr = Op::op(node[--r], prodr);
            }
            l >>= 1;
            r >>= 1;
        }
        return Op::op(prodl, prodr);
    }
};

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

#endif
// ===== new_library/data_structure/segment_tree.hpp =====
// ===== new_library/data_structure/operations.hpp =====
#ifndef OPERATIONS_HPP
#define OPERATIONS_HPP

#include <limits>

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

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

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

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

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

#endif
// ===== new_library/data_structure/operations.hpp =====

int main() {
    i32 n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    
    s = "(" + s + ")";
    n += 2;
    
    Vec<i32> height(n, 0), other(n, 0);
    stack<i32> idx;
    REP(i, n) {
        if (s[i] == '(') {
            height[i] = idx.size();
            idx.push(i);
        } else {
            i32 j = idx.top();
            idx.pop();
            other[i] = j;
            other[j] = i;
            height[i] = height[j];
        }
    }
    
    Vec<Vec<i32>> height_to_index(n);
    REP(i, n) {
        if (s[i] == '(') height_to_index[height[i]].push_back(i);
    }
    
    SegmentTree<Min<i32>> seg(height);
    
    while (q--) {
        i32 x, y;
        cin >> x >> y;
        i32 xl = x, xr = other[x], yl = y, yr = other[y];
        if (xl > xr) {
            swap(xl, xr);
        }
        if (yl > yr) {
            swap(yl, yr);
        }
        if ((xl >= yl && xr <= yr) || (yl >= xl && yr <= xr)) {
            cout << min(xl, yl) << ' ' << max(xr, yr) << '\n';
            continue;
        }
        if (xl > yl) {
            swap(xl, yl);
            swap(xr, yr);
        }
        i32 h = seg.prod(xr, yl + 1) - 1;
        i32 l = *prev(lower_bound(ALL(height_to_index[h]), yr), 1);
        i32 r = other[l];
        if (l == 0) {
            cout << "-1\n";
        } else {
            cout << l << ' ' << r << '\n';
        }
    }
}
0