답안 #571573

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
571573 2022-06-02T11:17:46 Z nttt Election (BOI18_election) C++14
0 / 100
2 ms 340 KB
#include<bits/stdc++.h>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x >> (i)) & 1)
#define fi first
#define se second
#define ll long long
#define task "name" 

const int oo = 1e9 + 7;
const ll loo = (ll)1e18 + 7;
const int MOD = 1e9 + 7;
const int N = 5e5 + 3;
const int BASE = 10;

template <typename T1, typename T2> bool minimize(T1 &a, T2 b)
{
    if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b)
{
    if (a < b) {a = b; return true;} return false;
}

struct nodes
{
    int sum, pr, sf, ans;
} node[4 * N];

nodes add(nodes l, nodes r)
{
    nodes a;
	a.sum = l.sum + r.sum;
	a.pr = max(l.pr, l.sum + r.pr);
	a.sf = max(r.sf, r.sum + l.sf);
	a.ans = max(l.sum + r.ans, l.ans + r.sum);
	a.ans = max(a.ans, l.pr + r.sf);
	return a;
}
void update(int id, int l, int r, int pos, int x)
{
    if(l > r || pos < l || r < pos) return;
    if(l == r)
    {
        node[id].sum = x;
		node[id].pr = x;
		node[id].sf = x;
		node[id].ans = x;
        return;
    }
    int mid = (l + r)/2;
    update(id * 2, l, mid, pos, x);
    update(id * 2 + 1, mid + 1, r, pos, x);
    node[id] = add(node[id * 2], node[id * 2 + 1]);
}
nodes get(int id, int l, int r, int u, int v)
{
    if(l > r || v < l || r < u) return {0, 0, 0, 0};
    if(u <= l && r <= v) return node[id];
    int mid = (l + r)/2;
    return add(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen(task".inp" , "r" , stdin);
    //freopen(task".out" , "w" , stdout);
    int n;
    string s;
    cin >> n >> s;
    for (int i = 0; i < n; i++)
    {
        if(s[i] == 'T') update(1, 1, n, i + 1, 1);
        else update(1, 1, n, i + 1, -1);
    }
    
    int q;
    cin >> q;
    while(q--)
    {
        int l, r;
        cin >> l >> r;
        cout << get(1, 1, n, l, r).ans << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Incorrect 2 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Incorrect 2 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Incorrect 2 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -