답안 #765034

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765034 2023-06-24T07:47:17 Z The_Samurai Izbori (COCI22_izbori) C++17
110 / 110
106 ms 13964 KB
#include "bits/stdc++.h"

using namespace std;
using ll = long long;

ll sum(ll l, ll r) {
    // l^2 + ... + r^2
    l--;
    return r * (r + 1) / 2 - l * (l + 1) / 2;
    return r * (r + 1) * (2 * r + 1) / 6 - l * (l + 1) * (2 * l + 1) / 6;
}

int brute(int n, vector<int> &a) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
        vector<int> cnt(n);
        int mx = 0;
        for (int j = i; j < n; j++) {
            cnt[a[j]]++;
            mx = max(mx, cnt[a[j]]);
            if (2 * mx > (j - i + 1)) {
                ans++;
            }
        }
    }
    return ans;
}

void solve() {
    int n;
    cin >> n;
    map<int, int> mp;
    vector<int> cnt, a(n);
    {
        for (int &x: a) {
            cin >> x;
            mp[x];
        }
        int z = 0;
        for (auto &it: mp) {
            it.second = z++;
        }
        cnt.assign(mp.size(), 0);
        for (int &x: a) {
            x = mp[x];
            cnt[x]++;
        }
    }
    vector<vector<int>> pos(mp.size());
    for (int i = 0; i < n; i++) {
        pos[a[i]].emplace_back(i);
    }
    int N = 3 * sqrt(n);
    ll ans = 0;
    for (int i = 0; i < mp.size(); i++) {
        if (cnt[i] <= N) {
            for (int l = 0; l < cnt[i]; l++) {
                for (int r = l; r < cnt[i]; r++) {
                    int left = 0, right = n - 1, plus = r - l + 1;
                    int have = 2 * plus - (pos[i][r] - pos[i][l] + 1);
                    if (have <= 0) {
                        continue;
                    }
                    if (l > 0) {
                        left = pos[i][l - 1] + 1;
                    }
                    if (r + 1 < cnt[i]) {
                        right = pos[i][r + 1] - 1;
                    }
                    right = min(right, pos[i][r] + (have - 1));
                    left = max(left, pos[i][l] - (have - 1));
                    int right2 = left + (2 * plus - 1) - 1;
                    int left2 = right - (2 * plus - 1) + 1;
                    if (right2 < right) {
                        int x = pos[i][l] - left2 + 1;
                        ans += sum(x, right - right2 + x - 1);
                    } else {
                        right2 = right;
                    }
                    ans += (pos[i][l] - left + 1) * (right2 - pos[i][r] + 1);
                }
            }
        } else {
            vector<int> pref(n + 1), cs(2 * n + 1);
            for (int j = 0; j < n; j++) {
                pref[j + 1] = pref[j] + (a[j] == i ? 1 : -1);
            }
            for (int j = 0; j <= n; j++) {
                pref[j] += n;
            }
            cs[pref[n]]++;
            int now = 0;
            for (int j = n - 1; j >= 0; j--) {
                // count of k that pref[k] > pref[j] and k > j
                if (pref[j] < pref[j + 1]) {
                    now += cs[pref[j + 1]];
                } else {
                    now -= cs[pref[j]];
                }
                ans += now;
                cs[pref[j]]++;
            }
        }
    }
    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);

    int queries = 1;
#ifdef test_cases
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> queries;
#else
    //    cin >> queries;
#endif

    for (int test_case = 1; test_case <= queries; test_case++) {
#ifdef test_cases
        cout << "Test case: " << test_case << '\n';
#endif
        solve();
        cout << '\n';
    }
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:55:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |     for (int i = 0; i < mp.size(); i++) {
      |                     ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 3052 KB Output is correct
2 Correct 15 ms 3768 KB Output is correct
3 Correct 8 ms 2364 KB Output is correct
4 Correct 14 ms 3976 KB Output is correct
5 Correct 14 ms 4168 KB Output is correct
6 Correct 15 ms 4428 KB Output is correct
7 Correct 15 ms 4428 KB Output is correct
8 Correct 15 ms 4408 KB Output is correct
9 Correct 15 ms 4428 KB Output is correct
10 Correct 15 ms 4432 KB Output is correct
11 Correct 11 ms 4320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 10 ms 3052 KB Output is correct
19 Correct 15 ms 3768 KB Output is correct
20 Correct 8 ms 2364 KB Output is correct
21 Correct 14 ms 3976 KB Output is correct
22 Correct 14 ms 4168 KB Output is correct
23 Correct 15 ms 4428 KB Output is correct
24 Correct 15 ms 4428 KB Output is correct
25 Correct 15 ms 4408 KB Output is correct
26 Correct 15 ms 4428 KB Output is correct
27 Correct 15 ms 4432 KB Output is correct
28 Correct 11 ms 4320 KB Output is correct
29 Correct 18 ms 4304 KB Output is correct
30 Correct 18 ms 4140 KB Output is correct
31 Correct 39 ms 7308 KB Output is correct
32 Correct 106 ms 13964 KB Output is correct
33 Correct 46 ms 7884 KB Output is correct
34 Correct 43 ms 7936 KB Output is correct
35 Correct 27 ms 5588 KB Output is correct
36 Correct 19 ms 3692 KB Output is correct
37 Correct 19 ms 4268 KB Output is correct
38 Correct 18 ms 4468 KB Output is correct
39 Correct 18 ms 4304 KB Output is correct
40 Correct 18 ms 4428 KB Output is correct
41 Correct 18 ms 4304 KB Output is correct
42 Correct 18 ms 4616 KB Output is correct
43 Correct 31 ms 4784 KB Output is correct
44 Correct 32 ms 4688 KB Output is correct
45 Correct 31 ms 4692 KB Output is correct
46 Correct 36 ms 4692 KB Output is correct
47 Correct 31 ms 4692 KB Output is correct
48 Correct 83 ms 4472 KB Output is correct
49 Correct 83 ms 4452 KB Output is correct
50 Correct 67 ms 4956 KB Output is correct
51 Correct 68 ms 4968 KB Output is correct
52 Correct 47 ms 2004 KB Output is correct
53 Correct 57 ms 1968 KB Output is correct
54 Correct 72 ms 2004 KB Output is correct