提出 #70488354
ソースコード 拡げる
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
const int N=1e7+5;
string s;
int n;
//store sum, max pf, min pf
typedef array<int,3> arin;
const int ts=1048576;
arin a[ts];
arin zero;
arin merge(arin x,arin y){
arin z;
z[0]=x[0]+y[0];
z[1]=max(x[1],x[0]+y[1]);
z[2]=min(x[2],x[0]+y[2]);
return z;;
}
void build(int id,int l,int r){
if(l==r){
if(s[l]=='A') a[id]={1,1,0};
else a[id]={-1,0,-1};
return;
}
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
a[id]=merge(a[id*2],a[id*2+1]);
}/*
int findl(int id,int l,int r,int ql,int qr,arin z){
if(l==r){
return l;
}
if(ql<=l)
int mid=(l+r)/2;
auto w=merge(z,arin[id*2]);
if(w[2]<0) return findl(id*2,l,mid,ql,qr,z);
else
}*/
arin qry(int id,int l,int r,int ql,int qr){
if(l>qr || r<ql || ql>qr) return zero;
if(ql<=l && r<=qr) return a[id];
int mid=(l+r)/2;
return merge(qry(id*2,l,mid,ql,qr),qry(id*2+1,mid+1,r,ql,qr));
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);
cin >> s;n=s.size();s='?'+s;
build(1,1,n);
int q;cin >> q;
for(int i=1; i<=q ;i++){
int l,r;cin >> l >> r;
arin frog=qry(1,1,n,l,r);
int sum=frog[0];
int cb=-frog[2];
int ca=sum-frog[2];
ll ans=1e9;
for(int seed=0; seed<2 ;seed++){//way 1
int cur_idb=cb;
int cur_ida=ca;
ll cost=ca+cb;
int flag=seed;
while(cur_idb>0 && cur_ida>0){
flag^=1;
if(flag%2==0){
int bl=l,br=r+1;
while(bl!=br){
int mid=(bl+br)/2;
auto tmp=qry(1,1,n,l,mid);
if(tmp[1]<cur_ida) bl=mid+1;
else br=mid;
}
if(br==r+1){
cost=1e9;break;
}
auto tmp=qry(1,1,n,l,bl);
if(-tmp[2]>=cur_idb){
cost=1e9;break;
}
cur_idb=-tmp[2];
cost+=(cur_ida+cur_idb)*2;
}
else{
int al=l-1,ar=r;
while(al!=ar){
int mid=(al+ar+1)/2;
auto tmp=qry(1,1,n,mid,r);
if(-(tmp[0]-tmp[1])<cur_idb) al=mid-1;
else ar=mid;
}
if(al==l-1){
cost=1e9;break;
}
auto tmp=qry(1,1,n,ar,r);
if(tmp[0]-tmp[2]>=cur_ida){
cost=1e9;break;
}
cur_ida=tmp[0]-tmp[2];
cost+=(cur_ida+cur_idb)*2;
}
}
ans=min(ans,cost);
}
if(ans==1e9) cout << "-1\n";
else cout << ans << '\n';
}
}
提出情報
| 提出日時 |
|
| 問題 |
E - Delete AB |
| ユーザ |
mulgokizary |
| 言語 |
C++ 23 (gcc 12.2) |
| 得点 |
0 |
| コード長 |
2374 Byte |
| 結果 |
TLE |
| 実行時間 |
4419 ms |
| メモリ |
16108 KiB |
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
0 / 1200 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
000-sample-0.txt |
| All |
000-sample-0.txt, 001-00.txt, 001-01.txt, 001-02.txt, 001-03.txt, 001-04.txt, 001-05.txt, 001-06.txt, 001-07.txt, 001-08.txt, 001-09.txt, 001-10.txt, 010-0.txt, 010-1.txt, 010-2.txt, 010-3.txt, 010-4.txt, 011-0.txt, 011-1.txt, 011-2.txt, 011-3.txt, 011-4.txt, 012-0.txt, 012-1.txt, 012-2.txt, 012-3.txt, 012-4.txt, 013-0.txt, 013-1.txt, 013-2.txt, 013-3.txt, 013-4.txt, 014-0.txt, 014-1.txt, 014-2.txt, 014-3.txt, 014-4.txt, 015-0.txt, 015-1.txt, 015-2.txt, 015-3.txt, 015-4.txt, 016-0.txt, 016-1.txt, 016-2.txt, 016-3.txt, 016-4.txt, 017-0.txt, 017-1.txt, 017-2.txt, 017-3.txt, 017-4.txt, 018-0.txt, 018-1.txt, 018-2.txt, 018-3.txt, 018-4.txt, 018-5.txt, 018-6.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 000-sample-0.txt |
TLE |
4414 ms |
3208 KiB |
| 001-00.txt |
TLE |
4414 ms |
3632 KiB |
| 001-01.txt |
TLE |
4414 ms |
4036 KiB |
| 001-02.txt |
TLE |
4415 ms |
4020 KiB |
| 001-03.txt |
TLE |
4415 ms |
4064 KiB |
| 001-04.txt |
TLE |
4414 ms |
3964 KiB |
| 001-05.txt |
TLE |
4415 ms |
4096 KiB |
| 001-06.txt |
TLE |
4414 ms |
4016 KiB |
| 001-07.txt |
TLE |
4415 ms |
4000 KiB |
| 001-08.txt |
TLE |
4415 ms |
4068 KiB |
| 001-09.txt |
TLE |
4415 ms |
4808 KiB |
| 001-10.txt |
TLE |
4415 ms |
4056 KiB |
| 010-0.txt |
TLE |
4414 ms |
3344 KiB |
| 010-1.txt |
TLE |
4414 ms |
3304 KiB |
| 010-2.txt |
TLE |
4414 ms |
3260 KiB |
| 010-3.txt |
TLE |
4414 ms |
3232 KiB |
| 010-4.txt |
TLE |
4414 ms |
3188 KiB |
| 011-0.txt |
TLE |
4418 ms |
15872 KiB |
| 011-1.txt |
TLE |
4415 ms |
15964 KiB |
| 011-2.txt |
TLE |
4415 ms |
15872 KiB |
| 011-3.txt |
TLE |
4415 ms |
16036 KiB |
| 011-4.txt |
TLE |
4415 ms |
16040 KiB |
| 012-0.txt |
TLE |
4415 ms |
15984 KiB |
| 012-1.txt |
TLE |
4415 ms |
15948 KiB |
| 012-2.txt |
TLE |
4415 ms |
15996 KiB |
| 012-3.txt |
TLE |
4415 ms |
16012 KiB |
| 012-4.txt |
TLE |
4415 ms |
15992 KiB |
| 013-0.txt |
TLE |
4415 ms |
16032 KiB |
| 013-1.txt |
TLE |
4415 ms |
16104 KiB |
| 013-2.txt |
TLE |
4415 ms |
16036 KiB |
| 013-3.txt |
TLE |
4415 ms |
15980 KiB |
| 013-4.txt |
TLE |
4415 ms |
16024 KiB |
| 014-0.txt |
TLE |
4415 ms |
16056 KiB |
| 014-1.txt |
TLE |
4419 ms |
16108 KiB |
| 014-2.txt |
TLE |
4415 ms |
15944 KiB |
| 014-3.txt |
TLE |
4415 ms |
15992 KiB |
| 014-4.txt |
TLE |
4412 ms |
15976 KiB |
| 015-0.txt |
TLE |
4415 ms |
16052 KiB |
| 015-1.txt |
TLE |
4415 ms |
16000 KiB |
| 015-2.txt |
TLE |
4415 ms |
16028 KiB |
| 015-3.txt |
TLE |
4415 ms |
15980 KiB |
| 015-4.txt |
TLE |
4415 ms |
15952 KiB |
| 016-0.txt |
TLE |
4415 ms |
16000 KiB |
| 016-1.txt |
TLE |
4415 ms |
15980 KiB |
| 016-2.txt |
TLE |
4415 ms |
16104 KiB |
| 016-3.txt |
TLE |
4415 ms |
16004 KiB |
| 016-4.txt |
TLE |
4415 ms |
15940 KiB |
| 017-0.txt |
TLE |
4415 ms |
16052 KiB |
| 017-1.txt |
TLE |
4415 ms |
16012 KiB |
| 017-2.txt |
TLE |
4415 ms |
16044 KiB |
| 017-3.txt |
TLE |
4415 ms |
15980 KiB |
| 017-4.txt |
TLE |
4415 ms |
15992 KiB |
| 018-0.txt |
TLE |
4415 ms |
16092 KiB |
| 018-1.txt |
TLE |
4415 ms |
15996 KiB |
| 018-2.txt |
TLE |
4415 ms |
15976 KiB |
| 018-3.txt |
TLE |
4415 ms |
15872 KiB |
| 018-4.txt |
TLE |
4415 ms |
16012 KiB |
| 018-5.txt |
TLE |
4415 ms |
15940 KiB |
| 018-6.txt |
TLE |
4415 ms |
15872 KiB |