Submission #549575

# Submission time Handle Problem Language Result Execution time Memory
549575 2022-04-16T04:30:04 Z minhcool Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 6184 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;
    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--;
        }
        //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;
    }
    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 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 14 ms 1288 KB Output is correct
5 Correct 29 ms 2456 KB Output is correct
6 Correct 42 ms 3012 KB Output is correct
7 Correct 37 ms 3940 KB Output is correct
8 Correct 47 ms 3036 KB Output is correct
9 Correct 44 ms 3076 KB Output is correct
10 Correct 43 ms 3348 KB Output is correct
11 Correct 43 ms 3148 KB Output is correct
12 Correct 45 ms 3072 KB Output is correct
13 Correct 44 ms 3044 KB Output is correct
14 Correct 42 ms 3228 KB Output is correct
15 Correct 43 ms 3112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 14 ms 1288 KB Output is correct
5 Correct 29 ms 2456 KB Output is correct
6 Correct 42 ms 3012 KB Output is correct
7 Correct 37 ms 3940 KB Output is correct
8 Correct 47 ms 3036 KB Output is correct
9 Correct 44 ms 3076 KB Output is correct
10 Correct 43 ms 3348 KB Output is correct
11 Correct 43 ms 3148 KB Output is correct
12 Correct 45 ms 3072 KB Output is correct
13 Correct 44 ms 3044 KB Output is correct
14 Correct 42 ms 3228 KB Output is correct
15 Correct 43 ms 3112 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 17 ms 1252 KB Output is correct
20 Correct 33 ms 2124 KB Output is correct
21 Correct 47 ms 3036 KB Output is correct
22 Correct 48 ms 2976 KB Output is correct
23 Correct 49 ms 2992 KB Output is correct
24 Correct 48 ms 2892 KB Output is correct
25 Correct 48 ms 3020 KB Output is correct
26 Correct 52 ms 2908 KB Output is correct
27 Correct 47 ms 3020 KB Output is correct
28 Correct 51 ms 3188 KB Output is correct
29 Correct 50 ms 2984 KB Output is correct
30 Correct 48 ms 2912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 14 ms 1288 KB Output is correct
5 Correct 29 ms 2456 KB Output is correct
6 Correct 42 ms 3012 KB Output is correct
7 Correct 37 ms 3940 KB Output is correct
8 Correct 47 ms 3036 KB Output is correct
9 Correct 44 ms 3076 KB Output is correct
10 Correct 43 ms 3348 KB Output is correct
11 Correct 43 ms 3148 KB Output is correct
12 Correct 45 ms 3072 KB Output is correct
13 Correct 44 ms 3044 KB Output is correct
14 Correct 42 ms 3228 KB Output is correct
15 Correct 43 ms 3112 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 17 ms 1252 KB Output is correct
20 Correct 33 ms 2124 KB Output is correct
21 Correct 47 ms 3036 KB Output is correct
22 Correct 48 ms 2976 KB Output is correct
23 Correct 49 ms 2992 KB Output is correct
24 Correct 48 ms 2892 KB Output is correct
25 Correct 48 ms 3020 KB Output is correct
26 Correct 52 ms 2908 KB Output is correct
27 Correct 47 ms 3020 KB Output is correct
28 Correct 51 ms 3188 KB Output is correct
29 Correct 50 ms 2984 KB Output is correct
30 Correct 48 ms 2912 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 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 1 ms 340 KB Output is correct
37 Correct 7 ms 724 KB Output is correct
38 Correct 5 ms 724 KB Output is correct
39 Correct 10 ms 1148 KB Output is correct
40 Correct 23 ms 1876 KB Output is correct
41 Correct 39 ms 2728 KB Output is correct
42 Correct 39 ms 2732 KB Output is correct
43 Correct 42 ms 2720 KB Output is correct
44 Correct 41 ms 2780 KB Output is correct
45 Correct 39 ms 2728 KB Output is correct
46 Correct 38 ms 2724 KB Output is correct
47 Correct 39 ms 2732 KB Output is correct
48 Correct 39 ms 2788 KB Output is correct
49 Correct 40 ms 2720 KB Output is correct
50 Correct 39 ms 2736 KB Output is correct
51 Correct 39 ms 2720 KB Output is correct
52 Correct 42 ms 2724 KB Output is correct
53 Correct 41 ms 2768 KB Output is correct
54 Correct 40 ms 2716 KB Output is correct
55 Correct 38 ms 2732 KB Output is correct
56 Correct 37 ms 2736 KB Output is correct
57 Correct 40 ms 2764 KB Output is correct
58 Correct 38 ms 2712 KB Output is correct
59 Correct 38 ms 2700 KB Output is correct
60 Correct 39 ms 2764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 14 ms 1288 KB Output is correct
15 Correct 29 ms 2456 KB Output is correct
16 Correct 42 ms 3012 KB Output is correct
17 Correct 37 ms 3940 KB Output is correct
18 Correct 47 ms 3036 KB Output is correct
19 Correct 44 ms 3076 KB Output is correct
20 Correct 43 ms 3348 KB Output is correct
21 Correct 43 ms 3148 KB Output is correct
22 Correct 45 ms 3072 KB Output is correct
23 Correct 44 ms 3044 KB Output is correct
24 Correct 42 ms 3228 KB Output is correct
25 Correct 43 ms 3112 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 17 ms 1252 KB Output is correct
30 Correct 33 ms 2124 KB Output is correct
31 Correct 47 ms 3036 KB Output is correct
32 Correct 48 ms 2976 KB Output is correct
33 Correct 49 ms 2992 KB Output is correct
34 Correct 48 ms 2892 KB Output is correct
35 Correct 48 ms 3020 KB Output is correct
36 Correct 52 ms 2908 KB Output is correct
37 Correct 47 ms 3020 KB Output is correct
38 Correct 51 ms 3188 KB Output is correct
39 Correct 50 ms 2984 KB Output is correct
40 Correct 48 ms 2912 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 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 1 ms 340 KB Output is correct
47 Correct 7 ms 724 KB Output is correct
48 Correct 5 ms 724 KB Output is correct
49 Correct 10 ms 1148 KB Output is correct
50 Correct 23 ms 1876 KB Output is correct
51 Correct 39 ms 2728 KB Output is correct
52 Correct 39 ms 2732 KB Output is correct
53 Correct 42 ms 2720 KB Output is correct
54 Correct 41 ms 2780 KB Output is correct
55 Correct 39 ms 2728 KB Output is correct
56 Correct 38 ms 2724 KB Output is correct
57 Correct 39 ms 2732 KB Output is correct
58 Correct 39 ms 2788 KB Output is correct
59 Correct 40 ms 2720 KB Output is correct
60 Correct 39 ms 2736 KB Output is correct
61 Correct 39 ms 2720 KB Output is correct
62 Correct 42 ms 2724 KB Output is correct
63 Correct 41 ms 2768 KB Output is correct
64 Correct 40 ms 2716 KB Output is correct
65 Correct 38 ms 2732 KB Output is correct
66 Correct 37 ms 2736 KB Output is correct
67 Correct 40 ms 2764 KB Output is correct
68 Correct 38 ms 2712 KB Output is correct
69 Correct 38 ms 2700 KB Output is correct
70 Correct 39 ms 2764 KB Output is correct
71 Correct 5 ms 724 KB Output is correct
72 Correct 5 ms 724 KB Output is correct
73 Correct 5 ms 724 KB Output is correct
74 Correct 5 ms 724 KB Output is correct
75 Correct 5 ms 724 KB Output is correct
76 Correct 10 ms 1108 KB Output is correct
77 Correct 8 ms 1108 KB Output is correct
78 Correct 9 ms 1104 KB Output is correct
79 Correct 8 ms 1108 KB Output is correct
80 Correct 9 ms 1180 KB Output is correct
81 Correct 19 ms 2004 KB Output is correct
82 Correct 17 ms 2004 KB Output is correct
83 Correct 18 ms 2132 KB Output is correct
84 Correct 17 ms 2004 KB Output is correct
85 Correct 17 ms 2004 KB Output is correct
86 Correct 17 ms 1984 KB Output is correct
87 Correct 18 ms 2020 KB Output is correct
88 Correct 18 ms 2004 KB Output is correct
89 Correct 22 ms 1976 KB Output is correct
90 Correct 18 ms 1992 KB Output is correct
91 Correct 28 ms 3412 KB Output is correct
92 Correct 27 ms 3432 KB Output is correct
93 Correct 29 ms 3440 KB Output is correct
94 Correct 26 ms 3372 KB Output is correct
95 Correct 29 ms 3480 KB Output is correct
96 Correct 30 ms 4140 KB Output is correct
97 Correct 31 ms 4180 KB Output is correct
98 Correct 32 ms 4240 KB Output is correct
99 Correct 31 ms 4248 KB Output is correct
100 Correct 30 ms 4212 KB Output is correct
101 Correct 30 ms 4272 KB Output is correct
102 Correct 32 ms 4260 KB Output is correct
103 Correct 32 ms 4220 KB Output is correct
104 Correct 32 ms 4172 KB Output is correct
105 Correct 33 ms 4180 KB Output is correct
106 Correct 32 ms 4636 KB Output is correct
107 Correct 30 ms 4564 KB Output is correct
108 Correct 30 ms 4556 KB Output is correct
109 Correct 32 ms 4632 KB Output is correct
110 Correct 33 ms 4636 KB Output is correct
111 Correct 31 ms 4564 KB Output is correct
112 Correct 31 ms 4564 KB Output is correct
113 Correct 31 ms 4632 KB Output is correct
114 Correct 30 ms 4556 KB Output is correct
115 Correct 42 ms 4516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 14 ms 1288 KB Output is correct
15 Correct 29 ms 2456 KB Output is correct
16 Correct 42 ms 3012 KB Output is correct
17 Correct 37 ms 3940 KB Output is correct
18 Correct 47 ms 3036 KB Output is correct
19 Correct 44 ms 3076 KB Output is correct
20 Correct 43 ms 3348 KB Output is correct
21 Correct 43 ms 3148 KB Output is correct
22 Correct 45 ms 3072 KB Output is correct
23 Correct 44 ms 3044 KB Output is correct
24 Correct 42 ms 3228 KB Output is correct
25 Correct 43 ms 3112 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 17 ms 1252 KB Output is correct
30 Correct 33 ms 2124 KB Output is correct
31 Correct 47 ms 3036 KB Output is correct
32 Correct 48 ms 2976 KB Output is correct
33 Correct 49 ms 2992 KB Output is correct
34 Correct 48 ms 2892 KB Output is correct
35 Correct 48 ms 3020 KB Output is correct
36 Correct 52 ms 2908 KB Output is correct
37 Correct 47 ms 3020 KB Output is correct
38 Correct 51 ms 3188 KB Output is correct
39 Correct 50 ms 2984 KB Output is correct
40 Correct 48 ms 2912 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 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 1 ms 340 KB Output is correct
47 Correct 7 ms 724 KB Output is correct
48 Correct 5 ms 724 KB Output is correct
49 Correct 10 ms 1148 KB Output is correct
50 Correct 23 ms 1876 KB Output is correct
51 Correct 39 ms 2728 KB Output is correct
52 Correct 39 ms 2732 KB Output is correct
53 Correct 42 ms 2720 KB Output is correct
54 Correct 41 ms 2780 KB Output is correct
55 Correct 39 ms 2728 KB Output is correct
56 Correct 38 ms 2724 KB Output is correct
57 Correct 39 ms 2732 KB Output is correct
58 Correct 39 ms 2788 KB Output is correct
59 Correct 40 ms 2720 KB Output is correct
60 Correct 39 ms 2736 KB Output is correct
61 Correct 39 ms 2720 KB Output is correct
62 Correct 42 ms 2724 KB Output is correct
63 Correct 41 ms 2768 KB Output is correct
64 Correct 40 ms 2716 KB Output is correct
65 Correct 38 ms 2732 KB Output is correct
66 Correct 37 ms 2736 KB Output is correct
67 Correct 40 ms 2764 KB Output is correct
68 Correct 38 ms 2712 KB Output is correct
69 Correct 38 ms 2700 KB Output is correct
70 Correct 39 ms 2764 KB Output is correct
71 Correct 5 ms 724 KB Output is correct
72 Correct 5 ms 724 KB Output is correct
73 Correct 5 ms 724 KB Output is correct
74 Correct 5 ms 724 KB Output is correct
75 Correct 5 ms 724 KB Output is correct
76 Correct 10 ms 1108 KB Output is correct
77 Correct 8 ms 1108 KB Output is correct
78 Correct 9 ms 1104 KB Output is correct
79 Correct 8 ms 1108 KB Output is correct
80 Correct 9 ms 1180 KB Output is correct
81 Correct 19 ms 2004 KB Output is correct
82 Correct 17 ms 2004 KB Output is correct
83 Correct 18 ms 2132 KB Output is correct
84 Correct 17 ms 2004 KB Output is correct
85 Correct 17 ms 2004 KB Output is correct
86 Correct 17 ms 1984 KB Output is correct
87 Correct 18 ms 2020 KB Output is correct
88 Correct 18 ms 2004 KB Output is correct
89 Correct 22 ms 1976 KB Output is correct
90 Correct 18 ms 1992 KB Output is correct
91 Correct 28 ms 3412 KB Output is correct
92 Correct 27 ms 3432 KB Output is correct
93 Correct 29 ms 3440 KB Output is correct
94 Correct 26 ms 3372 KB Output is correct
95 Correct 29 ms 3480 KB Output is correct
96 Correct 30 ms 4140 KB Output is correct
97 Correct 31 ms 4180 KB Output is correct
98 Correct 32 ms 4240 KB Output is correct
99 Correct 31 ms 4248 KB Output is correct
100 Correct 30 ms 4212 KB Output is correct
101 Correct 30 ms 4272 KB Output is correct
102 Correct 32 ms 4260 KB Output is correct
103 Correct 32 ms 4220 KB Output is correct
104 Correct 32 ms 4172 KB Output is correct
105 Correct 33 ms 4180 KB Output is correct
106 Correct 32 ms 4636 KB Output is correct
107 Correct 30 ms 4564 KB Output is correct
108 Correct 30 ms 4556 KB Output is correct
109 Correct 32 ms 4632 KB Output is correct
110 Correct 33 ms 4636 KB Output is correct
111 Correct 31 ms 4564 KB Output is correct
112 Correct 31 ms 4564 KB Output is correct
113 Correct 31 ms 4632 KB Output is correct
114 Correct 30 ms 4556 KB Output is correct
115 Correct 42 ms 4516 KB Output is correct
116 Correct 888 ms 2928 KB Output is correct
117 Correct 675 ms 3036 KB Output is correct
118 Correct 1315 ms 3676 KB Output is correct
119 Correct 596 ms 3704 KB Output is correct
120 Correct 356 ms 3772 KB Output is correct
121 Correct 775 ms 4316 KB Output is correct
122 Correct 473 ms 4416 KB Output is correct
123 Correct 2451 ms 5544 KB Output is correct
124 Correct 778 ms 5776 KB Output is correct
125 Correct 622 ms 6012 KB Output is correct
126 Execution timed out 7003 ms 6184 KB Time limit exceeded
127 Halted 0 ms 0 KB -