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;
#define pb push_back
#define mp make_pair
#define pii pair <int, int>
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define debug(); cerr <<"bug!"<<endl;
const int maxn = 5e5 + 2;
int l[maxn], t[maxn];
int ind[maxn], big[maxn];
vector <int> vec[maxn];
vector <int> bigs;
vector <pii> v;
ll seg[maxn * 4], c[maxn];
int f, k, m;
int sum(ll x, ll y){
return (x % m + y % m) % m;
// return x + y;
}
int mul(ll x, ll y){
return (1LL * x % m * y % m) % m;
// return x * y;
}
void add(int l, int r, int ind, int x){
if(l + 1 >= r){
seg[x] = sum(seg[x], 1);
return;
}
int mid = (l + r) / 2;
if(ind < mid){
add(l, mid, ind, x * 2);
}else{
add(mid, r, ind, x * 2 + 1);
}
seg[x] = mul(seg[x * 2], seg[x * 2 + 1]);
}
ll get(int l, int r, int L, int R, int x){
if(l >= R || r <= L){
return (ll)1;
}
if(l >= L && r <= R){
return seg[x] % m;
}
int mid = (l + r) / 2;
return mul(get(l, mid, L, R, x * 2), get(mid, r, L, R, x * 2 + 1));
}
int pt = 0;
void upd_c(int x, int i){
while(l[v[pt].S] * 2 <= l[x]){
int y = v[pt].S;
c[t[y]] ++;
add(0, k, ind[t[y]], 1);
pt ++;
}
}
int find_ind(int le, int x, int r = k){
while(le + 1 < r){
int mid = (le + r) / 2;
int f = bigs[mid];
int y = v[f].S;
if(l[y] < l[v[x].S] * 2){
le = mid;
}else{
r = mid;
}
}
return r;
}
int main(){
fast_io;
fill(seg, seg + maxn * 4, 1);
fill(c, c + maxn, 1);
cin >> f >> k >> m;
for(int i = 0; i < f; i ++){
cin >> l[i] >> t[i];
v.pb({l[i], i});
big[t[i]] = -1;
}
sort(v.begin(), v.end());
for(int i = 0; i < f; i ++){
vec[t[v[i].S]].pb(i);
}
int num = 0;
for(int i = f - 1; i >= 0; i --){
if(big[t[v[i].S]] == -1){
big[t[v[i].S]] = i;
ind[t[v[i].S]] = k - num - 1;
bigs.pb(i);
num ++;
}
}
reverse(bigs.begin(), bigs.end());
ll ans = 0;
for(int i = 0; i < f; i ++){
int x = v[i].S;
upd_c(x, i);
if(big[t[x]] == i){
ll p = get(0, k, 0, ind[t[x]], 1);
int in = find_ind(ind[t[x]], vec[t[x]][c[t[x]] - 1]);
ll val = get(0, k, ind[t[x]] + 1, in, 1);
ll v1 = mul(p, c[t[x]] - 1);
ll v2 = mul(val, p);
ans = sum(ans, sum(v1, v2));
}
}
cout << ans % m;
return 0;
}
# | 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... |