Submission #650654

# Submission time Handle Problem Language Result Execution time Memory
650654 2022-10-14T13:08:17 Z 1bin Diversity (CEOI21_diversity) C++14
100 / 100
1829 ms 7836 KB
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 3e5 + 5;
const int b = 550;
ll n, Q, a[NMAX], ans[NMAX], l, r, c[NMAX], cnt[NMAX], ls, rs, x, y, res, in[NMAX];
vector<int> mp;
struct query{
    int i, l, r;
    bool operator <(const query& x){
        if(l / b == x.l / b) return (l / b & 1) ? r > x.r : r < x.r;
        else return l / b < x.l / b;
    }
} q[NMAX];

void f(int x, int v){
    cnt[c[x]]--;
    c[x] += v;
    if(!cnt[c[x]]++ && !in[c[x]]) mp.emplace_back(c[x]), in[c[x]] = 1;
    return;
}

ll cal(ll l, ll x, ll c){ return x * (x + 1) * (2 * x + 1) / 6 * c * c + c * (l - c) * x * (x + 1) + (l - c) * (l - c) * x;}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> n >> Q;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 0; i < Q; i++) cin >> q[i].l >> q[i].r, q[i].i = i;
    sort(q, q + Q);
    l = 1, r = 0;
    for(int i = 0; i < Q; i++){
        while(l > q[i].l) f(a[--l], 1);
        while(r < q[i].r) f(a[++r], 1);
        while(l < q[i].l) f(a[l++], -1);
        while(r > q[i].r) f(a[r--], -1);
        for(int i = 0; i < mp.size(); i++)
            if(!cnt[mp[i]]) {
                in[mp[i]] = 0;
                swap(mp[i], mp.back());
                mp.pop_back(); i--;
            }
        sort(all(mp));
        ls = rs = res = 0;
        for(auto& c : mp){
            if(!c) continue;
            ll n = r - l + 1, t = cnt[c];
            x = (t + 1) / 2; y = t - x;
            res += (n * n + c) * t;
            res -= cal(ls, x, c) + cal(n - ls - c - (x - 1) * c, x, c);
            res -= cal(rs, y, c) + cal(n - rs - c - (y - 1) * c, y, c);
            ls += x * c; rs += y * c;
            if(x > y) swap(ls, rs);
        }
        ans[q[i].i] = res / 2;
    }
    
    for(int i = 0; i < Q; i++) cout << ans[i] << '\n';
    return 0;
}

Compilation message

diversity.cpp: In function 'int main()':
diversity.cpp:42:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |         for(int i = 0; i < mp.size(); i++)
      |                        ~~^~~~~~~~~~~
# 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 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 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 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 1 ms 468 KB Output is correct
4 Correct 6 ms 1704 KB Output is correct
5 Correct 13 ms 3544 KB Output is correct
6 Correct 20 ms 3960 KB Output is correct
7 Correct 22 ms 5980 KB Output is correct
8 Correct 19 ms 3800 KB Output is correct
9 Correct 23 ms 3800 KB Output is correct
10 Correct 21 ms 4788 KB Output is correct
11 Correct 20 ms 4368 KB Output is correct
12 Correct 21 ms 3896 KB Output is correct
13 Correct 19 ms 3784 KB Output is correct
14 Correct 18 ms 4312 KB Output is correct
15 Correct 19 ms 4324 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 1 ms 468 KB Output is correct
4 Correct 6 ms 1704 KB Output is correct
5 Correct 13 ms 3544 KB Output is correct
6 Correct 20 ms 3960 KB Output is correct
7 Correct 22 ms 5980 KB Output is correct
8 Correct 19 ms 3800 KB Output is correct
9 Correct 23 ms 3800 KB Output is correct
10 Correct 21 ms 4788 KB Output is correct
11 Correct 20 ms 4368 KB Output is correct
12 Correct 21 ms 3896 KB Output is correct
13 Correct 19 ms 3784 KB Output is correct
14 Correct 18 ms 4312 KB Output is correct
15 Correct 19 ms 4324 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 7 ms 1620 KB Output is correct
20 Correct 20 ms 2632 KB Output is correct
21 Correct 22 ms 3880 KB Output is correct
22 Correct 21 ms 3560 KB Output is correct
23 Correct 21 ms 3792 KB Output is correct
24 Correct 20 ms 3532 KB Output is correct
25 Correct 21 ms 3644 KB Output is correct
26 Correct 19 ms 3564 KB Output is correct
27 Correct 19 ms 3680 KB Output is correct
28 Correct 20 ms 4308 KB Output is correct
29 Correct 20 ms 3568 KB Output is correct
30 Correct 19 ms 3664 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 1 ms 468 KB Output is correct
4 Correct 6 ms 1704 KB Output is correct
5 Correct 13 ms 3544 KB Output is correct
6 Correct 20 ms 3960 KB Output is correct
7 Correct 22 ms 5980 KB Output is correct
8 Correct 19 ms 3800 KB Output is correct
9 Correct 23 ms 3800 KB Output is correct
10 Correct 21 ms 4788 KB Output is correct
11 Correct 20 ms 4368 KB Output is correct
12 Correct 21 ms 3896 KB Output is correct
13 Correct 19 ms 3784 KB Output is correct
14 Correct 18 ms 4312 KB Output is correct
15 Correct 19 ms 4324 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 7 ms 1620 KB Output is correct
20 Correct 20 ms 2632 KB Output is correct
21 Correct 22 ms 3880 KB Output is correct
22 Correct 21 ms 3560 KB Output is correct
23 Correct 21 ms 3792 KB Output is correct
24 Correct 20 ms 3532 KB Output is correct
25 Correct 21 ms 3644 KB Output is correct
26 Correct 19 ms 3564 KB Output is correct
27 Correct 19 ms 3680 KB Output is correct
28 Correct 20 ms 4308 KB Output is correct
29 Correct 20 ms 3568 KB Output is correct
30 Correct 19 ms 3664 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 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 4 ms 724 KB Output is correct
38 Correct 5 ms 724 KB Output is correct
39 Correct 8 ms 1108 KB Output is correct
40 Correct 14 ms 1868 KB Output is correct
41 Correct 21 ms 2772 KB Output is correct
42 Correct 20 ms 2720 KB Output is correct
43 Correct 21 ms 2764 KB Output is correct
44 Correct 20 ms 2772 KB Output is correct
45 Correct 20 ms 2632 KB Output is correct
46 Correct 20 ms 2672 KB Output is correct
47 Correct 21 ms 2732 KB Output is correct
48 Correct 21 ms 2772 KB Output is correct
49 Correct 23 ms 2772 KB Output is correct
50 Correct 23 ms 2684 KB Output is correct
51 Correct 22 ms 2772 KB Output is correct
52 Correct 24 ms 2752 KB Output is correct
53 Correct 22 ms 2652 KB Output is correct
54 Correct 24 ms 2772 KB Output is correct
55 Correct 23 ms 2704 KB Output is correct
56 Correct 21 ms 2736 KB Output is correct
57 Correct 21 ms 2792 KB Output is correct
58 Correct 21 ms 2744 KB Output is correct
59 Correct 22 ms 2736 KB Output is correct
60 Correct 22 ms 2744 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 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 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 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 1 ms 468 KB Output is correct
14 Correct 6 ms 1704 KB Output is correct
15 Correct 13 ms 3544 KB Output is correct
16 Correct 20 ms 3960 KB Output is correct
17 Correct 22 ms 5980 KB Output is correct
18 Correct 19 ms 3800 KB Output is correct
19 Correct 23 ms 3800 KB Output is correct
20 Correct 21 ms 4788 KB Output is correct
21 Correct 20 ms 4368 KB Output is correct
22 Correct 21 ms 3896 KB Output is correct
23 Correct 19 ms 3784 KB Output is correct
24 Correct 18 ms 4312 KB Output is correct
25 Correct 19 ms 4324 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 7 ms 1620 KB Output is correct
30 Correct 20 ms 2632 KB Output is correct
31 Correct 22 ms 3880 KB Output is correct
32 Correct 21 ms 3560 KB Output is correct
33 Correct 21 ms 3792 KB Output is correct
34 Correct 20 ms 3532 KB Output is correct
35 Correct 21 ms 3644 KB Output is correct
36 Correct 19 ms 3564 KB Output is correct
37 Correct 19 ms 3680 KB Output is correct
38 Correct 20 ms 4308 KB Output is correct
39 Correct 20 ms 3568 KB Output is correct
40 Correct 19 ms 3664 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 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 4 ms 724 KB Output is correct
48 Correct 5 ms 724 KB Output is correct
49 Correct 8 ms 1108 KB Output is correct
50 Correct 14 ms 1868 KB Output is correct
51 Correct 21 ms 2772 KB Output is correct
52 Correct 20 ms 2720 KB Output is correct
53 Correct 21 ms 2764 KB Output is correct
54 Correct 20 ms 2772 KB Output is correct
55 Correct 20 ms 2632 KB Output is correct
56 Correct 20 ms 2672 KB Output is correct
57 Correct 21 ms 2732 KB Output is correct
58 Correct 21 ms 2772 KB Output is correct
59 Correct 23 ms 2772 KB Output is correct
60 Correct 23 ms 2684 KB Output is correct
61 Correct 22 ms 2772 KB Output is correct
62 Correct 24 ms 2752 KB Output is correct
63 Correct 22 ms 2652 KB Output is correct
64 Correct 24 ms 2772 KB Output is correct
65 Correct 23 ms 2704 KB Output is correct
66 Correct 21 ms 2736 KB Output is correct
67 Correct 21 ms 2792 KB Output is correct
68 Correct 21 ms 2744 KB Output is correct
69 Correct 22 ms 2736 KB Output is correct
70 Correct 22 ms 2744 KB Output is correct
71 Correct 5 ms 724 KB Output is correct
72 Correct 6 ms 724 KB Output is correct
73 Correct 4 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 8 ms 1124 KB Output is correct
77 Correct 8 ms 1108 KB Output is correct
78 Correct 10 ms 1108 KB Output is correct
79 Correct 8 ms 1108 KB Output is correct
80 Correct 8 ms 1108 KB Output is correct
81 Correct 15 ms 2004 KB Output is correct
82 Correct 16 ms 2004 KB Output is correct
83 Correct 15 ms 1952 KB Output is correct
84 Correct 15 ms 2000 KB Output is correct
85 Correct 15 ms 2004 KB Output is correct
86 Correct 20 ms 2044 KB Output is correct
87 Correct 16 ms 1980 KB Output is correct
88 Correct 18 ms 1980 KB Output is correct
89 Correct 18 ms 1960 KB Output is correct
90 Correct 16 ms 2080 KB Output is correct
91 Correct 26 ms 3420 KB Output is correct
92 Correct 37 ms 3404 KB Output is correct
93 Correct 36 ms 3412 KB Output is correct
94 Correct 29 ms 3384 KB Output is correct
95 Correct 24 ms 3440 KB Output is correct
96 Correct 31 ms 4204 KB Output is correct
97 Correct 29 ms 4192 KB Output is correct
98 Correct 30 ms 4172 KB Output is correct
99 Correct 29 ms 4180 KB Output is correct
100 Correct 31 ms 4240 KB Output is correct
101 Correct 31 ms 4252 KB Output is correct
102 Correct 30 ms 4252 KB Output is correct
103 Correct 29 ms 4172 KB Output is correct
104 Correct 31 ms 4216 KB Output is correct
105 Correct 30 ms 4172 KB Output is correct
106 Correct 29 ms 4648 KB Output is correct
107 Correct 29 ms 4564 KB Output is correct
108 Correct 39 ms 4520 KB Output is correct
109 Correct 40 ms 4596 KB Output is correct
110 Correct 32 ms 4644 KB Output is correct
111 Correct 32 ms 4564 KB Output is correct
112 Correct 30 ms 4736 KB Output is correct
113 Correct 33 ms 4584 KB Output is correct
114 Correct 31 ms 4548 KB Output is correct
115 Correct 34 ms 4540 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 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 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 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 1 ms 468 KB Output is correct
14 Correct 6 ms 1704 KB Output is correct
15 Correct 13 ms 3544 KB Output is correct
16 Correct 20 ms 3960 KB Output is correct
17 Correct 22 ms 5980 KB Output is correct
18 Correct 19 ms 3800 KB Output is correct
19 Correct 23 ms 3800 KB Output is correct
20 Correct 21 ms 4788 KB Output is correct
21 Correct 20 ms 4368 KB Output is correct
22 Correct 21 ms 3896 KB Output is correct
23 Correct 19 ms 3784 KB Output is correct
24 Correct 18 ms 4312 KB Output is correct
25 Correct 19 ms 4324 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 7 ms 1620 KB Output is correct
30 Correct 20 ms 2632 KB Output is correct
31 Correct 22 ms 3880 KB Output is correct
32 Correct 21 ms 3560 KB Output is correct
33 Correct 21 ms 3792 KB Output is correct
34 Correct 20 ms 3532 KB Output is correct
35 Correct 21 ms 3644 KB Output is correct
36 Correct 19 ms 3564 KB Output is correct
37 Correct 19 ms 3680 KB Output is correct
38 Correct 20 ms 4308 KB Output is correct
39 Correct 20 ms 3568 KB Output is correct
40 Correct 19 ms 3664 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 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 4 ms 724 KB Output is correct
48 Correct 5 ms 724 KB Output is correct
49 Correct 8 ms 1108 KB Output is correct
50 Correct 14 ms 1868 KB Output is correct
51 Correct 21 ms 2772 KB Output is correct
52 Correct 20 ms 2720 KB Output is correct
53 Correct 21 ms 2764 KB Output is correct
54 Correct 20 ms 2772 KB Output is correct
55 Correct 20 ms 2632 KB Output is correct
56 Correct 20 ms 2672 KB Output is correct
57 Correct 21 ms 2732 KB Output is correct
58 Correct 21 ms 2772 KB Output is correct
59 Correct 23 ms 2772 KB Output is correct
60 Correct 23 ms 2684 KB Output is correct
61 Correct 22 ms 2772 KB Output is correct
62 Correct 24 ms 2752 KB Output is correct
63 Correct 22 ms 2652 KB Output is correct
64 Correct 24 ms 2772 KB Output is correct
65 Correct 23 ms 2704 KB Output is correct
66 Correct 21 ms 2736 KB Output is correct
67 Correct 21 ms 2792 KB Output is correct
68 Correct 21 ms 2744 KB Output is correct
69 Correct 22 ms 2736 KB Output is correct
70 Correct 22 ms 2744 KB Output is correct
71 Correct 5 ms 724 KB Output is correct
72 Correct 6 ms 724 KB Output is correct
73 Correct 4 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 8 ms 1124 KB Output is correct
77 Correct 8 ms 1108 KB Output is correct
78 Correct 10 ms 1108 KB Output is correct
79 Correct 8 ms 1108 KB Output is correct
80 Correct 8 ms 1108 KB Output is correct
81 Correct 15 ms 2004 KB Output is correct
82 Correct 16 ms 2004 KB Output is correct
83 Correct 15 ms 1952 KB Output is correct
84 Correct 15 ms 2000 KB Output is correct
85 Correct 15 ms 2004 KB Output is correct
86 Correct 20 ms 2044 KB Output is correct
87 Correct 16 ms 1980 KB Output is correct
88 Correct 18 ms 1980 KB Output is correct
89 Correct 18 ms 1960 KB Output is correct
90 Correct 16 ms 2080 KB Output is correct
91 Correct 26 ms 3420 KB Output is correct
92 Correct 37 ms 3404 KB Output is correct
93 Correct 36 ms 3412 KB Output is correct
94 Correct 29 ms 3384 KB Output is correct
95 Correct 24 ms 3440 KB Output is correct
96 Correct 31 ms 4204 KB Output is correct
97 Correct 29 ms 4192 KB Output is correct
98 Correct 30 ms 4172 KB Output is correct
99 Correct 29 ms 4180 KB Output is correct
100 Correct 31 ms 4240 KB Output is correct
101 Correct 31 ms 4252 KB Output is correct
102 Correct 30 ms 4252 KB Output is correct
103 Correct 29 ms 4172 KB Output is correct
104 Correct 31 ms 4216 KB Output is correct
105 Correct 30 ms 4172 KB Output is correct
106 Correct 29 ms 4648 KB Output is correct
107 Correct 29 ms 4564 KB Output is correct
108 Correct 39 ms 4520 KB Output is correct
109 Correct 40 ms 4596 KB Output is correct
110 Correct 32 ms 4644 KB Output is correct
111 Correct 32 ms 4564 KB Output is correct
112 Correct 30 ms 4736 KB Output is correct
113 Correct 33 ms 4584 KB Output is correct
114 Correct 31 ms 4548 KB Output is correct
115 Correct 34 ms 4540 KB Output is correct
116 Correct 236 ms 1796 KB Output is correct
117 Correct 260 ms 1800 KB Output is correct
118 Correct 379 ms 2264 KB Output is correct
119 Correct 335 ms 2236 KB Output is correct
120 Correct 217 ms 2244 KB Output is correct
121 Correct 432 ms 2636 KB Output is correct
122 Correct 279 ms 2644 KB Output is correct
123 Correct 894 ms 3568 KB Output is correct
124 Correct 459 ms 3572 KB Output is correct
125 Correct 402 ms 3604 KB Output is correct
126 Correct 1815 ms 4336 KB Output is correct
127 Correct 1791 ms 6048 KB Output is correct
128 Correct 1815 ms 6076 KB Output is correct
129 Correct 1783 ms 6124 KB Output is correct
130 Correct 1781 ms 6020 KB Output is correct
131 Correct 1829 ms 6148 KB Output is correct
132 Correct 1823 ms 6168 KB Output is correct
133 Correct 1746 ms 6152 KB Output is correct
134 Correct 1733 ms 6272 KB Output is correct
135 Correct 1756 ms 6172 KB Output is correct
136 Correct 1765 ms 6204 KB Output is correct
137 Correct 1767 ms 6296 KB Output is correct
138 Correct 1802 ms 6192 KB Output is correct
139 Correct 1749 ms 6156 KB Output is correct
140 Correct 1779 ms 6112 KB Output is correct
141 Correct 1423 ms 6156 KB Output is correct
142 Correct 1461 ms 6152 KB Output is correct
143 Correct 1451 ms 6152 KB Output is correct
144 Correct 1405 ms 6168 KB Output is correct
145 Correct 1452 ms 6148 KB Output is correct
146 Correct 819 ms 6400 KB Output is correct
147 Correct 807 ms 6400 KB Output is correct
148 Correct 836 ms 6408 KB Output is correct
149 Correct 804 ms 6348 KB Output is correct
150 Correct 825 ms 6404 KB Output is correct
151 Correct 792 ms 6472 KB Output is correct
152 Correct 785 ms 6476 KB Output is correct
153 Correct 793 ms 6476 KB Output is correct
154 Correct 770 ms 6468 KB Output is correct
155 Correct 776 ms 6476 KB Output is correct
156 Correct 1679 ms 7536 KB Output is correct
157 Correct 1625 ms 7628 KB Output is correct
158 Correct 1590 ms 7468 KB Output is correct
159 Correct 1544 ms 7372 KB Output is correct
160 Correct 1563 ms 7376 KB Output is correct
161 Correct 1434 ms 7836 KB Output is correct
162 Correct 1439 ms 7572 KB Output is correct
163 Correct 1424 ms 7464 KB Output is correct
164 Correct 1424 ms 7552 KB Output is correct
165 Correct 1459 ms 7436 KB Output is correct
166 Correct 1734 ms 6916 KB Output is correct
167 Correct 1736 ms 6800 KB Output is correct
168 Correct 1715 ms 6804 KB Output is correct
169 Correct 1748 ms 7036 KB Output is correct
170 Correct 1769 ms 6856 KB Output is correct
171 Correct 1785 ms 6780 KB Output is correct
172 Correct 1786 ms 6788 KB Output is correct
173 Correct 1796 ms 6788 KB Output is correct
174 Correct 1801 ms 6812 KB Output is correct
175 Correct 1822 ms 6944 KB Output is correct