Submission #252082

# Submission time Handle Problem Language Result Execution time Memory
252082 2020-07-24T06:04:23 Z jeqcho Strange Device (APIO19_strange_device) C++14
100 / 100
1945 ms 158584 KB
#include <bits/stdc++.h>
#define fr first
#define sc second
#define m_p make_pair
using namespace std;

typedef long long ll;

const ll llinf = 1e18;

const int maxn = 2e6 + 100, inf = 1e9 + 100;

pair<pair<ll, bool>, ll> mas[2 * maxn];

pair<ll, ll> arr[maxn];

pair<ll, ll> seg[maxn];

bool arr_all;

int szmas, szarr, szseg, szind;

ll y;

ll ind[maxn];

int val[maxn];

bool good[maxn];

void add_mas(ll l, ll r, ll w) {
    mas[szmas++] = {{l, 0}, w};
    mas[szmas++] = {{r + 1, 1}, w};
    ind[szind++] = w;
}

void add_arr(ll l, ll r) {
    arr[szarr++] = {l, r};
}

ll cur;

void add_seg(ll l, ll r) {
    seg[szseg++] = {l, r};
    cur += r - l + 1;
}

int get_ind(ll w) {
    return lower_bound(ind, ind + szind, w) - ind;
}

void add(ll w) {
    int id = get_ind(w);
    if (good[id])
        return;
    if (val[id] == 0)
        cur++;
    val[id]++;
}

void rem(ll w) {
    int id = get_ind(w);
    if (good[id])
        return;
    val[id]--;
    if (val[id] == 0)
        cur--;
}

