답안 #533551

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
533551 2022-03-06T09:17:22 Z kartel Izbori (COCI22_izbori) C++14
110 / 110
357 ms 19096 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define pb push_back
#define sz(x) (int)x.size()
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-O3")
#define el "\n"
using namespace std;
typedef long long ll;

const int N = 2e5 + 500;
const int SQ = 800;

int t[N];
vector <int> vl, vals;
int pf[N], a[N], n, b[N];
vector <int> ps1[N], ps2[N];
ll ans = 0;

void upd(int v, int val) {for (; v < N; v = (v | (v + 1))) t[v] += val;}
int get(int v) {int ret = 0; for (; v >= 0; v = (v & (v + 1)) - 1) ret += t[v]; return ret;}

void calc_big(int x) {
    vl.clear();
    for (int i = 1; i <= n; i++) {
        pf[i] = pf[i - 1] + (x == a[i]);
        vl.pb(2 * pf[i - 1] - i + 1);
    }
    sort(all(vl));
    vl.resize(unique(all(vl)) - vl.begin());

    for (int i = 1; i <= n; i++) {
        b[i] = lower_bound(all(vl), 2 * pf[i - 1] - i + 1) - vl.begin();
        upd(b[i], 1);
        ans += get(lower_bound(all(vl), 2 * pf[i] - i) - vl.begin() - 1);
    }
    for (int i = 0; i <= n; i++) {
        t[i] = 0;
    }
}

ll get_ar(ll a0, ll cnt, ll d) {
    return ((2 * a0 + d * (cnt - 1)) * cnt) / 2ll;
}

void calc_small(int x) {
    int n = sz(ps1[x]), m = sz(ps2[x]);
    for (int i = 1; i < n; i++) {
        for (int j = i - 1; j < m - 1; j++) {
            int cnt = (j + 1) - i + 1;
            int mx_len = min(ps2[x][j + 1] - ps1[x][i - 1] - 1, 2 * cnt - 1);
            int mn_len = ps2[x][j] - ps1[x][i] + 1;
            if (mn_len > mx_len) {
                continue;
            }

            int R = min(ps2[x][j + 1] - 1, ps1[x][i] + mx_len - 1);
            int seg = R - ps2[x][j] + 1;
            int ps_l = max(R - mx_len + 1, ps1[x][i - 1] + 1);
            ans += max(0ll, (ps1[x][i] - ps_l + 1) * 1ll * seg); /// [ps_l; ps1[i]] = all_segnent
            int len = max(0, min((ps_l - 1 - max(ps2[x][j] - mx_len + 1, ps1[x][i - 1] + 1) + 1),
                                 seg - 1));
            ans += get_ar(seg - 1, len, -1);

//            for (int len = ps2[x][j] - ps1[x][i] + 1; len <= mx_len; len++) {
//                cerr << x << " " << i << " " << j << " " << len << " " << max(0, min(ps1[x][i], min(ps1[x][i] + len - 1, ps2[x][j + 1] - 1) - len + 1) - max(ps2[x][j] - len + 1, ps1[x][i - 1] + 1)) + 1 << el;
//                ans += max(0, min(ps1[x][i], min(ps1[x][i] + len - 1, ps2[x][j + 1] - 1) - len + 1) - max(ps2[x][j] - len + 1, ps1[x][i - 1] + 1)) + 1;
//            }
//            cerr << el;
        }
    }
}

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

    cin >> n;
    mt19937 rnd(time(0));
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
//        a[i] = rnd() % 2;
        vals.pb(a[i]);
    }

    sort(all(vals));
    vals.resize(unique(all(vals)) - vals.begin());

    for (int i = 1; i <= n; i++){
        a[i] = lower_bound(all(vals), a[i]) - vals.begin();
        ps1[a[i]].pb(i);
        ps2[a[i]].pb(i);
    }

    ll sum = 0;
    for (int x = 0; x < min(SQ, sz(vals)); x++) {
        sum += (sz(ps1[x]) + 1) * 1ll * (sz(ps1[x]) + 1);
    }

    for (int x = SQ; x < sz(vals); x++) {
        sum += 2ll * n * 17;
    }
    if (sum < (ll)5e8) {
        for (int x = 0; x < sz(vals); x++) {
            ps1[x].pb(0);
            ps2[x].pb(n + 1);
            sort(all(ps1[x]));
            if (x < SQ) {
                calc_small(x);
            } else {
                calc_big(x);
            }
        }
    } else {
        for (int x = 0; x < sz(vals); x++) {
            if (sz(ps1[x]) < SQ) {
                ps1[x].pb(0);
                ps2[x].pb(n + 1);
                sort(all(ps1[x]));
                calc_small(x);
            } else {
                calc_big(x);
            }
        }
    }
    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 8 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 8 ms 9676 KB Output is correct
