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 task "E"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 5e5 + 2, INF = 1e9;
int n, q, ans[N], a[N];
string s;
vii qu[N];
struct Node
{
int sum, suf;
} it[N << 2];
void build(int id, int l, int r)
{
if (l == r)
{
it[id] = {a[l], a[l]};
return;
}
int m = (l + r) >> 1;
build(id << 1, l, m);
build((id << 1) | 1, m + 1, r);
it[id] = {it[id << 1].sum + it[(id << 1) | 1].sum, min(it[id << 1].suf + it[(id << 1) | 1].sum, it[(id << 1) | 1].suf)};
}
void update(int id, int l, int r, int p, int v)
{
if (l > p || r < p) return;
if (l == r)
{
it[id] = {v, v};
return;
}
int m = (l + r) >> 1;
update(id << 1, l, m, p, v);
update((id << 1) | 1, m + 1, r, p, v);
it[id] = {it[id << 1].sum + it[(id << 1) | 1].sum, min(it[id << 1].suf + it[(id << 1) | 1].sum, it[(id << 1) | 1].suf)};
}
Node query(int id, int l, int r, int L, int R)
{
if (l > R || r < L) return {0, INF};
if (L <= l && r <= R) return it[id];
int m = (l + r) >> 1;
Node Le = query(id << 1, l, m, L, R), Ri = query((id << 1) | 1, m + 1, r, L, R);
return {Le.sum + Ri.sum, min(Le.suf + Ri.sum, Ri.suf)};
}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n >> s >> q;
rep(i, 1, q)
{
int l, r;
cin >> l >> r;
qu[l].eb(r, i);
}
rep(i, 1, n) a[i] = (s[i - 1] == 'C' ? 1 : -1);
build(1, 1, n);
vi st;
Rep(i, n, 1)
{
if (a[i] == 1)
{
if (!st.empty())
{
update(1, 1, n, st.back(), a[st.back()]);
st.pop_back();
}
} else
{
update(1, 1, n, i, 0);
st.pb(i);
}
for (const ii &Q : qu[i])
{
// PR(st, 0, SZ(st) - 1);
int l = 0, r = SZ(st) - 1;
while (l <= r)
{
int m = (l + r) >> 1;
if (st[m] <= Q.F) l = m + 1;
else r = m - 1;
}
ans[Q.S] = l;
Node res = query(1, 1, n, i, Q.F);
ans[Q.S] -= min(0, res.suf);
}
}
rep(i, 1, q) cout << ans[i] << '\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... |