/**
* author : Lăng Trọng Đạt
* created: 14-10-2024
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int long long
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define FOR(i, a, b) for (int i = (a); (i) <= (b); (i++))
#define FD(i, lo, up) for (int i = (up); (i) >= (lo); i--)
#define si(x) (int)(x.size())
bool mx(int& a, int b) { if (b > a) {a = b; return true;} return false;}
bool mi(int& a, int b) { if (b < a) {a = b; return true;} return false;}
using pii = pair<int, int>;
const int INF = 1e18 + 5;
const int MOD = 1e9 + 7;
const int N = 1e6 + 5;
int g[N];
int n, m, k, q, a, b, c;
string s;
struct Data {
int res, open;
Data(int a, int b) {
res = a; open = b;
}
Data() {
}
};
struct Info {
Data up, down;
// up: {number of deleted vote, number of open}
} st[4*N];
Data operator+(Data& a, Data& b) {
return {a.res + b.res - min(b.res, a.open), a.open + b.open - min(b.res, a.open)};
}
Info operator+(Info a, Info b) {
return {a.up + b.up, b.down + a.down};
}
void build(int v = 1, int l = 1, int r = n) {
if (l == r) {
st[v].up = st[v].down = (s[l - 1] == 'C' ? Data(0, 1) : Data(1, 0));
} else {
int mid = (l + r) / 2;
build(2*v, l, mid);
build(2*v + 1, mid + 1, r);
st[v] = st[2*v] + st[2*v + 1];
}
}
Info get(int p, int q, int v = 1, int l = 1, int r = n) {
if (p > r or l > q) return st[0];
else if (p <= l && r <= q) return st[v];
else {
int mid = (l + r) / 2;
return get(p, q, 2*v, l, mid) + get(p, q, 2*v + 1, mid + 1, r);
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("hi.inp", "r")) {
freopen("hi.inp", "r", stdin);
// freopen("hi.out", "w", stdout);
}
cin >> n >> s >> q;
build();
FOR(i, 1, q) {
cin >> a >> b;
int open = 0, res = 0, res2 = 0;
FOR(i, a, b) {
(s[i - 1] == 'C' ? open++ : open--);
if (open == -1) {
res++;
open = 0;
}
}
open = 0;
FD(i, a, b) {
(s[i - 1] == 'C' ? open++ : open--);
if (open == -1) {
res2++;
open = 0;
}
}
cout << max(res, res2) << "\n";
// Info res = get(a, b);
// cout << max(res.down.res, res.up.res) << "\n";
}
}
Compilation message
election.cpp: In function 'int32_t main()':
election.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen("hi.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |