Submission #734078

# Submission time Handle Problem Language Result Execution time Memory
734078 2023-05-01T16:10:02 Z lorenzoferrari Fish (IOI08_fish) C++17
100 / 100
1004 ms 65536 KB
#include "bits/stdc++.h"
using namespace std;

#define int long long

struct fish {
    int l;
    int g;
    bool operator<(const fish& oth) {
        return l < oth.l;
    }
};

struct Segtree {
    int n;
    int m;
    vector<int> t;
    Segtree(int n, int m) : n(n), m(m), t(2*n, 1) {}
    void inc(int p) {
        t[p + n] %= m;
        for (t[p += n] += 1; p > 1; p >>= 1) {
            t[p >> 1] = t[p] * t[p ^ 1] % m;
        }
    }
    int query(int l, int r) {
        int ans = 1;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) ans = ans * t[l++] % m;
            if (r & 1) ans = ans * t[--r] % m;
        }
        return ans;
    }
};

signed main() {
    int n; cin >> n;
    int k; cin >> k;
    int m; cin >> m;
    vector<fish> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].l >> a[i].g; --a[i].g;
    }
    sort(begin(a), end(a));
    vector<int> last(k);
    vector<vector<int>> o(k);
    for (int i = 0; i < n; ++i) {
        o[a[i].g].emplace_back(a[i].l);
        last[a[i].g] = i;
    }
    vector<int> cs;
    vector<int> idx(k);
    for (int i = 0; i < n; ++i) {
        if (last[a[i].g] == i) {
            idx[a[i].g] = cs.size();
            cs.push_back(a[i].g);
        }
    }

    Segtree st(k, m);

    int ans = 0;
    vector<int> frq(k);
    for (int r = 0, l = 0; r < n; ++r) {
        int c = a[r].g;
        if (last[c] != r) continue;
        while (2*a[l].l <= a[r].l) {
            st.inc(idx[a[l].g]);
            ++frq[a[l].g];
            ++l;
        }

        int cur1 = st.query(0, idx[c]) * (frq[c] % m) % m;

        int ll = idx[c], rr = k;
        while (rr - ll > 1) {
            int mm = (ll + rr) / 2;
            if (o[cs[mm]].back() < 2 * o[c][frq[c]]) {
                ll = mm;
            } else {
                rr = mm;
            }
        }

        int cur2 = st.query(0, idx[c]) * st.query(idx[c]+1, ll+1) % m;

        ans = (ans + cur1 + cur2) % m;
    }
    
    cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 328 ms 12608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 5988 KB Output is correct
2 Correct 189 ms 8888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 7 ms 468 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 207 ms 9028 KB Output is correct
2 Correct 489 ms 14628 KB Output is correct
3 Correct 365 ms 20360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 311 ms 14524 KB Output is correct
2 Correct 377 ms 16076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 211 ms 9080 KB Output is correct
2 Correct 392 ms 13852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 333 ms 13720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 401 ms 15932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 313 ms 14796 KB Output is correct
2 Correct 485 ms 22708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 725 ms 25628 KB Output is correct
2 Correct 523 ms 26012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 498 ms 26276 KB Output is correct
2 Correct 607 ms 30664 KB Output is correct
3 Correct 552 ms 37960 KB Output is correct
4 Correct 672 ms 30504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 876 ms 30712 KB Output is correct
2 Correct 793 ms 65536 KB Output is correct
3 Correct 786 ms 65536 KB Output is correct
4 Correct 1004 ms 57396 KB Output is correct