답안 #617132

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
617132 2022-08-01T08:54:01 Z dxz05 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 16392 KB
#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

typedef long long ll;
const int MOD = 998244353;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e6 + 3e2;
const int M = 1555;

long long diversity(const vector<int> &cnt){
    int n = (int)cnt.size();

    long long res = 0;
    for (int i = 0; i < n; i++){
        res += (ll) cnt[i] * (cnt[i] + 1) / 2;
    }

    ll suf = 0, sum = 0;
    for (int i = n - 1; i >= 0; i--){
        res += cnt[i] * (sum + suf);
        suf += cnt[i];
        sum += suf;
    }

    return res;
}

void solve(int TC) {
    int n, q;
    cin >> n >> q;

    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    int m = *max_element(all(a)) + 1;

    vector<int> cnt(m, 0);
    while (q--){
        int l, r;
        cin >> l >> r;
        --l, --r;

        for (int i = l; i <= r; i++) cnt[a[i]]++;

        set<pair<int, int>> s;
        for (int i = l; i <= r; i++){
            s.insert(MP(cnt[a[i]], a[i]));
        }

        deque<int> dq;
        int flag = 0;
        while (!s.empty()){
            int x = s.rbegin()->first, y = s.rbegin()->second;
            s.erase(*s.rbegin());
            cnt[y] = 0;
            if (flag) dq.push_back(x); else
                dq.push_front(x);
            flag ^= 1;
        }

        vector<int> b;
        for (int x : dq){
            b.push_back(x);
        }

//        for (int x : b) cout << x << ' '; cout << endl;

        cout << diversity(b) << endl;

    }

}

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

