답안 #758240

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
758240 2023-06-14T10:10:24 Z LucaIlie Election (BOI18_election) C++17
0 / 100
5 ms 340 KB
#include <bits/stdc++.h>

using namespace std;

struct info {
    int elim, sum, minPref, minSuf;

    info operator + ( const info &r ) const {
        info l = *this;
        info ans;

        ans.elim = -min( l.minPref + r.minSuf, min( r.sum - l.elim, l.sum - r.elim ) );
        ans.sum = l.sum + r.sum;
        ans.minPref = l.minPref + (l.minPref == l.sum ? r.minPref : 0);
        ans.minSuf = r.minSuf + (r.minSuf == r.sum ? l.minPref : 0);

        return ans;
    }
};

const int MAX_N = 5e5;
int vote[MAX_N + 1];
info segTree[4 * MAX_N];

void init( int v, int l, int r ) {
    if ( l == r ) {
        segTree[v] = { (vote[l] == -1), vote[l], -(vote[l] == -1), -(vote[l] == -1) };
        return;
    }
    int mid = (l + r) / 2;
    init( v * 2 + 1, l, mid );
    init( v * 2 + 2, mid + 1, r );
    segTree[v] = segTree[v * 2 + 1] + segTree[v * 2 + 2];
}

info query( int v, int l, int r, int lq, int rq ) {
    if ( lq <= l && r <= rq )
        return segTree[v];

    int mid = (l + r) / 2;
    if ( mid + 1 > rq )
        return query( v * 2 + 1, l, mid, lq, rq );
    if ( mid < lq )
        return query( v * 2 + 2, mid + 1, r, lq, rq );
    return query( v * 2 + 1, l, mid, lq, rq ) + query( v * 2 + 2, mid + 1, r, lq, rq );
}

int main() {
    int n, q;

    cin >> n;
    for ( int i = 1; i <= n; i++ ) {
        char ch;
        cin >> ch;
        vote[i] = (ch == 'C' ? 1 : -1);
    }

    init( 0, 1, n );

    cin >> q;
    for ( int i = 0; i < q; i++ ) {
        int l, r;
        cin >> l >> r;
        cout << query( 0, 1, n, l, r ).elim << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -