답안 #1087321

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1087321 2024-09-12T13:33:07 Z vladilius Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 16720 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{
    vector<ll> bit, a;
    int n;
    FT(int ns){
        n = ns;
        bit.resize(n + 1);
        a.resize(n + 1);
    }
    void upd(int v, ll 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 = 2000;
    auto cmp = [&](arr3 x, arr3 y){
        if (x[0] / S == y[0] / S){
            return x[1] < y[1];
        }
        return x[0] < y[0];
    };
    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;
    vector<int> f(n + 1), cnt(A + 1);
    vector<int> :: iterator it;
    FT T1(n), T2(n);
    auto add = [&](int x){
        x = a[x];
        it = upper_bound(f.begin() + 1, f.end(), cnt[x]); it--;
        int j1 = (int) (it - f.begin()), j = p[j1];
        
        sum += (f[j1] + 1);
        sum += (1LL * j * T1.get(1, j - 1));
        sum -= (T2.get(1, j - 1) - T1.get(1, j - 1));
        sum += T2.get(j + 1, n);
        sum -= 1LL * (j - 1) * T1.get(j + 1, n);
        
        cnt[x]++; f[j1]++;
        T1.upd(j, 1);
        T2.upd(j, j);
    };

    auto rem = [&](int x){
        x = a[x];
        it = lower_bound(f.begin() + 1, f.end(), cnt[x]);
        int j1 = (int) (it - f.begin()), j = p[j1];
        
        sum -= f[j1];
        sum -= (1LL * j * T1.get(1, j - 1));
        sum += (T2.get(1, j - 1) - T1.get(1, j - 1));
        sum -= T2.get(j + 1, n);
        sum += 1LL * (j - 1) * T1.get(j + 1, n);
        
        cnt[x]--; f[j1]--;
        T1.upd(j, -1);
        T2.upd(j, -j);
    };
    
    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";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1628 KB Output is correct
3 Correct 1 ms 1628 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 1 ms 1628 KB Output is correct
6 Correct 1 ms 1628 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 1 ms 1628 KB Output is correct
9 Correct 1 ms 1628 KB Output is correct
10 Correct 1 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1636 KB Output is correct
3 Correct 2 ms 1884 KB Output is correct
4 Correct 16 ms 6160 KB Output is correct
5 Correct 35 ms 10588 KB Output is correct
6 Correct 57 ms 15196 KB Output is correct
7 Correct 57 ms 14936 KB Output is correct
8 Correct 56 ms 14940 KB Output is correct
9 Correct 58 ms 15072 KB Output is correct
10 Correct 58 ms 15196 KB Output is correct
11 Correct 56 ms 15192 KB Output is correct
12 Correct 65 ms 15196 KB Output is correct
13 Correct 62 ms 15192 KB Output is correct
14 Correct 58 ms 14936 KB Output is correct
15 Correct 55 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1636 KB Output is correct
3 Correct 2 ms 1884 KB Output is correct
4 Correct 16 ms 6160 KB Output is correct
5 Correct 35 ms 10588 KB Output is correct
6 Correct 57 ms 15196 KB Output is correct
7 Correct 57 ms 14936 KB Output is correct
8 Correct 56 ms 14940 KB Output is correct
9 Correct 58 ms 15072 KB Output is correct
10 Correct 58 ms 15196 KB Output is correct
11 Correct 56 ms 15192 KB Output is correct
12 Correct 65 ms 15196 KB Output is correct
13 Correct 62 ms 15192 KB Output is correct
14 Correct 58 ms 14936 KB Output is correct
15 Correct 55 ms 14940 KB Output is correct
16 Correct 1 ms 1624 KB Output is correct
17 Correct 1 ms 1628 KB Output is correct
18 Correct 2 ms 1884 KB Output is correct
19 Correct 16 ms 6240 KB Output is correct
20 Correct 34 ms 10584 KB Output is correct
21 Correct 57 ms 15448 KB Output is correct
22 Correct 58 ms 15196 KB Output is correct
23 Correct 55 ms 15188 KB Output is correct
24 Correct 60 ms 15184 KB Output is correct
25 Correct 59 ms 15184 KB Output is correct
26 Correct 64 ms 15184 KB Output is correct
27 Correct 59 ms 15196 KB Output is correct
28 Correct 58 ms 15192 KB Output is correct
29 Correct 55 ms 15196 KB Output is correct
30 Correct 56 ms 15340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1636 KB Output is correct
3 Correct 2 ms 1884 KB Output is correct
4 Correct 16 ms 6160 KB Output is correct
5 Correct 35 ms 10588 KB Output is correct
6 Correct 57 ms 15196 KB Output is correct
7 Correct 57 ms 14936 KB Output is correct
8 Correct 56 ms 14940 KB Output is correct
9 Correct 58 ms 15072 KB Output is correct
10 Correct 58 ms 15196 KB Output is correct
11 Correct 56 ms 15192 KB Output is correct
12 Correct 65 ms 15196 KB Output is correct
13 Correct 62 ms 15192 KB Output is correct
14 Correct 58 ms 14936 KB Output is correct
15 Correct 55 ms 14940 KB Output is correct
16 Correct 1 ms 1624 KB Output is correct
17 Correct 1 ms 1628 KB Output is correct
18 Correct 2 ms 1884 KB Output is correct
19 Correct 16 ms 6240 KB Output is correct
20 Correct 34 ms 10584 KB Output is correct
21 Correct 57 ms 15448 KB Output is correct
22 Correct 58 ms 15196 KB Output is correct
23 Correct 55 ms 15188 KB Output is correct
24 Correct 60 ms 15184 KB Output is correct
25 Correct 59 ms 15184 KB Output is correct
26 Correct 64 ms 15184 KB Output is correct
27 Correct 59 ms 15196 KB Output is correct
28 Correct 58 ms 15192 KB Output is correct
29 Correct 55 ms 15196 KB Output is correct
30 Correct 56 ms 15340 KB Output is correct
31 Correct 1 ms 1628 KB Output is correct
32 Correct 1 ms 1628 KB Output is correct
33 Correct 1 ms 1628 KB Output is correct
34 Correct 2 ms 2068 KB Output is correct
35 Correct 2 ms 1884 KB Output is correct
36 Correct 2 ms 1884 KB Output is correct
37 Correct 9 ms 3932 KB Output is correct
38 Correct 9 ms 3996 KB Output is correct
39 Correct 19 ms 6236 KB Output is correct
40 Correct 38 ms 10840 KB Output is correct
41 Correct 62 ms 15448 KB Output is correct
42 Correct 58 ms 15704 KB Output is correct
43 Correct 61 ms 15668 KB Output is correct
44 Correct 63 ms 15700 KB Output is correct
45 Correct 60 ms 15452 KB Output is correct
46 Correct 64 ms 15680 KB Output is correct
47 Correct 59 ms 15452 KB Output is correct
48 Correct 57 ms 15452 KB Output is correct
49 Correct 64 ms 15568 KB Output is correct
50 Correct 58 ms 15684 KB Output is correct
51 Correct 56 ms 15680 KB Output is correct
52 Correct 56 ms 15452 KB Output is correct
53 Correct 59 ms 15452 KB Output is correct
54 Correct 68 ms 15684 KB Output is correct
55 Correct 65 ms 15440 KB Output is correct
56 Correct 58 ms 15452 KB Output is correct
57 Correct 58 ms 15448 KB Output is correct
58 Correct 58 ms 15676 KB Output is correct
59 Correct 57 ms 15708 KB Output is correct
60 Correct 58 ms 15540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1628 KB Output is correct
3 Correct 1 ms 1628 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 1 ms 1628 KB Output is correct
6 Correct 1 ms 1628 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 1 ms 1628 KB Output is correct
9 Correct 1 ms 1628 KB Output is correct
10 Correct 1 ms 1628 KB Output is correct
11 Correct 1 ms 1628 KB Output is correct
12 Correct 1 ms 1636 KB Output is correct
13 Correct 2 ms 1884 KB Output is correct
14 Correct 16 ms 6160 KB Output is correct
15 Correct 35 ms 10588 KB Output is correct
16 Correct 57 ms 15196 KB Output is correct
17 Correct 57 ms 14936 KB Output is correct
18 Correct 56 ms 14940 KB Output is correct
19 Correct 58 ms 15072 KB Output is correct
20 Correct 58 ms 15196 KB Output is correct
21 Correct 56 ms 15192 KB Output is correct
22 Correct 65 ms 15196 KB Output is correct
23 Correct 62 ms 15192 KB Output is correct
24 Correct 58 ms 14936 KB Output is correct
25 Correct 55 ms 14940 KB Output is correct
26 Correct 1 ms 1624 KB Output is correct
27 Correct 1 ms 1628 KB Output is correct
28 Correct 2 ms 1884 KB Output is correct
29 Correct 16 ms 6240 KB Output is correct
30 Correct 34 ms 10584 KB Output is correct
31 Correct 57 ms 15448 KB Output is correct
32 Correct 58 ms 15196 KB Output is correct
33 Correct 55 ms 15188 KB Output is correct
34 Correct 60 ms 15184 KB Output is correct
35 Correct 59 ms 15184 KB Output is correct
36 Correct 64 ms 15184 KB Output is correct
37 Correct 59 ms 15196 KB Output is correct
38 Correct 58 ms 15192 KB Output is correct
39 Correct 55 ms 15196 KB Output is correct
40 Correct 56 ms 15340 KB Output is correct
41 Correct 1 ms 1628 KB Output is correct
42 Correct 1 ms 1628 KB Output is correct
43 Correct 1 ms 1628 KB Output is correct
44 Correct 2 ms 2068 KB Output is correct
45 Correct 2 ms 1884 KB Output is correct
46 Correct 2 ms 1884 KB Output is correct
47 Correct 9 ms 3932 KB Output is correct
48 Correct 9 ms 3996 KB Output is correct
49 Correct 19 ms 6236 KB Output is correct
50 Correct 38 ms 10840 KB Output is correct
51 Correct 62 ms 15448 KB Output is correct
52 Correct 58 ms 15704 KB Output is correct
53 Correct 61 ms 15668 KB Output is correct
54 Correct 63 ms 15700 KB Output is correct
55 Correct 60 ms 15452 KB Output is correct
56 Correct 64 ms 15680 KB Output is correct
57 Correct 59 ms 15452 KB Output is correct
58 Correct 57 ms 15452 KB Output is correct
59 Correct 64 ms 15568 KB Output is correct
60 Correct 58 ms 15684 KB Output is correct
61 Correct 56 ms 15680 KB Output is correct
62 Correct 56 ms 15452 KB Output is correct
63 Correct 59 ms 15452 KB Output is correct
64 Correct 68 ms 15684 KB Output is correct
65 Correct 65 ms 15440 KB Output is correct
66 Correct 58 ms 15452 KB Output is correct
67 Correct 58 ms 15448 KB Output is correct
68 Correct 58 ms 15676 KB Output is correct
69 Correct 57 ms 15708 KB Output is correct
70 Correct 58 ms 15540 KB Output is correct
71 Correct 10 ms 3932 KB Output is correct
72 Correct 11 ms 4020 KB Output is correct
73 Correct 10 ms 3932 KB Output is correct
74 Correct 10 ms 4024 KB Output is correct
75 Correct 13 ms 4012 KB Output is correct
76 Correct 21 ms 6236 KB Output is correct
77 Correct 20 ms 6428 KB Output is correct
78 Correct 20 ms 6352 KB Output is correct
79 Correct 21 ms 6236 KB Output is correct
80 Correct 21 ms 6428 KB Output is correct
81 Correct 45 ms 11100 KB Output is correct
82 Correct 44 ms 11096 KB Output is correct
83 Correct 43 ms 11100 KB Output is correct
84 Correct 42 ms 11096 KB Output is correct
85 Correct 44 ms 11204 KB Output is correct
86 Correct 44 ms 11100 KB Output is correct
87 Correct 45 ms 11100 KB Output is correct
88 Correct 46 ms 11096 KB Output is correct
89 Correct 45 ms 11100 KB Output is correct
90 Correct 44 ms 11100 KB Output is correct
91 Correct 66 ms 16204 KB Output is correct
92 Correct 66 ms 16264 KB Output is correct
93 Correct 67 ms 16208 KB Output is correct
94 Correct 70 ms 16280 KB Output is correct
95 Correct 68 ms 16100 KB Output is correct
96 Correct 68 ms 16220 KB Output is correct
97 Correct 72 ms 16216 KB Output is correct
98 Correct 68 ms 16212 KB Output is correct
99 Correct 70 ms 16212 KB Output is correct
100 Correct 68 ms 16208 KB Output is correct
101 Correct 70 ms 16208 KB Output is correct
102 Correct 71 ms 16220 KB Output is correct
103 Correct 67 ms 16428 KB Output is correct
104 Correct 68 ms 16216 KB Output is correct
105 Correct 68 ms 16208 KB Output is correct
106 Correct 70 ms 16472 KB Output is correct
107 Correct 67 ms 16456 KB Output is correct
108 Correct 73 ms 16720 KB Output is correct
109 Correct 66 ms 16216 KB Output is correct
110 Correct 65 ms 16472 KB Output is correct
111 Correct 66 ms 16476 KB Output is correct
112 Correct 67 ms 16212 KB Output is correct
113 Correct 67 ms 16212 KB Output is correct
114 Correct 66 ms 16456 KB Output is correct
115 Correct 67 ms 16348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1628 KB Output is correct
3 Correct 1 ms 1628 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 1 ms 1628 KB Output is correct
6 Correct 1 ms 1628 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 1 ms 1628 KB Output is correct
9 Correct 1 ms 1628 KB Output is correct
10 Correct 1 ms 1628 KB Output is correct
11 Correct 1 ms 1628 KB Output is correct
12 Correct 1 ms 1636 KB Output is correct
13 Correct 2 ms 1884 KB Output is correct
14 Correct 16 ms 6160 KB Output is correct
15 Correct 35 ms 10588 KB Output is correct
16 Correct 57 ms 15196 KB Output is correct
17 Correct 57 ms 14936 KB Output is correct
18 Correct 56 ms 14940 KB Output is correct
19 Correct 58 ms 15072 KB Output is correct
20 Correct 58 ms 15196 KB Output is correct
21 Correct 56 ms 15192 KB Output is correct
22 Correct 65 ms 15196 KB Output is correct
23 Correct 62 ms 15192 KB Output is correct
24 Correct 58 ms 14936 KB Output is correct
25 Correct 55 ms 14940 KB Output is correct
26 Correct 1 ms 1624 KB Output is correct
27 Correct 1 ms 1628 KB Output is correct
28 Correct 2 ms 1884 KB Output is correct
29 Correct 16 ms 6240 KB Output is correct
30 Correct 34 ms 10584 KB Output is correct
31 Correct 57 ms 15448 KB Output is correct
32 Correct 58 ms 15196 KB Output is correct
33 Correct 55 ms 15188 KB Output is correct
34 Correct 60 ms 15184 KB Output is correct
35 Correct 59 ms 15184 KB Output is correct
36 Correct 64 ms 15184 KB Output is correct
37 Correct 59 ms 15196 KB Output is correct
38 Correct 58 ms 15192 KB Output is correct
39 Correct 55 ms 15196 KB Output is correct
40 Correct 56 ms 15340 KB Output is correct
41 Correct 1 ms 1628 KB Output is correct
42 Correct 1 ms 1628 KB Output is correct
43 Correct 1 ms 1628 KB Output is correct
44 Correct 2 ms 2068 KB Output is correct
45 Correct 2 ms 1884 KB Output is correct
46 Correct 2 ms 1884 KB Output is correct
47 Correct 9 ms 3932 KB Output is correct
48 Correct 9 ms 3996 KB Output is correct
49 Correct 19 ms 6236 KB Output is correct
50 Correct 38 ms 10840 KB Output is correct
51 Correct 62 ms 15448 KB Output is correct
52 Correct 58 ms 15704 KB Output is correct
53 Correct 61 ms 15668 KB Output is correct
54 Correct 63 ms 15700 KB Output is correct
55 Correct 60 ms 15452 KB Output is correct
56 Correct 64 ms 15680 KB Output is correct
57 Correct 59 ms 15452 KB Output is correct
58 Correct 57 ms 15452 KB Output is correct
59 Correct 64 ms 15568 KB Output is correct
60 Correct 58 ms 15684 KB Output is correct
61 Correct 56 ms 15680 KB Output is correct
62 Correct 56 ms 15452 KB Output is correct
63 Correct 59 ms 15452 KB Output is correct
64 Correct 68 ms 15684 KB Output is correct
65 Correct 65 ms 15440 KB Output is correct
66 Correct 58 ms 15452 KB Output is correct
67 Correct 58 ms 15448 KB Output is correct
68 Correct 58 ms 15676 KB Output is correct
69 Correct 57 ms 15708 KB Output is correct
70 Correct 58 ms 15540 KB Output is correct
71 Correct 10 ms 3932 KB Output is correct
72 Correct 11 ms 4020 KB Output is correct
73 Correct 10 ms 3932 KB Output is correct
74 Correct 10 ms 4024 KB Output is correct
75 Correct 13 ms 4012 KB Output is correct
76 Correct 21 ms 6236 KB Output is correct
77 Correct 20 ms 6428 KB Output is correct
78 Correct 20 ms 6352 KB Output is correct
79 Correct 21 ms 6236 KB Output is correct
80 Correct 21 ms 6428 KB Output is correct
81 Correct 45 ms 11100 KB Output is correct
82 Correct 44 ms 11096 KB Output is correct
83 Correct 43 ms 11100 KB Output is correct
84 Correct 42 ms 11096 KB Output is correct
85 Correct 44 ms 11204 KB Output is correct
86 Correct 44 ms 11100 KB Output is correct
87 Correct 45 ms 11100 KB Output is correct
88 Correct 46 ms 11096 KB Output is correct
89 Correct 45 ms 11100 KB Output is correct
90 Correct 44 ms 11100 KB Output is correct
91 Correct 66 ms 16204 KB Output is correct
92 Correct 66 ms 16264 KB Output is correct
93 Correct 67 ms 16208 KB Output is correct
94 Correct 70 ms 16280 KB Output is correct
95 Correct 68 ms 16100 KB Output is correct
96 Correct 68 ms 16220 KB Output is correct
97 Correct 72 ms 16216 KB Output is correct
98 Correct 68 ms 16212 KB Output is correct
99 Correct 70 ms 16212 KB Output is correct
100 Correct 68 ms 16208 KB Output is correct
101 Correct 70 ms 16208 KB Output is correct
102 Correct 71 ms 16220 KB Output is correct
103 Correct 67 ms 16428 KB Output is correct
104 Correct 68 ms 16216 KB Output is correct
105 Correct 68 ms 16208 KB Output is correct
106 Correct 70 ms 16472 KB Output is correct
107 Correct 67 ms 16456 KB Output is correct
108 Correct 73 ms 16720 KB Output is correct
109 Correct 66 ms 16216 KB Output is correct
110 Correct 65 ms 16472 KB Output is correct
111 Correct 66 ms 16476 KB Output is correct
112 Correct 67 ms 16212 KB Output is correct
113 Correct 67 ms 16212 KB Output is correct
114 Correct 66 ms 16456 KB Output is correct
115 Correct 67 ms 16348 KB Output is correct
116 Correct 2338 ms 3792 KB Output is correct
117 Correct 2473 ms 3796 KB Output is correct
118 Correct 3145 ms 5832 KB Output is correct
119 Correct 3628 ms 5836 KB Output is correct
120 Correct 3924 ms 6032 KB Output is correct
121 Correct 4793 ms 8520 KB Output is correct
122 Correct 4761 ms 8576 KB Output is correct
123 Correct 6976 ms 13340 KB Output is correct
124 Execution timed out 7028 ms 12744 KB Time limit exceeded
125 Halted 0 ms 0 KB -