int main() {
    #ifdef ONPC
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
    #endif // ONPC
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    ll x;
    cin >> n >> x >> y;
    x /= __gcd(x, y + 1);
    for (int i = 0; i < n; i++) {
        ll l, r;
        cin >> l >> r;
        if (r - l < y && l % y <= r % y)
            add_mas(l % y, r % y, (l / y) % x);
        else {
            if (l % y != 0)
                add_mas(l % y, y - 1, (l / y) % x), l += y - l % y;
            if (r % y != y - 1)
                add_mas(0, r % y, (r / y) % x), r -= r % y + 1;
            if (l > r)
                continue;
            l /= y;
            r /= y;
            if (r - l + 1 >= x)
                arr_all = 1;
            else
                if (l % x <= r % x)
                    add_arr(l % x, r % x);
                else
                    add_arr(l % x, x - 1), add_arr(0, r % x);
        }
    }
    if (arr_all) {
        add_seg(0, x - 1);
    } else {
        sort(arr, arr + szarr);
        ll L = -1, R = -1;
        for (int i = 0; i < szarr; i++) {
            if (L == -1 || R + 1 < arr[i].fr) {
                if (L != -1)
                    add_seg(L, R);
                L = arr[i].fr;
                R = arr[i].sc;
            } else
                R = max(R, arr[i].sc);
        }
        if (L != -1)
            add_seg(L, R);
    }
    ll ans = 0;
    sort(ind, ind + szind);
    szind = unique(ind, ind + szind) - ind;
    {
        int it = 0;
        for (int i = 0; i < szind; i++) {
            while (it < szseg && seg[it].sc < ind[i])
                it++;
            if (it < szseg && seg[it].fr <= ind[i])
                good[i] = 1;
        }
    }
    sort(mas, mas + szmas);
    ll last = 0;
    int L = 0, R = 0;
    while (L < szmas) {
        ans += cur * (mas[L].fr.fr - last);
        while (R < szmas && mas[R].fr.fr == mas[L].fr.fr) {
            if (mas[R].fr.sc == 0)
                add(mas[R].sc);
            else
                rem(mas[R].sc);
            R++;
        }
        last = mas[L].fr.fr;
        L = R;
    }
    ans += cur * (y - last);
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 7 ms 896 KB Output is correct
3 Correct 8 ms 896 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 9 ms 896 KB Output is correct
17 Correct 74 ms 5880 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 383 ms 15972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 863 ms 59576 KB Output is correct
3 Correct 570 ms 15992 KB Output is correct
4 Correct 546 ms 16248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 863 ms 59576 KB Output is correct
3 Correct 570 ms 15992 KB Output is correct
4 Correct 546 ms 16248 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1470 ms 145648 KB Output is correct
7 Correct 845 ms 56656 KB Output is correct
8 Correct 1440 ms 145492 KB Output is correct
9 Correct 1569 ms 145144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 863 ms 59576 KB Output is correct
3 Correct 570 ms 15992 KB Output is correct
4 Correct 546 ms 16248 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 127 ms 11896 KB Output is correct
7 Correct 149 ms 14968 KB Output is correct
8 Correct 139 ms 15016 KB Output is correct
9 Correct 139 ms 14840 KB Output is correct
10 Correct 123 ms 10360 KB Output is correct
11 Correct 153 ms 14968 KB Output is correct
12 Correct 143 ms 14968 KB Output is correct
13 Correct 148 ms 15060 KB Output is correct
14 Correct 63 ms 5776 KB Output is correct
15 Correct 154 ms 11896 KB Output is correct
16 Correct 88 ms 6008 KB Output is correct
17 Correct 157 ms 14840 KB Output is correct
18 Correct 1857 ms 145964 KB Output is correct
19 Correct 1847 ms 145456 KB Output is correct
20 Correct 1942 ms 145276 KB Output is correct
21 Correct 167 ms 15096 KB Output is correct
22 Correct 146 ms 14968 KB Output is correct
23 Correct 175 ms 5752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 71 ms 5880 KB Output is correct
3 Correct 100 ms 5880 KB Output is correct
4 Correct 1358 ms 78840 KB Output is correct
5 Correct 105 ms 6392 KB Output is correct
6 Correct 96 ms 6264 KB Output is correct
7 Correct 94 ms 6264 KB Output is correct
8 Correct 108 ms 7032 KB Output is correct
9 Correct 108 ms 7160 KB Output is correct
10 Correct 84 ms 5880 KB Output is correct
11 Correct 63 ms 5880 KB Output is correct
12 Correct 65 ms 5880 KB Output is correct
13 Correct 103 ms 6520 KB Output is correct
14 Correct 793 ms 55160 KB Output is correct
15 Correct 72 ms 5884 KB Output is correct
16 Correct 699 ms 55224 KB Output is correct
17 Correct 1275 ms 77176 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 7 ms 896 KB Output is correct
3 Correct 8 ms 896 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 9 ms 896 KB Output is correct
17 Correct 74 ms 5880 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 0 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 0 ms 384 KB Output is correct
24 Correct 0 ms 384 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 383 ms 15972 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 863 ms 59576 KB Output is correct
31 Correct 570 ms 15992 KB Output is correct
32 Correct 546 ms 16248 KB Output is correct
33 Correct 0 ms 384 KB Output is correct
34 Correct 1470 ms 145648 KB Output is correct
35 Correct 845 ms 56656 KB Output is correct
36 Correct 1440 ms 145492 KB Output is correct
37 Correct 1569 ms 145144 KB Output is correct
38 Correct 0 ms 384 KB Output is correct
39 Correct 127 ms 11896 KB Output is correct
40 Correct 149 ms 14968 KB Output is correct
41 Correct 139 ms 15016 KB Output is correct
42 Correct 139 ms 14840 KB Output is correct
43 Correct 123 ms 10360 KB Output is correct
44 Correct 153 ms 14968 KB Output is correct
45 Correct 143 ms 14968 KB Output is correct
46 Correct 148 ms 15060 KB Output is correct
47 Correct 63 ms 5776 KB Output is correct
48 Correct 154 ms 11896 KB Output is correct
49 Correct 88 ms 6008 KB Output is correct
50 Correct 157 ms 14840 KB Output is correct
51 Correct 1857 ms 145964 KB Output is correct
52 Correct 1847 ms 145456 KB Output is correct
53 Correct 1942 ms 145276 KB Output is correct
54 Correct 167 ms 15096 KB Output is correct
55 Correct 146 ms 14968 KB Output is correct
56 Correct 175 ms 5752 KB Output is correct
57 Correct 0 ms 384 KB Output is correct
58 Correct 71 ms 5880 KB Output is correct
59 Correct 100 ms 5880 KB Output is correct
60 Correct 1358 ms 78840 KB Output is correct
61 Correct 105 ms 6392 KB Output is correct
62 Correct 96 ms 6264 KB Output is correct
63 Correct 94 ms 6264 KB Output is correct
64 Correct 108 ms 7032 KB Output is correct
65 Correct 108 ms 7160 KB Output is correct
66 Correct 84 ms 5880 KB Output is correct
67 Correct 63 ms 5880 KB Output is correct
68 Correct 65 ms 5880 KB Output is correct
69 Correct 103 ms 6520 KB Output is correct
70 Correct 793 ms 55160 KB Output is correct
71 Correct 72 ms 5884 KB Output is correct
72 Correct 699 ms 55224 KB Output is correct
73 Correct 1275 ms 77176 KB Output is correct
74 Correct 0 ms 384 KB Output is correct
75 Correct 0 ms 384 KB Output is correct
76 Correct 0 ms 384 KB Output is correct
77 Correct 0 ms 384 KB Output is correct
78 Correct 0 ms 388 KB Output is correct
79 Correct 12 ms 1664 KB Output is correct
80 Correct 1052 ms 69624 KB Output is correct
81 Correct 1385 ms 92368 KB Output is correct
82 Correct 1945 ms 158584 KB Output is correct
83 Correct 1929 ms 158328 KB Output is correct
84 Correct 1945 ms 158016 KB Output is correct
85 Correct 1938 ms 157340 KB Output is correct
86 Correct 174 ms 18168 KB Output is correct
87 Correct 1 ms 384 KB Output is correct