Submission #1106448

# Submission time Handle Problem Language Result Execution time Memory
1106448 2024-10-30T10:53:42 Z atom Izbori (COCI22_izbori) C++17
110 / 110
729 ms 6132 KB
    #include "bits/stdc++.h"
    // @JASPER'S BOILERPLATE
    using namespace std;
    using ll = long long;
     
    #ifdef JASPER
    #include "debug.h"
    #else
    #define debug(...) 166
    #endif
     
    const int N = 2e5 + 5;
    const int B = 450;
    int a[N];
    int n;
     
    bool sub12() {
        return (n <= 2000);
    }
     
    bool sub3() {
        for (int i = 1; i <= n; ++i)
            if (a[i] > 2) return false;
        return true;
    }
     
    template <typename T>
    struct Fenwick {
        vector <int> f;
        int n;
        Fenwick(int _n) {
            f.resize(_n + 5);
            n = _n;
        }
     
        void upd(int x, int val) {
            for (; x <= n; x += x & -x)
                f[x] += val;
        }
     
        int get(int x) {
            int ans = 0;
            for (; x > 0; x -= x & -x)
                ans += f[x];
            return ans;
        }
    };
     
    signed main() {
        cin.tie(0) -> sync_with_stdio(0);
        
        #ifdef JASPER
            freopen("in1", "r", stdin);
        #endif
     
        cin >> n;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
     
        if (sub12()) {
            ll ans = 0;
            for (int l = 1; l <= n; ++l) {
                map <int, int> cnt;
                
                pair <int, int> mx = {0, 0};
                for (int r = l; r <= n; ++r) {
                    cnt[a[r]]++;
                    if (cnt[a[r]] > mx.second) {
                        mx = make_pair(a[r], cnt[a[r]]);
                    }
                    if (mx.second > (r - l + 1) / 2) {
                        ++ans;
                    }
                }
            }
     
            cout << ans << "\n";
            return 0;
        }
     
        if (sub3()) {
            vector <int> prf[3];
            for (int i = 1; i <= 2; ++i)
                prf[i].resize(n + 5);
     
            for (int i = 1; i <= n; ++i) {
                int x = a[i];
                prf[1][i] = prf[1][i - 1] + (x == 1);
                prf[2][i] = prf[2][i - 1] + (x == 2);
            }
     
            vector <int> vals[3];
            vals[1].push_back(0);
            vals[2].push_back(0);
            for (int i = 1; i <= n; ++i) {
                vals[1].push_back(2 * prf[1][i] - i);
                vals[2].push_back(2 * prf[2][i] - i);
            }
     
            sort(vals[1].begin(), vals[1].end());
            vals[1].resize(unique(vals[1].begin(), vals[1].end()) - vals[1].begin());
            sort(vals[2].begin(), vals[2].end());
            vals[2].resize(unique(vals[2].begin(), vals[2].end()) - vals[2].begin());
     
            auto get = [&] (int x, int t) {
                return (int) (lower_bound(vals[t].begin(), vals[t].end(), x) - vals[t].begin() + 1);
            };
     
            Fenwick <int> f1(n), f2(n);
            ll ans = 0;
            f1.upd(get(0, 1), 1);
            f2.upd(get(0, 2), 1);
            for (int r = 1; r <= n; ++r) {
                int x1 = get(2 * prf[1][r] - r, 1);
                ans += f1.get(x1 - 1);
                f1.upd(x1, 1);
     
                int x2 = get(2 * prf[2][r] - r, 2);
                ans += f2.get(x2 - 1);
                f2.upd(x2, 1);
            }
     
            cout << ans << "\n";
            return 0;
        }
     
        vector <int> vals;
        for (int i = 1; i <= n; ++i)
            vals.push_back(a[i]);
     
        sort(vals.begin(), vals.end());
        vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
     
        int sz = (int) vals.size();
        vector <int> cnt(sz, 0);
        for (int i = 1; i <= n; ++i) {
            a[i] = (int) (lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin());
            cnt[a[i]]++;
        }
     
        vector <bool> heavy(sz, false);
        for (int i = 0; i < sz; ++i) {
            if (cnt[i] >= B)
                heavy[i] = true;
        }
     
        ll ans = 0;
        // processing light queries: all elements appear less than sqrt(n)
        cnt.assign(sz, 0);
        for (int i = 1; i <= n; ++i) {
            int mx = 0;
            for (int j = i; j <= n && (j - i + 1) / 2 < B; ++j) {
                if (!heavy[a[j]]) {
                    cnt[a[j]]++;
                    if (cnt[a[j]] > mx) mx = cnt[a[j]];
                    if (mx > (j - i + 1) / 2) ++ans;
                }
            }
     
            for (int j = i; j <= n && (j - i + 1) / 2 < B; ++j)
                if (!heavy[a[j]]) cnt[a[j]]--;
        }
     
        // processing heavy queries: all elements appear at least sqrt(n)
        for (int x = 0; x < sz; ++x) {
            if (heavy[x]) {
                auto get = [&] (int _x) { debug(_x); return _x + n; };
     
                vector <int> prf(n + 5);
                for (int i = 1; i <= n; ++i)
                    prf[i] = prf[i - 1] + (a[i] == x? 1 : -1);
     
     
                vector <int> f(2 * n + 5, 0);
                f[get(0)]++;
                ll sum = 0;
                for (int i = 1; i <= n; ++i) {
                    if (a[i] == x)
                        sum += f[get(prf[i] - 1)];
                    else    
                        sum -= f[get(prf[i])];
                    ans += sum;
                    f[get(prf[i])]++;
                }
            }
        }
     
        cout << ans << "\n";
     
        
        return 0;
    }

Compilation message

Main.cpp: In lambda function:
Main.cpp:9:24: warning: statement has no effect [-Wunused-value]
    9 |     #define debug(...) 166
      |                        ^~~
Main.cpp:167:43: note: in expansion of macro 'debug'
  167 |                 auto get = [&] (int _x) { debug(_x); return _x + n; };
      |                                           ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 47 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 34 ms 336 KB Output is correct
10 Correct 34 ms 336 KB Output is correct
11 Correct 34 ms 504 KB Output is correct
12 Correct 34 ms 336 KB Output is correct
13 Correct 34 ms 336 KB Output is correct
14 Correct 34 ms 336 KB Output is correct
15 Correct 37 ms 336 KB Output is correct
16 Correct 34 ms 336 KB Output is correct
17 Correct 9 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 4316 KB Output is correct
2 Correct 29 ms 5308 KB Output is correct
3 Correct 17 ms 3380 KB Output is correct
4 Correct 31 ms 5576 KB Output is correct
5 Correct 30 ms 5812 KB Output is correct
6 Correct 32 ms 6068 KB Output is correct
7 Correct 45 ms 6132 KB Output is correct
8 Correct 33 ms 6076 KB Output is correct
9 Correct 42 ms 6068 KB Output is correct
10 Correct 33 ms 6076 KB Output is correct
11 Correct 40 ms 6076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 47 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 34 ms 336 KB Output is correct
10 Correct 34 ms 336 KB Output is correct
11 Correct 34 ms 504 KB Output is correct
12 Correct 34 ms 336 KB Output is correct
13 Correct 34 ms 336 KB Output is correct
14 Correct 34 ms 336 KB Output is correct
15 Correct 37 ms 336 KB Output is correct
16 Correct 34 ms 336 KB Output is correct
17 Correct 9 ms 336 KB Output is correct
18 Correct 24 ms 4316 KB Output is correct
19 Correct 29 ms 5308 KB Output is correct
20 Correct 17 ms 3380 KB Output is correct
21 Correct 31 ms 5576 KB Output is correct
22 Correct 30 ms 5812 KB Output is correct
23 Correct 32 ms 6068 KB Output is correct
24 Correct 45 ms 6132 KB Output is correct
25 Correct 33 ms 6076 KB Output is correct
26 Correct 42 ms 6068 KB Output is correct
27 Correct 33 ms 6076 KB Output is correct
28 Correct 40 ms 6076 KB Output is correct
29 Correct 402 ms 4292 KB Output is correct
30 Correct 126 ms 980 KB Output is correct
31 Correct 275 ms 1236 KB Output is correct
32 Correct 729 ms 2256 KB Output is correct
33 Correct 295 ms 1488 KB Output is correct
34 Correct 310 ms 1532 KB Output is correct
35 Correct 194 ms 980 KB Output is correct
36 Correct 110 ms 848 KB Output is correct
37 Correct 140 ms 980 KB Output is correct
38 Correct 374 ms 4464 KB Output is correct
39 Correct 384 ms 4292 KB Output is correct
40 Correct 390 ms 4456 KB Output is correct
41 Correct 400 ms 4468 KB Output is correct
42 Correct 391 ms 4292 KB Output is correct
43 Correct 391 ms 4460 KB Output is correct
44 Correct 404 ms 4292 KB Output is correct
45 Correct 385 ms 4292 KB Output is correct
46 Correct 426 ms 4292 KB Output is correct
47 Correct 397 ms 4292 KB Output is correct
48 Correct 515 ms 4436 KB Output is correct
49 Correct 556 ms 4460 KB Output is correct
50 Correct 437 ms 4460 KB Output is correct
51 Correct 450 ms 4460 KB Output is correct
52 Correct 642 ms 4460 KB Output is correct
53 Correct 591 ms 4292 KB Output is correct
54 Correct 507 ms 4460 KB Output is correct