結果
| 問題 |
No.1802 Range Score Query for Bracket Sequence
|
| コンテスト | |
| ユーザー |
ytft
|
| 提出日時 | 2022-02-14 22:45:24 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,935 bytes |
| コンパイル時間 | 3,259 ms |
| コンパイル使用メモリ | 251,560 KB |
| 実行使用メモリ | 6,656 KB |
| 最終ジャッジ日時 | 2024-06-29 06:55:59 |
| 合計ジャッジ時間 | 9,193 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 5 WA * 17 RE * 2 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template<typename T>
struct segment_tree{
vector<T> tree;
int depth;
function<T(T,T)> comp;
T unit;
segment_tree(vector<T>& a,function<T(T,T)> f,T unit=0){
this->unit=unit;
comp=f;
depth=(int)ceil(log(a.size())/log(2))+1;
tree.resize((1<<depth)-1);
fill(tree.begin()+(1<<(depth-1)-1),tree.end(),unit);
copy(a.begin(),a.end(),tree.begin()+((1<<(depth-1))-1));
for(int i=(1<<(depth-1))-2;i>=0;--i){
tree[i]=comp(tree[i*2+1],tree[i*2+2]);
}
}
void update(int index,T value){
index+=(1<<(depth-1))-1;
tree[index]=value;
while(index!=0){
index=(index-1)/2;
tree[index]=comp(tree[index*2+1],tree[index*2+2]);
}
}
T get(int begin,int end){
T ret=unit;
begin+=(1<<(depth-1))-1;
end+=(1<<(depth-1))-1;
while(begin<end){
if(begin%2==0){
ret=comp(ret,tree[begin]);
}
begin/=2;
if(end%2==0){
ret=comp(ret,tree[end-1]);
}
end=(end-1)/2;
}
return ret;
}
};
int main(){
int N,Q,q;
string S;
cin>>N>>Q>>S;
vector<int> array(N-1,0);
segment_tree<int> T(array,[](int a,int b){return a+b;},0);
for(int i=0;i<N-1;++i){
if((S[i]=='(')&&(S[i+1]==')')){
T.update(i,1);
}
}
for(int i=0;i<Q;++i){
cin>>q;
if(q==1){
cin>>q;
S[q-1]=(S[q-1]=='('?')':'(');
for(int i=max(0,q-2);i<min(q,N-1);++i){
if((S[i]=='(')&&(S[i+1]==')')){
T.update(i,1);
}else{
T.update(i,0);
}
}
}else{
int l,r;
cin>>l>>r;
cout<<T.get(l-1,r)<<endl;
}
}
}
ytft