답안 #571465

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
571465 2022-06-02T09:38:53 Z BackNoob Election (BOI18_election) C++14
100 / 100
1095 ms 64404 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1) 
#define TASK "task"
#define sz(s) (int) (s).size()

using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 24211007;
const ll infll = 1e18 + 7;
const int base = 200;
const int LOG = 29;
 
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;
}

int pre[mxN];

struct IT{
	int n;
	vector<int> tmax, tmin;
	IT(){}
	IT(int _n) {
		n = _n;
		tmax.resize(n * 4 + 7, -inf);
		tmin.resize(n * 4 + 7, inf);
	}

	void update(int v, int tl, int tr, int pos, int val)
	{
		if(tl == tr) {
			tmax[v] = val;
			tmin[v] = val;
		}
		else {
			int tm = (tl + tr) / 2;
			if(pos <= tm) update(v * 2, tl, tm, pos, val);
			else update(v * 2 + 1, tm + 1, tr, pos, val);
			tmax[v] = max(tmax[v * 2], tmax[v * 2 + 1]);
			tmin[v] = min(tmin[v * 2], tmin[v * 2 + 1]);
		}
	}
	int getmax(int v, int tl, int tr, int l, int r)
	{
		if(l > r) return -inf;
		if(tl == l && tr == r) return tmax[v];
		int tm = (tl + tr) / 2;
		int m1 = getmax(v * 2, tl, tm, l, min(r, tm));
		int m2 = getmax(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
		return max(m1, m2);
	}
	int getmin(int v, int tl, int tr, int l, int r)
	{
		if(l > r) return inf;
		if(tl == l && tr == r) return tmin[v];
		int tm = (tl + tr) / 2;
		int m1 = getmin(v * 2, tl, tm, l, min(r, tm));
		int m2 = getmin(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
		return min(m1, m2);
	}
	void update(int pos, int val)
	{
		update(1, 1, n, pos, val);
	}

	int getmax(int l, int r)
	{
		return getmax(1, 1, n, l, r);
	}
	int getmin(int l, int r)
	{
		return getmin(1, 1, n, l, r);
	}

} seg;

struct node{
	int lef, rig, sum, mx;
};

struct IT1{
	int n;
	vector<node> t;
	IT1(){}
	IT1(int _n) {
		n = _n;
		t.resize(n * 4 + 7, {0, 0, 0, -inf});
	}

	node modify(node a, node b)
	{
		if(a.mx == -inf) return b;
		if(b.mx == -inf) return a;

		node res;
		res.mx = max(a.mx, b.mx);
		res.sum = a.sum + b.sum;

		res.lef = max(a.lef, a.sum + b.lef);
		res.rig = max(b.rig, b.sum + a.rig);

		maximize(res.mx, res.lef);
		maximize(res.mx, res.rig);
		maximize(res.mx, a.rig + b.lef);
		return res;
	}

	void update(int v, int tl, int tr, int pos, int val)
	{
		if(tl == tr) {
			if(val == -1) t[v] = {0, 0, -1, 0};
			else t[v] = {1, 1, 1, 1};
		}
		else {
			int tm = (tl + tr) / 2;
			if(pos <= tm) update(v * 2, tl, tm, pos, val);
			else update(v * 2 + 1, tm + 1, tr, pos, val);
			t[v] = modify(t[v * 2], t[v * 2 + 1]);
		}
	}

	node getans(int v, int tl, int tr, int l, int r)
	{
		if(l > r) return {0, 0, 0, -inf};
		if(tl == l && tr == r) return t[v];
		int tm = (tl + tr) / 2;
		node m1 = getans(v * 2, tl, tm, l, min(r, tm));
		node m2 = getans(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
		node res = modify(m1, m2);
		// cout << m1.lef << ' ' << m1.rig << ' ' << m1.sum << ' ' << m1.mx << endl;
		// cout << m2.lef << ' ' << m2.rig << ' ' << m2.sum << ' ' << m2.mx << endl;
		// cout << res.lef << ' ' << res.rig << ' ' << res.sum << ' ' << res.mx << endl;
		return res;
	}
	int getans(int l, int r)
	{
		return getans(1, 1, n, l, r).mx;
	}

	void update(int pos, int val)
	{
		update(1, 1, n, pos, val);
	}
} seg1;

int n;
int q;
int a[mxN];
int maxleng[mxN];
string s;


void solve()
{	
	cin >> n;
	cin >> s;

	for(int i = 1; i <= n; i++) {
		if(s[i - 1] == 'C') a[i] = 1;
		else a[i] = -1;
	}

	for(int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];
	seg = IT(n);
	seg1 = IT1(n);
	for(int i = 1; i <= n; i++) seg.update(i, pre[i]);

	for(int i = 1; i <= n; i++) seg1.update(i, a[i]);
	
	for(int i = 1; i <= n; i++) {
		if(a[i] == -1) maxleng[i] = 0;
		else maxleng[i] = maxleng[i - 1] + a[i];
	}

	cin >> q;
	while(q--) {
		int l, r;
		cin >> l >> r;

		int mn = seg.getmin(l, r) - pre[l - 1];
		int res = 0;
		int last = pre[r] - pre[l - 1];
		// cout << last << ' ';
		if(mn < 0) {
			res += -mn;
			last += -mn;
		}

		// cout << last << ' ' << seg1.getans(l, r) << endl;


		res += max(0, seg1.getans(l, r) - last);
		cout << res << endl;
	}
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen(TASK".in" , "r" , stdin);
    //freopen(TASK".out" , "w" , stdout);
		
    int tc = 1;
    // cin >> tc;
    while(tc--) {
    	solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 116 ms 8516 KB Output is correct
7 Correct 97 ms 9044 KB Output is correct
8 Correct 96 ms 9036 KB Output is correct
9 Correct 87 ms 9036 KB Output is correct
10 Correct 115 ms 9004 KB Output is correct
11 Correct 95 ms 9200 KB Output is correct
12 Correct 94 ms 9136 KB Output is correct
13 Correct 93 ms 9184 KB Output is correct
14 Correct 108 ms 9164 KB Output is correct
15 Correct 100 ms 9116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 116 ms 8516 KB Output is correct
7 Correct 97 ms 9044 KB Output is correct
8 Correct 96 ms 9036 KB Output is correct
9 Correct 87 ms 9036 KB Output is correct
10 Correct 115 ms 9004 KB Output is correct
11 Correct 95 ms 9200 KB Output is correct
12 Correct 94 ms 9136 KB Output is correct
13 Correct 93 ms 9184 KB Output is correct
14 Correct 108 ms 9164 KB Output is correct
15 Correct 100 ms 9116 KB Output is correct
16 Correct 917 ms 63524 KB Output is correct
17 Correct 772 ms 63004 KB Output is correct
18 Correct 873 ms 63224 KB Output is correct
19 Correct 782 ms 62688 KB Output is correct
20 Correct 976 ms 62812 KB Output is correct
21 Correct 1010 ms 64232 KB Output is correct
22 Correct 1016 ms 64104 KB Output is correct
23 Correct 1095 ms 64404 KB Output is correct
24 Correct 981 ms 64048 KB Output is correct
25 Correct 921 ms 63560 KB Output is correct