Submission #617129

# Submission time Handle Problem Language Result Execution time Memory
617129 2022-08-01T08:53:14 Z dxz05 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 17848 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;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 320 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 8 ms 844 KB Output is correct
5 Correct 15 ms 1492 KB Output is correct
6 Correct 21 ms 2104 KB Output is correct
7 Correct 20 ms 2004 KB Output is correct
8 Correct 25 ms 2004 KB Output is correct
9 Correct 22 ms 2128 KB Output is correct
10 Correct 22 ms 2124 KB Output is correct
11 Correct 24 ms 2128 KB Output is correct
12 Correct 24 ms 2152 KB Output is correct
13 Correct 23 ms 2132 KB Output is correct
14 Correct 25 ms 2132 KB Output is correct
15 Correct 24 ms 2132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 8 ms 844 KB Output is correct
5 Correct 15 ms 1492 KB Output is correct
6 Correct 21 ms 2104 KB Output is correct
7 Correct 20 ms 2004 KB Output is correct
8 Correct 25 ms 2004 KB Output is correct
9 Correct 22 ms 2128 KB Output is correct
10 Correct 22 ms 2124 KB Output is correct
11 Correct 24 ms 2128 KB Output is correct
12 Correct 24 ms 2152 KB Output is correct
13 Correct 23 ms 2132 KB Output is correct
14 Correct 25 ms 2132 KB Output is correct
15 Correct 24 ms 2132 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 13 ms 980 KB Output is correct
20 Correct 21 ms 1620 KB Output is correct
21 Correct 33 ms 2308 KB Output is correct
22 Correct 29 ms 2272 KB Output is correct
23 Correct 35 ms 2292 KB Output is correct
24 Correct 27 ms 2256 KB Output is correct
25 Correct 27 ms 2260 KB Output is correct
26 Correct 27 ms 2256 KB Output is correct
27 Correct 33 ms 2252 KB Output is correct
28 Correct 29 ms 2132 KB Output is correct
29 Correct 36 ms 2132 KB Output is correct
30 Correct 28 ms 2252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 8 ms 844 KB Output is correct
5 Correct 15 ms 1492 KB Output is correct
6 Correct 21 ms 2104 KB Output is correct
7 Correct 20 ms 2004 KB Output is correct
8 Correct 25 ms 2004 KB Output is correct
9 Correct 22 ms 2128 KB Output is correct
10 Correct 22 ms 2124 KB Output is correct
11 Correct 24 ms 2128 KB Output is correct
12 Correct 24 ms 2152 KB Output is correct
13 Correct 23 ms 2132 KB Output is correct
14 Correct 25 ms 2132 KB Output is correct
15 Correct 24 ms 2132 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 13 ms 980 KB Output is correct
20 Correct 21 ms 1620 KB Output is correct
21 Correct 33 ms 2308 KB Output is correct
22 Correct 29 ms 2272 KB Output is correct
23 Correct 35 ms 2292 KB Output is correct
24 Correct 27 ms 2256 KB Output is correct
25 Correct 27 ms 2260 KB Output is correct
26 Correct 27 ms 2256 KB Output is correct
27 Correct 33 ms 2252 KB Output is correct
28 Correct 29 ms 2132 KB Output is correct
29 Correct 36 ms 2132 KB Output is correct
30 Correct 28 ms 2252 KB Output is correct
31 Correct 1 ms 276 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 392 KB Output is correct
35 Correct 3 ms 336 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 7 ms 724 KB Output is correct
38 Correct 10 ms 724 KB Output is correct
39 Correct 15 ms 1108 KB Output is correct
40 Correct 30 ms 1876 KB Output is correct
41 Correct 48 ms 2688 KB Output is correct
42 Correct 42 ms 2688 KB Output is correct
43 Correct 44 ms 2644 KB Output is correct
44 Correct 50 ms 2692 KB Output is correct
45 Correct 58 ms 2692 KB Output is correct
46 Correct 48 ms 2692 KB Output is correct
47 Correct 40 ms 2648 KB Output is correct
48 Correct 51 ms 2692 KB Output is correct
49 Correct 42 ms 2680 KB Output is correct
50 Correct 49 ms 2644 KB Output is correct
51 Correct 47 ms 2636 KB Output is correct
52 Correct 55 ms 2628 KB Output is correct
53 Correct 49 ms 2560 KB Output is correct
54 Correct 43 ms 2516 KB Output is correct
55 Correct 52 ms 2560 KB Output is correct
56 Correct 46 ms 2560 KB Output is correct
57 Correct 57 ms 2500 KB Output is correct
58 Correct 46 ms 2440 KB Output is correct
59 Correct 43 ms 2440 KB Output is correct
60 Correct 41 ms 2444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 320 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 8 ms 844 KB Output is correct
15 Correct 15 ms 1492 KB Output is correct
16 Correct 21 ms 2104 KB Output is correct
17 Correct 20 ms 2004 KB Output is correct
18 Correct 25 ms 2004 KB Output is correct
19 Correct 22 ms 2128 KB Output is correct
20 Correct 22 ms 2124 KB Output is correct
21 Correct 24 ms 2128 KB Output is correct
22 Correct 24 ms 2152 KB Output is correct
23 Correct 23 ms 2132 KB Output is correct
24 Correct 25 ms 2132 KB Output is correct
25 Correct 24 ms 2132 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 13 ms 980 KB Output is correct
30 Correct 21 ms 1620 KB Output is correct
31 Correct 33 ms 2308 KB Output is correct
32 Correct 29 ms 2272 KB Output is correct
33 Correct 35 ms 2292 KB Output is correct
34 Correct 27 ms 2256 KB Output is correct
35 Correct 27 ms 2260 KB Output is correct
36 Correct 27 ms 2256 KB Output is correct
37 Correct 33 ms 2252 KB Output is correct
38 Correct 29 ms 2132 KB Output is correct
39 Correct 36 ms 2132 KB Output is correct
40 Correct 28 ms 2252 KB Output is correct
41 Correct 1 ms 276 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 392 KB Output is correct
45 Correct 3 ms 336 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 7 ms 724 KB Output is correct
48 Correct 10 ms 724 KB Output is correct
49 Correct 15 ms 1108 KB Output is correct
50 Correct 30 ms 1876 KB Output is correct
51 Correct 48 ms 2688 KB Output is correct
52 Correct 42 ms 2688 KB Output is correct
53 Correct 44 ms 2644 KB Output is correct
54 Correct 50 ms 2692 KB Output is correct
55 Correct 58 ms 2692 KB Output is correct
56 Correct 48 ms 2692 KB Output is correct
57 Correct 40 ms 2648 KB Output is correct
58 Correct 51 ms 2692 KB Output is correct
59 Correct 42 ms 2680 KB Output is correct
60 Correct 49 ms 2644 KB Output is correct
61 Correct 47 ms 2636 KB Output is correct
62 Correct 55 ms 2628 KB Output is correct
63 Correct 49 ms 2560 KB Output is correct
64 Correct 43 ms 2516 KB Output is correct
65 Correct 52 ms 2560 KB Output is correct
66 Correct 46 ms 2560 KB Output is correct
67 Correct 57 ms 2500 KB Output is correct
68 Correct 46 ms 2440 KB Output is correct
69 Correct 43 ms 2440 KB Output is correct
70 Correct 41 ms 2444 KB Output is correct
71 Correct 9 ms 852 KB Output is correct
72 Correct 9 ms 852 KB Output is correct
73 Correct 8 ms 776 KB Output is correct
74 Correct 14 ms 852 KB Output is correct
75 Correct 11 ms 752 KB Output is correct
76 Correct 34 ms 1344 KB Output is correct
77 Correct 20 ms 1444 KB Output is correct
78 Correct 20 ms 1364 KB Output is correct
79 Correct 19 ms 1432 KB Output is correct
80 Correct 21 ms 1436 KB Output is correct
81 Correct 45 ms 2228 KB Output is correct
82 Correct 44 ms 2236 KB Output is correct
83 Correct 45 ms 2212 KB Output is correct
84 Correct 45 ms 2244 KB Output is correct
85 Correct 46 ms 2212 KB Output is correct
86 Correct 50 ms 2740 KB Output is correct
87 Correct 55 ms 2736 KB Output is correct
88 Correct 52 ms 2876 KB Output is correct
89 Correct 56 ms 2784 KB Output is correct
90 Correct 63 ms 2800 KB Output is correct
91 Correct 148 ms 7904 KB Output is correct
92 Correct 104 ms 7852 KB Output is correct
93 Correct 115 ms 7924 KB Output is correct
94 Correct 120 ms 7892 KB Output is correct
95 Correct 103 ms 7920 KB Output is correct
96 Correct 169 ms 13612 KB Output is correct
97 Correct 177 ms 15020 KB Output is correct
98 Correct 219 ms 14932 KB Output is correct
99 Correct 185 ms 14944 KB Output is correct
100 Correct 217 ms 14976 KB Output is correct
101 Correct 200 ms 15128 KB Output is correct
102 Correct 195 ms 14884 KB Output is correct
103 Correct 227 ms 14872 KB Output is correct
104 Correct 203 ms 14948 KB Output is correct
105 Correct 215 ms 14928 KB Output is correct
106 Correct 230 ms 17668 KB Output is correct
107 Correct 232 ms 17652 KB Output is correct
108 Correct 212 ms 17632 KB Output is correct
109 Correct 193 ms 17848 KB Output is correct
110 Correct 257 ms 17704 KB Output is correct
111 Correct 194 ms 17708 KB Output is correct
112 Correct 212 ms 17608 KB Output is correct
113 Correct 192 ms 17620 KB Output is correct
114 Correct 259 ms 17760 KB Output is correct
115 Correct 255 ms 17688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 320 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 8 ms 844 KB Output is correct
15 Correct 15 ms 1492 KB Output is correct
16 Correct 21 ms 2104 KB Output is correct
17 Correct 20 ms 2004 KB Output is correct
18 Correct 25 ms 2004 KB Output is correct
19 Correct 22 ms 2128 KB Output is correct
20 Correct 22 ms 2124 KB Output is correct
21 Correct 24 ms 2128 KB Output is correct
22 Correct 24 ms 2152 KB Output is correct
23 Correct 23 ms 2132 KB Output is correct
24 Correct 25 ms 2132 KB Output is correct
25 Correct 24 ms 2132 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 13 ms 980 KB Output is correct
30 Correct 21 ms 1620 KB Output is correct
31 Correct 33 ms 2308 KB Output is correct
32 Correct 29 ms 2272 KB Output is correct
33 Correct 35 ms 2292 KB Output is correct
34 Correct 27 ms 2256 KB Output is correct
35 Correct 27 ms 2260 KB Output is correct
36 Correct 27 ms 2256 KB Output is correct
37 Correct 33 ms 2252 KB Output is correct
38 Correct 29 ms 2132 KB Output is correct
39 Correct 36 ms 2132 KB Output is correct
40 Correct 28 ms 2252 KB Output is correct
41 Correct 1 ms 276 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 392 KB Output is correct
45 Correct 3 ms 336 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 7 ms 724 KB Output is correct
48 Correct 10 ms 724 KB Output is correct
49 Correct 15 ms 1108 KB Output is correct
50 Correct 30 ms 1876 KB Output is correct
51 Correct 48 ms 2688 KB Output is correct
52 Correct 42 ms 2688 KB Output is correct
53 Correct 44 ms 2644 KB Output is correct
54 Correct 50 ms 2692 KB Output is correct
55 Correct 58 ms 2692 KB Output is correct
56 Correct 48 ms 2692 KB Output is correct
57 Correct 40 ms 2648 KB Output is correct
58 Correct 51 ms 2692 KB Output is correct
59 Correct 42 ms 2680 KB Output is correct
60 Correct 49 ms 2644 KB Output is correct
61 Correct 47 ms 2636 KB Output is correct
62 Correct 55 ms 2628 KB Output is correct
63 Correct 49 ms 2560 KB Output is correct
64 Correct 43 ms 2516 KB Output is correct
65 Correct 52 ms 2560 KB Output is correct
66 Correct 46 ms 2560 KB Output is correct
67 Correct 57 ms 2500 KB Output is correct
68 Correct 46 ms 2440 KB Output is correct
69 Correct 43 ms 2440 KB Output is correct
70 Correct 41 ms 2444 KB Output is correct
71 Correct 9 ms 852 KB Output is correct
72 Correct 9 ms 852 KB Output is correct
73 Correct 8 ms 776 KB Output is correct
74 Correct 14 ms 852 KB Output is correct
75 Correct 11 ms 752 KB Output is correct
76 Correct 34 ms 1344 KB Output is correct
77 Correct 20 ms 1444 KB Output is correct
78 Correct 20 ms 1364 KB Output is correct
79 Correct 19 ms 1432 KB Output is correct
80 Correct 21 ms 1436 KB Output is correct
81 Correct 45 ms 2228 KB Output is correct
82 Correct 44 ms 2236 KB Output is correct
83 Correct 45 ms 2212 KB Output is correct
84 Correct 45 ms 2244 KB Output is correct
85 Correct 46 ms 2212 KB Output is correct
86 Correct 50 ms 2740 KB Output is correct
87 Correct 55 ms 2736 KB Output is correct
88 Correct 52 ms 2876 KB Output is correct
89 Correct 56 ms 2784 KB Output is correct
90 Correct 63 ms 2800 KB Output is correct
91 Correct 148 ms 7904 KB Output is correct
92 Correct 104 ms 7852 KB Output is correct
93 Correct 115 ms 7924 KB Output is correct
94 Correct 120 ms 7892 KB Output is correct
95 Correct 103 ms 7920 KB Output is correct
96 Correct 169 ms 13612 KB Output is correct
97 Correct 177 ms 15020 KB Output is correct
98 Correct 219 ms 14932 KB Output is correct
99 Correct 185 ms 14944 KB Output is correct
100 Correct 217 ms 14976 KB Output is correct
101 Correct 200 ms 15128 KB Output is correct
102 Correct 195 ms 14884 KB Output is correct
103 Correct 227 ms 14872 KB Output is correct
104 Correct 203 ms 14948 KB Output is correct
105 Correct 215 ms 14928 KB Output is correct
106 Correct 230 ms 17668 KB Output is correct
107 Correct 232 ms 17652 KB Output is correct
108 Correct 212 ms 17632 KB Output is correct
109 Correct 193 ms 17848 KB Output is correct
110 Correct 257 ms 17704 KB Output is correct
111 Correct 194 ms 17708 KB Output is correct
112 Correct 212 ms 17608 KB Output is correct
113 Correct 192 ms 17620 KB Output is correct
114 Correct 259 ms 17760 KB Output is correct
115 Correct 255 ms 17688 KB Output is correct
116 Correct 6820 ms 1276 KB Output is correct
117 Execution timed out 7083 ms 1132 KB Time limit exceeded
118 Halted 0 ms 0 KB -