This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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});
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |