結果
| 問題 |
No.1778 括弧列クエリ / Bracketed Sequence Query
|
| コンテスト | |
| ユーザー |
Forested
|
| 提出日時 | 2021-12-07 21:21:29 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 122 ms / 2,000 ms |
| コード長 | 6,160 bytes |
| コンパイル時間 | 1,270 ms |
| コンパイル使用メモリ | 126,036 KB |
| 最終ジャッジ日時 | 2025-01-26 06:36:01 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 27 |
ソースコード
#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';
}
}
}
Forested