Submission #1087381

# Submission time Handle Problem Language Result Execution time Memory
1087381 2024-09-12T15:00:59 Z vladilius Diversity (CEOI21_diversity) C++17
64 / 100
53 ms 25000 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define arr3 array<int, 3>
const int A = 3e5;

struct FT{
    ll bit[A + 1], a[A + 1], s;
    int n;
    FT(int ns){
        n = ns;
        for (int i = 1; i <= n; i++){
            bit[i] = a[i] = 0;
        }
        s = 0;
    }
    void upd(int v, ll x){
        s += x; a[v] += x;
        while (v <= n){
            bit[v] += x;
            v |= (v + 1);
        }
    }
    ll get(int v){
        ll out = 0;
        while (v > 0){
            out += bit[v];
            v = (v & (v + 1)) - 1;
        }
        return out;
    }
    ll get(int l, int r){
        if (l > r) return 0;
        return get(r) - get(l - 1);
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, q; cin>>n>>q;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++){
        cin>>a[i];
    }
    
    vector<arr3> qs;
    for (int i = 1; i <= q; i++){
        int l, r; cin>>l>>r;
        qs.pb({l, r, i});
    }
    
    const int S = sqrt(n);
    auto cmp = [&](arr3 x, arr3 y){
        int x1 = x[0] / S, y1 = y[0] / S;
        if (x1 != y1){
            return x[0] < y[0];
        }
        return (x1 & 1) ? (x[1] < y[1]) : (x[1] > y[1]);
    };
    sort(qs.begin(), qs.end(), cmp);
    
    vector<int> p(n + 1);
    for (int i = 1; i <= n; i++){
        if (i % 2){
            p[i] = (i + 1) / 2;
        }
        else {
            p[i] = n - i / 2 + 1;
        }
    }
    
    ll sum = 0, s1, s2;
    vector<int> f(n + 1), cnt(A + 1);
    pii rr[n + 1]; rr[0] = {1, n};
    for (int i = 1; i <= n; i++) rr[i] = {0, 0};
    FT T1(n), T2(n);
    auto add = [&](int x){
        x = a[x];
        int k = cnt[x], j1 = rr[k].ss, j = p[j1];
        
        sum += (f[j1] + 1);
        s1 = T1.get(1, j - 1); s2 = T2.get(1, j - 1);
        sum += (1LL * j * s1);
        sum -= (s2 - s1);
        sum += (T2.s - s2 - T2.a[j]);
        sum -= 1LL * (j - 1) * (T1.s - s1 - T1.a[j]);
        
        if (rr[k].ff == rr[k].ss) rr[k] = {0, 0};
        else rr[k].ss--;
        
        cnt[x]++; f[j1]++; k++;
        T1.upd(j, 1);
        T2.upd(j, j);
        
        if (!rr[k].ff) rr[k] = {j1, j1};
        else rr[k].ff--;
    };

    auto rem = [&](int x){
        x = a[x];
        int k = cnt[x], j1 = rr[k].ff, j = p[j1];

        sum -= f[j1];
        s1 = T1.get(1, j - 1); s2 = T2.get(1, j - 1);
        sum -= (1LL * j * s1);
        sum += (s2 - s1);
        sum -= (T2.s - s2 - T2.a[j]);
        sum += 1LL * (j - 1) * (T1.s - s1 - T1.a[j]);
        
        if (rr[k].ff == rr[k].ss) rr[k] = {0, 0};
        else rr[k].ff++;
        
        cnt[x]--; f[j1]--; k--;
        T1.upd(j, -1);
        T2.upd(j, -j);
        
        if (!rr[k].ff) rr[k] = {j1, j1};
        else rr[k].ss++;
    };
    
    vector<ll> out(q + 1);
    int lc = 1, rc = 0;
    for (auto [l, r, ii]: qs){
        while (lc < l) rem(lc++);
        while (lc > l) add(--lc);
        while (rc < r) add(++rc);
        while (rc > r) rem(rc--);
        
        out[ii] = sum;
    }
    
