#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef queue<int> qi;
typedef set<int> si;
typedef priority_queue<int> pqi;
typedef priority_queue<ll> pql;
typedef priority_queue<int,vi,greater<int> > pqi_g;
typedef priority_queue<ll,vll,greater<ll> > pql_g;
#define F first
#define S second
#define PB push_back
#define PF push_front
#define MP make_pair
#define FOR(i,a,b) for (int i=a; i<=b; i++)
#define _FOR(i,a,b) for (int i=a; i>=b; i--)
#define F0R(i,a,b,x) for (int i=a; i<=b; i+=x)
#define FOR_(x,v) for (auto x : v)
#define F0R_(x,v) for (auto &x : v)
const int INF = 0x3f3f3f3f;
const int maxn = 5e5+5;
const ll MOD = 1e9+7;
//? link: https://oj.uz/problem/view/BOI18_election
//? sol:
int n, q;
class segtree {
ll d[4*maxn];
private:
ll inline ls(ll x) {return x<<1;}
ll inline rs(ll x) {return x<<1|1;}
ll inline op(ll u,ll v) {return min(u,v);} // good for generalization
public:
ll a[maxn];
void init(ll s=1,ll t=n,ll p=1) {
if (s==t) {
d[p] = a[s];
return;
}
ll mid = s + ((t-s)>>1);
init(s, mid, ls(p));
init(mid+1, t, rs(p));
d[p] = op(d[ls(p)], d[rs(p)]);
}
ll query(ll l,ll r,ll s=1,ll t=n,ll p=1) { // query(l,r)
if (l<=s && t<=r) return d[p];
ll mid = s + ((t-s)>>1), cnt=0;
if (l<=mid) cnt=op(cnt, query(l,r,s,mid,ls(p)));
if (r>mid) cnt=op(cnt, query(l,r,mid+1,t,rs(p)));
return cnt;
}
void upd(ll v,ll x,ll s=1,ll t=n,ll p=1) { // upd pos v into value x ~ p=cur pos, s,t=cur range
if (s==t) {
d[p] = x;
return;
}
ll mid = s + ((t-s)>>1);
if (v<=mid) upd(v,x,s,mid,ls(p));
else upd(v,x,mid+1,t,rs(p));
d[p] = op(d[ls(p)], d[rs(p)]);
}
} pre, suf;
int a[maxn];
int main()
{
// faster cin/cout
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("c.in","r",stdin);
cin >> n;
FOR(i,1,n) {
char c; cin>>c;
a[i] = (c=='C'? 1:-1);
}
FOR(i,1,n) pre.a[i]=pre.a[i-1]+a[i];
_FOR(i,n,1) suf.a[i]=suf.a[i+1]+a[i];
pre.init();
suf.init();
cin >> q;
FOR(_,1,q) {
int l,r; cin>>l>>r;
int pr = 0-min((pre.query(l,r)-pre.a[l-1]-1)/2,(ll)0);
int sf = 0-min((suf.query(l,r)-suf.a[r+1]-1)/2,(ll)0);
cout << pr+sf << endl;
}
return 0;
}
Compilation message
election.cpp: In function 'int main()':
election.cpp:83:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen("c.in","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
154 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
154 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
154 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |