Submission #1262220

#TimeUsernameProblemLanguageResultExecution timeMemory
1262220InvMODElection (BOI18_election)C++17
100 / 100
450 ms34488 KiB
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define eb emplace_back

#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())

template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}

#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"

using ll = long long;
using ld = long double;

template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}

struct Node{ // copy code USACO
    int pre, suf, ans, sum;

    Node(int v = 0): sum(v), pre(min(v, 0)), suf(min(v, 0)), ans(min(v, 0)) {}

    friend Node operator + (Node a, Node b){
        Node val;

        val.pre = min(a.pre, a.sum + b.pre);
        val.suf = min(b.suf, b.sum + a.suf);
        val.ans = min({a.sum + b.ans, a.ans + b.sum, a.pre + b.suf});
        val.sum = a.sum + b.sum;

        return val;
    }
};

struct SMT{
    vector<Node> st; int trsz;

    SMT(int n = 0): st((n << 2) | 1), trsz(n) {}

    void update(int id, int l, int r, int x, int val){
        if(l == r){
            st[id] = Node(val);
        }
        else{
            int m = l+r>>1;
            if(x <= m)
                update(id << 1, l, m, x, val);
            else update(id << 1|1, m + 1, r, x, val);
            st[id] = st[id << 1] + st[id << 1|1];
        }
    }

    Node get(int id, int l, int r, int u, int v){
        if(l > v || r < u) return Node();
        if(l >= u && r <= v) return st[id];

        int m = l+r>>1;
        return get(id << 1, l, m, u, v) + get(id << 1|1, m+1, r, u, v);
    }

    void update(int x, int val){
        update(1, 1, trsz, x, val);
    }

    int query(int l, int r){
        return -1 * get(1, 1, trsz, l, r).ans;
    }
};


void Main()
{
    int n; cin >> n;

    SMT st(n);
    FOR(i, 1, n){
        char c; cin >> c;

        st.update(i, (c == 'C' ? +1 : -1));
    }

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

        cout << st.query(l, r) << el;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    int t = 1; while(t--) Main();
    return 0;
}

Compilation message (stderr)

election.cpp: In function 'int32_t main()':
election.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...