Submission #697525

# Submission time Handle Problem Language Result Execution time Memory
697525 2023-02-10T09:00:13 Z piOOE Diversity (CEOI21_diversity) C++17
64 / 100
477 ms 72520 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct SegmentTree {
    vector<ll> t;
    vector<ll> tag;

    int sz;

    void init(vector<int> a) {
        int n = a.size();
        sz = 1 << __lg(n) + !!(n & (n - 1));
        t.resize(sz << 1), tag.resize(sz << 1);

        for (int i = 0; i < n; ++i) {
            t[i + sz] = a[i];
        }
        for (int i = sz - 1; i > 0; --i) {
            t[i] = t[i << 1] + t[i << 1 | 1];
        }
    }

    void rangeAdd(int l, int r, int v, int x, int lx, int rx) {
        if (l >= rx || lx >= r) {
            return;
        }

        if (l <= lx && rx <= r) {
            tag[x] += v;
            t[x] += 1LL * v * (rx - lx);
            return;
        }

        int mid = (lx + rx) / 2;

        rangeAdd(l, r, v, x << 1, lx, mid), rangeAdd(l, r, v, x << 1 | 1, mid, rx);

        t[x] = t[x << 1] + t[x << 1 | 1] + (rx - lx) * tag[x];
    }

    void rangeAdd(int l, int r, int v) {
        rangeAdd(l, r, v, 1, 0, sz);
    }

    ll rangeSum(int l, int r, int x, int lx, int rx) {
        if (l >= rx || lx >= r) {
            return 0;
        }

        if (l <= lx && rx <= r) {
            return t[x];
        }

        int mid = (lx + rx) / 2;

        return rangeSum(l, r, x << 1, lx, mid) + rangeSum(l, r, x << 1 | 1, mid, rx) +
               (min(rx, r) - max(lx, l)) * tag[x];
    }

    ll rangeSum(int l, int r) {
        return rangeSum(l, r, 1, 0, sz);
    }
} t[2][2]; // (left/right), (L/R)

struct Query {
    int l, r, block, idx;

    bool operator<(Query oth) const {
        if (oth.block != block) {
            return block < oth.block;
        }

        return (block & 1) ? r > oth.r : r < oth.r;
    }
};

constexpr int B = 500;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q;
    cin >> n >> q;

    vector<int> a(n);

    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    vector<int> yy = a;
    sort(yy.begin(), yy.end());
    yy.resize(unique(yy.begin(), yy.end()) - yy.begin());

    for (int &x: a) {
        x = lower_bound(yy.begin(), yy.end(), x) - yy.begin();
    }

    const int m = yy.size();

    vector<Query> queries(q);
    vector<ll> answers(q);

    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        l -= 1;

        queries[i].l = l, queries[i].r = r, queries[i].block = l / B, queries[i].idx = i;
    }

    sort(queries.begin(), queries.end());

    vector<int> head(n + 1);

    for (int i = 1; i <= n; ++i) {
        head[i] = m;
    }

    t[0][0].init(vector(n, 0));
    t[0][1].init(vector(n, 0));
    t[1][0].init(vector(n, 0));
    t[1][1].init(vector(n, 0));

    vector<int> pos(m), cnt(m), ord(m);
    iota(pos.begin(), pos.end(), 0);
    ord = pos;

    int siz[2]{(m + 1) / 2, m / 2};

    ll ans = 0;

    int len = 0;

    auto eval = [&](int i) {
        return 1LL * cnt[i] * (len - cnt[i]) + 1LL * (cnt[i] + 1) * cnt[i] / 2;
    };

    auto increase = [&](int i) {
        ans += len;

        len += 1;

        ans -= eval(i);


        int p = pos[i];
        int tail = head[cnt[i] + 1] - 1;

        if (p != tail) {
            int id = ord[tail];
            pos[id] = p;
            ord[p] = id;
            pos[i] = tail;
            ord[tail] = i;
            p = tail;
        }

        int side = p % 2;
        int s = p / 2;

        if (side) {
            ans += t[side][0].rangeSum(s + 1, siz[side]);
            t[side][1].rangeAdd(s + 1, siz[side], 1);

            t[side][0].rangeAdd(0, s, 1);
            ans += t[side][1].rangeSum(0, s);

            ans += t[side ^ 1][0].rangeSum(0, siz[side ^ 1]);
            t[side ^ 1][1].rangeAdd(0, siz[side ^ 1], 1);
        } else {
            ans += t[side][1].rangeSum(s + 1, siz[side]);
            t[side][0].rangeAdd(s + 1, siz[side], 1);

            t[side][1].rangeAdd(0, s, 1);
            ans += t[side][0].rangeSum(0, s);

//            cout << t[side][1].rangeSum(s, s + 1) << endl;

            ans += t[side ^ 1][1].rangeSum(0, siz[side ^ 1]);
            t[side ^ 1][0].rangeAdd(0, siz[side ^ 1], 1);
        }

//        for (int j = 0; j < m; ++j) {
//            cout << j << ": " << t[pos[j] & 1][0].rangeSum(pos[j] / 2, pos[j] / 2 + 1) << " "
//                 << t[pos[j] & 1][1].rangeSum(pos[j] / 2, pos[j] / 2 + 1) << endl;
//        }
//        cout << endl;

        cnt[i] += 1;
        head[cnt[i]] = p;

        ans += eval(i);
    };

    for (int i = 0; i < n; ++i) {
        increase(a[i]);
    }

    cout << ans << '\n';

    return 0;
}

Compilation message

diversity.cpp: In member function 'void SegmentTree::init(std::vector<int>)':
diversity.cpp:14:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   14 |         sz = 1 << __lg(n) + !!(n & (n - 1));
      |                   ~~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 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 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 8 ms 2516 KB Output is correct
4 Correct 104 ms 18328 KB Output is correct
5 Correct 178 ms 36312 KB Output is correct
6 Correct 312 ms 70712 KB Output is correct
7 Correct 283 ms 70816 KB Output is correct
8 Correct 315 ms 70580 KB Output is correct
9 Correct 301 ms 70704 KB Output is correct
10 Correct 315 ms 70604 KB Output is correct
11 Correct 303 ms 70716 KB Output is correct
12 Correct 318 ms 70712 KB Output is correct
13 Correct 329 ms 70708 KB Output is correct
14 Correct 332 ms 70704 KB Output is correct
15 Correct 303 ms 70596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 8 ms 2516 KB Output is correct
4 Correct 104 ms 18328 KB Output is correct
5 Correct 178 ms 36312 KB Output is correct
6 Correct 312 ms 70712 KB Output is correct
7 Correct 283 ms 70816 KB Output is correct
8 Correct 315 ms 70580 KB Output is correct
9 Correct 301 ms 70704 KB Output is correct
10 Correct 315 ms 70604 KB Output is correct
11 Correct 303 ms 70716 KB Output is correct
12 Correct 318 ms 70712 KB Output is correct
13 Correct 329 ms 70708 KB Output is correct
14 Correct 332 ms 70704 KB Output is correct
15 Correct 303 ms 70596 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 8 ms 2516 KB Output is correct
19 Correct 89 ms 18324 KB Output is correct
20 Correct 189 ms 36412 KB Output is correct
21 Correct 332 ms 70712 KB Output is correct
22 Correct 318 ms 70712 KB Output is correct
23 Correct 324 ms 70788 KB Output is correct
24 Correct 319 ms 70708 KB Output is correct
25 Correct 310 ms 70704 KB Output is correct
26 Correct 313 ms 70716 KB Output is correct
27 Correct 322 ms 70708 KB Output is correct
28 Correct 308 ms 70652 KB Output is correct
29 Correct 331 ms 70708 KB Output is correct
30 Correct 307 ms 70596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 8 ms 2516 KB Output is correct
4 Correct 104 ms 18328 KB Output is correct
5 Correct 178 ms 36312 KB Output is correct
6 Correct 312 ms 70712 KB Output is correct
7 Correct 283 ms 70816 KB Output is correct
8 Correct 315 ms 70580 KB Output is correct
9 Correct 301 ms 70704 KB Output is correct
10 Correct 315 ms 70604 KB Output is correct
11 Correct 303 ms 70716 KB Output is correct
12 Correct 318 ms 70712 KB Output is correct
13 Correct 329 ms 70708 KB Output is correct
14 Correct 332 ms 70704 KB Output is correct
15 Correct 303 ms 70596 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 8 ms 2516 KB Output is correct
19 Correct 89 ms 18324 KB Output is correct
20 Correct 189 ms 36412 KB Output is correct
21 Correct 332 ms 70712 KB Output is correct
22 Correct 318 ms 70712 KB Output is correct
23 Correct 324 ms 70788 KB Output is correct
24 Correct 319 ms 70708 KB Output is correct
25 Correct 310 ms 70704 KB Output is correct
26 Correct 313 ms 70716 KB Output is correct
27 Correct 322 ms 70708 KB Output is correct
28 Correct 308 ms 70652 KB Output is correct
29 Correct 331 ms 70708 KB Output is correct
30 Correct 307 ms 70596 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 11 ms 2516 KB Output is correct
35 Correct 15 ms 2516 KB Output is correct
36 Correct 13 ms 2516 KB Output is correct
37 Correct 51 ms 9352 KB Output is correct
38 Correct 57 ms 9340 KB Output is correct
39 Correct 106 ms 18236 KB Output is correct
40 Correct 245 ms 36300 KB Output is correct
41 Correct 368 ms 70704 KB Output is correct
42 Correct 376 ms 70692 KB Output is correct
43 Correct 368 ms 70704 KB Output is correct
44 Correct 352 ms 70708 KB Output is correct
45 Correct 365 ms 70708 KB Output is correct
46 Correct 361 ms 70596 KB Output is correct
47 Correct 410 ms 70712 KB Output is correct
48 Correct 364 ms 70716 KB Output is correct
49 Correct 395 ms 70732 KB Output is correct
50 Correct 374 ms 70584 KB Output is correct
51 Correct 369 ms 70744 KB Output is correct
52 Correct 359 ms 70748 KB Output is correct
53 Correct 386 ms 70736 KB Output is correct
54 Correct 366 ms 70612 KB Output is correct
55 Correct 388 ms 70724 KB Output is correct
56 Correct 359 ms 70584 KB Output is correct
57 Correct 374 ms 70736 KB Output is correct
58 Correct 357 ms 70708 KB Output is correct
59 Correct 374 ms 70608 KB Output is correct
60 Correct 377 ms 70712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 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 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 8 ms 2516 KB Output is correct
14 Correct 104 ms 18328 KB Output is correct
15 Correct 178 ms 36312 KB Output is correct
16 Correct 312 ms 70712 KB Output is correct
17 Correct 283 ms 70816 KB Output is correct
18 Correct 315 ms 70580 KB Output is correct
19 Correct 301 ms 70704 KB Output is correct
20 Correct 315 ms 70604 KB Output is correct
21 Correct 303 ms 70716 KB Output is correct
22 Correct 318 ms 70712 KB Output is correct
23 Correct 329 ms 70708 KB Output is correct
24 Correct 332 ms 70704 KB Output is correct
25 Correct 303 ms 70596 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 8 ms 2516 KB Output is correct
29 Correct 89 ms 18324 KB Output is correct
30 Correct 189 ms 36412 KB Output is correct
31 Correct 332 ms 70712 KB Output is correct
32 Correct 318 ms 70712 KB Output is correct
33 Correct 324 ms 70788 KB Output is correct
34 Correct 319 ms 70708 KB Output is correct
35 Correct 310 ms 70704 KB Output is correct
36 Correct 313 ms 70716 KB Output is correct
37 Correct 322 ms 70708 KB Output is correct
38 Correct 308 ms 70652 KB Output is correct
39 Correct 331 ms 70708 KB Output is correct
40 Correct 307 ms 70596 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 11 ms 2516 KB Output is correct
45 Correct 15 ms 2516 KB Output is correct
46 Correct 13 ms 2516 KB Output is correct
47 Correct 51 ms 9352 KB Output is correct
48 Correct 57 ms 9340 KB Output is correct
49 Correct 106 ms 18236 KB Output is correct
50 Correct 245 ms 36300 KB Output is correct
51 Correct 368 ms 70704 KB Output is correct
52 Correct 376 ms 70692 KB Output is correct
53 Correct 368 ms 70704 KB Output is correct
54 Correct 352 ms 70708 KB Output is correct
55 Correct 365 ms 70708 KB Output is correct
56 Correct 361 ms 70596 KB Output is correct
57 Correct 410 ms 70712 KB Output is correct
58 Correct 364 ms 70716 KB Output is correct
59 Correct 395 ms 70732 KB Output is correct
60 Correct 374 ms 70584 KB Output is correct
61 Correct 369 ms 70744 KB Output is correct
62 Correct 359 ms 70748 KB Output is correct
63 Correct 386 ms 70736 KB Output is correct
64 Correct 366 ms 70612 KB Output is correct
65 Correct 388 ms 70724 KB Output is correct
66 Correct 359 ms 70584 KB Output is correct
67 Correct 374 ms 70736 KB Output is correct
68 Correct 357 ms 70708 KB Output is correct
69 Correct 374 ms 70608 KB Output is correct
70 Correct 377 ms 70712 KB Output is correct
71 Correct 53 ms 9292 KB Output is correct
72 Correct 62 ms 9332 KB Output is correct
73 Correct 53 ms 9296 KB Output is correct
74 Correct 53 ms 9344 KB Output is correct
75 Correct 52 ms 9360 KB Output is correct
76 Correct 116 ms 18232 KB Output is correct
77 Correct 119 ms 18324 KB Output is correct
78 Correct 119 ms 18260 KB Output is correct
79 Correct 123 ms 18332 KB Output is correct
80 Correct 118 ms 18328 KB Output is correct
81 Correct 263 ms 36288 KB Output is correct
82 Correct 264 ms 36320 KB Output is correct
83 Correct 263 ms 36392 KB Output is correct
84 Correct 248 ms 36296 KB Output is correct
85 Correct 263 ms 36280 KB Output is correct
86 Correct 273 ms 36320 KB Output is correct
87 Correct 296 ms 36348 KB Output is correct
88 Correct 269 ms 36300 KB Output is correct
89 Correct 262 ms 36284 KB Output is correct
90 Correct 290 ms 36320 KB Output is correct
91 Correct 436 ms 70708 KB Output is correct
92 Correct 458 ms 70716 KB Output is correct
93 Correct 449 ms 70708 KB Output is correct
94 Correct 431 ms 70832 KB Output is correct
95 Correct 477 ms 70736 KB Output is correct
96 Correct 416 ms 71884 KB Output is correct
97 Correct 457 ms 71884 KB Output is correct
98 Correct 423 ms 71876 KB Output is correct
99 Correct 429 ms 71880 KB Output is correct
100 Correct 414 ms 71876 KB Output is correct
101 Correct 427 ms 71884 KB Output is correct
102 Correct 468 ms 71884 KB Output is correct
103 Correct 425 ms 71876 KB Output is correct
104 Correct 422 ms 71880 KB Output is correct
105 Correct 426 ms 71876 KB Output is correct
106 Correct 449 ms 72520 KB Output is correct
107 Correct 422 ms 72504 KB Output is correct
108 Correct 447 ms 72400 KB Output is correct
109 Correct 434 ms 72460 KB Output is correct
110 Correct 458 ms 72472 KB Output is correct
111 Correct 444 ms 72464 KB Output is correct
112 Correct 450 ms 72464 KB Output is correct
113 Correct 464 ms 72468 KB Output is correct
114 Correct 444 ms 72412 KB Output is correct
115 Correct 466 ms 72464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 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 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 8 ms 2516 KB Output is correct
14 Correct 104 ms 18328 KB Output is correct
15 Correct 178 ms 36312 KB Output is correct
16 Correct 312 ms 70712 KB Output is correct
17 Correct 283 ms 70816 KB Output is correct
18 Correct 315 ms 70580 KB Output is correct
19 Correct 301 ms 70704 KB Output is correct
20 Correct 315 ms 70604 KB Output is correct
21 Correct 303 ms 70716 KB Output is correct
22 Correct 318 ms 70712 KB Output is correct
23 Correct 329 ms 70708 KB Output is correct
24 Correct 332 ms 70704 KB Output is correct
25 Correct 303 ms 70596 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 8 ms 2516 KB Output is correct
29 Correct 89 ms 18324 KB Output is correct
30 Correct 189 ms 36412 KB Output is correct
31 Correct 332 ms 70712 KB Output is correct
32 Correct 318 ms 70712 KB Output is correct
33 Correct 324 ms 70788 KB Output is correct
34 Correct 319 ms 70708 KB Output is correct
35 Correct 310 ms 70704 KB Output is correct
36 Correct 313 ms 70716 KB Output is correct
37 Correct 322 ms 70708 KB Output is correct
38 Correct 308 ms 70652 KB Output is correct
39 Correct 331 ms 70708 KB Output is correct
40 Correct 307 ms 70596 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 11 ms 2516 KB Output is correct
45 Correct 15 ms 2516 KB Output is correct
46 Correct 13 ms 2516 KB Output is correct
47 Correct 51 ms 9352 KB Output is correct
48 Correct 57 ms 9340 KB Output is correct
49 Correct 106 ms 18236 KB Output is correct
50 Correct 245 ms 36300 KB Output is correct
51 Correct 368 ms 70704 KB Output is correct
52 Correct 376 ms 70692 KB Output is correct
53 Correct 368 ms 70704 KB Output is correct
54 Correct 352 ms 70708 KB Output is correct
55 Correct 365 ms 70708 KB Output is correct
56 Correct 361 ms 70596 KB Output is correct
57 Correct 410 ms 70712 KB Output is correct
58 Correct 364 ms 70716 KB Output is correct
59 Correct 395 ms 70732 KB Output is correct
60 Correct 374 ms 70584 KB Output is correct
61 Correct 369 ms 70744 KB Output is correct
62 Correct 359 ms 70748 KB Output is correct
63 Correct 386 ms 70736 KB Output is correct
64 Correct 366 ms 70612 KB Output is correct
65 Correct 388 ms 70724 KB Output is correct
66 Correct 359 ms 70584 KB Output is correct
67 Correct 374 ms 70736 KB Output is correct
68 Correct 357 ms 70708 KB Output is correct
69 Correct 374 ms 70608 KB Output is correct
70 Correct 377 ms 70712 KB Output is correct
71 Correct 53 ms 9292 KB Output is correct
72 Correct 62 ms 9332 KB Output is correct
73 Correct 53 ms 9296 KB Output is correct
74 Correct 53 ms 9344 KB Output is correct
75 Correct 52 ms 9360 KB Output is correct
76 Correct 116 ms 18232 KB Output is correct
77 Correct 119 ms 18324 KB Output is correct
78 Correct 119 ms 18260 KB Output is correct
79 Correct 123 ms 18332 KB Output is correct
80 Correct 118 ms 18328 KB Output is correct
81 Correct 263 ms 36288 KB Output is correct
82 Correct 264 ms 36320 KB Output is correct
83 Correct 263 ms 36392 KB Output is correct
84 Correct 248 ms 36296 KB Output is correct
85 Correct 263 ms 36280 KB Output is correct
86 Correct 273 ms 36320 KB Output is correct
87 Correct 296 ms 36348 KB Output is correct
88 Correct 269 ms 36300 KB Output is correct
89 Correct 262 ms 36284 KB Output is correct
90 Correct 290 ms 36320 KB Output is correct
91 Correct 436 ms 70708 KB Output is correct
92 Correct 458 ms 70716 KB Output is correct
93 Correct 449 ms 70708 KB Output is correct
94 Correct 431 ms 70832 KB Output is correct
95 Correct 477 ms 70736 KB Output is correct
96 Correct 416 ms 71884 KB Output is correct
97 Correct 457 ms 71884 KB Output is correct
98 Correct 423 ms 71876 KB Output is correct
99 Correct 429 ms 71880 KB Output is correct
100 Correct 414 ms 71876 KB Output is correct
101 Correct 427 ms 71884 KB Output is correct
102 Correct 468 ms 71884 KB Output is correct
103 Correct 425 ms 71876 KB Output is correct
104 Correct 422 ms 71880 KB Output is correct
105 Correct 426 ms 71876 KB Output is correct
106 Correct 449 ms 72520 KB Output is correct
107 Correct 422 ms 72504 KB Output is correct
108 Correct 447 ms 72400 KB Output is correct
109 Correct 434 ms 72460 KB Output is correct
110 Correct 458 ms 72472 KB Output is correct
111 Correct 444 ms 72464 KB Output is correct
112 Correct 450 ms 72464 KB Output is correct
113 Correct 464 ms 72468 KB Output is correct
114 Correct 444 ms 72412 KB Output is correct
115 Correct 466 ms 72464 KB Output is correct
116 Incorrect 20 ms 4032 KB Output isn't correct
117 Halted 0 ms 0 KB -