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;
#define ll long long
const int nx=2e5+5, kx=18;
ll n, m, pa[nx][kx], sm[nx][kx], t, vl[nx][2], sh[nx][2], ans;
map<ll, ll> mp, rvmp;
struct segtree
{
pair<ll, ll> d[16*nx];
void update(int l, int r, int i, int idx, pair<ll, ll> vl)
{
if (idx<l||r<idx) return;
if (l==r) return d[i]=max(d[i], vl), void();
int md=(l+r)/2;
update(l, md, 2*i, idx, vl);
update(md+1, r, 2*i+1, idx, vl);
d[i]=max(d[2*i], d[2*i+1]);
}
pair<ll, ll> query(int l, int r, int i, int ql, int qr)
{
if (qr<l||r<ql) return {0, 0};
if (ql<=l&&r<=qr) return d[i];
int md=(l+r)/2;
return max(query(l, md, 2*i, ql, qr), query(md+1, r, 2*i+1, ql, qr));
}
} s;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++)
{
cin>>vl[i][0]>>vl[i][1];
if (vl[i][1]<vl[i][0]) vl[i][1]+=m;
sh[i][0]=vl[i][0]+m;
sh[i][1]=vl[i][1]+m;
mp[vl[i][0]]=mp[vl[i][1]]=mp[sh[i][0]]=mp[sh[i][1]]=0;
}
for (auto &[x, y]:mp) y=++t;
for (auto [x, y]:mp) rvmp[y]=x;
for (int i=1; i<=n ;i++) vl[i][0]=mp[vl[i][0]], vl[i][1]=mp[vl[i][1]], sh[i][0]=mp[sh[i][0]], sh[i][1]=mp[sh[i][1]];
for (int i=1; i<=n; i++) s.update(1, 4*n, 1, vl[i][0], {vl[i][1], i}), s.update(1, 4*n, 1, sh[i][0], {sh[i][1], i});
for (int i=1; i<=n; i++)
{
auto mx=s.query(1, 4*n, 1, vl[i][0], vl[i][1]);
mx.first=rvmp[mx.first];
auto tmp=rvmp[vl[i][1]];
if (mx.first<=tmp) continue;
pa[i][0]=mx.second;
sm[i][0]=mx.first-tmp;
//cout<<"pa "<<i<<' '<<pa[i][0]<<' '<<sm[i][0]<<'\n';
}
for (int j=1; j<kx; j++) for (int i=1; i<=n; i++) pa[i][j]=pa[pa[i][j-1]][j-1], sm[i][j]=sm[i][j-1]+sm[pa[i][j-1]][j-1];
for (int i=1; i<=n; i++)
{
ll cur=rvmp[vl[i][1]]-rvmp[vl[i][0]], u=i, cnt=1;
for (int j=kx-1; j>=0; j--) if (cur+sm[u][j]<m) cnt+=1<<j, cur+=sm[u][j], u=pa[u][j];
//cout<<"cur "<<cur<<' '<<cnt<<'\n';
if (cur+sm[u][0]<m||u==0) continue;
ans=max(ans, cnt+1);
}
if (ans==0) cout<<-1;
else cout<<ans;
}
# | 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... |