답안 #730491

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
730491 2023-04-26T03:24:20 Z vjudge1 Izbori (COCI22_izbori) C++17
110 / 110
2055 ms 20860 KB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/

#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
using namespace std;

const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const int block = 450;
const ll oo = 1e18;

int dp[mxN], fenw[2 * mxN];
int n, a[mxN], cnt[mxN];
map<int, int> num;
map<int, vi> mp;
bool vis[mxN];
ll ans;

void update(int idx, int val) {
    while(idx < 2 * mxN) {
        fenw[idx] += val;
        idx += (idx & (-idx));
    }
}

int get(int idx) {
    int res = 0;
    while(idx) {
        res += fenw[idx];
        idx -= (idx & (-idx));
    }
    return res;
}

void calc_light() {
    for(int i = 1; i <= n; ++i) {
        int max_len = min(n - i + 1, 2 * block), maxx = 0;
        vi cur;
        for(int len = 1; len <= max_len; ++len) {
            int r = i + len - 1;
            if(!vis[a[r]]) {
                continue;
            }
            cnt[a[r]]++;
            cur.emplace_back(a[r]);
            maxx = max(maxx, cnt[a[r]]);
            if(maxx * 2 > len) {
                ++ans;
            }
        }
        for(int &val : cur) {
            cnt[val] = 0;
        }
    }
}

void calc_heavy(vi &v) {
    memset(fenw, 0, sizeof fenw);
    int cur_sum = mxN, cur_idx = 0;
    update(cur_sum, 1);
    for(int i = 1; i <= n; ++i) {
        if(cur_idx < (int)v.size() && i == v[cur_idx]) {
            ++cur_sum, ++cur_idx;
        }
        else {
            --cur_sum;
        }

        ans += get(cur_sum - 1);
        update(cur_sum, 1);
    }
}

int comp(int val) {
    if(num[val] == 0) {
        num[val] = num.size();
    }
    return num[val];
}

void solve() {
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
        a[i] = comp(a[i]);
        mp[a[i]].emplace_back(i);
    }
    for(auto &[val, v] : mp) {
        if((int)mp[val].size() < block) {
            vis[val] = true;
        }
        else {
            calc_heavy(v);
        }
    }
    calc_light();
    cout << ans << endl;
}

signed main() {

#ifndef CDuongg
    if(fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
    auto end = chrono::high_resolution_clock::now();
    cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
    cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif

}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 9 ms 340 KB Output is correct
10 Correct 11 ms 340 KB Output is correct
11 Correct 10 ms 340 KB Output is correct
12 Correct 10 ms 388 KB Output is correct
13 Correct 10 ms 468 KB Output is correct
14 Correct 10 ms 340 KB Output is correct
15 Correct 11 ms 384 KB Output is correct
16 Correct 9 ms 340 KB Output is correct
17 Correct 2 ms 1876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 3172 KB Output is correct
2 Correct 132 ms 3404 KB Output is correct
3 Correct 77 ms 2832 KB Output is correct
4 Correct 169 ms 3464 KB Output is correct
5 Correct 136 ms 3608 KB Output is correct
6 Correct 149 ms 3700 KB Output is correct
7 Correct 142 ms 3692 KB Output is correct
8 Correct 173 ms 3664 KB Output is correct
9 Correct 151 ms 3780 KB Output is correct
10 Correct 145 ms 3608 KB Output is correct
11 Correct 142 ms 3592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 9 ms 340 KB Output is correct
10 Correct 11 ms 340 KB Output is correct
11 Correct 10 ms 340 KB Output is correct
12 Correct 10 ms 388 KB Output is correct
13 Correct 10 ms 468 KB Output is correct
14 Correct 10 ms 340 KB Output is correct
15 Correct 11 ms 384 KB Output is correct
16 Correct 9 ms 340 KB Output is correct
17 Correct 2 ms 1876 KB Output is correct
18 Correct 109 ms 3172 KB Output is correct
19 Correct 132 ms 3404 KB Output is correct
20 Correct 77 ms 2832 KB Output is correct
21 Correct 169 ms 3464 KB Output is correct
22 Correct 136 ms 3608 KB Output is correct
23 Correct 149 ms 3700 KB Output is correct
24 Correct 142 ms 3692 KB Output is correct
25 Correct 173 ms 3664 KB Output is correct
26 Correct 151 ms 3780 KB Output is correct
27 Correct 145 ms 3608 KB Output is correct
28 Correct 142 ms 3592 KB Output is correct
29 Correct 146 ms 3592 KB Output is correct
30 Correct 236 ms 6060 KB Output is correct
31 Correct 464 ms 10788 KB Output is correct
32 Correct 1284 ms 20860 KB Output is correct
33 Correct 518 ms 11468 KB Output is correct
34 Correct 542 ms 11852 KB Output is correct
35 Correct 356 ms 8432 KB Output is correct
36 Correct 205 ms 5376 KB Output is correct
37 Correct 243 ms 6348 KB Output is correct
38 Correct 167 ms 3716 KB Output is correct
39 Correct 175 ms 3624 KB Output is correct
40 Correct 176 ms 3756 KB Output is correct
41 Correct 172 ms 3612 KB Output is correct
42 Correct 169 ms 3856 KB Output is correct
43 Correct 237 ms 4008 KB Output is correct
44 Correct 201 ms 3932 KB Output is correct
45 Correct 196 ms 3920 KB Output is correct
46 Correct 207 ms 3928 KB Output is correct
47 Correct 199 ms 4108 KB Output is correct
48 Correct 1148 ms 3764 KB Output is correct
49 Correct 1194 ms 3804 KB Output is correct
50 Correct 571 ms 4124 KB Output is correct
51 Correct 555 ms 4096 KB Output is correct
52 Correct 2055 ms 3644 KB Output is correct
53 Correct 1885 ms 3668 KB Output is correct
54 Correct 1118 ms 3632 KB Output is correct