Submission #618191

# Submission time Handle Problem Language Result Execution time Memory
618191 2022-08-02T02:25:07 Z Joshc Diversity (CEOI21_diversity) C++11
64 / 100
7000 ms 4484 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long

int cnt[300005], a[300005], l[300005], r[300005];
ll n, out[50005];
const int SQRT = 900;

ll f(ll a, ll b, ll x) {
    // sum((a+bi) choose 2) with x terms
    return x*a*a + a*b*x*(x-1) + x*(x-1)*(2*x-1)/6*b*b - (2*a+b*(x-1))*x/2;
}

ll val(ll start, ll sz, ll num) {
    // We have num blocks of size sz, starting at position start
    // What is the total contribution?
    if (!num || !sz) return 0;
    ll res = n*(n+1)*num;
    res -= f(start, sz, num);
    res -= f(n-(start+sz*num-1)+1, sz, num);
    return res/2;
}

bool comp(int x, int y) {
    int p = l[x]/SQRT, q = l[y]/SQRT;
    if (p != q) return p < q;
    return p&1 ? (r[x] < r[y]) : (r[x] > r[y]);
}

map<int, int> v;
int freq[300005];

void add(int x) {
    if (freq[x] && !--v[freq[x]]) v.erase(freq[x]);
    v[++freq[x]]++;
}

void remove(int x) {
    if (!--v[freq[x]]) v.erase(freq[x]);
    v[--freq[x]]++;
}

ll solve() {
    ll ans = 0;
    int lsum = 0, rsum = 0;
    for (auto p : v) {
        int sz = p.first;
        int num = p.second;
        int lv = 0, rv = 0;
        if (lsum < rsum) {
            lv = num-num/2;
            rv = num/2;
        } else {
            rv = num-num/2;
            lv = num/2;
        }
        ans += val(lsum+1, sz, lv) + val(rsum+1, sz, rv);
        lsum += sz*lv;
        rsum += sz*rv;
    }
    return ans;
}

int main() {
    int q, x;
    scanf("%lld%d", &n, &q);
    vector<int> order;
    for (int i=1; i<=n; i++) scanf("%d", &a[i]);
    for (int i=1; i<=q; i++) {
        scanf("%d%d", &l[i], &r[i]);
        order.push_back(i);
    }
    sort(order.begin(), order.end(), comp);
    
    int L = 1, R = 0;

    for (int i : order) {
        while (R < r[i]) add(a[++R]);
        while (L > l[i]) add(a[--L]);
        while (R > r[i]) remove(a[R--]);
        while (L < l[i]) remove(a[L++]);
        n = R-L+1;
        out[i] = solve();
    }

    for (int i=1; i<=q; i++) printf("%lld\n", out[i]);
}

Compilation message

diversity.cpp: In function 'int main()':
diversity.cpp:66:12: warning: unused variable 'x' [-Wunused-variable]
   66 |     int q, x;
      |            ^
diversity.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     scanf("%lld%d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
diversity.cpp:69:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     for (int i=1; i<=n; i++) scanf("%d", &a[i]);
      |                              ~~~~~^~~~~~~~~~~~~
diversity.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         scanf("%d%d", &l[i], &r[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 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 316 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 328 KB Output is correct
4 Correct 16 ms 844 KB Output is correct
5 Correct 33 ms 1492 KB Output is correct
6 Correct 48 ms 2112 KB Output is correct
7 Correct 43 ms 2092 KB Output is correct
8 Correct 50 ms 2108 KB Output is correct
9 Correct 47 ms 2112 KB Output is correct
10 Correct 48 ms 2172 KB Output is correct
11 Correct 47 ms 2128 KB Output is correct
12 Correct 61 ms 2144 KB Output is correct
13 Correct 47 ms 2124 KB Output is correct
14 Correct 46 ms 2020 KB Output is correct
15 Correct 47 ms 2132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 328 KB Output is correct
4 Correct 16 ms 844 KB Output is correct
5 Correct 33 ms 1492 KB Output is correct
6 Correct 48 ms 2112 KB Output is correct
7 Correct 43 ms 2092 KB Output is correct
8 Correct 50 ms 2108 KB Output is correct
9 Correct 47 ms 2112 KB Output is correct
10 Correct 48 ms 2172 KB Output is correct
11 Correct 47 ms 2128 KB Output is correct
12 Correct 61 ms 2144 KB Output is correct
13 Correct 47 ms 2124 KB Output is correct
14 Correct 46 ms 2020 KB Output is correct
15 Correct 47 ms 2132 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 19 ms 992 KB Output is correct
20 Correct 36 ms 1564 KB Output is correct
21 Correct 62 ms 2328 KB Output is correct
22 Correct 54 ms 2268 KB Output is correct
23 Correct 53 ms 2164 KB Output is correct
24 Correct 56 ms 2276 KB Output is correct
25 Correct 61 ms 2264 KB Output is correct
26 Correct 54 ms 2240 KB Output is correct
27 Correct 57 ms 2200 KB Output is correct
28 Correct 57 ms 2196 KB Output is correct
29 Correct 66 ms 2232 KB Output is correct
30 Correct 56 ms 2288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 328 KB Output is correct
4 Correct 16 ms 844 KB Output is correct
5 Correct 33 ms 1492 KB Output is correct
6 Correct 48 ms 2112 KB Output is correct
7 Correct 43 ms 2092 KB Output is correct
8 Correct 50 ms 2108 KB Output is correct
9 Correct 47 ms 2112 KB Output is correct
10 Correct 48 ms 2172 KB Output is correct
11 Correct 47 ms 2128 KB Output is correct
12 Correct 61 ms 2144 KB Output is correct
13 Correct 47 ms 2124 KB Output is correct
14 Correct 46 ms 2020 KB Output is correct
15 Correct 47 ms 2132 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 19 ms 992 KB Output is correct
20 Correct 36 ms 1564 KB Output is correct
21 Correct 62 ms 2328 KB Output is correct
22 Correct 54 ms 2268 KB Output is correct
23 Correct 53 ms 2164 KB Output is correct
24 Correct 56 ms 2276 KB Output is correct
25 Correct 61 ms 2264 KB Output is correct
26 Correct 54 ms 2240 KB Output is correct
27 Correct 57 ms 2200 KB Output is correct
28 Correct 57 ms 2196 KB Output is correct
29 Correct 66 ms 2232 KB Output is correct
30 Correct 56 ms 2288 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 316 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 3 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 10 ms 724 KB Output is correct
38 Correct 8 ms 724 KB Output is correct
39 Correct 16 ms 1040 KB Output is correct
40 Correct 38 ms 1876 KB Output is correct
41 Correct 68 ms 2640 KB Output is correct
42 Correct 62 ms 2636 KB Output is correct
43 Correct 62 ms 2636 KB Output is correct
44 Correct 64 ms 2656 KB Output is correct
45 Correct 61 ms 2660 KB Output is correct
46 Correct 62 ms 2660 KB Output is correct
47 Correct 61 ms 2652 KB Output is correct
48 Correct 71 ms 2692 KB Output is correct
49 Correct 61 ms 2636 KB Output is correct
50 Correct 61 ms 2636 KB Output is correct
51 Correct 62 ms 2584 KB Output is correct
52 Correct 60 ms 2584 KB Output is correct
53 Correct 64 ms 2656 KB Output is correct
54 Correct 62 ms 2644 KB Output is correct
55 Correct 60 ms 2644 KB Output is correct
56 Correct 59 ms 2628 KB Output is correct
57 Correct 71 ms 2556 KB Output is correct
58 Correct 61 ms 2652 KB Output is correct
59 Correct 63 ms 2624 KB Output is correct
60 Correct 63 ms 2640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 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 316 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 328 KB Output is correct
14 Correct 16 ms 844 KB Output is correct
15 Correct 33 ms 1492 KB Output is correct
16 Correct 48 ms 2112 KB Output is correct
17 Correct 43 ms 2092 KB Output is correct
18 Correct 50 ms 2108 KB Output is correct
19 Correct 47 ms 2112 KB Output is correct
20 Correct 48 ms 2172 KB Output is correct
21 Correct 47 ms 2128 KB Output is correct
22 Correct 61 ms 2144 KB Output is correct
23 Correct 47 ms 2124 KB Output is correct
24 Correct 46 ms 2020 KB Output is correct
25 Correct 47 ms 2132 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 316 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 19 ms 992 KB Output is correct
30 Correct 36 ms 1564 KB Output is correct
31 Correct 62 ms 2328 KB Output is correct
32 Correct 54 ms 2268 KB Output is correct
33 Correct 53 ms 2164 KB Output is correct
34 Correct 56 ms 2276 KB Output is correct
35 Correct 61 ms 2264 KB Output is correct
36 Correct 54 ms 2240 KB Output is correct
37 Correct 57 ms 2200 KB Output is correct
38 Correct 57 ms 2196 KB Output is correct
39 Correct 66 ms 2232 KB Output is correct
40 Correct 56 ms 2288 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 316 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 3 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 332 KB Output is correct
47 Correct 10 ms 724 KB Output is correct
48 Correct 8 ms 724 KB Output is correct
49 Correct 16 ms 1040 KB Output is correct
50 Correct 38 ms 1876 KB Output is correct
51 Correct 68 ms 2640 KB Output is correct
52 Correct 62 ms 2636 KB Output is correct
53 Correct 62 ms 2636 KB Output is correct
54 Correct 64 ms 2656 KB Output is correct
55 Correct 61 ms 2660 KB Output is correct
56 Correct 62 ms 2660 KB Output is correct
57 Correct 61 ms 2652 KB Output is correct
58 Correct 71 ms 2692 KB Output is correct
59 Correct 61 ms 2636 KB Output is correct
60 Correct 61 ms 2636 KB Output is correct
61 Correct 62 ms 2584 KB Output is correct
62 Correct 60 ms 2584 KB Output is correct
63 Correct 64 ms 2656 KB Output is correct
64 Correct 62 ms 2644 KB Output is correct
65 Correct 60 ms 2644 KB Output is correct
66 Correct 59 ms 2628 KB Output is correct
67 Correct 71 ms 2556 KB Output is correct
68 Correct 61 ms 2652 KB Output is correct
69 Correct 63 ms 2624 KB Output is correct
70 Correct 63 ms 2640 KB Output is correct
71 Correct 7 ms 724 KB Output is correct
72 Correct 8 ms 648 KB Output is correct
73 Correct 8 ms 724 KB Output is correct
74 Correct 6 ms 724 KB Output is correct
75 Correct 7 ms 724 KB Output is correct
76 Correct 14 ms 1116 KB Output is correct
77 Correct 12 ms 1172 KB Output is correct
78 Correct 12 ms 1168 KB Output is correct
79 Correct 12 ms 1108 KB Output is correct
80 Correct 12 ms 1108 KB Output is correct
81 Correct 24 ms 2004 KB Output is correct
82 Correct 24 ms 2092 KB Output is correct
83 Correct 24 ms 2096 KB Output is correct
84 Correct 25 ms 2124 KB Output is correct
85 Correct 34 ms 2108 KB Output is correct
86 Correct 26 ms 2184 KB Output is correct
87 Correct 24 ms 2176 KB Output is correct
88 Correct 26 ms 2240 KB Output is correct
89 Correct 24 ms 2132 KB Output is correct
90 Correct 24 ms 2240 KB Output is correct
91 Correct 30 ms 3588 KB Output is correct
92 Correct 33 ms 3608 KB Output is correct
93 Correct 32 ms 3504 KB Output is correct
94 Correct 31 ms 3548 KB Output is correct
95 Correct 31 ms 3528 KB Output is correct
96 Correct 33 ms 4044 KB Output is correct
97 Correct 33 ms 4044 KB Output is correct
98 Correct 36 ms 4148 KB Output is correct
99 Correct 33 ms 4108 KB Output is correct
100 Correct 33 ms 4048 KB Output is correct
101 Correct 33 ms 4276 KB Output is correct
102 Correct 34 ms 4156 KB Output is correct
103 Correct 35 ms 4080 KB Output is correct
104 Correct 52 ms 4088 KB Output is correct
105 Correct 36 ms 4136 KB Output is correct
106 Correct 34 ms 4372 KB Output is correct
107 Correct 32 ms 4288 KB Output is correct
108 Correct 34 ms 4336 KB Output is correct
109 Correct 41 ms 4300 KB Output is correct
110 Correct 35 ms 4300 KB Output is correct
111 Correct 32 ms 4388 KB Output is correct
112 Correct 33 ms 4324 KB Output is correct
113 Correct 32 ms 4264 KB Output is correct
114 Correct 33 ms 4340 KB Output is correct
115 Correct 33 ms 4336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 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 316 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 328 KB Output is correct
14 Correct 16 ms 844 KB Output is correct
15 Correct 33 ms 1492 KB Output is correct
16 Correct 48 ms 2112 KB Output is correct
17 Correct 43 ms 2092 KB Output is correct
18 Correct 50 ms 2108 KB Output is correct
19 Correct 47 ms 2112 KB Output is correct
20 Correct 48 ms 2172 KB Output is correct
21 Correct 47 ms 2128 KB Output is correct
22 Correct 61 ms 2144 KB Output is correct
23 Correct 47 ms 2124 KB Output is correct
24 Correct 46 ms 2020 KB Output is correct
25 Correct 47 ms 2132 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 316 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 19 ms 992 KB Output is correct
30 Correct 36 ms 1564 KB Output is correct
31 Correct 62 ms 2328 KB Output is correct
32 Correct 54 ms 2268 KB Output is correct
33 Correct 53 ms 2164 KB Output is correct
34 Correct 56 ms 2276 KB Output is correct
35 Correct 61 ms 2264 KB Output is correct
36 Correct 54 ms 2240 KB Output is correct
37 Correct 57 ms 2200 KB Output is correct
38 Correct 57 ms 2196 KB Output is correct
39 Correct 66 ms 2232 KB Output is correct
40 Correct 56 ms 2288 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 316 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 3 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 332 KB Output is correct
47 Correct 10 ms 724 KB Output is correct
48 Correct 8 ms 724 KB Output is correct
49 Correct 16 ms 1040 KB Output is correct
50 Correct 38 ms 1876 KB Output is correct
51 Correct 68 ms 2640 KB Output is correct
52 Correct 62 ms 2636 KB Output is correct
53 Correct 62 ms 2636 KB Output is correct
54 Correct 64 ms 2656 KB Output is correct
55 Correct 61 ms 2660 KB Output is correct
56 Correct 62 ms 2660 KB Output is correct
57 Correct 61 ms 2652 KB Output is correct
58 Correct 71 ms 2692 KB Output is correct
59 Correct 61 ms 2636 KB Output is correct
60 Correct 61 ms 2636 KB Output is correct
61 Correct 62 ms 2584 KB Output is correct
62 Correct 60 ms 2584 KB Output is correct
63 Correct 64 ms 2656 KB Output is correct
64 Correct 62 ms 2644 KB Output is correct
65 Correct 60 ms 2644 KB Output is correct
66 Correct 59 ms 2628 KB Output is correct
67 Correct 71 ms 2556 KB Output is correct
68 Correct 61 ms 2652 KB Output is correct
69 Correct 63 ms 2624 KB Output is correct
70 Correct 63 ms 2640 KB Output is correct
71 Correct 7 ms 724 KB Output is correct
72 Correct 8 ms 648 KB Output is correct
73 Correct 8 ms 724 KB Output is correct
74 Correct 6 ms 724 KB Output is correct
75 Correct 7 ms 724 KB Output is correct
76 Correct 14 ms 1116 KB Output is correct
77 Correct 12 ms 1172 KB Output is correct
78 Correct 12 ms 1168 KB Output is correct
79 Correct 12 ms 1108 KB Output is correct
80 Correct 12 ms 1108 KB Output is correct
81 Correct 24 ms 2004 KB Output is correct
82 Correct 24 ms 2092 KB Output is correct
83 Correct 24 ms 2096 KB Output is correct
84 Correct 25 ms 2124 KB Output is correct
85 Correct 34 ms 2108 KB Output is correct
86 Correct 26 ms 2184 KB Output is correct
87 Correct 24 ms 2176 KB Output is correct
88 Correct 26 ms 2240 KB Output is correct
89 Correct 24 ms 2132 KB Output is correct
90 Correct 24 ms 2240 KB Output is correct
91 Correct 30 ms 3588 KB Output is correct
92 Correct 33 ms 3608 KB Output is correct
93 Correct 32 ms 3504 KB Output is correct
94 Correct 31 ms 3548 KB Output is correct
95 Correct 31 ms 3528 KB Output is correct
96 Correct 33 ms 4044 KB Output is correct
97 Correct 33 ms 4044 KB Output is correct
98 Correct 36 ms 4148 KB Output is correct
99 Correct 33 ms 4108 KB Output is correct
100 Correct 33 ms 4048 KB Output is correct
101 Correct 33 ms 4276 KB Output is correct
102 Correct 34 ms 4156 KB Output is correct
103 Correct 35 ms 4080 KB Output is correct
104 Correct 52 ms 4088 KB Output is correct
105 Correct 36 ms 4136 KB Output is correct
106 Correct 34 ms 4372 KB Output is correct
107 Correct 32 ms 4288 KB Output is correct
108 Correct 34 ms 4336 KB Output is correct
109 Correct 41 ms 4300 KB Output is correct
110 Correct 35 ms 4300 KB Output is correct
111 Correct 32 ms 4388 KB Output is correct
112 Correct 33 ms 4324 KB Output is correct
113 Correct 32 ms 4264 KB Output is correct
114 Correct 33 ms 4340 KB Output is correct
115 Correct 33 ms 4336 KB Output is correct
116 Correct 1912 ms 2352 KB Output is correct
117 Correct 1662 ms 2352 KB Output is correct
118 Correct 2631 ms 2836 KB Output is correct
119 Correct 1620 ms 2888 KB Output is correct
120 Correct 1198 ms 2888 KB Output is correct
121 Correct 1942 ms 3296 KB Output is correct
122 Correct 1411 ms 3396 KB Output is correct
123 Correct 4360 ms 4120 KB Output is correct
124 Correct 2092 ms 4432 KB Output is correct
125 Correct 1546 ms 4484 KB Output is correct
126 Execution timed out 7081 ms 4312 KB Time limit exceeded
127 Halted 0 ms 0 KB -