//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
pii a[MAXN];
vi arr[MAXN];
priority_queue<pii> pq;
int ind[MAXN], rev[MAXN], cnt[MAXN];
ll seg[MAXN*4];
void build(int l, int r, int k) {
if (l == r) {
seg[k] = 1;
return;
}
int m = (l+r) / 2;
build(l, m, k*2);
build(m+1, r, k*2+1);
seg[k] = (seg[k*2] * seg[k*2+1]) % MOD;
}
void upd(int l, int r, int k, int a, int v) {
if (a <= l && r <= a) {
seg[k] = v;
return;
}
int m = (l+r) / 2;
if (a <= m)
upd(l, m, k*2, a, v);
else
upd(m+1, r, k*2+1, a, v);
seg[k] = (seg[k*2] * seg[k*2+1]) % MOD;
}
ll qry(int l, int r, int k, int a, int b) {
if (r < a || b < l || a > b) return 1;
if (a <= l && r <= b) return seg[k];
int m = (l+r) / 2;
return (qry(l,m,k*2,a,b)*qry(m+1,r,k*2+1,a,b)) % MOD;
}
int main() {
int n, k;
scanf("%d %d %d", &n, &k, &MOD);
a[0] = mp(0, 0);
vii tmp;
for (int i = 1; i <= n; i++) {
scanf("%d %d", &a[i].fi, &a[i].se);
arr[a[i].se].pb(a[i].fi);
pq.push(a[i]);
}
for (int i = 1; i <= k; i++) {
ind[i] = i;
sort(arr[i].begin(), arr[i].end());
}
build(1, k, 1);
sort(ind + 1, ind + k+1, [](int l, int r) {
return arr[l].back() > arr[r].back();
});
for (int i = 1; i <= k; i++)
rev[ind[i]] = i;
ll ans = 0;
for (int i = 1; i <= k; i++) {
cnt[ind[i]] = (int)arr[ind[i]].size();
upd(1, k, 1, i, cnt[ind[i]] % MOD + 1);
}
for (int i = 1; i <= k; i++) {
int sz = arr[ind[i]].back() / 2;
while (!pq.empty()) {
if (pq.top().fi <= sz)
break;
pii cur = pq.top(); pq.pop();
cnt[cur.se]--;
upd(1, k, 1, rev[cur.se], cnt[cur.se] % MOD + 1);
}
ll v1 = cnt[ind[i]], v2 = qry(1, k, 1, i + 1, k);
int lo = 1, hi = i;
while (lo < hi) {
int mi = (lo+hi) / 2;
if (upper_bound(arr[ind[i]].begin(), arr[ind[i]].end(), arr[ind[mi]].back() / 2) - arr[ind[i]].begin() == v1)
hi = mi;
else
lo = mi + 1;
}
ll v3 = qry(1, k, 1, lo, i - 1);
v1 %= MOD;
ans = (ans + (v1 * v2) % MOD + (v2 * v3) % MOD) % MOD;
}
printf("%lld\n", ans);
return 0;
}
Compilation message
fish.cpp: In function 'int main()':
fish.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &n, &k, &MOD);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
fish.cpp:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a[i].fi, &a[i].se);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23908 KB |
Output is correct |
2 |
Correct |
22 ms |
23852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23928 KB |
Output is correct |
2 |
Correct |
263 ms |
40284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
30620 KB |
Output is correct |
2 |
Correct |
156 ms |
32648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24132 KB |
Output is correct |
2 |
Correct |
27 ms |
24192 KB |
Output is correct |
3 |
Correct |
26 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
35392 KB |
Output is correct |
2 |
Correct |
360 ms |
40876 KB |
Output is correct |
3 |
Correct |
323 ms |
41948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
372 ms |
40816 KB |
Output is correct |
2 |
Correct |
317 ms |
41948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
35684 KB |
Output is correct |
2 |
Correct |
372 ms |
41948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
368 ms |
40544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
467 ms |
43140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
476 ms |
40620 KB |
Output is correct |
2 |
Correct |
924 ms |
47756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1137 ms |
49176 KB |
Output is correct |
2 |
Correct |
870 ms |
43996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1035 ms |
48296 KB |
Output is correct |
2 |
Correct |
1033 ms |
47616 KB |
Output is correct |
3 |
Correct |
1016 ms |
52548 KB |
Output is correct |
4 |
Correct |
1004 ms |
47580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1431 ms |
52700 KB |
Output is correct |
2 |
Correct |
1755 ms |
65536 KB |
Output is correct |
3 |
Correct |
1975 ms |
65536 KB |
Output is correct |
4 |
Correct |
1940 ms |
65316 KB |
Output is correct |