7 Correct 17 ms 9864 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 8 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 6 ms 9676 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 6 ms 9676 KB Output is correct
15 Correct 5 ms 9676 KB Output is correct
16 Correct 7 ms 9804 KB Output is correct
17 Correct 16 ms 9804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 15292 KB Output is correct
2 Correct 69 ms 16608 KB Output is correct
3 Correct 29 ms 13636 KB Output is correct
4 Correct 52 ms 16916 KB Output is correct
5 Correct 50 ms 17256 KB Output is correct
6 Correct 59 ms 17676 KB Output is correct
7 Correct 58 ms 17560 KB Output is correct
8 Correct 61 ms 17608 KB Output is correct
9 Correct 52 ms 17348 KB Output is correct
10 Correct 62 ms 17232 KB Output is correct
11 Correct 63 ms 16100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 8 ms 9676 KB Output is correct
7 Correct 17 ms 9864 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 8 ms 9676 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 6 ms 9676 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 6 ms 9676 KB Output is correct
15 Correct 5 ms 9676 KB Output is correct
16 Correct 7 ms 9804 KB Output is correct
17 Correct 16 ms 9804 KB Output is correct
18 Correct 42 ms 15292 KB Output is correct
19 Correct 69 ms 16608 KB Output is correct
20 Correct 29 ms 13636 KB Output is correct
21 Correct 52 ms 16916 KB Output is correct
22 Correct 50 ms 17256 KB Output is correct
23 Correct 59 ms 17676 KB Output is correct
24 Correct 58 ms 17560 KB Output is correct
25 Correct 61 ms 17608 KB Output is correct
26 Correct 52 ms 17348 KB Output is correct
27 Correct 62 ms 17232 KB Output is correct
28 Correct 63 ms 16100 KB Output is correct
29 Correct 60 ms 16112 KB Output is correct
30 Correct 30 ms 12116 KB Output is correct
31 Correct 46 ms 14204 KB Output is correct
32 Correct 144 ms 19096 KB Output is correct
33 Correct 49 ms 14584 KB Output is correct
34 Correct 56 ms 14692 KB Output is correct
35 Correct 35 ms 13168 KB Output is correct
36 Correct 21 ms 11896 KB Output is correct
37 Correct 35 ms 12252 KB Output is correct
38 Correct 164 ms 17808 KB Output is correct
39 Correct 200 ms 17632 KB Output is correct
40 Correct 169 ms 17728 KB Output is correct
41 Correct 159 ms 17424 KB Output is correct
42 Correct 162 ms 17712 KB Output is correct
43 Correct 347 ms 17720 KB Output is correct
44 Correct 346 ms 17964 KB Output is correct
45 Correct 340 ms 17776 KB Output is correct
46 Correct 343 ms 17780 KB Output is correct
47 Correct 357 ms 17868 KB Output is correct
48 Correct 149 ms 13364 KB Output is correct
49 Correct 165 ms 13968 KB Output is correct
50 Correct 308 ms 14764 KB Output is correct
51 Correct 327 ms 14868 KB Output is correct
52 Correct 91 ms 13832 KB Output is correct
53 Correct 110 ms 13632 KB Output is correct
54 Correct 149 ms 13812 KB Output is correct