답안 #712651

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
712651 2023-03-19T11:38:56 Z ssense Election (BOI18_election) C++17
0 / 100
2 ms 1104 KB
#include <bits/stdc++.h>
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
typedef long long  ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define pb push_back
#define sc second
#define fr first
//#define int long long
#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);} int factorial(int n, int mod) {if (n > 1)return (n * factorial(n - 1, mod)) % mod; else return 1;} int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}struct prefix_sum{vint pref;void build(vint a){pref.pb(0);for(int i = 0; i < a.size(); i++){pref.pb(pref.back()+a[i]);}}int get(int l, int r){return pref[r]-pref[l-1];}};//mesanu

const int N = 2005;

pair<int, int> sparsePrefix[19][N+1], sparseSuffix[19][N+1];

void solve()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    int now = 0;
    vint pref, suffix(n+2);
    pref.pb(0);
    for(int i = 0; i < n; i++)
    {
        if(s[i] == 'C')
        {
            pref.push_back(--now);
        }
        else
        {
            pref.push_back(++now);
        }
    }
    now = 0;
    for(int i = n; i >= 1; i--)
    {
        if(s[i-1] == 'C')
        {
            suffix[i] = --now;
        }
        else
        {
            suffix[i] = ++now;
        }
    };
    for(int i = 1; i <= n; i++)
    {
        sparsePrefix[0][i] = {pref[i], i};
        sparseSuffix[0][i] = {suffix[i], i};
    }
    for(int l = 1; l < 19; l++)
    {
        for(int i = 1; i <= n; i++)
        {
            if(i+(1<<l)-1 > n){continue;}
            //PREFIX
            int mx = max(sparsePrefix[l-1][i].fr, sparsePrefix[l-1][i+(1<<(l-1))].fr);
            int ind = 0;
            if(mx == sparsePrefix[l-1][i].fr){ind = sparsePrefix[l-1][i].sc;}
            else{ind = sparsePrefix[l-1][i+(1<<(l-1))].sc;}
            sparsePrefix[l][i] = {mx, ind};
            //SUFFIX
            mx = max(sparseSuffix[l-1][i].fr, sparseSuffix[l-1][i+(1<<(l-1))].fr);
            ind = 0;
            if(mx == sparseSuffix[l-1][i+(1<<(l-1))].fr){ind = sparseSuffix[l-1][i+(1<<(l-1))].sc;}
            else{ind = sparseSuffix[l-1][i].sc;}
            sparseSuffix[l][i] = {mx, ind};
        }
    }
    int q;
    cin >> q;
    while(q--)
    {
        int l, r;
        cin >> l >> r;
        //PREFIX
        int dist = 31-__builtin_clz(r-l+1);
        int mxPref = max(sparsePrefix[dist][l].fr, sparsePrefix[dist][r-(1<<(dist))+1].fr);
        int indPref = 0;
        if(mxPref == sparsePrefix[dist][l].fr){indPref = sparsePrefix[dist][l].sc;}
        else{indPref = sparsePrefix[dist][r-(1<<(dist))+1].sc;}
        mxPref-=pref[l-1];
        
        int mxSuff = max(sparseSuffix[dist][l].fr, sparseSuffix[dist][r-(1<<l)+1].fr);
        int indSuff = 0;
        if(mxSuff == sparseSuffix[dist][r-(1<<l)+1].fr){indSuff = sparseSuffix[dist][r-(1<<l)+1].sc;}
        else{indSuff = sparseSuffix[dist][l].sc;}
        mxSuff-=suffix[r+1];
        int ans = mxPref+mxSuff;
        if(indPref < indSuff)
        {
            cout << ans << endl;
            continue;
        }
        int nwr = indSuff-1;
        int beforePref = 0;
        if(nwr >= l)
        {
            int temp = 31-__builtin_clz(nwr-l+1);
            beforePref+=max(sparsePrefix[temp][l].fr, sparsePrefix[temp][nwr-(1<<(temp))+1].fr)-pref[l-1];
            beforePref = max(0, beforePref);
        }
        int nwl = indPref+1;
        int beforeSuff = 0;
        if(nwl <= r)
        {
            int temp = 31-__builtin_clz(r-nwl+1);
            beforeSuff+=max(sparseSuffix[temp][nwl].fr, sparseSuffix[temp][r-(1<<(temp))+1].fr)-suffix[r+1];
            beforeSuff = max(0, beforeSuff);
        }
        ans-=min(mxPref-beforePref, mxSuff-beforeSuff);
        cout << ans << endl;
    }
}

int32_t main(){
    startt
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}


/*
11
CCCTTTTTTCC
3
1 11
4 9
1 6
 */

Compilation message

election.cpp: In member function 'void prefix_sum::build(std::vector<int>)':
election.cpp:20:667: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);} int factorial(int n, int mod) {if (n > 1)return (n * factorial(n - 1, mod)) % mod; else return 1;} int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}struct prefix_sum{vint pref;void build(vint a){pref.pb(0);for(int i = 0; i < a.size(); i++){pref.pb(pref.back()+a[i]);}}int get(int l, int r){return pref[r]-pref[l-1];}};//mesanu
      |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 1104 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 1104 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 1104 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -