Submission #1368813

#TimeUsernameProblemLanguageResultExecution timeMemory
1368813blackscreen1Election (BOI18_election)C++20
100 / 100
518 ms85564 KiB
#include <bits//stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;
#define ll long long
#define iloop(m, h) for (auto i = m; i != h; i += (m < h ? 1 : -1))
#define jloop(m, h) for (auto j = m; j != h; j += (m < h ? 1 : -1))
#define kloop(m, h) for (auto k = m; k != h; k += (m < h ? 1 : -1))
#define lloop(m, h) for (auto l = m; l != h; l += (m < h ? 1 : -1))
#define pll pair<ll, ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
ll n, q, l, r;
ll a[500005];
char c;
struct nv {
	ll pv, sv, tv, v;
	nv () {pv = sv = v = tv = 0;}
	nv (ll x) {
		pv = sv = v = max(0LL, x);
		tv = x;
	}
	nv operator +(const nv &other) {
		nv res = nv();
		res.pv = max(this->pv, this->tv + other.pv);
		res.sv = max(other.sv, other.tv + this->sv);
		res.tv = this->tv + other.tv;
		res.v = max({this->pv + other.sv, this->tv + other.v, this->v + other.tv});
		return res;
	}
};
struct node {
	ll s, e, m;
	nv v;
	node *l, *r;
	node (ll S, ll E) {
		s = S, e = E, m = (S+E)>>1;
		if (s == e) {v = nv(a[s]); return;}
		l = new node(s, m);
		r = new node(m+1, e);
		v = l->v + r->v;
	}
	nv qu(ll S, ll E) {
		if (s == S && e == E) return v;
		if (E <= m) return l->qu(S, E);
		if (S > m) return r->qu(S, E);
		return l->qu(S, m) + r->qu(m+1, E);
	}
};
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	iloop(0, n) {cin >> c; a[i] = (c == 'C' ? -1 : 1);}
	cin >> q;
	node root = node(0, n-1);
	while (q--) {
		cin >> l >> r;
		l--, r--;
		cout << root.qu(l, r).v << "\n";
	}
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...