답안 #999066

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999066 2024-06-15T06:01:43 Z crafticat Diversity (CEOI21_diversity) C++17
0 / 100
1 ms 1628 KB
#include <bits/stdc++.h>

using namespace std;
using pii = pair<int,int>;
using ll = long long;

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

    int n, q; cin >> n >> q;
    vector<int> arr(n);
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
    for (int i = 0; i < q; ++i) {
        int l, r; cin >> l >> r;
        l--;
        vector<int> v;
        vector<int> app(300002);
        v.reserve(r - l);
        int s = r - l;
        for (int j = l; j < r; ++j) {
            v.push_back(arr[j]);
        }
        for (auto x : v) {
            app[x]++;
        }
        vector<pii> v2;
        for (auto x : v) {
            v2.emplace_back(app[x],x);
        }
        std::sort(v2.begin(), v2.end());
        vector<pii> orderA, orderB;
        int last = -1;
        int leftS = 0, rightS = 0;
        std::reverse(v2.begin(), v2.end());
        for (auto [a, x] : v2) {
            if (last != x) {
                if (leftS <= rightS) {
                    orderA.emplace_back(x, a);
                    leftS += a;
                }
                else {
                    orderB.emplace_back(x,a);
                    rightS += a;
                }
            }
            last = x;
        }
        vector<pii> order;
        for (auto x : orderA) {
            order.push_back(x);
        }
        std::reverse(orderB.begin(), orderB.end());
        for (auto x : orderB) {
            order.push_back(x);
        }

        int a = 0;
        ll ans = 0;
        for (auto [x, amount] : order) {
            int b = a + amount;
            ll includeSub = a * (s - b);
            ll includePar = a * amount + (s - b) * amount;
            ll self = amount * (amount + 1) / 2;
            ans += self + includePar + includeSub;
            a += amount;
        }

        cout << ans << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1624 KB Output is correct
3 Incorrect 1 ms 1628 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1624 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1624 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1624 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1624 KB Output is correct
3 Incorrect 1 ms 1628 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1624 KB Output is correct
3 Incorrect 1 ms 1628 KB Output isn't correct
4 Halted 0 ms 0 KB -