結果
| 問題 |
No.1778 括弧列クエリ / Bracketed Sequence Query
|
| コンテスト | |
| ユーザー |
沙耶花
|
| 提出日時 | 2021-12-07 00:14:33 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 290 ms / 2,000 ms |
| コード長 | 2,398 bytes |
| コンパイル時間 | 4,124 ms |
| コンパイル使用メモリ | 258,976 KB |
| 最終ジャッジ日時 | 2025-01-26 06:14:03 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 27 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:141:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
141 | scanf("%d %d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~~
ソースコード
#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define Inf 1000000001
string S;
vector<vector<int>> ps(1,vector<int>(2,-1));
vector<vector<int>> E(1);
void dfs(int cur,int &pos){
while(pos!=S.size()){
if(S[pos]=='('){
int to = E.size();
E[cur].push_back(to);
E.push_back(vector<int>(1,cur));
ps.push_back(vector<int>(1,pos));
pos++;
dfs(to,pos);
}
else{
ps[cur].push_back(pos);
pos++;
return;
}
}
}
struct lca{
vector<int> depth;
vector<vector<int>> parents;
int max_j=18;
lca(int n,vector<vector<int>> &E){
rep(i,100){
if((1<<i)>E.size()){
max_j = i;
break;
}
}
depth.assign(E.size(),-1);
parents.assign(E.size(),vector<int>(max_j,-1));
stack<int> s;
s.push(n);
depth[n] = 0;
while(s.size()!=0){
int k = s.top();
s.pop();
for(int i=0;i<E[k].size();i++){
int x = E[k][i];
if(depth[x]!=-1)continue;
s.push(x);
depth[x] = depth[k]+1;
for(int j=0;j<max_j;j++){
if(j==0){
parents[x][j] = k;
}
else{
parents[x][j] = parents[parents[x][j-1]][j-1];
}
if(parents[x][j] == -1)break;
}
}
}
}
int kth_parent(int u,int k){
for(int i=0;i<max_j;i++){
if(k==0)break;
if(u==-1)break;
if(k%2){
u = parents[u][i];
}
k/=2;
}
return u;
}
int query(int u,int v){
if(depth[u]<depth[v])swap(u,v);
u = kth_parent(u,depth[u]-depth[v]);
if(u==v){
return u;
}
for(int i=max_j-1;i>=0;i--){
if(parents[u][i]!=parents[v][i]){
u = parents[u][i];
v = parents[v][i];
}
}
return parents[u][0];
}
int get_distance(int u,int v){
return depth[u]+depth[v]-2*depth[query(u,v)];
}
};
int main(){
int N,Q;
cin>>N>>Q;
cin>>S;
int p = 0;
dfs(0,p);
/*
rep(i,ps.size()){
cout<<i<<endl;
rep(j,ps[i].size()){
cout<<ps[i][j]<<',';
}
cout<<endl;
}*/
lca L(0,E);
vector<int> pos(S.size());
rep(i,ps.size()){
if(ps[i][0]==-1)continue;
rep(j,2)pos[ps[i][j]] = i;
}
rep(_,Q){
int a,b;
scanf("%d %d",&a,&b);
a--;b--;
int l = L.query(pos[a],pos[b]);
if(ps[l][0]==-1){
printf("-1\n");
}
else{
int x = ps[l][0],y = ps[l][1];
if(x>y)swap(x,y);
x++,y++;
printf("%d %d\n",x,y);
}
}
return 0;
}
沙耶花