Submission #650655

# Submission time Handle Problem Language Result Execution time Memory
650655 2022-10-14T13:12:01 Z 1bin Diversity (CEOI21_diversity) C++14
100 / 100
1872 ms 5900 KB
#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:41:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |         for(int i = 0; i < mp.size(); i++)
      |                        ~~^~~~~~~~~~~
# 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 0 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 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 1 ms 468 KB Output is correct
4 Correct 7 ms 1748 KB Output is correct
5 Correct 13 ms 3500 KB Output is correct
6 Correct 21 ms 3928 KB Output is correct
7 Correct 21 ms 5900 KB Output is correct
8 Correct 19 ms 3840 KB Output is correct
9 Correct 18 ms 3856 KB Output is correct
10 Correct 21 ms 4692 KB Output is correct
11 Correct 20 ms 4308 KB Output is correct
12 Correct 19 ms 3780 KB Output is correct
13 Correct 19 ms 3800 KB Output is correct
14 Correct 20 ms 4380 KB Output is correct
15 Correct 19 ms 4376 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 7 ms 1748 KB Output is correct
5 Correct 13 ms 3500 KB Output is correct
6 Correct 21 ms 3928 KB Output is correct
7 Correct 21 ms 5900 KB Output is correct
8 Correct 19 ms 3840 KB Output is correct
9 Correct 18 ms 3856 KB Output is correct
10 Correct 21 ms 4692 KB Output is correct
11 Correct 20 ms 4308 KB Output is correct
12 Correct 19 ms 3780 KB Output is correct
13 Correct 19 ms 3800 KB Output is correct
14 Correct 20 ms 4380 KB Output is correct
15 Correct 19 ms 4376 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 468 KB Output is correct
19 Correct 8 ms 1620 KB Output is correct
20 Correct 14 ms 2540 KB Output is correct
21 Correct 22 ms 3920 KB Output is correct
22 Correct 24 ms 3520 KB Output is correct
23 Correct 20 ms 3800 KB Output is correct
24 Correct 19 ms 3484 KB Output is correct
25 Correct 20 ms 3648 KB Output is correct
26 Correct 20 ms 3540 KB Output is correct
27 Correct 21 ms 3636 KB Output is correct
28 Correct 21 ms 4336 KB Output is correct
29 Correct 20 ms 3608 KB Output is correct
30 Correct 20 ms 3684 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 7 ms 1748 KB Output is correct
5 Correct 13 ms 3500 KB Output is correct
6 Correct 21 ms 3928 KB Output is correct
7 Correct 21 ms 5900 KB Output is correct
8 Correct 19 ms 3840 KB Output is correct
9 Correct 18 ms 3856 KB Output is correct
10 Correct 21 ms 4692 KB Output is correct
11 Correct 20 ms 4308 KB Output is correct
12 Correct 19 ms 3780 KB Output is correct
13 Correct 19 ms 3800 KB Output is correct
14 Correct 20 ms 4380 KB Output is correct
15 Correct 19 ms 4376 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 468 KB Output is correct
19 Correct 8 ms 1620 KB Output is correct
20 Correct 14 ms 2540 KB Output is correct
21 Correct 22 ms 3920 KB Output is correct
22 Correct 24 ms 3520 KB Output is correct
23 Correct 20 ms 3800 KB Output is correct
24 Correct 19 ms 3484 KB Output is correct
25 Correct 20 ms 3648 KB Output is correct
26 Correct 20 ms 3540 KB Output is correct
27 Correct 21 ms 3636 KB Output is correct
28 Correct 21 ms 4336 KB Output is correct
29 Correct 20 ms 3608 KB Output is correct
30 Correct 20 ms 3684 KB Output is correct
31 Correct 1 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 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 5 ms 724 KB Output is correct
38 Correct 4 ms 724 KB Output is correct
39 Correct 7 ms 1108 KB Output is correct
40 Correct 15 ms 2000 KB Output is correct
41 Correct 22 ms 2748 KB Output is correct
42 Correct 21 ms 2768 KB Output is correct
43 Correct 21 ms 2760 KB Output is correct
44 Correct 21 ms 2756 KB Output is correct
45 Correct 20 ms 2644 KB Output is correct
46 Correct 21 ms 2744 KB Output is correct
47 Correct 22 ms 2672 KB Output is correct
48 Correct 21 ms 2672 KB Output is correct
49 Correct 21 ms 2744 KB Output is correct
50 Correct 22 ms 2676 KB Output is correct
51 Correct 20 ms 2772 KB Output is correct
52 Correct 21 ms 2664 KB Output is correct
53 Correct 22 ms 2772 KB Output is correct
54 Correct 23 ms 2772 KB Output is correct
55 Correct 21 ms 2644 KB Output is correct
56 Correct 21 ms 2724 KB Output is correct
57 Correct 21 ms 2632 KB Output is correct
58 Correct 24 ms 2740 KB Output is correct
59 Correct 20 ms 2772 KB Output is correct
60 Correct 22 ms 2768 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 0 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 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 1 ms 468 KB Output is correct
14 Correct 7 ms 1748 KB Output is correct
15 Correct 13 ms 3500 KB Output is correct
16 Correct 21 ms 3928 KB Output is correct
17 Correct 21 ms 5900 KB Output is correct
18 Correct 19 ms 3840 KB Output is correct
19 Correct 18 ms 3856 KB Output is correct
20 Correct 21 ms 4692 KB Output is correct
21 Correct 20 ms 4308 KB Output is correct
22 Correct 19 ms 3780 KB Output is correct
23 Correct 19 ms 3800 KB Output is correct
24 Correct 20 ms 4380 KB Output is correct
25 Correct 19 ms 4376 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 468 KB Output is correct
29 Correct 8 ms 1620 KB Output is correct
30 Correct 14 ms 2540 KB Output is correct
31 Correct 22 ms 3920 KB Output is correct
32 Correct 24 ms 3520 KB Output is correct
33 Correct 20 ms 3800 KB Output is correct
34 Correct 19 ms 3484 KB Output is correct
35 Correct 20 ms 3648 KB Output is correct
36 Correct 20 ms 3540 KB Output is correct
37 Correct 21 ms 3636 KB Output is correct
38 Correct 21 ms 4336 KB Output is correct
39 Correct 20 ms 3608 KB Output is correct
40 Correct 20 ms 3684 KB Output is correct
41 Correct 1 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 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 5 ms 724 KB Output is correct
48 Correct 4 ms 724 KB Output is correct
49 Correct 7 ms 1108 KB Output is correct
50 Correct 15 ms 2000 KB Output is correct
51 Correct 22 ms 2748 KB Output is correct
52 Correct 21 ms 2768 KB Output is correct
53 Correct 21 ms 2760 KB Output is correct
54 Correct 21 ms 2756 KB Output is correct
55 Correct 20 ms 2644 KB Output is correct
56 Correct 21 ms 2744 KB Output is correct
57 Correct 22 ms 2672 KB Output is correct
58 Correct 21 ms 2672 KB Output is correct
59 Correct 21 ms 2744 KB Output is correct
60 Correct 22 ms 2676 KB Output is correct
61 Correct 20 ms 2772 KB Output is correct
62 Correct 21 ms 2664 KB Output is correct
63 Correct 22 ms 2772 KB Output is correct
64 Correct 23 ms 2772 KB Output is correct
65 Correct 21 ms 2644 KB Output is correct
66 Correct 21 ms 2724 KB Output is correct
67 Correct 21 ms 2632 KB Output is correct
68 Correct 24 ms 2740 KB Output is correct
69 Correct 20 ms 2772 KB Output is correct
70 Correct 22 ms 2768 KB Output is correct
71 Correct 4 ms 724 KB Output is correct
72 Correct 4 ms 724 KB Output is correct
73 Correct 5 ms 768 KB Output is correct
74 Correct 5 ms 716 KB Output is correct
75 Correct 4 ms 772 KB Output is correct
76 Correct 7 ms 1108 KB Output is correct
77 Correct 9 ms 1108 KB Output is correct
78 Correct 8 ms 1108 KB Output is correct
79 Correct 8 ms 1168 KB Output is correct
80 Correct 8 ms 1096 KB Output is correct
81 Correct 15 ms 1952 KB Output is correct
82 Correct 16 ms 1936 KB Output is correct
83 Correct 15 ms 2004 KB Output is correct
84 Correct 15 ms 1896 KB Output is correct
85 Correct 17 ms 1904 KB Output is correct
86 Correct 16 ms 2004 KB Output is correct
87 Correct 17 ms 1968 KB Output is correct
88 Correct 18 ms 2032 KB Output is correct
89 Correct 18 ms 2088 KB Output is correct
90 Correct 17 ms 2020 KB Output is correct
91 Correct 26 ms 3436 KB Output is correct
92 Correct 26 ms 3400 KB Output is correct
93 Correct 26 ms 3460 KB Output is correct
94 Correct 26 ms 3420 KB Output is correct
95 Correct 26 ms 3412 KB Output is correct
96 Correct 30 ms 4252 KB Output is correct
97 Correct 30 ms 4180 KB Output is correct
98 Correct 29 ms 4188 KB Output is correct
99 Correct 28 ms 4356 KB Output is correct
100 Correct 29 ms 4232 KB Output is correct
101 Correct 31 ms 4180 KB Output is correct
102 Correct 32 ms 4328 KB Output is correct
103 Correct 30 ms 4248 KB Output is correct
104 Correct 30 ms 4180 KB Output is correct
105 Correct 31 ms 4188 KB Output is correct
106 Correct 30 ms 4540 KB Output is correct
107 Correct 31 ms 4556 KB Output is correct
108 Correct 30 ms 4564 KB Output is correct
109 Correct 30 ms 4644 KB Output is correct
110 Correct 32 ms 4556 KB Output is correct
111 Correct 30 ms 4644 KB Output is correct
112 Correct 30 ms 4564 KB Output is correct
113 Correct 31 ms 4548 KB Output is correct
114 Correct 29 ms 4564 KB Output is correct
115 Correct 30 ms 4608 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 0 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 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 1 ms 468 KB Output is correct
14 Correct 7 ms 1748 KB Output is correct
15 Correct 13 ms 3500 KB Output is correct
16 Correct 21 ms 3928 KB Output is correct
17 Correct 21 ms 5900 KB Output is correct
18 Correct 19 ms 3840 KB Output is correct
19 Correct 18 ms 3856 KB Output is correct
20 Correct 21 ms 4692 KB Output is correct
21 Correct 20 ms 4308 KB Output is correct
22 Correct 19 ms 3780 KB Output is correct
23 Correct 19 ms 3800 KB Output is correct
24 Correct 20 ms 4380 KB Output is correct
25 Correct 19 ms 4376 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 468 KB Output is correct
29 Correct 8 ms 1620 KB Output is correct
30 Correct 14 ms 2540 KB Output is correct
31 Correct 22 ms 3920 KB Output is correct
32 Correct 24 ms 3520 KB Output is correct
33 Correct 20 ms 3800 KB Output is correct
34 Correct 19 ms 3484 KB Output is correct
35 Correct 20 ms 3648 KB Output is correct
36 Correct 20 ms 3540 KB Output is correct
37 Correct 21 ms 3636 KB Output is correct
38 Correct 21 ms 4336 KB Output is correct
39 Correct 20 ms 3608 KB Output is correct
40 Correct 20 ms 3684 KB Output is correct
41 Correct 1 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 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 5 ms 724 KB Output is correct
48 Correct 4 ms 724 KB Output is correct
49 Correct 7 ms 1108 KB Output is correct
50 Correct 15 ms 2000 KB Output is correct
51 Correct 22 ms 2748 KB Output is correct
52 Correct 21 ms 2768 KB Output is correct
53 Correct 21 ms 2760 KB Output is correct
54 Correct 21 ms 2756 KB Output is correct
55 Correct 20 ms 2644 KB Output is correct
56 Correct 21 ms 2744 KB Output is correct
57 Correct 22 ms 2672 KB Output is correct
58 Correct 21 ms 2672 KB Output is correct
59 Correct 21 ms 2744 KB Output is correct
60 Correct 22 ms 2676 KB Output is correct
61 Correct 20 ms 2772 KB Output is correct
62 Correct 21 ms 2664 KB Output is correct
63 Correct 22 ms 2772 KB Output is correct
64 Correct 23 ms 2772 KB Output is correct
65 Correct 21 ms 2644 KB Output is correct
66 Correct 21 ms 2724 KB Output is correct
67 Correct 21 ms 2632 KB Output is correct
68 Correct 24 ms 2740 KB Output is correct
69 Correct 20 ms 2772 KB Output is correct
70 Correct 22 ms 2768 KB Output is correct
71 Correct 4 ms 724 KB Output is correct
72 Correct 4 ms 724 KB Output is correct
73 Correct 5 ms 768 KB Output is correct
74 Correct 5 ms 716 KB Output is correct
75 Correct 4 ms 772 KB Output is correct
76 Correct 7 ms 1108 KB Output is correct
77 Correct 9 ms 1108 KB Output is correct
78 Correct 8 ms 1108 KB Output is correct
79 Correct 8 ms 1168 KB Output is correct
80 Correct 8 ms 1096 KB Output is correct
81 Correct 15 ms 1952 KB Output is correct
82 Correct 16 ms 1936 KB Output is correct
83 Correct 15 ms 2004 KB Output is correct
84 Correct 15 ms 1896 KB Output is correct
85 Correct 17 ms 1904 KB Output is correct
86 Correct 16 ms 2004 KB Output is correct
87 Correct 17 ms 1968 KB Output is correct
88 Correct 18 ms 2032 KB Output is correct
89 Correct 18 ms 2088 KB Output is correct
90 Correct 17 ms 2020 KB Output is correct
91 Correct 26 ms 3436 KB Output is correct
92 Correct 26 ms 3400 KB Output is correct
93 Correct 26 ms 3460 KB Output is correct
94 Correct 26 ms 3420 KB Output is correct
95 Correct 26 ms 3412 KB Output is correct
96 Correct 30 ms 4252 KB Output is correct
97 Correct 30 ms 4180 KB Output is correct
98 Correct 29 ms 4188 KB Output is correct
99 Correct 28 ms 4356 KB Output is correct
100 Correct 29 ms 4232 KB Output is correct
101 Correct 31 ms 4180 KB Output is correct
102 Correct 32 ms 4328 KB Output is correct
103 Correct 30 ms 4248 KB Output is correct
104 Correct 30 ms 4180 KB Output is correct
105 Correct 31 ms 4188 KB Output is correct
106 Correct 30 ms 4540 KB Output is correct
107 Correct 31 ms 4556 KB Output is correct
108 Correct 30 ms 4564 KB Output is correct
109 Correct 30 ms 4644 KB Output is correct
110 Correct 32 ms 4556 KB Output is correct
111 Correct 30 ms 4644 KB Output is correct
112 Correct 30 ms 4564 KB Output is correct
113 Correct 31 ms 4548 KB Output is correct
114 Correct 29 ms 4564 KB Output is correct
115 Correct 30 ms 4608 KB Output is correct
116 Correct 234 ms 1808 KB Output is correct
117 Correct 240 ms 1796 KB Output is correct
118 Correct 369 ms 2268 KB Output is correct
119 Correct 319 ms 2208 KB Output is correct
120 Correct 241 ms 2244 KB Output is correct
121 Correct 446 ms 2640 KB Output is correct
122 Correct 326 ms 2716 KB Output is correct
123 Correct 913 ms 3564 KB Output is correct
124 Correct 603 ms 3568 KB Output is correct
125 Correct 565 ms 3604 KB Output is correct
126 Correct 1872 ms 4336 KB Output is correct
127 Correct 1836 ms 4328 KB Output is correct
128 Correct 1865 ms 4368 KB Output is correct
129 Correct 1814 ms 4432 KB Output is correct
130 Correct 1804 ms 4296 KB Output is correct
131 Correct 1825 ms 4496 KB Output is correct
132 Correct 1836 ms 4400 KB Output is correct
133 Correct 1862 ms 4396 KB Output is correct
134 Correct 1828 ms 4404 KB Output is correct
135 Correct 1842 ms 4448 KB Output is correct
136 Correct 1827 ms 4548 KB Output is correct
137 Correct 1820 ms 4396 KB Output is correct
138 Correct 1845 ms 4436 KB Output is correct
139 Correct 1837 ms 4392 KB Output is correct
140 Correct 1845 ms 4424 KB Output is correct
141 Correct 1557 ms 4300 KB Output is correct
142 Correct 1589 ms 4348 KB Output is correct
143 Correct 1587 ms 4376 KB Output is correct
144 Correct 1599 ms 4368 KB Output is correct
145 Correct 1585 ms 4360 KB Output is correct
146 Correct 1072 ms 4472 KB Output is correct
147 Correct 1052 ms 4352 KB Output is correct
148 Correct 1080 ms 4392 KB Output is correct
149 Correct 1061 ms 4376 KB Output is correct
150 Correct 1068 ms 4448 KB Output is correct
151 Correct 1061 ms 4392 KB Output is correct
152 Correct 1050 ms 4380 KB Output is correct
153 Correct 1075 ms 4400 KB Output is correct
154 Correct 1056 ms 4344 KB Output is correct
155 Correct 1048 ms 4332 KB Output is correct
156 Correct 1743 ms 5160 KB Output is correct
157 Correct 1727 ms 5256 KB Output is correct
158 Correct 1736 ms 4984 KB Output is correct
159 Correct 1737 ms 5004 KB Output is correct
160 Correct 1738 ms 5008 KB Output is correct
161 Correct 1549 ms 5492 KB Output is correct
162 Correct 1554 ms 5104 KB Output is correct
163 Correct 1574 ms 4992 KB Output is correct
164 Correct 1583 ms 5084 KB Output is correct
165 Correct 1541 ms 5088 KB Output is correct
166 Correct 1817 ms 4664 KB Output is correct
167 Correct 1794 ms 4596 KB Output is correct
168 Correct 1797 ms 4476 KB Output is correct
169 Correct 1809 ms 4656 KB Output is correct
170 Correct 1812 ms 4608 KB Output is correct
171 Correct 1800 ms 4460 KB Output is correct
172 Correct 1815 ms 4584 KB Output is correct
173 Correct 1820 ms 4468 KB Output is correct
174 Correct 1839 ms 4616 KB Output is correct
175 Correct 1843 ms 4616 KB Output is correct