//Author: timg8710
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> pii;
#define pb push_back
#define f first
#define s second
struct node{
int sum, l, r, res;
node(int v){
sum = v;
l = r = res = min(0, v);
}
node(){
sum = l = r = res = 0;
}
node comb(node& other){
node h;
h.sum = sum + other.sum;
h.l = min(l, sum + other.l);
h.r = min(other.r, other.sum + r);
h.res = min({l + other.r, sum + other.res, res + other.sum});
return h;
}
};
template <typename T>
struct segtree
{
T none; //comb(a, none) = a
T val;
int gL, gR, mid;
segtree<T> *left;
segtree<T> *right;
T comb(T& l, T& r)
{
return l.comb(r);
}
segtree(int l, int r, T arr[], T non)
{ //modify arr type
none = non;
gL = l; gR = r; mid = (gL+gR)/2;
if (l == r)
{
val = arr[l];
}
else
{
left = new segtree<T>(l, mid, arr, non); right = new segtree<T>(mid + 1, r, arr, non);
val = comb(
left->val, right->val);
}
// cout << l << " " << r << " ";
// cout << val.l << " "<< val.r << " " << val.fr << endl;
}
T query(int l, int r)
{
if (gL > r || gR < l)
{
return none;
}
if (gL == l && gR == r)
{
return val;
}
T a = left->query(l, min(r, mid)); T b = right->query(max(l, mid + 1), r);
return comb(a, b); //might be wrong
}
void update(int idx, T set)
{
if (gL == gR)
{
val = set;
}
else
{
if (idx <= (gL + gR) / 2)
{
left->update(idx, set);
}
else
{
right->update(idx, set);
}
val = comb(left->val, right->val);
}
}
};
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n, q, u, v;
char c;
cin >> n;
node nums[n];
for(int i = 0; i<n; i++) {
cin >> c;
nums[i] = node((c == 'T' ? -1 : 1));
}
cin >> q;
segtree<node> sgt(0,n-1, nums, node());
while(q--){
cin >> u; cin >> v;
// cout << res.l << " "<< res.r << " " << res.fr << endl;
cout << -(sgt.query(u-1, v-1).res) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
580 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
572 KB |
Output is correct |
4 |
Correct |
3 ms |
572 KB |
Output is correct |
5 |
Correct |
2 ms |
576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
580 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
572 KB |
Output is correct |
4 |
Correct |
3 ms |
572 KB |
Output is correct |
5 |
Correct |
2 ms |
576 KB |
Output is correct |
6 |
Correct |
130 ms |
13548 KB |
Output is correct |
7 |
Correct |
75 ms |
13496 KB |
Output is correct |
8 |
Correct |
94 ms |
13468 KB |
Output is correct |
9 |
Correct |
92 ms |
13376 KB |
Output is correct |
10 |
Correct |
103 ms |
13400 KB |
Output is correct |
11 |
Correct |
102 ms |
13504 KB |
Output is correct |
12 |
Correct |
117 ms |
13556 KB |
Output is correct |
13 |
Correct |
126 ms |
13700 KB |
Output is correct |
14 |
Correct |
126 ms |
13492 KB |
Output is correct |
15 |
Correct |
124 ms |
13504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
580 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
572 KB |
Output is correct |
4 |
Correct |
3 ms |
572 KB |
Output is correct |
5 |
Correct |
2 ms |
576 KB |
Output is correct |
6 |
Correct |
130 ms |
13548 KB |
Output is correct |
7 |
Correct |
75 ms |
13496 KB |
Output is correct |
8 |
Correct |
94 ms |
13468 KB |
Output is correct |
9 |
Correct |
92 ms |
13376 KB |
Output is correct |
10 |
Correct |
103 ms |
13400 KB |
Output is correct |
11 |
Correct |
102 ms |
13504 KB |
Output is correct |
12 |
Correct |
117 ms |
13556 KB |
Output is correct |
13 |
Correct |
126 ms |
13700 KB |
Output is correct |
14 |
Correct |
126 ms |
13492 KB |
Output is correct |
15 |
Correct |
124 ms |
13504 KB |
Output is correct |
16 |
Correct |
1435 ms |
96120 KB |
Output is correct |
17 |
Correct |
939 ms |
95560 KB |
Output is correct |
18 |
Correct |
1165 ms |
96068 KB |
Output is correct |
19 |
Correct |
986 ms |
95296 KB |
Output is correct |
20 |
Correct |
1383 ms |
95056 KB |
Output is correct |
21 |
Correct |
1454 ms |
96880 KB |
Output is correct |
22 |
Correct |
1458 ms |
96904 KB |
Output is correct |
23 |
Correct |
1436 ms |
97128 KB |
Output is correct |
24 |
Correct |
1493 ms |
96728 KB |
Output is correct |
25 |
Correct |
1459 ms |
96360 KB |
Output is correct |