답안 #1100550

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100550 2024-10-14T08:44:05 Z NoLove Election (BOI18_election) C++14
28 / 100
3000 ms 3080 KB
/**
 *    author : Lăng Trọng Đạt
 *    created: 14-10-2024 
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int long long
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define FOR(i, a, b) for (int i = (a); (i) <= (b); (i++))
#define FD(i, lo, up) for (int i = (up); (i) >= (lo); i--)
#define si(x) (int)(x.size())
bool mx(int& a, int b) { if (b > a) {a = b; return true;} return false;}
bool mi(int& a, int b) { if (b < a) {a = b; return true;} return false;}
using pii = pair<int, int>;
const int INF = 1e18 + 5;
const int MOD = 1e9 + 7;

const int N = 1e6 + 5;
int g[N];
int n, m, k, q, a, b, c;
string s;

struct Data {
    int res, open;
    Data(int a, int b) {
        res = a; open = b;
    }
    Data() {

    }
};
struct Info {
    Data up, down;
    // up: {number of deleted vote, number of open}
} st[4*N];
Data operator+(Data& a, Data& b) {
    return {a.res + b.res - min(b.res, a.open), a.open + b.open - min(b.res, a.open)};
}
Info operator+(Info a, Info b) {
    return {a.up + b.up, b.down + a.down};
}

void build(int v = 1, int l = 1, int r = n) {
    if (l == r) {
        st[v].up = st[v].down = (s[l - 1] == 'C' ? Data(0, 1) : Data(1, 0));
    } else {
        int mid = (l + r) / 2;
        build(2*v, l, mid);
        build(2*v + 1, mid + 1, r);
        st[v] = st[2*v] + st[2*v + 1];
    }
}
Info get(int p, int q, int v = 1, int l = 1, int r = n) {
    if (p > r or l > q) return st[0];
    else if (p <= l && r <= q) return st[v];
    else {
        int mid = (l + r) / 2;
        return get(p, q, 2*v, l, mid) + get(p, q, 2*v + 1, mid + 1, r);
    }
}

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    if (fopen("hi.inp", "r")) {
        freopen("hi.inp", "r", stdin);
//        freopen("hi.out", "w", stdout);
    } 

    cin >> n >> s >> q;
    // build();

    FOR(i, 1, q) {
        cin >> a >> b;
        int open = 0, res = 0, res2 = 0; 
        vector<int> skip;
        FOR(i, a, b) {
            (s[i - 1] == 'C' ? open++ : open--);
            if (open == -1) {
                skip.push_back(i);
                res++;
                open = 0;
            }
        }
        open = 0; 
        FD(i, a, b) {
            if (binary_search(all(skip), i)) continue;
            (s[i - 1] == 'C' ? open++ : open--);
            if (open == -1) {
                res2++;
                open = 0;
            }
        }
        cout << res + res2 << "\n";
        // Info res = get(a, b);
        // cout << max(res.down.res, res.up.res) << "\n";
    }
}

Compilation message

election.cpp: In function 'int32_t main()':
election.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen("hi.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 2384 KB Output is correct
2 Correct 12 ms 2556 KB Output is correct
3 Correct 6 ms 2388 KB Output is correct
4 Correct 24 ms 2560 KB Output is correct
5 Correct 9 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 2384 KB Output is correct
2 Correct 12 ms 2556 KB Output is correct
3 Correct 6 ms 2388 KB Output is correct
4 Correct 24 ms 2560 KB Output is correct
5 Correct 9 ms 2388 KB Output is correct
6 Execution timed out 3066 ms 3080 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 2384 KB Output is correct
2 Correct 12 ms 2556 KB Output is correct
3 Correct 6 ms 2388 KB Output is correct
4 Correct 24 ms 2560 KB Output is correct
5 Correct 9 ms 2388 KB Output is correct
6 Execution timed out 3066 ms 3080 KB Time limit exceeded
7 Halted 0 ms 0 KB -