답안 #765033

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765033 2023-06-24T07:46:45 Z The_Samurai Izbori (COCI22_izbori) C++17
110 / 110
243 ms 14180 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 = 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 1 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 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 244 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 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 3272 KB Output is correct
2 Correct 13 ms 3856 KB Output is correct
3 Correct 7 ms 2368 KB Output is correct
4 Correct 14 ms 3908 KB Output is correct
5 Correct 14 ms 4128 KB Output is correct
6 Correct 16 ms 4424 KB Output is correct
7 Correct 22 ms 4428 KB Output is correct
8 Correct 14 ms 4384 KB Output is correct
9 Correct 14 ms 4436 KB Output is correct
10 Correct 15 ms 4424 KB Output is correct
11 Correct 14 ms 4304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 244 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 0 ms 340 KB Output is correct
18 Correct 10 ms 3272 KB Output is correct
19 Correct 13 ms 3856 KB Output is correct
20 Correct 7 ms 2368 KB Output is correct
21 Correct 14 ms 3908 KB Output is correct
22 Correct 14 ms 4128 KB Output is correct
23 Correct 16 ms 4424 KB Output is correct
24 Correct 22 ms 4428 KB Output is correct
25 Correct 14 ms 4384 KB Output is correct
26 Correct 14 ms 4436 KB Output is correct
27 Correct 15 ms 4424 KB Output is correct
28 Correct 14 ms 4304 KB Output is correct
29 Correct 17 ms 4308 KB Output is correct
30 Correct 18 ms 4012 KB Output is correct
31 Correct 40 ms 7568 KB Output is correct
32 Correct 106 ms 14180 KB Output is correct
33 Correct 43 ms 7860 KB Output is correct
34 Correct 45 ms 8248 KB Output is correct
35 Correct 28 ms 5812 KB Output is correct
36 Correct 18 ms 3900 KB Output is correct
37 Correct 19 ms 4532 KB Output is correct
38 Correct 21 ms 4744 KB Output is correct
39 Correct 19 ms 4548 KB Output is correct
40 Correct 19 ms 4692 KB Output is correct
41 Correct 18 ms 4548 KB Output is correct
42 Correct 18 ms 4864 KB Output is correct
43 Correct 32 ms 4948 KB Output is correct
44 Correct 32 ms 4920 KB Output is correct
45 Correct 31 ms 4900 KB Output is correct
46 Correct 32 ms 4952 KB Output is correct
47 Correct 33 ms 4980 KB Output is correct
48 Correct 137 ms 4780 KB Output is correct
49 Correct 138 ms 4776 KB Output is correct
50 Correct 68 ms 5204 KB Output is correct
51 Correct 70 ms 5268 KB Output is correct
52 Correct 243 ms 4652 KB Output is correct
53 Correct 229 ms 4556 KB Output is correct
54 Correct 128 ms 4628 KB Output is correct