Submission #549576

# Submission time Handle Problem Language Result Execution time Memory
549576 2022-04-16T04:36:01 Z minhcool Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 4928 KB
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;

const int N = 4e5 + 5;

const int oo = 1e18 + 7, mod = 1e9 + 7;

int n, q, a[N];

int cnt[N];

int cntt[N];

int S = 500;

bool cmp(iii a, iii b){
    if(a.fi.fi / S != b.fi.fi / S) return (a.fi.fi / S < b.fi.fi / S);
    else{
        if(!((a.fi.fi / S) & 1)) return (a.fi.se < b.fi.se);
        else return (a.fi.se > b.fi.se);
    }
}

vector<iii> queries;
set<int> se;

void ins(int pos){
    cntt[cnt[a[pos]]]--;
    if(!cntt[cnt[a[pos]]]) se.erase(cnt[a[pos]]);
    cnt[a[pos]]++;
    //cout << a[pos] << " " << cnt[a[pos]] << "\n";
    cntt[cnt[a[pos]]]++;
    if(cntt[cnt[a[pos]]] == 1) se.insert(cnt[a[pos]]);
}

void er(int pos){
    cntt[cnt[a[pos]]]--;
    if(!cntt[cnt[a[pos]]]) se.erase(cnt[a[pos]]);
    cnt[a[pos]]--;
    //cout << a[pos] << " " << cnt[a[pos]] << "\n";
    cntt[cnt[a[pos]]]++;
    if(cntt[cnt[a[pos]]] == 1) se.insert(cnt[a[pos]]);
}

int answer[N];

void process(){
    cin >> n >> q;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        //cnt[a[i]].fi++;
    }
    for(int i = 1; i <= q; i++){
        int l, r;
        cin >> l >> r;
        queries.pb({{l, r}, i});
    }   
    sort(queries.begin(), queries.end(), cmp);
    int lstl = 0, lstr = 0;
    bool ck = 1;
    for(int i = 0; i < q; i++){
        //sort(cnt + 1, cnt + 300000 + 1, greater<ii>());
        while(lstr < queries[i].fi.se){
            //ins(a[lstr]);
            lstr++;
            ins(lstr);
        }
        while(lstl > queries[i].fi.fi){
            lstl--;
            ins(lstl);
        }
        while(lstl < queries[i].fi.fi){
            if(lstl) er(lstl);
            lstl++;
        }
        while(lstr > queries[i].fi.se){
            er(lstr);
            lstr--;
        }
        if(!ck) continue;
        //cout << lstl << " " << lstr << "\n";
        vector<ii> vc1, vc2;
        int total = 0;
        for(auto it : se){
            int temp1 = (cntt[it] + 1) / 2, temp2 = cntt[it] / 2;
            if(!(total & 1)){
                if(temp1) vc1.pb({it, temp1});
                if(temp2) vc2.pb({it, temp2});
            }
            else{
                if(temp2) vc1.pb({it, temp2});
                if(temp1) vc2.pb({it, temp1});
            }
            total += cntt[it];
        }
        //for(int j = 1; j <= 5; j++) cout << j << " " << cnt[j] << "\n";
        for(int j = vc2.size() - 1; j >= 0; j--) vc1.pb(vc2[j]);
        int tol = 0, tol2 = 0, toll = 1;
        for(auto it : vc1){
            //cout << lstl << " " << lstr << " " << it.fi << " " << it.se << "\n";
            int temp = ((it.se * (it.se + 1) * (2 * it.se + 1)) / 6 + (it.se * (it.se + 1)) / 2) / 2 - it.se;
            temp = temp * it.fi * it.fi + toll * it.fi * it.se;
            temp += it.se * (it.fi * (it.fi - 1) / 2);
            temp += tol * ((it.se * (it.se - 1)) / 2) * it.fi;
            toll += ((it.se * (it.se + 1)) / 2) * it.fi + tol * (it.se - 1);
            tol += it.fi * it.se;
            toll += tol;
            tol2 += temp;
        }
        answer[queries[i].se] = tol2;
        if(!(i % 1000) && (long double)clock() / (long double)CLOCKS_PER_SEC >= 3.0) ck = 0;
    }
    for(int i = 1; i <= q; i++) cout << answer[i] << "\n";
}

