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>
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
using namespace std;
ll ans;
ll n,k,mod,in[500005],s[2000005],pown=1,m[500005];
vector<ll>v[500005],t[500005];
void change(ll x){
x = pown + x;
s[x]++;
s[x] %= mod;
x /= 2;
while(x){
s[x] = s[2 * x] * s[2 * x + 1] % mod;
x /= 2;
}
}
ll get(ll x,ll L,ll R,ll l,ll r){
if(L > r || R < l)return 1;
if(L >= l && R <= r)return s[x];
ll k1 = get(2 * x , L , (L + R) / 2 , l , r);
ll k2 = get(2 * x + 1 , (L + R) / 2 + 1 , R , l , r);
return k1 * k2 % mod;
}
int main(){
ios::sync_with_stdio(false);
cin >> n >> k >> mod;
while(pown <= k + 2)
pown *= 2;
for(int i=1; i<=n; i++){
ll x,y;
cin >> x >> y;
v[y].pb(x);
}
vector<pair<ll,ll> >g;
for(int i=1; i<=k; i++){
sort(v[i].begin() , v[i].end());
if(v[i].size()){
g.pb(mp(v[i][v[i].size() - 1] , i));
}
}
for(int i=0; i<k; i++){
change(i);
}
sort(g.begin() , g.end());
for(int i=0; i<g.size(); i++){
ll ind = g[i].s;
t[i] = v[ind];
}
set<pair<ll,ll> >st;
for(int i=0; i<k; i++){
v[i] = t[i];
st.insert(mp(v[i][0] , i));
m[i] = v[i][(int)v[i].size() - 1];
}
for(int i=0; i<k; i++){
if(v[i].size() == 0)continue;
while(st.size() && (*st.begin()).f * 2 <= m[i]){
pair<ll,ll>p = (*st.begin());
change(p.s);
in[p.s]++;
st.erase(st.begin());
if(in[p.s] != v[p.s].size()){
st.insert(mp(v[p.s][in[p.s]] , p.s));
}
}
ll l = i + 1,r = k - 1,mid,ind = k;
while(r >= l){
mid = (l + r) / 2;
if(m[mid] >= 2 * v[i][in[i]]){
r = mid - 1;
ind = mid;
}
else {
l = mid + 1;
}
}
ind--;
ans = (ans + get(1 , 1 , pown , 1 , i) * get(1 , 1 , pown , i + 2 , ind + 1)) % mod;
ans = (ans + in[i] * get(1 , 1 , pown , 1 , i)) % mod;
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
fish.cpp: In function 'int main()':
fish.cpp:51:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i=0; i<g.size(); i++){
| ~^~~~~~~~~
fish.cpp:69:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | if(in[p.s] != v[p.s].size()){
| ~~~~~~~~^~~~~~~~~~~~~~~~
# | 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... |