Submission #583580

#TimeUsernameProblemLanguageResultExecution timeMemory
583580Tam_theguideElection (BOI18_election)C++14
100 / 100
819 ms46052 KiB
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define FORC(i, a, b, c) for (int i = (a), _b = (b); i <= _b; i+=(c))
#define FORM(i, a, b, c) for (int i = (a), _b = (b); i <= _b; i*=(c))
#define FORS(i, a, b) for (int i = (a), _b = (b); i < _b; i++)
#define ll long long
#define fi first
#define se second
#define pb push_back
#define MASK(i) (1 << (i))
#define MASKLL(i) (1LL << (i))
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
#define revall(x) (x).rbegin(), (x).rend()
#define print(x) cout<<"["<<(x)<<"] "
#define gc(x) ((x) - 'a')
#define popcountll(x) __builtin_popcountll((x))
#define popcount(x) __builtin_popcount((x))
void minimize(int& a, int b){ a = min(a, b); }
void maximize(int& a, int b){ a = max(a, b); }
void setIO(string name = ""){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if ((int)(name.size())){
        freopen((name + ".inp").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}
const int N = 5e5 + 5;
const int INF = 1e9;
//--segtree---
struct node{
    int sum, suf;
}seg[4 * N];
node comb(node a, node b){
    node tmp;
    tmp.sum = a.sum + b.sum;
    tmp.suf = min(b.suf, b.sum + a.suf);
    return tmp;
}
void update(int root, int tl, int tr, int pos, int val){
    if (tl == tr){
        seg[root].sum = seg[root].suf = val;
        return;
    }
    int tm = (tl + tr) >> 1;
    if (pos <= tm) update(2 * root, tl, tm, pos, val);
    else update(2 * root + 1, tm + 1, tr, pos, val);
    seg[root] = comb(seg[2 * root], seg[2 * root + 1]);
}
node query(int root, int tl, int tr, int l, int r){
    if (tl > r || tr < l) return {0, INF};
    if (tl >= l && tr <= r) return seg[root];
    int tm = (tl + tr) >> 1;
    return comb(query(2 * root, tl, tm, l, r), query(2 * root + 1, tm + 1, tr, l, r));
}
//--basic inp---
int n , q , a[N];
//--truy van va kq---
vector<pii> qr[N];
vector<int> zeropos;
int res[N];
int bs(int j){
    int mid, left = 0, right = zeropos.size() - 1;
    int ret = zeropos.size();
    while (left <= right){
        mid = left + (right - left) / 2;
        if (zeropos[mid] <= j) ret = mid, right = mid - 1;
        else left = mid + 1;
    }
    return zeropos.size() - ret;
}
int main(){
    setIO();
    cin >> n;
    FOR(i, 1, n){
        char x; cin >> x;
        int v = (x == 'C' ? 1 : -1);
        a[i] = v;
        update(1, 1, n, i, v);
    }
    cin >> q;
    FOR(i, 1, q){
        int l, r; cin >> l >> r;
        qr[l].pb({r, i});
    }
    FORD(i, n, 1){
        if (a[i] == -1){
            a[i] = 0;
            zeropos.pb(i);
            update(1, 1, n, i, 0);
        }else{
            if (!zeropos.empty()){
                int tmp = zeropos.back();
                update(1, 1, n, tmp, -1);
                a[tmp] = -1;
                zeropos.pop_back();
            }
            update(1, 1, n, i, 1);
        }
        for (auto c: qr[i]){
            res[c.se] = -min(0, query(1, 1, n, i, c.fi).suf) + bs(c.fi);
        }
    }
    FOR(i, 1, q) cout << res[i] << '\n';
}

Compilation message (stderr)

election.cpp: In function 'void setIO(std::string)':
election.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         freopen((name + ".inp").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...