Submission #1003427

# Submission time Handle Problem Language Result Execution time Memory
1003427 2024-06-20T10:12:20 Z Whisper Izbori (COCI22_izbori) C++17
110 / 110
2163 ms 12732 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

//#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }

#define MAX             200005
#define BLOCK_SIZE      450
#define off_set         200000

int nArr;
int A[MAX];

vector<int> pos[MAX];


template<class T = vector<int>>
    T unique (T v){
        sort(v.begin(), v.end());
        v.resize(unique(v.begin(), v.end()) - v.begin());
        return v;
    }

template<class T = int>
    T find(T x, const vector<int>& v){
        return (int)(lower_bound(v.begin(), v.end(), x) - v.begin() + 1);
    }

bool hvy[MAX];
int cnt[MAX];
struct custom_hash{
    size_t operator ()(uint64_t x) const{
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        x ^= FIXED_RANDOM;
        return x ^ (x >> 16);
    }
};

template<class T = int> struct FenwickTree{
    T *F = nullptr;
    int n;
    FenwickTree(int _n = 0){
        resize(_n);
    }
    void reset(void){
        memset(F, 0, (n + 1) * sizeof(long long));
    }
    void resize(int _n){
        if(F != nullptr) delete[] F;

        this -> n = _n;
        F = new T [n + 5];
        reset();
    }
    void Modify(int p, int val, int c){
        for (; p <= n; p += p & (-p)) F[p] += val;
    }
    T Query(int p){
        int res = 0;
        for (; p > 0; p -= p & (-p)) res += F[p];
        return res;
    }
};

int g[MAX];
void process(void){
    cin >> nArr;
    vector<int> comp;
    for (int i = 1; i <= nArr; ++i) cin >> A[i], comp.push_back(A[i]);
    comp = unique(comp);
    for (int i = 1; i <= nArr; ++i) A[i] = find(A[i], comp), cnt[A[i]]++;
    for (int i = 1; i <= nArr; ++i) {
        hvy[i] = (cnt[i] >= BLOCK_SIZE);
    }
    long long ans = 0;
    for (int l = 1; l <= nArr; ++l){
        int max_occurence = 0;
        for (int r = l; r <= nArr && (r - l + 1 < 2 * BLOCK_SIZE); ++r){
            if(hvy[A[r]]) continue;
            g[A[r]]++;
            maximize(max_occurence, g[A[r]]);
            if (2 * max_occurence > (r - l + 1)) ++ans;
        }
        for (int r = l; r <= nArr && (r - l + 1 < 2 * BLOCK_SIZE); ++r){
            if(!hvy[A[r]]) --g[A[r]];
        }
    }

    FenwickTree<long long> fen(2 * MAX);
    for (int i = 1; i <= nArr; ++i){
        if (!hvy[i]) continue;
        vector<int> sm(nArr + 1);
        fen.Modify(off_set, 1, 1);
        for (int j = 1; j <= nArr; ++j){
            sm[j] = sm[j - 1] + (A[j] == i ? 1 : -1);
            ans += fen.Query(sm[j] + off_set - 1);
            fen.Modify(sm[j] + off_set, 1, 1);
        }
        fen.reset();
    }
    cout << ans;
}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    process();
}



