#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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 using Vec = vector; template bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } template 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 #include #include template class SegmentTree { public: using Value = typename Op::Value; private: std::size_t old_length; std::size_t length; std::vector 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 &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 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 template struct Add { using Value = T; static Value id() { return T(0); } static Value op(const T &lhs, const T &rhs) { return lhs + rhs; } }; template struct Mul { using Value = T; static Value id() { return T(1); } static Value op(const T &lhs, const T &rhs) { return lhs * rhs; } }; template struct Min { using Value = T; static Value id() { return std::numeric_limits::max(); } static Value op(const T &lhs, const T &rhs) { return std::min(lhs, rhs); } }; template struct Max { using Value = T; static Value id() { return std::numeric_limits::min(); } static Value op(const T &lhs, const T &rhs) { return std::max(lhs, rhs); } }; template 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 height(n, 0), other(n, 0); stack 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> height_to_index(n); REP(i, n) { if (s[i] == '(') height_to_index[height[i]].push_back(i); } SegmentTree> 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'; } } }