    for (int i = 1; i <= q; i++){
        cout<<out[i]<<"\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10840 KB Output is correct
2 Correct 6 ms 10844 KB Output is correct
3 Correct 5 ms 10844 KB Output is correct
4 Correct 6 ms 10844 KB Output is correct
5 Correct 5 ms 10840 KB Output is correct
6 Correct 5 ms 10844 KB Output is correct
7 Correct 5 ms 10996 KB Output is correct
8 Correct 5 ms 10844 KB Output is correct
9 Correct 5 ms 10868 KB Output is correct
10 Correct 5 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10840 KB Output is correct
2 Correct 5 ms 10900 KB Output is correct
3 Correct 6 ms 11100 KB Output is correct
4 Correct 14 ms 13176 KB Output is correct
5 Correct 22 ms 15196 KB Output is correct
6 Correct 38 ms 17540 KB Output is correct
7 Correct 35 ms 17500 KB Output is correct
8 Correct 34 ms 17496 KB Output is correct
9 Correct 34 ms 17496 KB Output is correct
10 Correct 36 ms 17488 KB Output is correct
11 Correct 37 ms 17500 KB Output is correct
12 Correct 34 ms 17328 KB Output is correct
13 Correct 34 ms 17500 KB Output is correct
14 Correct 38 ms 17516 KB Output is correct
15 Correct 36 ms 17480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10840 KB Output is correct
2 Correct 5 ms 10900 KB Output is correct
3 Correct 6 ms 11100 KB Output is correct
4 Correct 14 ms 13176 KB Output is correct
5 Correct 22 ms 15196 KB Output is correct
6 Correct 38 ms 17540 KB Output is correct
7 Correct 35 ms 17500 KB Output is correct
8 Correct 34 ms 17496 KB Output is correct
9 Correct 34 ms 17496 KB Output is correct
10 Correct 36 ms 17488 KB Output is correct
11 Correct 37 ms 17500 KB Output is correct
12 Correct 34 ms 17328 KB Output is correct
13 Correct 34 ms 17500 KB Output is correct
14 Correct 38 ms 17516 KB Output is correct
15 Correct 36 ms 17480 KB Output is correct
16 Correct 5 ms 10844 KB Output is correct
17 Correct 5 ms 11044 KB Output is correct
18 Correct 6 ms 11100 KB Output is correct
19 Correct 14 ms 13264 KB Output is correct
20 Correct 24 ms 15452 KB Output is correct
21 Correct 41 ms 17536 KB Output is correct
22 Correct 34 ms 17500 KB Output is correct
23 Correct 36 ms 17496 KB Output is correct
24 Correct 34 ms 17496 KB Output is correct
25 Correct 36 ms 17492 KB Output is correct
26 Correct 37 ms 17496 KB Output is correct
27 Correct 37 ms 17652 KB Output is correct
28 Correct 40 ms 17500 KB Output is correct
29 Correct 36 ms 17372 KB Output is correct
30 Correct 38 ms 17500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10840 KB Output is correct
2 Correct 5 ms 10900 KB Output is correct
3 Correct 6 ms 11100 KB Output is correct
4 Correct 14 ms 13176 KB Output is correct
5 Correct 22 ms 15196 KB Output is correct
6 Correct 38 ms 17540 KB Output is correct
7 Correct 35 ms 17500 KB Output is correct
8 Correct 34 ms 17496 KB Output is correct
9 Correct 34 ms 17496 KB Output is correct
10 Correct 36 ms 17488 KB Output is correct
11 Correct 37 ms 17500 KB Output is correct
12 Correct 34 ms 17328 KB Output is correct
13 Correct 34 ms 17500 KB Output is correct
14 Correct 38 ms 17516 KB Output is correct
15 Correct 36 ms 17480 KB Output is correct
16 Correct 5 ms 10844 KB Output is correct
17 Correct 5 ms 11044 KB Output is correct
18 Correct 6 ms 11100 KB Output is correct
19 Correct 14 ms 13264 KB Output is correct
20 Correct 24 ms 15452 KB Output is correct
21 Correct 41 ms 17536 KB Output is correct
22 Correct 34 ms 17500 KB Output is correct
23 Correct 36 ms 17496 KB Output is correct
24 Correct 34 ms 17496 KB Output is correct
25 Correct 36 ms 17492 KB Output is correct
26 Correct 37 ms 17496 KB Output is correct
27 Correct 37 ms 17652 KB Output is correct
28 Correct 40 ms 17500 KB Output is correct
29 Correct 36 ms 17372 KB Output is correct
30 Correct 38 ms 17500 KB Output is correct
31 Correct 5 ms 10844 KB Output is correct
32 Correct 6 ms 10844 KB Output is correct
33 Correct 6 ms 10956 KB Output is correct
34 Correct 6 ms 11100 KB Output is correct
35 Correct 7 ms 11140 KB Output is correct
36 Correct 6 ms 11100 KB Output is correct
37 Correct 9 ms 12124 KB Output is correct
38 Correct 9 ms 12092 KB Output is correct
39 Correct 15 ms 13148 KB Output is correct
40 Correct 26 ms 15708 KB Output is correct
41 Correct 38 ms 17928 KB Output is correct
42 Correct 41 ms 18012 KB Output is correct
43 Correct 40 ms 18012 KB Output is correct
44 Correct 38 ms 17788 KB Output is correct
45 Correct 48 ms 17800 KB Output is correct
46 Correct 38 ms 18012 KB Output is correct
47 Correct 38 ms 18012 KB Output is correct
48 Correct 38 ms 18012 KB Output is correct
49 Correct 39 ms 18012 KB Output is correct
50 Correct 44 ms 17920 KB Output is correct
51 Correct 38 ms 18012 KB Output is correct
52 Correct 37 ms 18012 KB Output is correct
53 Correct 39 ms 17920 KB Output is correct
54 Correct 37 ms 18008 KB Output is correct
55 Correct 41 ms 18012 KB Output is correct
56 Correct 38 ms 18012 KB Output is correct
57 Correct 42 ms 18004 KB Output is correct
58 Correct 37 ms 18008 KB Output is correct
59 Correct 39 ms 18004 KB Output is correct
60 Correct 39 ms 18012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10840 KB Output is correct
2 Correct 6 ms 10844 KB Output is correct
3 Correct 5 ms 10844 KB Output is correct
4 Correct 6 ms 10844 KB Output is correct
5 Correct 5 ms 10840 KB Output is correct
6 Correct 5 ms 10844 KB Output is correct
7 Correct 5 ms 10996 KB Output is correct
8 Correct 5 ms 10844 KB Output is correct
9 Correct 5 ms 10868 KB Output is correct
10 Correct 5 ms 10844 KB Output is correct
11 Correct 6 ms 10840 KB Output is correct
12 Correct 5 ms 10900 KB Output is correct
13 Correct 6 ms 11100 KB Output is correct
14 Correct 14 ms 13176 KB Output is correct
15 Correct 22 ms 15196 KB Output is correct
16 Correct 38 ms 17540 KB Output is correct
17 Correct 35 ms 17500 KB Output is correct
18 Correct 34 ms 17496 KB Output is correct
19 Correct 34 ms 17496 KB Output is correct
20 Correct 36 ms 17488 KB Output is correct
21 Correct 37 ms 17500 KB Output is correct
22 Correct 34 ms 17328 KB Output is correct
23 Correct 34 ms 17500 KB Output is correct
24 Correct 38 ms 17516 KB Output is correct
25 Correct 36 ms 17480 KB Output is correct
26 Correct 5 ms 10844 KB Output is correct
27 Correct 5 ms 11044 KB Output is correct
28 Correct 6 ms 11100 KB Output is correct
29 Correct 14 ms 13264 KB Output is correct
30 Correct 24 ms 15452 KB Output is correct
31 Correct 41 ms 17536 KB Output is correct
32 Correct 34 ms 17500 KB Output is correct
33 Correct 36 ms 17496 KB Output is correct
34 Correct 34 ms 17496 KB Output is correct
35 Correct 36 ms 17492 KB Output is correct
36 Correct 37 ms 17496 KB Output is correct
37 Correct 37 ms 17652 KB Output is correct
38 Correct 40 ms 17500 KB Output is correct
39 Correct 36 ms 17372 KB Output is correct
40 Correct 38 ms 17500 KB Output is correct
41 Correct 5 ms 10844 KB Output is correct
42 Correct 6 ms 10844 KB Output is correct
43 Correct 6 ms 10956 KB Output is correct
44 Correct 6 ms 11100 KB Output is correct
45 Correct 7 ms 11140 KB Output is correct
46 Correct 6 ms 11100 KB Output is correct
47 Correct 9 ms 12124 KB Output is correct
48 Correct 9 ms 12092 KB Output is correct
49 Correct 15 ms 13148 KB Output is correct
50 Correct 26 ms 15708 KB Output is correct
51 Correct 38 ms 17928 KB Output is correct
52 Correct 41 ms 18012 KB Output is correct
53 Correct 40 ms 18012 KB Output is correct
54 Correct 38 ms 17788 KB Output is correct
55 Correct 48 ms 17800 KB Output is correct
56 Correct 38 ms 18012 KB Output is correct
57 Correct 38 ms 18012 KB Output is correct
58 Correct 38 ms 18012 KB Output is correct
59 Correct 39 ms 18012 KB Output is correct
60 Correct 44 ms 17920 KB Output is correct
61 Correct 38 ms 18012 KB Output is correct
62 Correct 37 ms 18012 KB Output is correct
63 Correct 39 ms 17920 KB Output is correct
64 Correct 37 ms 18008 KB Output is correct
65 Correct 41 ms 18012 KB Output is correct
66 Correct 38 ms 18012 KB Output is correct
67 Correct 42 ms 18004 KB Output is correct
68 Correct 37 ms 18008 KB Output is correct
69 Correct 39 ms 18004 KB Output is correct
70 Correct 39 ms 18012 KB Output is correct
71 Correct 10 ms 12124 KB Output is correct
72 Correct 13 ms 12120 KB Output is correct
73 Correct 11 ms 12120 KB Output is correct
74 Correct 11 ms 12124 KB Output is correct
75 Correct 11 ms 12192 KB Output is correct
76 Correct 16 ms 13272 KB Output is correct
77 Correct 16 ms 13276 KB Output is correct
78 Correct 16 ms 13420 KB Output is correct
79 Correct 16 ms 13400 KB Output is correct
80 Correct 17 ms 13404 KB Output is correct
81 Correct 28 ms 15732 KB Output is correct
82 Correct 27 ms 15708 KB Output is correct
83 Correct 32 ms 15744 KB Output is correct
84 Correct 30 ms 15708 KB Output is correct
85 Correct 27 ms 15708 KB Output is correct
86 Correct 30 ms 15964 KB Output is correct
87 Correct 30 ms 16004 KB Output is correct
88 Correct 29 ms 15964 KB Output is correct
89 Correct 29 ms 15964 KB Output is correct
90 Correct 29 ms 15964 KB Output is correct
91 Correct 44 ms 18520 KB Output is correct
92 Correct 48 ms 18524 KB Output is correct
93 Correct 44 ms 18524 KB Output is correct
94 Correct 46 ms 18520 KB Output is correct
95 Correct 43 ms 18524 KB Output is correct
96 Correct 44 ms 18568 KB Output is correct
97 Correct 44 ms 18768 KB Output is correct
98 Correct 51 ms 18768 KB Output is correct
99 Correct 49 ms 18516 KB Output is correct
100 Correct 45 ms 18780 KB Output is correct
101 Correct 45 ms 18780 KB Output is correct
102 Correct 45 ms 18780 KB Output is correct
103 Correct 47 ms 18772 KB Output is correct
104 Correct 48 ms 18560 KB Output is correct
105 Correct 45 ms 18532 KB Output is correct
106 Correct 46 ms 18780 KB Output is correct
107 Correct 46 ms 18780 KB Output is correct
108 Correct 44 ms 18772 KB Output is correct
109 Correct 44 ms 18776 KB Output is correct
110 Correct 50 ms 18768 KB Output is correct
111 Correct 46 ms 18772 KB Output is correct
112 Correct 52 ms 18692 KB Output is correct
113 Correct 44 ms 18776 KB Output is correct
114 Correct 48 ms 18772 KB Output is correct
115 Correct 53 ms 18808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10840 KB Output is correct
2 Correct 6 ms 10844 KB Output is correct
3 Correct 5 ms 10844 KB Output is correct
4 Correct 6 ms 10844 KB Output is correct
5 Correct 5 ms 10840 KB Output is correct
6 Correct 5 ms 10844 KB Output is correct
7 Correct 5 ms 10996 KB Output is correct
8 Correct 5 ms 10844 KB Output is correct
9 Correct 5 ms 10868 KB Output is correct
10 Correct 5 ms 10844 KB Output is correct
11 Correct 6 ms 10840 KB Output is correct
12 Correct 5 ms 10900 KB Output is correct
13 Correct 6 ms 11100 KB Output is correct
14 Correct 14 ms 13176 KB Output is correct
15 Correct 22 ms 15196 KB Output is correct
16 Correct 38 ms 17540 KB Output is correct
17 Correct 35 ms 17500 KB Output is correct
18 Correct 34 ms 17496 KB Output is correct
19 Correct 34 ms 17496 KB Output is correct
20 Correct 36 ms 17488 KB Output is correct
21 Correct 37 ms 17500 KB Output is correct
22 Correct 34 ms 17328 KB Output is correct
23 Correct 34 ms 17500 KB Output is correct
24 Correct 38 ms 17516 KB Output is correct
25 Correct 36 ms 17480 KB Output is correct
26 Correct 5 ms 10844 KB Output is correct
27 Correct 5 ms 11044 KB Output is correct
28 Correct 6 ms 11100 KB Output is correct
29 Correct 14 ms 13264 KB Output is correct
30 Correct 24 ms 15452 KB Output is correct
31 Correct 41 ms 17536 KB Output is correct
32 Correct 34 ms 17500 KB Output is correct
33 Correct 36 ms 17496 KB Output is correct
34 Correct 34 ms 17496 KB Output is correct
35 Correct 36 ms 17492 KB Output is correct
36 Correct 37 ms 17496 KB Output is correct
37 Correct 37 ms 17652 KB Output is correct
38 Correct 40 ms 17500 KB Output is correct
39 Correct 36 ms 17372 KB Output is correct
40 Correct 38 ms 17500 KB Output is correct
41 Correct 5 ms 10844 KB Output is correct
42 Correct 6 ms 10844 KB Output is correct
43 Correct 6 ms 10956 KB Output is correct
44 Correct 6 ms 11100 KB Output is correct
45 Correct 7 ms 11140 KB Output is correct
46 Correct 6 ms 11100 KB Output is correct
47 Correct 9 ms 12124 KB Output is correct
48 Correct 9 ms 12092 KB Output is correct
49 Correct 15 ms 13148 KB Output is correct
50 Correct 26 ms 15708 KB Output is correct
51 Correct 38 ms 17928 KB Output is correct
52 Correct 41 ms 18012 KB Output is correct
53 Correct 40 ms 18012 KB Output is correct
54 Correct 38 ms 17788 KB Output is correct
55 Correct 48 ms 17800 KB Output is correct
56 Correct 38 ms 18012 KB Output is correct
57 Correct 38 ms 18012 KB Output is correct
58 Correct 38 ms 18012 KB Output is correct
59 Correct 39 ms 18012 KB Output is correct
60 Correct 44 ms 17920 KB Output is correct
61 Correct 38 ms 18012 KB Output is correct
62 Correct 37 ms 18012 KB Output is correct
63 Correct 39 ms 17920 KB Output is correct
64 Correct 37 ms 18008 KB Output is correct
65 Correct 41 ms 18012 KB Output is correct
66 Correct 38 ms 18012 KB Output is correct
67 Correct 42 ms 18004 KB Output is correct
68 Correct 37 ms 18008 KB Output is correct
69 Correct 39 ms 18004 KB Output is correct
70 Correct 39 ms 18012 KB Output is correct
71 Correct 10 ms 12124 KB Output is correct
72 Correct 13 ms 12120 KB Output is correct
73 Correct 11 ms 12120 KB Output is correct
74 Correct 11 ms 12124 KB Output is correct
75 Correct 11 ms 12192 KB Output is correct
76 Correct 16 ms 13272 KB Output is correct
77 Correct 16 ms 13276 KB Output is correct
78 Correct 16 ms 13420 KB Output is correct
79 Correct 16 ms 13400 KB Output is correct
80 Correct 17 ms 13404 KB Output is correct
81 Correct 28 ms 15732 KB Output is correct
82 Correct 27 ms 15708 KB Output is correct
83 Correct 32 ms 15744 KB Output is correct
84 Correct 30 ms 15708 KB Output is correct
85 Correct 27 ms 15708 KB Output is correct
86 Correct 30 ms 15964 KB Output is correct
87 Correct 30 ms 16004 KB Output is correct
88 Correct 29 ms 15964 KB Output is correct
89 Correct 29 ms 15964 KB Output is correct
90 Correct 29 ms 15964 KB Output is correct
91 Correct 44 ms 18520 KB Output is correct
92 Correct 48 ms 18524 KB Output is correct
93 Correct 44 ms 18524 KB Output is correct
94 Correct 46 ms 18520 KB Output is correct
95 Correct 43 ms 18524 KB Output is correct
96 Correct 44 ms 18568 KB Output is correct
97 Correct 44 ms 18768 KB Output is correct
98 Correct 51 ms 18768 KB Output is correct
99 Correct 49 ms 18516 KB Output is correct
100 Correct 45 ms 18780 KB Output is correct
101 Correct 45 ms 18780 KB Output is correct
102 Correct 45 ms 18780 KB Output is correct
103 Correct 47 ms 18772 KB Output is correct
104 Correct 48 ms 18560 KB Output is correct
105 Correct 45 ms 18532 KB Output is correct
106 Correct 46 ms 18780 KB Output is correct
107 Correct 46 ms 18780 KB Output is correct
108 Correct 44 ms 18772 KB Output is correct
109 Correct 44 ms 18776 KB Output is correct
110 Correct 50 ms 18768 KB Output is correct
111 Correct 46 ms 18772 KB Output is correct
112 Correct 52 ms 18692 KB Output is correct
113 Correct 44 ms 18776 KB Output is correct
114 Correct 48 ms 18772 KB Output is correct
115 Correct 53 ms 18808 KB Output is correct
116 Runtime error 25 ms 25000 KB Execution killed with signal 11
117 Halted 0 ms 0 KB -