답안 #878680

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
878680 2023-11-25T05:00:55 Z noiaint Izbori (COCI22_izbori) C++17
110 / 110
596 ms 45952 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

#define file ""

#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()

#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define popcount __builtin_popcountll

mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
    return l + rd() % (r - l + 1);
}

const int N = 1e6 + 5;
const int mod = (int)1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int oo = 1e9;
const long long ooo = 1e18;

template<class X, class Y> bool mini(X &a, Y b) {
    return a > b ? (a = b, true) : false;
}
template<class X, class Y> bool maxi(X &a, Y b) {
    return a < b ? (a = b, true) : false;
}
void add(int &a, int b) {
    a += b;
    if (a >= mod) a -= mod;
    if (a < 0) a += mod;
}

long long sol = 0;
int n;
int a[N];

void dnc(int l, int r) {
    if (l > r) {
        return;
    }
    if (l == r) {
        sol++;
        return;
    }
    int m = (l + r) >> 1;
    map<int, int> maj, fr;
    for (int i = m; i >= l; --i) {
        fr[a[i]]++;
        if (fr[a[i]] >= (m - i + 1) / 2 + 1) maj[a[i]] = 1;
    }
    fr.clear();
    for (int i = m + 1; i <= r; ++i) {
        fr[a[i]]++;
        if (fr[a[i]] >= (i - m) / 2 + 1) maj[a[i]] = 1;
    }
    int add = r - l + 1, lim = 2 * (r - l + 5);
    int *freq = (int*)malloc(lim * sizeof(int));
    for (auto it : maj) {
        memset(freq, 0, lim * sizeof(int));
        int act = 0;
        freq[0 + add]++;
        for (int i = l; i <= m; ++i) {
            if (a[i] == it.fi) ++act;
            else --act;
            if (i < m) freq[act + add]++;
        }
        for (int i = 1; i < lim; ++i) {
            freq[i] += freq[i - 1];
        }
        for (int i = m + 1; i <= r; ++i) {
            if (a[i] == it.fi) ++act;
            else --act;
            sol += freq[act + add - 1];
        }
    }
    dnc(l, m);
    dnc(m + 1, r);
}

void process() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    dnc(1, n);
    cout << sol;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    #endif

    int tc = 1;
    // cin >> tc;
    
    while (tc--) {
        process();
    }

    return 0;
}

/*

*/  
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 748 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 744 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 744 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 26728 KB Output is correct
2 Correct 141 ms 34096 KB Output is correct
3 Correct 74 ms 20304 KB Output is correct
4 Correct 138 ms 35712 KB Output is correct
5 Correct 146 ms 37204 KB Output is correct
6 Correct 153 ms 39500 KB Output is correct
7 Correct 152 ms 39504 KB Output is correct
8 Correct 152 ms 39508 KB Output is correct
9 Correct 156 ms 39504 KB Output is correct
10 Correct 152 ms 39504 KB Output is correct
11 Correct 80 ms 39508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 748 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 744 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 744 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 98 ms 26728 KB Output is correct
19 Correct 141 ms 34096 KB Output is correct
20 Correct 74 ms 20304 KB Output is correct
21 Correct 138 ms 35712 KB Output is correct
22 Correct 146 ms 37204 KB Output is correct
23 Correct 153 ms 39500 KB Output is correct
24 Correct 152 ms 39504 KB Output is correct
25 Correct 152 ms 39508 KB Output is correct
26 Correct 156 ms 39504 KB Output is correct
27 Correct 152 ms 39504 KB Output is correct
28 Correct 80 ms 39508 KB Output is correct
29 Correct 87 ms 40888 KB Output is correct
30 Correct 90 ms 8536 KB Output is correct
31 Correct 204 ms 19556 KB Output is correct
32 Correct 596 ms 45952 KB Output is correct
33 Correct 219 ms 20636 KB Output is correct
34 Correct 232 ms 21588 KB Output is correct
35 Correct 142 ms 14932 KB Output is correct
36 Correct 76 ms 7588 KB Output is correct
37 Correct 96 ms 9312 KB Output is correct
38 Correct 177 ms 39504 KB Output is correct
39 Correct 183 ms 39648 KB Output is correct
40 Correct 180 ms 39504 KB Output is correct
41 Correct 175 ms 39352 KB Output is correct
42 Correct 183 ms 39464 KB Output is correct
43 Correct 206 ms 41056 KB Output is correct
44 Correct 208 ms 40964 KB Output is correct
45 Correct 208 ms 41092 KB Output is correct
46 Correct 212 ms 40920 KB Output is correct
47 Correct 206 ms 40932 KB Output is correct
48 Correct 353 ms 40056 KB Output is correct
49 Correct 351 ms 39764 KB Output is correct
50 Correct 294 ms 39672 KB Output is correct
51 Correct 288 ms 39784 KB Output is correct
52 Correct 277 ms 40528 KB Output is correct
53 Correct 263 ms 39860 KB Output is correct
54 Correct 241 ms 39764 KB Output is correct