Submission #549579

# Submission time Handle Problem Language Result Execution time Memory
549579 2022-04-16T04:46:52 Z minhcool Diversity (CEOI21_diversity) C++17
64 / 100
6611 ms 4704 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(cnt[a[pos]] >= S && !cntt[cnt[a[pos]]]) se.erase(cnt[a[pos]]);
    cnt[a[pos]]++;
    //cout << a[pos] << " " << cnt[a[pos]] << "\n";
    cntt[cnt[a[pos]]]++;
    if(cnt[a[pos]] >= S && cntt[cnt[a[pos]]] == 1) se.insert(cnt[a[pos]]);
}
 
void er(int pos){
    cntt[cnt[a[pos]]]--;
    if(cnt[a[pos]] >= S && !cntt[cnt[a[pos]]]) se.erase(cnt[a[pos]]);
    cnt[a[pos]]--;
    //cout << a[pos] << " " << cnt[a[pos]] << "\n";
    cntt[cnt[a[pos]]]++;
    if(cnt[a[pos]] >= S && 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(int it = 1; it < S; it++){
            if(!cntt[it]) continue;
            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(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 0 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 18 ms 1328 KB Output is correct
5 Correct 31 ms 2420 KB Output is correct
6 Correct 41 ms 3016 KB Output is correct
7 Correct 37 ms 3964 KB Output is correct
8 Correct 42 ms 3056 KB Output is correct
9 Correct 43 ms 3072 KB Output is correct
10 Correct 39 ms 3428 KB Output is correct
11 Correct 45 ms 3200 KB Output is correct
12 Correct 44 ms 3020 KB Output is correct
13 Correct 41 ms 2956 KB Output is correct
14 Correct 41 ms 3236 KB Output is correct
15 Correct 41 ms 3200 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 18 ms 1328 KB Output is correct
5 Correct 31 ms 2420 KB Output is correct
6 Correct 41 ms 3016 KB Output is correct
7 Correct 37 ms 3964 KB Output is correct
8 Correct 42 ms 3056 KB Output is correct
9 Correct 43 ms 3072 KB Output is correct
10 Correct 39 ms 3428 KB Output is correct
11 Correct 45 ms 3200 KB Output is correct
12 Correct 44 ms 3020 KB Output is correct
13 Correct 41 ms 2956 KB Output is correct
14 Correct 41 ms 3236 KB Output is correct
15 Correct 41 ms 3200 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 21 ms 1196 KB Output is correct
20 Correct 36 ms 2136 KB Output is correct
21 Correct 49 ms 3036 KB Output is correct
22 Correct 46 ms 2992 KB Output is correct
23 Correct 47 ms 3020 KB Output is correct
24 Correct 47 ms 2852 KB Output is correct
25 Correct 49 ms 3020 KB Output is correct
26 Correct 47 ms 2892 KB Output is correct
27 Correct 45 ms 3004 KB Output is correct
28 Correct 48 ms 3284 KB Output is correct
29 Correct 61 ms 3008 KB Output is correct
30 Correct 46 ms 2996 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 18 ms 1328 KB Output is correct
5 Correct 31 ms 2420 KB Output is correct
6 Correct 41 ms 3016 KB Output is correct
7 Correct 37 ms 3964 KB Output is correct
8 Correct 42 ms 3056 KB Output is correct
9 Correct 43 ms 3072 KB Output is correct
10 Correct 39 ms 3428 KB Output is correct
11 Correct 45 ms 3200 KB Output is correct
12 Correct 44 ms 3020 KB Output is correct
13 Correct 41 ms 2956 KB Output is correct
14 Correct 41 ms 3236 KB Output is correct
15 Correct 41 ms 3200 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 21 ms 1196 KB Output is correct
20 Correct 36 ms 2136 KB Output is correct
21 Correct 49 ms 3036 KB Output is correct
22 Correct 46 ms 2992 KB Output is correct
23 Correct 47 ms 3020 KB Output is correct
24 Correct 47 ms 2852 KB Output is correct
25 Correct 49 ms 3020 KB Output is correct
26 Correct 47 ms 2892 KB Output is correct
27 Correct 45 ms 3004 KB Output is correct
28 Correct 48 ms 3284 KB Output is correct
29 Correct 61 ms 3008 KB Output is correct
30 Correct 46 ms 2996 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 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 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 5 ms 724 KB Output is correct
38 Correct 4 ms 724 KB Output is correct
39 Correct 8 ms 1140 KB Output is correct
40 Correct 17 ms 1876 KB Output is correct
41 Correct 32 ms 2772 KB Output is correct
42 Correct 34 ms 2716 KB Output is correct
43 Correct 45 ms 2652 KB Output is correct
44 Correct 28 ms 2688 KB Output is correct
45 Correct 28 ms 2716 KB Output is correct
46 Correct 32 ms 2644 KB Output is correct
47 Correct 28 ms 2712 KB Output is correct
48 Correct 29 ms 2660 KB Output is correct
49 Correct 29 ms 2660 KB Output is correct
50 Correct 33 ms 2728 KB Output is correct
51 Correct 29 ms 2672 KB Output is correct
52 Correct 29 ms 2764 KB Output is correct
53 Correct 42 ms 2728 KB Output is correct
54 Correct 32 ms 2752 KB Output is correct
55 Correct 28 ms 2708 KB Output is correct
56 Correct 29 ms 2644 KB Output is correct
57 Correct 30 ms 2644 KB Output is correct
58 Correct 28 ms 2716 KB Output is correct
59 Correct 29 ms 2728 KB Output is correct
60 Correct 31 ms 2696 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 0 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 18 ms 1328 KB Output is correct
15 Correct 31 ms 2420 KB Output is correct
16 Correct 41 ms 3016 KB Output is correct
17 Correct 37 ms 3964 KB Output is correct
18 Correct 42 ms 3056 KB Output is correct
19 Correct 43 ms 3072 KB Output is correct
20 Correct 39 ms 3428 KB Output is correct
21 Correct 45 ms 3200 KB Output is correct
22 Correct 44 ms 3020 KB Output is correct
23 Correct 41 ms 2956 KB Output is correct
24 Correct 41 ms 3236 KB Output is correct
25 Correct 41 ms 3200 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 21 ms 1196 KB Output is correct
30 Correct 36 ms 2136 KB Output is correct
31 Correct 49 ms 3036 KB Output is correct
32 Correct 46 ms 2992 KB Output is correct
33 Correct 47 ms 3020 KB Output is correct
34 Correct 47 ms 2852 KB Output is correct
35 Correct 49 ms 3020 KB Output is correct
36 Correct 47 ms 2892 KB Output is correct
37 Correct 45 ms 3004 KB Output is correct
38 Correct 48 ms 3284 KB Output is correct
39 Correct 61 ms 3008 KB Output is correct
40 Correct 46 ms 2996 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 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 1 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 5 ms 724 KB Output is correct
48 Correct 4 ms 724 KB Output is correct
49 Correct 8 ms 1140 KB Output is correct
50 Correct 17 ms 1876 KB Output is correct
51 Correct 32 ms 2772 KB Output is correct
52 Correct 34 ms 2716 KB Output is correct
53 Correct 45 ms 2652 KB Output is correct
54 Correct 28 ms 2688 KB Output is correct
55 Correct 28 ms 2716 KB Output is correct
56 Correct 32 ms 2644 KB Output is correct
57 Correct 28 ms 2712 KB Output is correct
58 Correct 29 ms 2660 KB Output is correct
59 Correct 29 ms 2660 KB Output is correct
60 Correct 33 ms 2728 KB Output is correct
61 Correct 29 ms 2672 KB Output is correct
62 Correct 29 ms 2764 KB Output is correct
63 Correct 42 ms 2728 KB Output is correct
64 Correct 32 ms 2752 KB Output is correct
65 Correct 28 ms 2708 KB Output is correct
66 Correct 29 ms 2644 KB Output is correct
67 Correct 30 ms 2644 KB Output is correct
68 Correct 28 ms 2716 KB Output is correct
69 Correct 29 ms 2728 KB Output is correct
70 Correct 31 ms 2696 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 6 ms 724 KB Output is correct
75 Correct 6 ms 660 KB Output is correct
76 Correct 8 ms 1108 KB Output is correct
77 Correct 8 ms 1108 KB Output is correct
78 Correct 8 ms 1136 KB Output is correct
79 Correct 10 ms 1160 KB Output is correct
80 Correct 8 ms 1108 KB Output is correct
81 Correct 15 ms 2008 KB Output is correct
82 Correct 16 ms 1928 KB Output is correct
83 Correct 16 ms 2004 KB Output is correct
84 Correct 20 ms 1908 KB Output is correct
85 Correct 18 ms 2012 KB Output is correct
86 Correct 24 ms 1968 KB Output is correct
87 Correct 20 ms 2060 KB Output is correct
88 Correct 19 ms 2004 KB Output is correct
89 Correct 19 ms 1996 KB Output is correct
90 Correct 18 ms 2072 KB Output is correct
91 Correct 28 ms 3356 KB Output is correct
92 Correct 28 ms 3412 KB Output is correct
93 Correct 30 ms 3484 KB Output is correct
94 Correct 29 ms 3412 KB Output is correct
95 Correct 31 ms 3468 KB Output is correct
96 Correct 33 ms 4236 KB Output is correct
97 Correct 32 ms 4256 KB Output is correct
98 Correct 31 ms 4192 KB Output is correct
99 Correct 31 ms 4180 KB Output is correct
100 Correct 31 ms 4260 KB Output is correct
101 Correct 31 ms 4232 KB Output is correct
102 Correct 33 ms 4196 KB Output is correct
103 Correct 31 ms 4220 KB Output is correct
104 Correct 33 ms 4124 KB Output is correct
105 Correct 30 ms 4148 KB Output is correct
106 Correct 30 ms 4540 KB Output is correct
107 Correct 31 ms 4636 KB Output is correct
108 Correct 33 ms 4564 KB Output is correct
109 Correct 30 ms 4556 KB Output is correct
110 Correct 32 ms 4584 KB Output is correct
111 Correct 32 ms 4584 KB Output is correct
112 Correct 32 ms 4616 KB Output is correct
113 Correct 33 ms 4588 KB Output is correct
114 Correct 32 ms 4556 KB Output is correct
115 Correct 31 ms 4604 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 0 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 18 ms 1328 KB Output is correct
15 Correct 31 ms 2420 KB Output is correct
16 Correct 41 ms 3016 KB Output is correct
17 Correct 37 ms 3964 KB Output is correct
18 Correct 42 ms 3056 KB Output is correct
19 Correct 43 ms 3072 KB Output is correct
20 Correct 39 ms 3428 KB Output is correct
21 Correct 45 ms 3200 KB Output is correct
22 Correct 44 ms 3020 KB Output is correct
23 Correct 41 ms 2956 KB Output is correct
24 Correct 41 ms 3236 KB Output is correct
25 Correct 41 ms 3200 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 21 ms 1196 KB Output is correct
30 Correct 36 ms 2136 KB Output is correct
31 Correct 49 ms 3036 KB Output is correct
32 Correct 46 ms 2992 KB Output is correct
33 Correct 47 ms 3020 KB Output is correct
34 Correct 47 ms 2852 KB Output is correct
35 Correct 49 ms 3020 KB Output is correct
36 Correct 47 ms 2892 KB Output is correct
37 Correct 45 ms 3004 KB Output is correct
38 Correct 48 ms 3284 KB Output is correct
39 Correct 61 ms 3008 KB Output is correct
40 Correct 46 ms 2996 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 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 1 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 5 ms 724 KB Output is correct
48 Correct 4 ms 724 KB Output is correct
49 Correct 8 ms 1140 KB Output is correct
50 Correct 17 ms 1876 KB Output is correct
51 Correct 32 ms 2772 KB Output is correct
52 Correct 34 ms 2716 KB Output is correct
53 Correct 45 ms 2652 KB Output is correct
54 Correct 28 ms 2688 KB Output is correct
55 Correct 28 ms 2716 KB Output is correct
56 Correct 32 ms 2644 KB Output is correct
57 Correct 28 ms 2712 KB Output is correct
58 Correct 29 ms 2660 KB Output is correct
59 Correct 29 ms 2660 KB Output is correct
60 Correct 33 ms 2728 KB Output is correct
61 Correct 29 ms 2672 KB Output is correct
62 Correct 29 ms 2764 KB Output is correct
63 Correct 42 ms 2728 KB Output is correct
64 Correct 32 ms 2752 KB Output is correct
65 Correct 28 ms 2708 KB Output is correct
66 Correct 29 ms 2644 KB Output is correct
67 Correct 30 ms 2644 KB Output is correct
68 Correct 28 ms 2716 KB Output is correct
69 Correct 29 ms 2728 KB Output is correct
70 Correct 31 ms 2696 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 6 ms 724 KB Output is correct
75 Correct 6 ms 660 KB Output is correct
76 Correct 8 ms 1108 KB Output is correct
77 Correct 8 ms 1108 KB Output is correct
78 Correct 8 ms 1136 KB Output is correct
79 Correct 10 ms 1160 KB Output is correct
80 Correct 8 ms 1108 KB Output is correct
81 Correct 15 ms 2008 KB Output is correct
82 Correct 16 ms 1928 KB Output is correct
83 Correct 16 ms 2004 KB Output is correct
84 Correct 20 ms 1908 KB Output is correct
85 Correct 18 ms 2012 KB Output is correct
86 Correct 24 ms 1968 KB Output is correct
87 Correct 20 ms 2060 KB Output is correct
88 Correct 19 ms 2004 KB Output is correct
89 Correct 19 ms 1996 KB Output is correct
90 Correct 18 ms 2072 KB Output is correct
91 Correct 28 ms 3356 KB Output is correct
92 Correct 28 ms 3412 KB Output is correct
93 Correct 30 ms 3484 KB Output is correct
94 Correct 29 ms 3412 KB Output is correct
95 Correct 31 ms 3468 KB Output is correct
96 Correct 33 ms 4236 KB Output is correct
97 Correct 32 ms 4256 KB Output is correct
98 Correct 31 ms 4192 KB Output is correct
99 Correct 31 ms 4180 KB Output is correct
100 Correct 31 ms 4260 KB Output is correct
101 Correct 31 ms 4232 KB Output is correct
102 Correct 33 ms 4196 KB Output is correct
103 Correct 31 ms 4220 KB Output is correct
104 Correct 33 ms 4124 KB Output is correct
105 Correct 30 ms 4148 KB Output is correct
106 Correct 30 ms 4540 KB Output is correct
107 Correct 31 ms 4636 KB Output is correct
108 Correct 33 ms 4564 KB Output is correct
109 Correct 30 ms 4556 KB Output is correct
110 Correct 32 ms 4584 KB Output is correct
111 Correct 32 ms 4584 KB Output is correct
112 Correct 32 ms 4616 KB Output is correct
113 Correct 33 ms 4588 KB Output is correct
114 Correct 32 ms 4556 KB Output is correct
115 Correct 31 ms 4604 KB Output is correct
116 Correct 214 ms 2528 KB Output is correct
117 Correct 240 ms 2512 KB Output is correct
118 Correct 504 ms 3068 KB Output is correct
119 Correct 336 ms 3012 KB Output is correct
120 Correct 270 ms 3060 KB Output is correct
121 Correct 459 ms 3360 KB Output is correct
122 Correct 362 ms 3428 KB Output is correct
123 Correct 1145 ms 4268 KB Output is correct
124 Correct 665 ms 4412 KB Output is correct
125 Correct 629 ms 4368 KB Output is correct
126 Incorrect 6611 ms 4704 KB Output isn't correct
127 Halted 0 ms 0 KB -