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>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct FenwickTree{
int n;
vector<int> a;
FenwickTree(int _n){
n = _n;
a.resize(n+1);
}
void update(int i, int v){
while(i <= n){
a[i] += v;
i += LASTBIT(i);
}
}
int get(int i){
int ans = 0;
while(i > 0){
ans += a[i];
i -= LASTBIT(i);
}
return ans;
}
int get(int l, int r){return get(r) - get(l-1);}
};
struct SegmentTree{
int n;
vector<int> a, lazy;
SegmentTree(int _n){
n = _n;
a.resize(n * 4 + 4); lazy.resize(n * 4 + 4);
}
void down(int id){
#define push(x) {lazy[x] += lazy[id]; a[x] += lazy[id];}
push(id * 2); push(id * 2 + 1);
lazy[id] = 0;
}
void update(int u, int v, int val, int l, int r, int id){
if (u <= l && r <= v){
a[id] += val;
lazy[id] += val;
return;
}
if (lazy[id]) down(id);
int mid = (l + r) >> 1;
if (u <= mid) update(u, v, val, l, mid, id * 2);
if (v > mid) update(u, v, val, mid + 1, r, id * 2 + 1);
a[id] = min(a[id * 2], a[id * 2 + 1]);
}
void update(int u, int v, int val){
update(u, v, val, 1, n, 1);
}
int get(int u, int v, int l, int r, int id){
if (u <= l && r <= v) return a[id];
if (lazy[id]) down(id);
int mid = (l + r) >> 1;
int ans = 1e9;
if (u <= mid) minimize(ans, get(u, v, l, mid, id * 2));
if (v > mid) minimize(ans, get(u, v, mid + 1, r, id * 2 + 1));
return ans;
}
int get(int u, int v){return get(u, v, 1, n, 1);}
};
const int N = 5e5 + 69;
vector<int> graph[N];
vector<pair<int, int>> queries[N];
int ans[N];
void dfs(int u, FenwickTree &bit, SegmentTree &st){
if (u) {
bit.update(u, 1);
st.update(1, u, 1);
}
for(pair<int, int> j: queries[u+1]){
int idx = j.second, r = j.first;
ans[idx] = bit.get(u + 1, r) + (st.get(r + 1, r + 1) - st.get(u + 1, r + 1));
}
for(int v: graph[u]) dfs(v, bit, st);
if (u) {
bit.update(u, -1);
st.update(1, u, -1);
}
}
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
int n; cin >> n;
string s; cin >> s;
s = "#" + s;
vector<int> a(n+1);
for(int i= 1; i<=n; ++i){
a[i] = a[i-1];
if (s[i] == 'C') a[i]++;
else a[i]--;
}
vector<int> st;
vector<int> nxt(n+1, n+1);
for(int i = n; i>=0; --i){
while(st.size() && a[st.back()] >= a[i]) st.pop_back();
if (st.size()) nxt[i] = st.back();
st.push_back(i);
}
for(int i = 0; i<=n; ++i) graph[nxt[i]].push_back(i);
int q; cin >> q;
for(int i = 1; i<=q; ++i) {
int l, r; cin >> l >> r;
queries[l].push_back({r, i});
}
vector<int> b(n+2);
for(int i = n; i>=1; --i){
b[i] = b[i+1];
if (s[i] == 'C') b[i]++;
else b[i]--;
}
FenwickTree bit(n);
SegmentTree segg(n+1);
for(int i = 1; i<=n+1; ++i) segg.update(i, i, b[i]);
dfs(n+1, bit, segg);
for(int i = 1; i<=q; ++i)
cout << ans[i] << " ";
cout << "\n";
cerr << "Time elapsed: " << clock() - start << " ms\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... |