#ifdef dddxxz
    freopen("input.txt", "rg", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int TC = 1;
    //cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

#ifdef dddxxz
    cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
#endif

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 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 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 7 ms 852 KB Output is correct
5 Correct 15 ms 1232 KB Output is correct
6 Correct 24 ms 1628 KB Output is correct
7 Correct 25 ms 1620 KB Output is correct
8 Correct 26 ms 1620 KB Output is correct
9 Correct 21 ms 1620 KB Output is correct
10 Correct 29 ms 1624 KB Output is correct
11 Correct 27 ms 1620 KB Output is correct
12 Correct 25 ms 1612 KB Output is correct
13 Correct 32 ms 1616 KB Output is correct
14 Correct 31 ms 1620 KB Output is correct
15 Correct 32 ms 1620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 7 ms 852 KB Output is correct
5 Correct 15 ms 1232 KB Output is correct
6 Correct 24 ms 1628 KB Output is correct
7 Correct 25 ms 1620 KB Output is correct
8 Correct 26 ms 1620 KB Output is correct
9 Correct 21 ms 1620 KB Output is correct
10 Correct 29 ms 1624 KB Output is correct
11 Correct 27 ms 1620 KB Output is correct
12 Correct 25 ms 1612 KB Output is correct
13 Correct 32 ms 1616 KB Output is correct
14 Correct 31 ms 1620 KB Output is correct
15 Correct 32 ms 1620 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 12 ms 852 KB Output is correct
20 Correct 17 ms 1248 KB Output is correct
21 Correct 27 ms 1620 KB Output is correct
22 Correct 26 ms 1624 KB Output is correct
23 Correct 24 ms 1620 KB Output is correct
24 Correct 31 ms 1616 KB Output is correct
25 Correct 26 ms 1620 KB Output is correct
26 Correct 25 ms 1620 KB Output is correct
27 Correct 34 ms 1632 KB Output is correct
28 Correct 37 ms 1620 KB Output is correct
29 Correct 27 ms 1672 KB Output is correct
30 Correct 29 ms 1620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 7 ms 852 KB Output is correct
5 Correct 15 ms 1232 KB Output is correct
6 Correct 24 ms 1628 KB Output is correct
7 Correct 25 ms 1620 KB Output is correct
8 Correct 26 ms 1620 KB Output is correct
9 Correct 21 ms 1620 KB Output is correct
10 Correct 29 ms 1624 KB Output is correct
11 Correct 27 ms 1620 KB Output is correct
12 Correct 25 ms 1612 KB Output is correct
13 Correct 32 ms 1616 KB Output is correct
14 Correct 31 ms 1620 KB Output is correct
15 Correct 32 ms 1620 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 12 ms 852 KB Output is correct
20 Correct 17 ms 1248 KB Output is correct
21 Correct 27 ms 1620 KB Output is correct
22 Correct 26 ms 1624 KB Output is correct
23 Correct 24 ms 1620 KB Output is correct
24 Correct 31 ms 1616 KB Output is correct
25 Correct 26 ms 1620 KB Output is correct
26 Correct 25 ms 1620 KB Output is correct
27 Correct 34 ms 1632 KB Output is correct
28 Correct 37 ms 1620 KB Output is correct
29 Correct 27 ms 1672 KB Output is correct
30 Correct 29 ms 1620 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 464 KB Output is correct
37 Correct 8 ms 596 KB Output is correct
38 Correct 8 ms 648 KB Output is correct
39 Correct 14 ms 856 KB Output is correct
40 Correct 32 ms 1236 KB Output is correct
41 Correct 41 ms 1620 KB Output is correct
42 Correct 40 ms 1668 KB Output is correct
43 Correct 39 ms 1664 KB Output is correct
44 Correct 40 ms 1620 KB Output is correct
45 Correct 40 ms 1672 KB Output is correct
46 Correct 39 ms 1688 KB Output is correct
47 Correct 43 ms 1616 KB Output is correct
48 Correct 47 ms 1620 KB Output is correct
49 Correct 54 ms 1620 KB Output is correct
50 Correct 43 ms 1668 KB Output is correct
51 Correct 46 ms 1620 KB Output is correct
52 Correct 43 ms 1632 KB Output is correct
53 Correct 48 ms 1788 KB Output is correct
54 Correct 46 ms 1860 KB Output is correct
55 Correct 40 ms 1744 KB Output is correct
56 Correct 41 ms 1792 KB Output is correct
57 Correct 53 ms 1740 KB Output is correct
58 Correct 46 ms 1860 KB Output is correct
59 Correct 55 ms 1800 KB Output is correct
60 Correct 50 ms 1884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 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 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 7 ms 852 KB Output is correct
15 Correct 15 ms 1232 KB Output is correct
16 Correct 24 ms 1628 KB Output is correct
17 Correct 25 ms 1620 KB Output is correct
18 Correct 26 ms 1620 KB Output is correct
19 Correct 21 ms 1620 KB Output is correct
20 Correct 29 ms 1624 KB Output is correct
21 Correct 27 ms 1620 KB Output is correct
22 Correct 25 ms 1612 KB Output is correct
23 Correct 32 ms 1616 KB Output is correct
24 Correct 31 ms 1620 KB Output is correct
25 Correct 32 ms 1620 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 12 ms 852 KB Output is correct
30 Correct 17 ms 1248 KB Output is correct
31 Correct 27 ms 1620 KB Output is correct
32 Correct 26 ms 1624 KB Output is correct
33 Correct 24 ms 1620 KB Output is correct
34 Correct 31 ms 1616 KB Output is correct
35 Correct 26 ms 1620 KB Output is correct
36 Correct 25 ms 1620 KB Output is correct
37 Correct 34 ms 1632 KB Output is correct
38 Correct 37 ms 1620 KB Output is correct
39 Correct 27 ms 1672 KB Output is correct
40 Correct 29 ms 1620 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 464 KB Output is correct
47 Correct 8 ms 596 KB Output is correct
48 Correct 8 ms 648 KB Output is correct
49 Correct 14 ms 856 KB Output is correct
50 Correct 32 ms 1236 KB Output is correct
51 Correct 41 ms 1620 KB Output is correct
52 Correct 40 ms 1668 KB Output is correct
53 Correct 39 ms 1664 KB Output is correct
54 Correct 40 ms 1620 KB Output is correct
55 Correct 40 ms 1672 KB Output is correct
56 Correct 39 ms 1688 KB Output is correct
57 Correct 43 ms 1616 KB Output is correct
58 Correct 47 ms 1620 KB Output is correct
59 Correct 54 ms 1620 KB Output is correct
60 Correct 43 ms 1668 KB Output is correct
61 Correct 46 ms 1620 KB Output is correct
62 Correct 43 ms 1632 KB Output is correct
63 Correct 48 ms 1788 KB Output is correct
64 Correct 46 ms 1860 KB Output is correct
65 Correct 40 ms 1744 KB Output is correct
66 Correct 41 ms 1792 KB Output is correct
67 Correct 53 ms 1740 KB Output is correct
68 Correct 46 ms 1860 KB Output is correct
69 Correct 55 ms 1800 KB Output is correct
70 Correct 50 ms 1884 KB Output is correct
71 Correct 9 ms 724 KB Output is correct
72 Correct 8 ms 744 KB Output is correct
73 Correct 9 ms 660 KB Output is correct
74 Correct 9 ms 724 KB Output is correct
75 Correct 9 ms 772 KB Output is correct
76 Correct 19 ms 1044 KB Output is correct
77 Correct 20 ms 1100 KB Output is correct
78 Correct 20 ms 1088 KB Output is correct
79 Correct 18 ms 1104 KB Output is correct
80 Correct 20 ms 1096 KB Output is correct
81 Correct 47 ms 1856 KB Output is correct
82 Correct 42 ms 1816 KB Output is correct
83 Correct 41 ms 1856 KB Output is correct
84 Correct 53 ms 1868 KB Output is correct
85 Correct 43 ms 1832 KB Output is correct
86 Correct 49 ms 2360 KB Output is correct
87 Correct 48 ms 2308 KB Output is correct
88 Correct 49 ms 2336 KB Output is correct
89 Correct 54 ms 2412 KB Output is correct
90 Correct 61 ms 2524 KB Output is correct
91 Correct 106 ms 7444 KB Output is correct
92 Correct 116 ms 7500 KB Output is correct
93 Correct 131 ms 7444 KB Output is correct
94 Correct 114 ms 7448 KB Output is correct
95 Correct 112 ms 7508 KB Output is correct
96 Correct 214 ms 13312 KB Output is correct
97 Correct 217 ms 13224 KB Output is correct
98 Correct 215 ms 13300 KB Output is correct
99 Correct 237 ms 13360 KB Output is correct
100 Correct 223 ms 13260 KB Output is correct
101 Correct 162 ms 13272 KB Output is correct
102 Correct 177 ms 13304 KB Output is correct
103 Correct 217 ms 13260 KB Output is correct
104 Correct 160 ms 13324 KB Output is correct
105 Correct 184 ms 13276 KB Output is correct
106 Correct 239 ms 16160 KB Output is correct
107 Correct 199 ms 16088 KB Output is correct
108 Correct 223 ms 16172 KB Output is correct
109 Correct 236 ms 16328 KB Output is correct
110 Correct 261 ms 16296 KB Output is correct
111 Correct 226 ms 16236 KB Output is correct
112 Correct 261 ms 16208 KB Output is correct
113 Correct 213 ms 16268 KB Output is correct
114 Correct 218 ms 16392 KB Output is correct
115 Correct 236 ms 16284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 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 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 7 ms 852 KB Output is correct
15 Correct 15 ms 1232 KB Output is correct
16 Correct 24 ms 1628 KB Output is correct
17 Correct 25 ms 1620 KB Output is correct
18 Correct 26 ms 1620 KB Output is correct
19 Correct 21 ms 1620 KB Output is correct
20 Correct 29 ms 1624 KB Output is correct
21 Correct 27 ms 1620 KB Output is correct
22 Correct 25 ms 1612 KB Output is correct
23 Correct 32 ms 1616 KB Output is correct
24 Correct 31 ms 1620 KB Output is correct
25 Correct 32 ms 1620 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 12 ms 852 KB Output is correct
30 Correct 17 ms 1248 KB Output is correct
31 Correct 27 ms 1620 KB Output is correct
32 Correct 26 ms 1624 KB Output is correct
33 Correct 24 ms 1620 KB Output is correct
34 Correct 31 ms 1616 KB Output is correct
35 Correct 26 ms 1620 KB Output is correct
36 Correct 25 ms 1620 KB Output is correct
37 Correct 34 ms 1632 KB Output is correct
38 Correct 37 ms 1620 KB Output is correct
39 Correct 27 ms 1672 KB Output is correct
40 Correct 29 ms 1620 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 464 KB Output is correct
47 Correct 8 ms 596 KB Output is correct
48 Correct 8 ms 648 KB Output is correct
49 Correct 14 ms 856 KB Output is correct
50 Correct 32 ms 1236 KB Output is correct
51 Correct 41 ms 1620 KB Output is correct
52 Correct 40 ms 1668 KB Output is correct
53 Correct 39 ms 1664 KB Output is correct
54 Correct 40 ms 1620 KB Output is correct
55 Correct 40 ms 1672 KB Output is correct
56 Correct 39 ms 1688 KB Output is correct
57 Correct 43 ms 1616 KB Output is correct
58 Correct 47 ms 1620 KB Output is correct
59 Correct 54 ms 1620 KB Output is correct
60 Correct 43 ms 1668 KB Output is correct
61 Correct 46 ms 1620 KB Output is correct
62 Correct 43 ms 1632 KB Output is correct
63 Correct 48 ms 1788 KB Output is correct
64 Correct 46 ms 1860 KB Output is correct
65 Correct 40 ms 1744 KB Output is correct
66 Correct 41 ms 1792 KB Output is correct
67 Correct 53 ms 1740 KB Output is correct
68 Correct 46 ms 1860 KB Output is correct
69 Correct 55 ms 1800 KB Output is correct
70 Correct 50 ms 1884 KB Output is correct
71 Correct 9 ms 724 KB Output is correct
72 Correct 8 ms 744 KB Output is correct
73 Correct 9 ms 660 KB Output is correct
74 Correct 9 ms 724 KB Output is correct
75 Correct 9 ms 772 KB Output is correct
76 Correct 19 ms 1044 KB Output is correct
77 Correct 20 ms 1100 KB Output is correct
78 Correct 20 ms 1088 KB Output is correct
79 Correct 18 ms 1104 KB Output is correct
80 Correct 20 ms 1096 KB Output is correct
81 Correct 47 ms 1856 KB Output is correct
82 Correct 42 ms 1816 KB Output is correct
83 Correct 41 ms 1856 KB Output is correct
84 Correct 53 ms 1868 KB Output is correct
85 Correct 43 ms 1832 KB Output is correct
86 Correct 49 ms 2360 KB Output is correct
87 Correct 48 ms 2308 KB Output is correct
88 Correct 49 ms 2336 KB Output is correct
89 Correct 54 ms 2412 KB Output is correct
90 Correct 61 ms 2524 KB Output is correct
91 Correct 106 ms 7444 KB Output is correct
92 Correct 116 ms 7500 KB Output is correct
93 Correct 131 ms 7444 KB Output is correct
94 Correct 114 ms 7448 KB Output is correct
95 Correct 112 ms 7508 KB Output is correct
96 Correct 214 ms 13312 KB Output is correct
97 Correct 217 ms 13224 KB Output is correct
98 Correct 215 ms 13300 KB Output is correct
99 Correct 237 ms 13360 KB Output is correct
100 Correct 223 ms 13260 KB Output is correct
101 Correct 162 ms 13272 KB Output is correct
102 Correct 177 ms 13304 KB Output is correct
103 Correct 217 ms 13260 KB Output is correct
104 Correct 160 ms 13324 KB Output is correct
105 Correct 184 ms 13276 KB Output is correct
106 Correct 239 ms 16160 KB Output is correct
107 Correct 199 ms 16088 KB Output is correct
108 Correct 223 ms 16172 KB Output is correct
109 Correct 236 ms 16328 KB Output is correct
110 Correct 261 ms 16296 KB Output is correct
111 Correct 226 ms 16236 KB Output is correct
112 Correct 261 ms 16208 KB Output is correct
113 Correct 213 ms 16268 KB Output is correct
114 Correct 218 ms 16392 KB Output is correct
115 Correct 236 ms 16284 KB Output is correct
116 Correct 6631 ms 1000 KB Output is correct
117 Execution timed out 7019 ms 1096 KB Time limit exceeded
118 Halted 0 ms 0 KB -