This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mod 1000000007
#define dancila 3.14159265359
#define eps 1e-9
using namespace std;
typedef long long ll;
int n, q;
string s;
int sp[500002], sp2[500002], lstT[500002], lstDT[500002], sT[500002];
int aint[2000002], aint2[2000002];
int wh[2000002], wh2[2000002];
void build(int nod, int st, int dr)
{
if(st == dr)
{
aint[nod] = sp[st];
wh[nod] = st;
return;
}
int mid = (st + dr) / 2;
build(nod << 1, st, mid);
build(nod << 1|1, mid+1, dr);
if(aint[nod << 1] <= aint[nod << 1|1])
{
aint[nod] = aint[nod << 1];
wh[nod] = wh[nod << 1];
}
else
{
aint[nod] = aint[nod << 1|1];
wh[nod] = wh[nod << 1|1];
}
}
void build2(int nod, int st, int dr)
{
if(st == dr)
{
aint2[nod] = sp2[st];
wh2[nod] = st;
return;
}
int mid = (st + dr) / 2;
build2(nod << 1, st, mid);
build2(nod << 1|1, mid+1, dr);
if(aint2[nod << 1] < aint2[nod << 1|1])
{
aint2[nod] = aint2[nod << 1];
wh2[nod] = wh2[nod << 1];
}
else
{
aint2[nod] = aint2[nod << 1|1];
wh2[nod] = wh2[nod << 1|1];
}
}
pair<int, int> query(int nod, int st, int dr, int L, int R)
{
if(dr < L || st > R)
return {100000001, 100000001};
if(L <= st && dr <= R)
return {aint[nod], wh[nod]};
int mid = (st + dr) / 2;
pair<int, int> xst = query(nod << 1, st, mid, L, R);
pair<int, int> xdr = query(nod << 1|1, mid+1, dr, L, R);
if(xst.fi <= xdr.fi)
return xst;
return xdr;
}
pair<int, int> query2(int nod, int st, int dr, int L, int R)
{
if(dr < L || st > R)
return {100000001, 100000001};
if(L <= st && dr <= R)
return {aint2[nod], wh2[nod]};
int mid = (st + dr) / 2;
pair<int, int> xst = query2(nod << 1, st, mid, L, R);
pair<int, int> xdr = query2(nod << 1|1, mid+1, dr, L, R);
if(xst.fi < xdr.fi)
return xst;
return xdr;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
cin >> s;
s = ' ' + s;
for(int i = 1; i <= n; ++i)
sp[i] = sp[i-1] + (s[i] == 'C' ? 1 : -1);
for(int i = n; i >= 1; --i)
sp2[i] = sp2[i+1] + (s[i] == 'C' ? 1 : -1);
for(int i = n; i >= 1; --i)
if(s[i] == 'T')
{
if(i+1 > n || s[i+1] == 'C')
lstT[i] = i;
else
lstT[i] = lstT[i+1];
}
for(int i = 1; i <= n; ++i)
if(s[i] == 'T')
{
if(i == 1 || s[i-1] == 'C')
lstDT[i] = i;
else
lstDT[i] = lstDT[i-1];
}
for(int i = 1; i <= n; ++i)
sT[i] = sT[i-1] + (s[i] == 'T');
build(1, 1, n);
build2(1, 1, n);
cin >> q;
for(; q; --q)
{
int st, dr;
cin >> st >> dr;
if(sT[dr] - sT[st-1] == dr - st + 1)
{
cout << dr - st + 1 << '\n';
continue;
}
int ans = 0;
if(s[st] == 'T')
{
ans += lstT[st] - st + 1;
st = lstT[st] + 1;
}
if(s[dr] == 'T')
{
ans += dr - lstDT[dr] + 1;
dr = lstDT[dr] - 1;
}
// cout << st << " " << dr << " " << ans << '\n';
pair<int, int> bst = query(1, 1, n, st, dr);
pair<int, int> bst2 = query2(1, 1, n, st, dr);
// cout << bst.se << " " << bst2.se << " " << sp[st - 1] << " " << bst.fi << " " << sp2[dr + 1] << " " << bst2.fi << '\n';
if(bst.se < bst2.se)
{
cout << ans - (min(0, bst.fi - sp[st - 1]) + min(0, bst2.fi - sp2[dr + 1])) << '\n';
continue;
}
int val1 = 0, val2 = 0;
// left side
if(sp[st - 1] - bst.fi <= 0);
else
{
pair<int, int> bst3 = query(1, 1, n, st, lstDT[bst.se] - 1);
ans += max(0, sp[st - 1] - bst3.fi);
val1 = bst.fi - sp[st - 1] + max(0, sp[st - 1] - bst3.fi);
// cout << st - 1 << " " << sp[st - 1] << " " << bst3.fi << '\n';
}
if(sp2[dr + 1] - bst2.fi <= 0);
else
{
pair<int, int> bst4 = query2(1, 1, n, lstT[bst2.se] + 1, dr);
ans += max(0, sp2[dr + 1] - bst4.fi);
val2 = bst2.fi - sp2[dr + 1] + max(0, sp2[dr + 1] - bst4.fi);
// cout << dr + 1 << " " << sp2[dr + 1] << " " << bst4.fi << '\n';
}
ans -= min(val1, val2);
// cout << val1 << " " << val2 << '\n';
cout << ans << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |