結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー t_mkt_mk
提出日時 2022-03-18 15:27:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,183 ms / 2,000 ms
コード長 1,550 bytes
コンパイル時間 2,175 ms
コンパイル使用メモリ 179,656 KB
実行使用メモリ 22,340 KB
最終ジャッジ日時 2024-04-12 10:42:26
合計ジャッジ時間 23,029 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 391 ms
12,632 KB
testcase_01 AC 390 ms
12,628 KB
testcase_02 AC 396 ms
12,628 KB
testcase_03 AC 358 ms
12,620 KB
testcase_04 AC 410 ms
12,628 KB
testcase_05 AC 487 ms
15,948 KB
testcase_06 AC 271 ms
12,932 KB
testcase_07 AC 22 ms
13,352 KB
testcase_08 AC 1,053 ms
22,276 KB
testcase_09 AC 95 ms
14,824 KB
testcase_10 AC 432 ms
19,344 KB
testcase_11 AC 377 ms
17,136 KB
testcase_12 AC 528 ms
15,868 KB
testcase_13 AC 1,024 ms
22,024 KB
testcase_14 AC 378 ms
18,572 KB
testcase_15 AC 6 ms
11,428 KB
testcase_16 AC 1,034 ms
22,328 KB
testcase_17 AC 1,040 ms
22,196 KB
testcase_18 AC 1,035 ms
22,200 KB
testcase_19 AC 1,049 ms
22,200 KB
testcase_20 AC 1,050 ms
22,200 KB
testcase_21 AC 5 ms
11,504 KB
testcase_22 AC 5 ms
11,500 KB
testcase_23 AC 713 ms
20,320 KB
testcase_24 AC 763 ms
22,324 KB
testcase_25 AC 1,183 ms
22,324 KB
testcase_26 AC 1,123 ms
22,212 KB
testcase_27 AC 477 ms
22,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i < (n);i++)
using ll = long long;
using P =pair<int,int>;
ll INF = 1LL << 60;
int LEN = 1 << 20;
vector<int> seg(2*LEN,0);
void add(int ind,int val){
  ind += LEN;
  seg[ind] = val;
  while(ind > 0){
    ind /= 2;
    seg[ind] = max(seg[ind*2], seg[ind*2+1]);
  }
}
int maxim(int l,int r){
  l += LEN;
  r += LEN;
  int ans = 0;
  while(l < r){
    if(l & 1)ans = max(ans,seg[l++]);
    if(r & 1)ans = max(ans,seg[--r]);
    l /= 2;
    r /= 2;
  }
  return ans;
}
int main(){
  int n,q;
  cin >> n >> q;
  string S;
  cin >> S;
  stack<int> st;
  map<int,int> mp;
  rep(i,n){
    if(S[i] == '(')st.push(i+1);
    else{
      int a = st.top();
      add(a,i+1);
      mp[a] = i+1;
      mp[i+1] = a;
      st.pop();
    }
  }
  auto solve = [&](int x,int y){
    if(maxim(0,x+1) < y)return -1;
    int high = x + 1;
    int low = 0;
    int mid = (high + low)/2;
    while(high - low > 1){
      mid = (high + low)/2;
      //cout << high << " " << low << " "<<mid << " " << maxim(mid,x+1) << endl;
      if(maxim(mid,x+1) >= y)low = mid;
      else high = mid;
    }
    return low;
  };
  vector<int> ans;
  rep(i,q){
    int x,y;
    cin >> x >> y;
    int x_ = mp[x],y_=mp[y];
    if(x > x_)swap(x,x_);
    if(y > y_)swap(y,y_);
    x = min(x,y);
    x_ = max(x_,y_);
    //cout << "solve" << x << " " << x_ << endl;
    ans.push_back(solve(x,x_));
  }
  for(int p:ans){
    if(p == -1)cout << -1 << endl;
    else cout << p << " " << mp[p] << endl;
  }
  return 0;
}
0