답안 #923262

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
923262 2024-02-07T04:02:33 Z math_rabbit_1028 Fish (IOI08_fish) C++14
100 / 100
1159 ms 61856 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

int n, k, MOD;
vector<int> vec[505050];
vector<int> longest;
ll dp[505050][2];
int p[505050];

bool compare(vector<int> A, vector<int> B) {
    return A.back() > B.back();
}

ll tree[2020202];
void init(int v, int st, int ed) {
    if (st == ed) {
        tree[v] = vec[st].size() + 1;
        return;
    }
    int mid = (st+ed)/2;
    init(2*v, st, mid);
    init(2*v+1, mid+1, ed);
    tree[v] = (tree[2*v]*tree[2*v+1])%MOD;
}

void update(int v, int st, int ed, int idx, int val) {
    if (st > idx || ed < idx) return;
    if (st == ed) {
        tree[v] += val;
        return;
    }
    int mid = (st+ed)/2;
    update(2*v, st, mid, idx, val);
    update(2*v+1, mid+1, ed, idx, val);
    tree[v] = tree[2*v] * tree[2*v+1] % MOD;
}

ll get(int v, int st, int ed, int lt, int rt) {
    if (lt > ed || rt < st) return 1;
    if (lt <= st && ed <= rt) return tree[v];
    int mid = (st+ed)/2;
    return get(2*v, st, mid, lt, rt) * get(2*v+1, mid+1, ed, lt, rt) % MOD;
}

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

    cin >> n >> k >> MOD;
    for (int i = 1; i <= n; i++) {
        int l, t;
        cin >> l >> t;
        vec[t].push_back(l);
    }

    for (int i = 1; i <= k; i++) sort(vec[i].begin(), vec[i].end());

    sort(vec + 1, vec + k + 1, compare);

    for (int j = 1; j <= k; j++) p[j] = vec[j].size();
    for (int i = k; i >= 1; i--) longest.push_back(vec[i].back());

    priority_queue<pii> pq;
    for (int i = 1; i <= k; i++) {
        for (int j = 0; j < vec[i].size(); j++) pq.push({vec[i][j], i});
    }

    init(1, 1, k);
    for (int i = 1; i <= k; i++) {

        while (pq.size() > 0 && pq.top().first > vec[i].back()/2) {
            p[pq.top().second]--;
            update(1, 1, k, pq.top().second, -1);
            pq.pop();
        }

        int cnt = upper_bound(vec[i].begin(), vec[i].end(), vec[i].back()/2) - vec[i].begin();
        dp[i][0] = 1;
        dp[i][1] = cnt;

        dp[i][0] = dp[i][0] * get(1, 1, k, i+1, k) % MOD;
        dp[i][1] = dp[i][1] * get(1, 1, k, i+1, k) % MOD;

        int idx = lower_bound(longest.begin(), longest.end(), 2*vec[i][cnt]) - longest.begin();
        dp[i][0] = dp[i][0] * get(1, 1, k, k-idx+1, i-1) % MOD;

        //cout << dp[i][0] << " " << dp[i][1] << "\n";
    }

    ll ans = 0;
    for (int i = 1; i <= k; i++) {
        ans += dp[i][0] + dp[i][1];
        ans %= MOD;
    }

    cout << ans << "\n";

    return 0;
}

Compilation message

fish.cpp: In function 'int main()':
fish.cpp:67:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |         for (int j = 0; j < vec[i].size(); j++) pq.push({vec[i][j], i});
      |                         ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 17244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 17088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 17244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 17244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 17244 KB Output is correct
2 Correct 4 ms 17244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 17244 KB Output is correct
2 Correct 135 ms 23636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 17244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 17240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 20300 KB Output is correct
2 Correct 78 ms 20896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 17240 KB Output is correct
2 Correct 6 ms 17500 KB Output is correct
3 Correct 7 ms 17380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 23108 KB Output is correct
2 Correct 219 ms 24300 KB Output is correct
3 Correct 198 ms 25048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 24520 KB Output is correct
2 Correct 169 ms 24372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 23292 KB Output is correct
2 Correct 213 ms 24328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 185 ms 26316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 27064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 26692 KB Output is correct
2 Correct 412 ms 38456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 496 ms 33096 KB Output is correct
2 Correct 403 ms 36656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 424 ms 34588 KB Output is correct
2 Correct 436 ms 38644 KB Output is correct
3 Correct 515 ms 46452 KB Output is correct
4 Correct 451 ms 38564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 629 ms 37176 KB Output is correct
2 Correct 1110 ms 60728 KB Output is correct
3 Correct 1159 ms 61856 KB Output is correct
4 Correct 1119 ms 58084 KB Output is correct