답안 #583580

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
583580 2022-06-25T15:51:32 Z Tam_theguide Election (BOI18_election) C++14
100 / 100
819 ms 46052 KB
#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

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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12116 KB Output is correct
2 Correct 8 ms 12116 KB Output is correct
3 Correct 8 ms 12116 KB Output is correct
4 Correct 8 ms 12116 KB Output is correct
5 Correct 9 ms 12096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12116 KB Output is correct
2 Correct 8 ms 12116 KB Output is correct
3 Correct 8 ms 12116 KB Output is correct
4 Correct 8 ms 12116 KB Output is correct
5 Correct 9 ms 12096 KB Output is correct
6 Correct 86 ms 17356 KB Output is correct
7 Correct 70 ms 16800 KB Output is correct
8 Correct 74 ms 16860 KB Output is correct
9 Correct 76 ms 17204 KB Output is correct
10 Correct 89 ms 17288 KB Output is correct
11 Correct 80 ms 17484 KB Output is correct
12 Correct 77 ms 17388 KB Output is correct
13 Correct 91 ms 17612 KB Output is correct
14 Correct 78 ms 17460 KB Output is correct
15 Correct 76 ms 17372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12116 KB Output is correct
2 Correct 8 ms 12116 KB Output is correct
3 Correct 8 ms 12116 KB Output is correct
4 Correct 8 ms 12116 KB Output is correct
5 Correct 9 ms 12096 KB Output is correct
6 Correct 86 ms 17356 KB Output is correct
7 Correct 70 ms 16800 KB Output is correct
8 Correct 74 ms 16860 KB Output is correct
9 Correct 76 ms 17204 KB Output is correct
10 Correct 89 ms 17288 KB Output is correct
11 Correct 80 ms 17484 KB Output is correct
12 Correct 77 ms 17388 KB Output is correct
13 Correct 91 ms 17612 KB Output is correct
14 Correct 78 ms 17460 KB Output is correct
15 Correct 76 ms 17372 KB Output is correct
16 Correct 722 ms 44424 KB Output is correct
17 Correct 704 ms 40488 KB Output is correct
18 Correct 643 ms 41512 KB Output is correct
19 Correct 613 ms 42996 KB Output is correct
20 Correct 677 ms 43448 KB Output is correct
21 Correct 737 ms 45784 KB Output is correct
22 Correct 819 ms 45356 KB Output is correct
23 Correct 793 ms 46052 KB Output is correct
24 Correct 663 ms 45428 KB Output is correct
25 Correct 773 ms 44688 KB Output is correct