답안 #617099

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
617099 2022-08-01T08:41:33 Z dxz05 Diversity (CEOI21_diversity) C++14
38 / 100
7000 ms 14932 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;
        for (int j = i + 1; j < n; j++){
            res += (ll) (j - i + 1) * cnt[i] * cnt[j];
        }
    }

    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;
            int 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 324 KB Output is correct
3 Correct 0 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 324 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 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 9 ms 920 KB Output is correct
5 Correct 14 ms 1528 KB Output is correct
6 Correct 25 ms 2136 KB Output is correct
7 Correct 19 ms 2120 KB Output is correct
8 Correct 22 ms 2120 KB Output is correct
9 Correct 22 ms 2132 KB Output is correct
10 Correct 22 ms 2132 KB Output is correct
11 Correct 22 ms 2140 KB Output is correct
12 Correct 25 ms 2136 KB Output is correct
13 Correct 24 ms 2136 KB Output is correct
14 Correct 21 ms 2132 KB Output is correct
15 Correct 23 ms 2140 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 1 ms 340 KB Output is correct
4 Correct 9 ms 920 KB Output is correct
5 Correct 14 ms 1528 KB Output is correct
6 Correct 25 ms 2136 KB Output is correct
7 Correct 19 ms 2120 KB Output is correct
8 Correct 22 ms 2120 KB Output is correct
9 Correct 22 ms 2132 KB Output is correct
10 Correct 22 ms 2132 KB Output is correct
11 Correct 22 ms 2140 KB Output is correct
12 Correct 25 ms 2136 KB Output is correct
13 Correct 24 ms 2136 KB Output is correct
14 Correct 21 ms 2132 KB Output is correct
15 Correct 23 ms 2140 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 392 KB Output is correct
19 Correct 8 ms 1000 KB Output is correct
20 Correct 17 ms 1636 KB Output is correct
21 Correct 26 ms 2316 KB Output is correct
22 Correct 24 ms 2264 KB Output is correct
23 Correct 27 ms 2264 KB Output is correct
24 Correct 26 ms 2260 KB Output is correct
25 Correct 26 ms 2264 KB Output is correct
26 Correct 25 ms 2260 KB Output is correct
27 Correct 30 ms 2272 KB Output is correct
28 Correct 28 ms 2128 KB Output is correct
29 Correct 26 ms 2132 KB Output is correct
30 Correct 25 ms 2260 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 1 ms 340 KB Output is correct
4 Correct 9 ms 920 KB Output is correct
5 Correct 14 ms 1528 KB Output is correct
6 Correct 25 ms 2136 KB Output is correct
7 Correct 19 ms 2120 KB Output is correct
8 Correct 22 ms 2120 KB Output is correct
9 Correct 22 ms 2132 KB Output is correct
10 Correct 22 ms 2132 KB Output is correct
11 Correct 22 ms 2140 KB Output is correct
12 Correct 25 ms 2136 KB Output is correct
13 Correct 24 ms 2136 KB Output is correct
14 Correct 21 ms 2132 KB Output is correct
15 Correct 23 ms 2140 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 392 KB Output is correct
19 Correct 8 ms 1000 KB Output is correct
20 Correct 17 ms 1636 KB Output is correct
21 Correct 26 ms 2316 KB Output is correct
22 Correct 24 ms 2264 KB Output is correct
23 Correct 27 ms 2264 KB Output is correct
24 Correct 26 ms 2260 KB Output is correct
25 Correct 26 ms 2264 KB Output is correct
26 Correct 25 ms 2260 KB Output is correct
27 Correct 30 ms 2272 KB Output is correct
28 Correct 28 ms 2128 KB Output is correct
29 Correct 26 ms 2132 KB Output is correct
30 Correct 25 ms 2260 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 3 ms 464 KB Output is correct
37 Correct 10 ms 720 KB Output is correct
38 Correct 10 ms 724 KB Output is correct
39 Correct 15 ms 1108 KB Output is correct
40 Correct 28 ms 1876 KB Output is correct
41 Correct 42 ms 2692 KB Output is correct
42 Correct 44 ms 2688 KB Output is correct
43 Correct 42 ms 2644 KB Output is correct
44 Correct 42 ms 2696 KB Output is correct
45 Correct 40 ms 2636 KB Output is correct
46 Correct 41 ms 2632 KB Output is correct
47 Correct 41 ms 2688 KB Output is correct
48 Correct 44 ms 2688 KB Output is correct
49 Correct 61 ms 2688 KB Output is correct
50 Correct 46 ms 2644 KB Output is correct
51 Correct 42 ms 2692 KB Output is correct
52 Correct 42 ms 2640 KB Output is correct
53 Correct 40 ms 2764 KB Output is correct
54 Correct 40 ms 2692 KB Output is correct
55 Correct 43 ms 2684 KB Output is correct
56 Correct 50 ms 2648 KB Output is correct
57 Correct 41 ms 2640 KB Output is correct
58 Correct 40 ms 2640 KB Output is correct
59 Correct 40 ms 2656 KB Output is correct
60 Correct 43 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 0 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 324 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 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 9 ms 920 KB Output is correct
15 Correct 14 ms 1528 KB Output is correct
16 Correct 25 ms 2136 KB Output is correct
17 Correct 19 ms 2120 KB Output is correct
18 Correct 22 ms 2120 KB Output is correct
19 Correct 22 ms 2132 KB Output is correct
20 Correct 22 ms 2132 KB Output is correct
21 Correct 22 ms 2140 KB Output is correct
22 Correct 25 ms 2136 KB Output is correct
23 Correct 24 ms 2136 KB Output is correct
24 Correct 21 ms 2132 KB Output is correct
25 Correct 23 ms 2140 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 392 KB Output is correct
29 Correct 8 ms 1000 KB Output is correct
30 Correct 17 ms 1636 KB Output is correct
31 Correct 26 ms 2316 KB Output is correct
32 Correct 24 ms 2264 KB Output is correct
33 Correct 27 ms 2264 KB Output is correct
34 Correct 26 ms 2260 KB Output is correct
35 Correct 26 ms 2264 KB Output is correct
36 Correct 25 ms 2260 KB Output is correct
37 Correct 30 ms 2272 KB Output is correct
38 Correct 28 ms 2128 KB Output is correct
39 Correct 26 ms 2132 KB Output is correct
40 Correct 25 ms 2260 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 464 KB Output is correct
47 Correct 10 ms 720 KB Output is correct
48 Correct 10 ms 724 KB Output is correct
49 Correct 15 ms 1108 KB Output is correct
50 Correct 28 ms 1876 KB Output is correct
51 Correct 42 ms 2692 KB Output is correct
52 Correct 44 ms 2688 KB Output is correct
53 Correct 42 ms 2644 KB Output is correct
54 Correct 42 ms 2696 KB Output is correct
55 Correct 40 ms 2636 KB Output is correct
56 Correct 41 ms 2632 KB Output is correct
57 Correct 41 ms 2688 KB Output is correct
58 Correct 44 ms 2688 KB Output is correct
59 Correct 61 ms 2688 KB Output is correct
60 Correct 46 ms 2644 KB Output is correct
61 Correct 42 ms 2692 KB Output is correct
62 Correct 42 ms 2640 KB Output is correct
63 Correct 40 ms 2764 KB Output is correct
64 Correct 40 ms 2692 KB Output is correct
65 Correct 43 ms 2684 KB Output is correct
66 Correct 50 ms 2648 KB Output is correct
67 Correct 41 ms 2640 KB Output is correct
68 Correct 40 ms 2640 KB Output is correct
69 Correct 40 ms 2656 KB Output is correct
70 Correct 43 ms 2688 KB Output is correct
71 Correct 10 ms 852 KB Output is correct
72 Correct 10 ms 848 KB Output is correct
73 Correct 10 ms 852 KB Output is correct
74 Correct 12 ms 860 KB Output is correct
75 Correct 10 ms 756 KB Output is correct
76 Correct 27 ms 1364 KB Output is correct
77 Correct 27 ms 1444 KB Output is correct
78 Correct 27 ms 1452 KB Output is correct
79 Correct 28 ms 1344 KB Output is correct
80 Correct 36 ms 1360 KB Output is correct
81 Correct 78 ms 2572 KB Output is correct
82 Correct 78 ms 2556 KB Output is correct
83 Correct 95 ms 2592 KB Output is correct
84 Correct 96 ms 2676 KB Output is correct
85 Correct 90 ms 2516 KB Output is correct
86 Correct 194 ms 3140 KB Output is correct
87 Correct 191 ms 3136 KB Output is correct
88 Correct 216 ms 3224 KB Output is correct
89 Correct 192 ms 3148 KB Output is correct
90 Correct 203 ms 3144 KB Output is correct
91 Correct 3677 ms 9000 KB Output is correct
92 Correct 3685 ms 8996 KB Output is correct
93 Correct 3624 ms 9008 KB Output is correct
94 Correct 4124 ms 9004 KB Output is correct
95 Correct 3651 ms 8996 KB Output is correct
96 Execution timed out 7038 ms 14932 KB Time limit exceeded
97 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 0 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 324 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 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 9 ms 920 KB Output is correct
15 Correct 14 ms 1528 KB Output is correct
16 Correct 25 ms 2136 KB Output is correct
17 Correct 19 ms 2120 KB Output is correct
18 Correct 22 ms 2120 KB Output is correct
19 Correct 22 ms 2132 KB Output is correct
20 Correct 22 ms 2132 KB Output is correct
21 Correct 22 ms 2140 KB Output is correct
22 Correct 25 ms 2136 KB Output is correct
23 Correct 24 ms 2136 KB Output is correct
24 Correct 21 ms 2132 KB Output is correct
25 Correct 23 ms 2140 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 392 KB Output is correct
29 Correct 8 ms 1000 KB Output is correct
30 Correct 17 ms 1636 KB Output is correct
31 Correct 26 ms 2316 KB Output is correct
32 Correct 24 ms 2264 KB Output is correct
33 Correct 27 ms 2264 KB Output is correct
34 Correct 26 ms 2260 KB Output is correct
35 Correct 26 ms 2264 KB Output is correct
36 Correct 25 ms 2260 KB Output is correct
37 Correct 30 ms 2272 KB Output is correct
38 Correct 28 ms 2128 KB Output is correct
39 Correct 26 ms 2132 KB Output is correct
40 Correct 25 ms 2260 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 3 ms 464 KB Output is correct
47 Correct 10 ms 720 KB Output is correct
48 Correct 10 ms 724 KB Output is correct
49 Correct 15 ms 1108 KB Output is correct
50 Correct 28 ms 1876 KB Output is correct
51 Correct 42 ms 2692 KB Output is correct
52 Correct 44 ms 2688 KB Output is correct
53 Correct 42 ms 2644 KB Output is correct
54 Correct 42 ms 2696 KB Output is correct
55 Correct 40 ms 2636 KB Output is correct
56 Correct 41 ms 2632 KB Output is correct
57 Correct 41 ms 2688 KB Output is correct
58 Correct 44 ms 2688 KB Output is correct
59 Correct 61 ms 2688 KB Output is correct
60 Correct 46 ms 2644 KB Output is correct
61 Correct 42 ms 2692 KB Output is correct
62 Correct 42 ms 2640 KB Output is correct
63 Correct 40 ms 2764 KB Output is correct
64 Correct 40 ms 2692 KB Output is correct
65 Correct 43 ms 2684 KB Output is correct
66 Correct 50 ms 2648 KB Output is correct
67 Correct 41 ms 2640 KB Output is correct
68 Correct 40 ms 2640 KB Output is correct
69 Correct 40 ms 2656 KB Output is correct
70 Correct 43 ms 2688 KB Output is correct
71 Correct 10 ms 852 KB Output is correct
72 Correct 10 ms 848 KB Output is correct
73 Correct 10 ms 852 KB Output is correct
74 Correct 12 ms 860 KB Output is correct
75 Correct 10 ms 756 KB Output is correct
76 Correct 27 ms 1364 KB Output is correct
77 Correct 27 ms 1444 KB Output is correct
78 Correct 27 ms 1452 KB Output is correct
79 Correct 28 ms 1344 KB Output is correct
80 Correct 36 ms 1360 KB Output is correct
81 Correct 78 ms 2572 KB Output is correct
82 Correct 78 ms 2556 KB Output is correct
83 Correct 95 ms 2592 KB Output is correct
84 Correct 96 ms 2676 KB Output is correct
85 Correct 90 ms 2516 KB Output is correct
86 Correct 194 ms 3140 KB Output is correct
87 Correct 191 ms 3136 KB Output is correct
88 Correct 216 ms 3224 KB Output is correct
89 Correct 192 ms 3148 KB Output is correct
90 Correct 203 ms 3144 KB Output is correct
91 Correct 3677 ms 9000 KB Output is correct
92 Correct 3685 ms 8996 KB Output is correct
93 Correct 3624 ms 9008 KB Output is correct
94 Correct 4124 ms 9004 KB Output is correct
95 Correct 3651 ms 8996 KB Output is correct
96 Execution timed out 7038 ms 14932 KB Time limit exceeded
97 Halted 0 ms 0 KB -