signed main(){
    ios_base::sync_with_stdio(0);
    process();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 13 ms 1400 KB Output is correct
5 Correct 28 ms 2568 KB Output is correct
6 Correct 43 ms 3272 KB Output is correct
7 Correct 40 ms 4048 KB Output is correct
8 Correct 46 ms 3136 KB Output is correct
9 Correct 43 ms 3156 KB Output is correct
10 Correct 41 ms 3588 KB Output is correct
11 Correct 41 ms 3292 KB Output is correct
12 Correct 44 ms 3192 KB Output is correct
13 Correct 43 ms 3184 KB Output is correct
14 Correct 42 ms 3308 KB Output is correct
15 Correct 45 ms 3248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 13 ms 1400 KB Output is correct
5 Correct 28 ms 2568 KB Output is correct
6 Correct 43 ms 3272 KB Output is correct
7 Correct 40 ms 4048 KB Output is correct
8 Correct 46 ms 3136 KB Output is correct
9 Correct 43 ms 3156 KB Output is correct
10 Correct 41 ms 3588 KB Output is correct
11 Correct 41 ms 3292 KB Output is correct
12 Correct 44 ms 3192 KB Output is correct
13 Correct 43 ms 3184 KB Output is correct
14 Correct 42 ms 3308 KB Output is correct
15 Correct 45 ms 3248 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 20 ms 1316 KB Output is correct
20 Correct 35 ms 2276 KB Output is correct
21 Correct 55 ms 3224 KB Output is correct
22 Correct 50 ms 3116 KB Output is correct
23 Correct 47 ms 3180 KB Output is correct
24 Correct 49 ms 3036 KB Output is correct
25 Correct 52 ms 3080 KB Output is correct
26 Correct 60 ms 3052 KB Output is correct
27 Correct 53 ms 3152 KB Output is correct
28 Correct 47 ms 3404 KB Output is correct
29 Correct 48 ms 3024 KB Output is correct
30 Correct 50 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 13 ms 1400 KB Output is correct
5 Correct 28 ms 2568 KB Output is correct
6 Correct 43 ms 3272 KB Output is correct
7 Correct 40 ms 4048 KB Output is correct
8 Correct 46 ms 3136 KB Output is correct
9 Correct 43 ms 3156 KB Output is correct
10 Correct 41 ms 3588 KB Output is correct
11 Correct 41 ms 3292 KB Output is correct
12 Correct 44 ms 3192 KB Output is correct
13 Correct 43 ms 3184 KB Output is correct
14 Correct 42 ms 3308 KB Output is correct
15 Correct 45 ms 3248 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 20 ms 1316 KB Output is correct
20 Correct 35 ms 2276 KB Output is correct
21 Correct 55 ms 3224 KB Output is correct
22 Correct 50 ms 3116 KB Output is correct
23 Correct 47 ms 3180 KB Output is correct
24 Correct 49 ms 3036 KB Output is correct
25 Correct 52 ms 3080 KB Output is correct
26 Correct 60 ms 3052 KB Output is correct
27 Correct 53 ms 3152 KB Output is correct
28 Correct 47 ms 3404 KB Output is correct
29 Correct 48 ms 3024 KB Output is correct
30 Correct 50 ms 3156 KB Output is correct
31 Correct 1 ms 340 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 468 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 7 ms 852 KB Output is correct
38 Correct 5 ms 848 KB Output is correct
39 Correct 11 ms 1276 KB Output is correct
40 Correct 23 ms 2080 KB Output is correct
41 Correct 38 ms 2856 KB Output is correct
42 Correct 39 ms 2872 KB Output is correct
43 Correct 40 ms 2868 KB Output is correct
44 Correct 39 ms 2892 KB Output is correct
45 Correct 39 ms 2840 KB Output is correct
46 Correct 42 ms 2916 KB Output is correct
47 Correct 39 ms 2792 KB Output is correct
48 Correct 39 ms 2836 KB Output is correct
49 Correct 38 ms 2796 KB Output is correct
50 Correct 40 ms 2848 KB Output is correct
51 Correct 40 ms 2896 KB Output is correct
52 Correct 39 ms 2876 KB Output is correct
53 Correct 39 ms 2884 KB Output is correct
54 Correct 39 ms 2876 KB Output is correct
55 Correct 39 ms 2816 KB Output is correct
56 Correct 40 ms 2824 KB Output is correct
57 Correct 38 ms 2860 KB Output is correct
58 Correct 40 ms 2912 KB Output is correct
59 Correct 41 ms 2988 KB Output is correct
60 Correct 42 ms 2868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 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 2 ms 468 KB Output is correct
14 Correct 13 ms 1400 KB Output is correct
15 Correct 28 ms 2568 KB Output is correct
16 Correct 43 ms 3272 KB Output is correct
17 Correct 40 ms 4048 KB Output is correct
18 Correct 46 ms 3136 KB Output is correct
19 Correct 43 ms 3156 KB Output is correct
20 Correct 41 ms 3588 KB Output is correct
21 Correct 41 ms 3292 KB Output is correct
22 Correct 44 ms 3192 KB Output is correct
23 Correct 43 ms 3184 KB Output is correct
24 Correct 42 ms 3308 KB Output is correct
25 Correct 45 ms 3248 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 20 ms 1316 KB Output is correct
30 Correct 35 ms 2276 KB Output is correct
31 Correct 55 ms 3224 KB Output is correct
32 Correct 50 ms 3116 KB Output is correct
33 Correct 47 ms 3180 KB Output is correct
34 Correct 49 ms 3036 KB Output is correct
35 Correct 52 ms 3080 KB Output is correct
36 Correct 60 ms 3052 KB Output is correct
37 Correct 53 ms 3152 KB Output is correct
38 Correct 47 ms 3404 KB Output is correct
39 Correct 48 ms 3024 KB Output is correct
40 Correct 50 ms 3156 KB Output is correct
41 Correct 1 ms 340 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 468 KB Output is correct
45 Correct 2 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 7 ms 852 KB Output is correct
48 Correct 5 ms 848 KB Output is correct
49 Correct 11 ms 1276 KB Output is correct
50 Correct 23 ms 2080 KB Output is correct
51 Correct 38 ms 2856 KB Output is correct
52 Correct 39 ms 2872 KB Output is correct
53 Correct 40 ms 2868 KB Output is correct
54 Correct 39 ms 2892 KB Output is correct
55 Correct 39 ms 2840 KB Output is correct
56 Correct 42 ms 2916 KB Output is correct
57 Correct 39 ms 2792 KB Output is correct
58 Correct 39 ms 2836 KB Output is correct
59 Correct 38 ms 2796 KB Output is correct
60 Correct 40 ms 2848 KB Output is correct
61 Correct 40 ms 2896 KB Output is correct
62 Correct 39 ms 2876 KB Output is correct
63 Correct 39 ms 2884 KB Output is correct
64 Correct 39 ms 2876 KB Output is correct
65 Correct 39 ms 2816 KB Output is correct
66 Correct 40 ms 2824 KB Output is correct
67 Correct 38 ms 2860 KB Output is correct
68 Correct 40 ms 2912 KB Output is correct
69 Correct 41 ms 2988 KB Output is correct
70 Correct 42 ms 2868 KB Output is correct
71 Correct 5 ms 860 KB Output is correct
72 Correct 5 ms 852 KB Output is correct
73 Correct 5 ms 896 KB Output is correct
74 Correct 6 ms 876 KB Output is correct
75 Correct 5 ms 856 KB Output is correct
76 Correct 10 ms 1216 KB Output is correct
77 Correct 9 ms 1300 KB Output is correct
78 Correct 11 ms 1296 KB Output is correct
79 Correct 9 ms 1252 KB Output is correct
80 Correct 9 ms 1236 KB Output is correct
81 Correct 16 ms 2136 KB Output is correct
82 Correct 20 ms 2040 KB Output is correct
83 Correct 17 ms 2128 KB Output is correct
84 Correct 16 ms 2128 KB Output is correct
85 Correct 17 ms 2132 KB Output is correct
86 Correct 17 ms 2132 KB Output is correct
87 Correct 18 ms 2124 KB Output is correct
88 Correct 18 ms 2136 KB Output is correct
89 Correct 19 ms 2168 KB Output is correct
90 Correct 19 ms 2132 KB Output is correct
91 Correct 28 ms 3680 KB Output is correct
92 Correct 31 ms 3740 KB Output is correct
93 Correct 29 ms 3740 KB Output is correct
94 Correct 28 ms 3616 KB Output is correct
95 Correct 28 ms 3656 KB Output is correct
96 Correct 36 ms 4504 KB Output is correct
97 Correct 35 ms 4464 KB Output is correct
98 Correct 32 ms 4440 KB Output is correct
99 Correct 35 ms 4452 KB Output is correct
100 Correct 32 ms 4428 KB Output is correct
101 Correct 35 ms 4400 KB Output is correct
102 Correct 31 ms 4432 KB Output is correct
103 Correct 32 ms 4524 KB Output is correct
104 Correct 33 ms 4448 KB Output is correct
105 Correct 31 ms 4428 KB Output is correct
106 Correct 34 ms 4880 KB Output is correct
107 Correct 33 ms 4848 KB Output is correct
108 Correct 32 ms 4812 KB Output is correct
109 Correct 33 ms 4896 KB Output is correct
110 Correct 31 ms 4804 KB Output is correct
111 Correct 32 ms 4844 KB Output is correct
112 Correct 33 ms 4796 KB Output is correct
113 Correct 35 ms 4812 KB Output is correct
114 Correct 32 ms 4912 KB Output is correct
115 Correct 32 ms 4812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 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 2 ms 468 KB Output is correct
14 Correct 13 ms 1400 KB Output is correct
15 Correct 28 ms 2568 KB Output is correct
16 Correct 43 ms 3272 KB Output is correct
17 Correct 40 ms 4048 KB Output is correct
18 Correct 46 ms 3136 KB Output is correct
19 Correct 43 ms 3156 KB Output is correct
20 Correct 41 ms 3588 KB Output is correct
21 Correct 41 ms 3292 KB Output is correct
22 Correct 44 ms 3192 KB Output is correct
23 Correct 43 ms 3184 KB Output is correct
24 Correct 42 ms 3308 KB Output is correct
25 Correct 45 ms 3248 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 20 ms 1316 KB Output is correct
30 Correct 35 ms 2276 KB Output is correct
31 Correct 55 ms 3224 KB Output is correct
32 Correct 50 ms 3116 KB Output is correct
33 Correct 47 ms 3180 KB Output is correct
34 Correct 49 ms 3036 KB Output is correct
35 Correct 52 ms 3080 KB Output is correct
36 Correct 60 ms 3052 KB Output is correct
37 Correct 53 ms 3152 KB Output is correct
38 Correct 47 ms 3404 KB Output is correct
39 Correct 48 ms 3024 KB Output is correct
40 Correct 50 ms 3156 KB Output is correct
41 Correct 1 ms 340 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 468 KB Output is correct
45 Correct 2 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 7 ms 852 KB Output is correct
48 Correct 5 ms 848 KB Output is correct
49 Correct 11 ms 1276 KB Output is correct
50 Correct 23 ms 2080 KB Output is correct
51 Correct 38 ms 2856 KB Output is correct
52 Correct 39 ms 2872 KB Output is correct
53 Correct 40 ms 2868 KB Output is correct
54 Correct 39 ms 2892 KB Output is correct
55 Correct 39 ms 2840 KB Output is correct
56 Correct 42 ms 2916 KB Output is correct
57 Correct 39 ms 2792 KB Output is correct
58 Correct 39 ms 2836 KB Output is correct
59 Correct 38 ms 2796 KB Output is correct
60 Correct 40 ms 2848 KB Output is correct
61 Correct 40 ms 2896 KB Output is correct
62 Correct 39 ms 2876 KB Output is correct
63 Correct 39 ms 2884 KB Output is correct
64 Correct 39 ms 2876 KB Output is correct
65 Correct 39 ms 2816 KB Output is correct
66 Correct 40 ms 2824 KB Output is correct
67 Correct 38 ms 2860 KB Output is correct
68 Correct 40 ms 2912 KB Output is correct
69 Correct 41 ms 2988 KB Output is correct
70 Correct 42 ms 2868 KB Output is correct
71 Correct 5 ms 860 KB Output is correct
72 Correct 5 ms 852 KB Output is correct
73 Correct 5 ms 896 KB Output is correct
74 Correct 6 ms 876 KB Output is correct
75 Correct 5 ms 856 KB Output is correct
76 Correct 10 ms 1216 KB Output is correct
77 Correct 9 ms 1300 KB Output is correct
78 Correct 11 ms 1296 KB Output is correct
79 Correct 9 ms 1252 KB Output is correct
80 Correct 9 ms 1236 KB Output is correct
81 Correct 16 ms 2136 KB Output is correct
82 Correct 20 ms 2040 KB Output is correct
83 Correct 17 ms 2128 KB Output is correct
84 Correct 16 ms 2128 KB Output is correct
85 Correct 17 ms 2132 KB Output is correct
86 Correct 17 ms 2132 KB Output is correct
87 Correct 18 ms 2124 KB Output is correct
88 Correct 18 ms 2136 KB Output is correct
89 Correct 19 ms 2168 KB Output is correct
90 Correct 19 ms 2132 KB Output is correct
91 Correct 28 ms 3680 KB Output is correct
92 Correct 31 ms 3740 KB Output is correct
93 Correct 29 ms 3740 KB Output is correct
94 Correct 28 ms 3616 KB Output is correct
95 Correct 28 ms 3656 KB Output is correct
96 Correct 36 ms 4504 KB Output is correct
97 Correct 35 ms 4464 KB Output is correct
98 Correct 32 ms 4440 KB Output is correct
99 Correct 35 ms 4452 KB Output is correct
100 Correct 32 ms 4428 KB Output is correct
101 Correct 35 ms 4400 KB Output is correct
102 Correct 31 ms 4432 KB Output is correct
103 Correct 32 ms 4524 KB Output is correct
104 Correct 33 ms 4448 KB Output is correct
105 Correct 31 ms 4428 KB Output is correct
106 Correct 34 ms 4880 KB Output is correct
107 Correct 33 ms 4848 KB Output is correct
108 Correct 32 ms 4812 KB Output is correct
109 Correct 33 ms 4896 KB Output is correct
110 Correct 31 ms 4804 KB Output is correct
111 Correct 32 ms 4844 KB Output is correct
112 Correct 33 ms 4796 KB Output is correct
113 Correct 35 ms 4812 KB Output is correct
114 Correct 32 ms 4912 KB Output is correct
115 Correct 32 ms 4812 KB Output is correct
116 Correct 886 ms 2880 KB Output is correct
117 Correct 682 ms 2748 KB Output is correct
118 Correct 1307 ms 3136 KB Output is correct
119 Correct 617 ms 3200 KB Output is correct
120 Correct 379 ms 3260 KB Output is correct
121 Correct 791 ms 3604 KB Output is correct
122 Correct 490 ms 3776 KB Output is correct
123 Correct 2440 ms 4496 KB Output is correct
124 Correct 795 ms 4544 KB Output is correct
125 Correct 632 ms 4668 KB Output is correct
126 Execution timed out 7075 ms 4928 KB Time limit exceeded
127 Halted 0 ms 0 KB -