# Verdict Execution time Memory Grader output
1 Correct 3 ms 8492 KB Output is correct
2 Correct 4 ms 8284 KB Output is correct
3 Correct 3 ms 8248 KB Output is correct
4 Correct 4 ms 8280 KB Output is correct
5 Correct 3 ms 8284 KB Output is correct
6 Correct 3 ms 8244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8492 KB Output is correct
2 Correct 4 ms 8284 KB Output is correct
3 Correct 3 ms 8248 KB Output is correct
4 Correct 4 ms 8280 KB Output is correct
5 Correct 3 ms 8284 KB Output is correct
6 Correct 3 ms 8244 KB Output is correct
7 Correct 7 ms 8284 KB Output is correct
8 Correct 3 ms 8280 KB Output is correct
9 Correct 8 ms 8284 KB Output is correct
10 Correct 9 ms 8284 KB Output is correct
11 Correct 7 ms 8284 KB Output is correct
12 Correct 8 ms 8284 KB Output is correct
13 Correct 9 ms 8300 KB Output is correct
14 Correct 8 ms 8284 KB Output is correct
15 Correct 7 ms 8284 KB Output is correct
16 Correct 7 ms 8284 KB Output is correct
17 Correct 6 ms 8256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 252 ms 10068 KB Output is correct
2 Correct 327 ms 10764 KB Output is correct
3 Correct 192 ms 9712 KB Output is correct
4 Correct 356 ms 10856 KB Output is correct
5 Correct 361 ms 10980 KB Output is correct
6 Correct 384 ms 11192 KB Output is correct
7 Correct 340 ms 11192 KB Output is correct
8 Correct 435 ms 11028 KB Output is correct
9 Correct 395 ms 11192 KB Output is correct
10 Correct 387 ms 11028 KB Output is correct
11 Correct 372 ms 11192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8492 KB Output is correct
2 Correct 4 ms 8284 KB Output is correct
3 Correct 3 ms 8248 KB Output is correct
4 Correct 4 ms 8280 KB Output is correct
5 Correct 3 ms 8284 KB Output is correct
6 Correct 3 ms 8244 KB Output is correct
7 Correct 7 ms 8284 KB Output is correct
8 Correct 3 ms 8280 KB Output is correct
9 Correct 8 ms 8284 KB Output is correct
10 Correct 9 ms 8284 KB Output is correct
11 Correct 7 ms 8284 KB Output is correct
12 Correct 8 ms 8284 KB Output is correct
13 Correct 9 ms 8300 KB Output is correct
14 Correct 8 ms 8284 KB Output is correct
15 Correct 7 ms 8284 KB Output is correct
16 Correct 7 ms 8284 KB Output is correct
17 Correct 6 ms 8256 KB Output is correct
18 Correct 252 ms 10068 KB Output is correct
19 Correct 327 ms 10764 KB Output is correct
20 Correct 192 ms 9712 KB Output is correct
21 Correct 356 ms 10856 KB Output is correct
22 Correct 361 ms 10980 KB Output is correct
23 Correct 384 ms 11192 KB Output is correct
24 Correct 340 ms 11192 KB Output is correct
25 Correct 435 ms 11028 KB Output is correct
26 Correct 395 ms 11192 KB Output is correct
27 Correct 387 ms 11028 KB Output is correct
28 Correct 372 ms 11192 KB Output is correct
29 Correct 385 ms 12592 KB Output is correct
30 Correct 107 ms 9280 KB Output is correct
31 Correct 243 ms 10040 KB Output is correct
32 Correct 695 ms 12180 KB Output is correct
33 Correct 305 ms 10124 KB Output is correct
34 Correct 332 ms 10128 KB Output is correct
35 Correct 165 ms 9612 KB Output is correct
36 Correct 94 ms 8836 KB Output is correct
37 Correct 127 ms 9280 KB Output is correct
38 Correct 386 ms 11148 KB Output is correct
39 Correct 386 ms 11192 KB Output is correct
40 Correct 422 ms 11028 KB Output is correct
41 Correct 389 ms 11044 KB Output is correct
42 Correct 398 ms 11036 KB Output is correct
43 Correct 428 ms 12732 KB Output is correct
44 Correct 403 ms 12620 KB Output is correct
45 Correct 381 ms 12692 KB Output is correct
46 Correct 416 ms 12620 KB Output is correct
47 Correct 404 ms 12616 KB Output is correct
48 Correct 1285 ms 11532 KB Output is correct
49 Correct 1312 ms 11532 KB Output is correct
50 Correct 819 ms 11220 KB Output is correct
51 Correct 763 ms 11220 KB Output is correct
52 Correct 2163 ms 11456 KB Output is correct
53 Correct 1826 ms 11448 KB Output is correct
54 Correct 1242 ms 11528 KB Output is correct