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 long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define rep(i, j, k) for (int i = j; i < k; i++)
#define pb push_back
#define all(x) x.begin(), x.end()
#define mt make_tuple
template <typename S, typename T>
inline bool smin(S& l, T r) {
return r < l ? l = r, 1 : 0;
}
template <typename S, typename T>
inline bool smax(S& l, T r) {
return l < r ? l = r, 1 : 0;
}
int MOD = 1e9 + 7; // 998244353;
template <typename S>
inline S mod(S& l) {
return l = (l % MOD + MOD) % MOD;
}
template <typename S, typename T>
inline S add(S& l, T r) {
return mod(l += r);
}
ll po(ll v, ll u) {
return u ? po(v * v % MOD, u >> 1) * (u & 1 ? v : 1) % MOD : 1;
}
ll gcd(ll v, ll u) { return v ? gcd(u % v, v) : u; }
inline void fileIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
inline ostream& operator<<(ostream& o, pll p) {
return o << '(' << p.first << ' ' << p.second << ')';
}
constexpr int N = 5e5 + 10;
int F, k;
pii p[N];
bitset<N> mark, have;
int arr[N], ptr;
ll answer;
pll seg[N << 2];
inline pll merge(pll l, pll r) {
return pll(l.first * r.first % MOD, (l.first * r.second + l.second) % MOD);
}
void segInit(int l = 0, int r = k, int id = 1) {
if (l == r - 1) {
seg[id] = {1, 1};
return;
}
int mid = l + r >> 1;
segInit(l, mid, id << 1);
segInit(mid, r, id << 1 | 1);
seg[id] = merge(seg[id << 1], seg[id << 1 | 1]);
}
void segAdd(int pos, int l = 0, int r = k, int id = 1) {
if (l == r - 1) {
seg[id].first++;
seg[id].second++;
return;
}
int mid = l + r >> 1;
if (pos < mid)
segAdd(pos, l, mid, id << 1);
else
segAdd(pos, mid, r, id << 1 | 1);
seg[id] = merge(seg[id << 1], seg[id << 1 | 1]);
}
pll segGet(int s, int t, int l = 0, int r = k, int id = 1) {
if (l >= s && r <= t) return seg[id];
if (l >= t || r <= s) return {1, 0};
int mid = l + r >> 1;
return merge(segGet(s, t, l, mid, id << 1),
segGet(s, t, mid, r, id << 1 | 1));
}
int main() {
#ifndef LOCAL
// fileIO("");
#endif
cin >> F >> k >> MOD;
rep(i, 0, F) cin >> p[i].first >> p[i].second;
sort(p, p + F);
for (int i = F - 1; ~i; i--)
if (!have[p[i].second]) {
arr[p[i].second] = ptr++;
mark[i] = true;
have[p[i].second] = true;
}
segInit();
int R = 0;
rep(i, 0, F) {
while (R < F && p[i].first * 2 > p[R].first) {
if (mark[R]) ptr--;
R++;
}
int id = arr[p[i].second];
if (id < ptr)
add(answer, segGet(0, id + 1).second * segGet(id + 1, k).first);
else
add(answer, segGet(0, ptr).second * segGet(ptr, id).first *
segGet(id + 1, k).first);
segAdd(id);
}
add(answer, k);
cout << answer << endl;
return 0;
}
Compilation message (stderr)
fish.cpp: In function 'void segInit(int, int, int)':
fish.cpp:71:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
fish.cpp: In function 'void segAdd(int, int, int, int)':
fish.cpp:83:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
fish.cpp: In function 'pll segGet(int, int, int, int, int)':
fish.cpp:94:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
# | 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... |