#include <bits/stdc++.h>
#define fi first
#define se second
#define For(i, a, b) for (int i = (a); i <= (b); ++i)
#define ForD(i, a, b) for (int i = (a); i >= (b); --i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repD(i, n) for (int i = (n) - 1; i >= 0; --i)
#define coutE(x) {cout << x << '\n'; return;}
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define bs binary_search
#define ub upper_bound
#define lb lower_bound
#define endl '\n'
#define db long double
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
using namespace std;
void file(string s){
if (s.empty()) return;
freopen((s + ".inp").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
template <class X, class Y>
bool minimize(X &x, Y y){
if (x > y){
x = y;
return 1;
}
return 0;
}
template <class X, class Y>
bool maximize(X &x, Y y){
if (x < y){
x = y;
return 1;
}
return 0;
}
mt19937_64 rng(time(0));
const int N = 1e6 + 5;
const int MOD = 1e9 + 7; //998244353;
const int INF = 1e9;
const long long LINF = 1e18;
const int dx[] = {1, 0, 0, -1};
const int dy[] = {0, 1, -1, 0};
int n, q, a[N];
char s[N];
struct Segment_Tree{
struct Node{
int pre, suf, sum;
} st[4 * N];
Node Merge(Node a, Node b){
Node c;
c.pre = max(a.pre, a.sum + b.pre);
c.suf = max(b.suf, b.sum + a.suf);
c.sum = a.sum + b.sum;
return c;
}
void build(int id, int l, int r){
if (l == r){
st[id] = {max(a[l], 0), max(a[l], 0), a[l]};
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = Merge(st[id << 1], st[id << 1 | 1]);
}
Node get(int id, int l, int r, int u, int v){
if (v < l || r < u) return {0, 0, 0};
if (u <= l && r <= v) return st[id];
int mid = (l + r) >> 1;
return Merge(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
} ST;
void solve(){
cin >> n;
cin >> (s + 1);
For(i, 1, n) a[i] = (s[i] == 'C') ? -1 : 1;
ST.build(1, 1, n);
cin >> q;
while (q--){
int l, r; cin >> l >> r;
Segment_Tree::Node x = ST.get(1, 1, n, l, r);
cout << max(x.pre, x.suf) << endl;
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
file("");
int T = 1;
//cin >> T;
while (T--) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
election.cpp: In function 'void file(std::string)':
election.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen((s + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |