Submission #1108755

# Submission time Handle Problem Language Result Execution time Memory
1108755 2024-11-05T01:52:25 Z kyaruru Fish (IOI08_fish) C++17
100 / 100
424 ms 47816 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int a, b, c;
    cin >> a >> b >> c;
    pll k[a + 1];
    for (int i = 1; i <= a; i++) {
        ll x, y;
        cin >> x >> y;
        k[i] = {x, y};
    }

    int h = ceil(log2(b));
    int n = (1 << h) - 1;
    vector<ll> seg(1 << (h + 1), 1), sega(1 << (h + 1), 1);

    sort(k + 1, k + a + 1);

    ll ed[b + 1], sv[b + 1], u[b + 1];
    memset(ed, -1, sizeof(ed));
    memset(u, -1, sizeof(u));
    memset(sv, 0, sizeof(sv));

    int m = b;
    for (int i = a; i >= 1; i--) {
        if (ed[k[i].second] == -1) ed[k[i].second] = m--;
        k[i].second = ed[k[i].second];
    }

    vector<int> ka;
    memset(ed, -1, sizeof(ed));
    for (int i = a; i >= 1; i--) {
        if (ed[k[i].second] == -1)
            ed[k[i].second] = i, ka.push_back(k[i].first),u[k[i].second]=k[i].first;
        else if (2 * k[i].first > k[ed[k[i].second]].first)
            u[k[i].second] = k[i].first;
    }

    sort(ka.begin(), ka.end());
    ll r = 1, ans = 0;
    for (int i = 1; i <= a; i++) {
        while (2 * k[r].first <= k[i].first) {
            if (ed[k[r].second] < i) {
                int j = k[r].second + n;
                seg[j]++;
                for (j /= 2; j >= 1; j /= 2) seg[j] = seg[2 * j] * seg[2 * j + 1] % c;
            } else
                sv[k[r].second]++;

            int j = k[r].second + n;
            sega[j]++;
            for (j /= 2; j >= 1; j /= 2) sega[j] = sega[2 * j] * sega[2 * j + 1] % c;
            r++;
        }
        if (ed[k[i].second] == i) {
            int j = k[i].second + n;
            seg[j] += sv[k[i].second];
            for (j /= 2; j >= 1; j /= 2) seg[j] = seg[2 * j] * seg[2 * j + 1] % c;
            ans = (ans + seg[1]) % c;

            j = k[i].second + n;
            ll p = sega[j];
            sega[j] = seg[j] = 1;
            for (j /= 2; j >= 1; j /= 2) {
                sega[j] = sega[2 * j] * sega[2 * j + 1] % c;
                seg[j] = seg[2 * j] * seg[2 * j + 1] % c;
            }

            int v = u[k[i].second] == -1 ? b : lower_bound(ka.begin(), ka.end(), 2 * u[k[i].second]) - ka.begin();
            ll l = 1 + n, r = v + n, t = 1, tt = 1;
            while (l <= r) {
                if (l % 2 == 1) {
                    t = t * sega[l] % c;
                    tt = tt * seg[l] % c;
                    l++;
                }
                if (r % 2 == 0) {
                    t = t * sega[r] % c;
                    tt = tt * seg[r] % c;
                    r--;
                }
                l /= 2, r /= 2;
            }
            ans += t - tt;

            j = k[i].second + n;
            sega[j] = seg[j] = p;
            for (j /= 2; j >= 1; j /= 2) {
                sega[j] = sega[2 * j] * sega[2 * j + 1] % c;
                seg[j] = seg[2 * j] * seg[2 * j + 1] % c;
            }
        }
    }

    cout << (ans % c + c) % c;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 102 ms 8272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 3408 KB Output is correct
2 Correct 60 ms 7520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 760 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 5136 KB Output is correct
2 Correct 129 ms 14920 KB Output is correct
3 Correct 118 ms 15184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 8276 KB Output is correct
2 Correct 119 ms 15432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 5200 KB Output is correct
2 Correct 125 ms 15688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 10312 KB Output is correct
2 Correct 153 ms 23136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 219 ms 20160 KB Output is correct
2 Correct 170 ms 22540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 20164 KB Output is correct
2 Correct 195 ms 22960 KB Output is correct
3 Correct 240 ms 28884 KB Output is correct
4 Correct 198 ms 22996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 282 ms 22216 KB Output is correct
2 Correct 424 ms 46788 KB Output is correct
3 Correct 339 ms 47816 KB Output is correct
4 Correct 395 ms 45204 KB Output is correct