結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー 👑 emthrmemthrm
提出日時 2021-12-07 00:14:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 192 ms / 2,000 ms
コード長 3,344 bytes
コンパイル時間 2,674 ms
コンパイル使用メモリ 217,076 KB
実行使用メモリ 26,916 KB
最終ジャッジ日時 2023-09-21 16:09:37
合計ジャッジ時間 8,480 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
4,380 KB
testcase_01 AC 57 ms
4,380 KB
testcase_02 AC 55 ms
4,380 KB
testcase_03 AC 47 ms
4,376 KB
testcase_04 AC 58 ms
4,380 KB
testcase_05 AC 70 ms
10,208 KB
testcase_06 AC 37 ms
4,492 KB
testcase_07 AC 9 ms
6,564 KB
testcase_08 AC 152 ms
20,940 KB
testcase_09 AC 21 ms
9,108 KB
testcase_10 AC 72 ms
16,724 KB
testcase_11 AC 59 ms
12,404 KB
testcase_12 AC 76 ms
10,284 KB
testcase_13 AC 141 ms
20,632 KB
testcase_14 AC 64 ms
15,124 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 151 ms
21,124 KB
testcase_17 AC 151 ms
21,164 KB
testcase_18 AC 149 ms
21,752 KB
testcase_19 AC 149 ms
21,208 KB
testcase_20 AC 154 ms
21,188 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 91 ms
15,900 KB
testcase_24 AC 107 ms
18,648 KB
testcase_25 AC 125 ms
18,580 KB
testcase_26 AC 192 ms
26,916 KB
testcase_27 AC 89 ms
26,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct LowestCommonAncestorByDoubling {
  std::vector<int> depth, dist;

  LowestCommonAncestorByDoubling(const std::vector<std::vector<int>> &graph) : graph(graph) {
    n = graph.size();
    depth.resize(n);
    dist.resize(n);
    while ((1 << table_h) <= n) ++table_h;
    parent.resize(table_h, std::vector<int>(n));
  }

  void build(int root = 0) {
    is_built = true;
    dfs(-1, root, 0, 0);
    for (int i = 0; i + 1 < table_h; ++i) for (int ver = 0; ver < n; ++ver) {
      parent[i + 1][ver] = parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]];
    }
  }

  int query(int u, int v) const {
    assert(is_built);
    if (depth[u] > depth[v]) std::swap(u, v);
    for (int i = 0; i < table_h; ++i) {
      if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v];
    }
    if (u == v) return u;
    for (int i = table_h - 1; i >= 0; --i) {
      if (parent[i][u] != parent[i][v]) {
        u = parent[i][u];
        v = parent[i][v];
      }
    }
    return parent[0][u];
  }

  int distance(int u, int v) const {
    assert(is_built);
    return dist[u] + dist[v] - dist[query(u, v)] * 2;
  }

private:
  bool is_built = false;
  int n, table_h = 1;
  std::vector<std::vector<int>> graph, parent;

  void dfs(int par, int ver, int now_depth, int now_dist) {
    depth[ver] = now_depth;
    dist[ver] = now_dist;
    parent[0][ver] = par;
    for (int e : graph[ver]) {
      if (e != par) dfs(ver, e, now_depth + 1, now_dist + 1);
    }
  }
};

int main() {
  int n, q; string s; cin >> n >> q >> s;
  n += 2;
  s = '(' + s + ')';
  vector<vector<int>> graph;
  vector<pair<int, int>> node;
  vector<int> rev(n, -1), l;
  REP(i, n) {
    if (s[i] == '(') {
      l.emplace_back(i);
    } else if (s[i] == ')') {
      const int idx = graph.size();
      graph.emplace_back();
      rev[i] = idx;
      while (l.back() < 0) {
        const int child = -l.back() - 1;
        graph.back().emplace_back(child);
        // graph[child].emplace_back(idx);
        l.pop_back();
      }
      node.emplace_back(l.back(), i);
      rev[l.back()] = idx;
      l.pop_back();
      l.emplace_back(-(idx + 1));
    }
  }
  const int m = graph.size();
  LowestCommonAncestorByDoubling lca(graph);
  lca.build(m - 1);
  while (q--) {
    int x, y; cin >> x >> y;
    const int ans = lca.query(rev[x], rev[y]);
    if (ans == m - 1) {
      cout << "-1\n";
    } else {
      cout << node[ans].first << ' ' << node[ans].second << '\n';
    }
  }
  return 0;
}
0