제출 #1267209

#제출 시각아이디문제언어결과실행 시간메모리
1267209gvancakFire (BOI24_fire)C++20
35 / 100
627 ms71388 KiB
#include <bits/stdc++.h> #define f first #define s second #define pb push_back //#define mp make_pair #define ll long long using namespace std; ll mod=1e9+7; const ll N=2*1e5; ll a[N+5],l,r,x,y,z,ans,t,n,q,mn,k,m,b[N+5],anss; map <ll,ll> mx,mp,fix,c,e; bool ok,okk; set <ll> s; pair <ll,ll> p[N+5],d[N+5]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n>>m; for (int i=1; i<=n; i++){ cin>>p[i].f>>p[i].s; } sort(p+1,p+n+1); for (int i=1; i<=n; i++){ if (p[i].f>p[i].s) b[0]=max(b[0],p[i].s),p[i].s+=m; } for (int i=1; i<=n; i++){ b[i]=max(b[i-1],p[i].s); } for (int i=1; i<=n; i++) a[i]=p[i].f; for (int i=1; i<=n; i++){ x=p[i].s; if (x>a[n]) y=n; else y=upper_bound(a+1,a+n+1,x)-a-1; mx[p[i].s]=b[y]; } for (int i=0; i<=n; i++) b[i]=0; ans=-1; for (int i=1; i<=n; i++) c[p[i].s]=-1,e[p[i].f]=-1; for (int i=1; i<=n; i++){ if (c[p[i].s]==-1) c[p[i].s]=p[i].f; c[p[i].s]=min(c[p[i].s],p[i].f); if (e[p[i].f]==-1) e[p[i].f]=p[i].s; e[p[i].f]=max(e[p[i].f],e[p[i].s]); } for (int i=1; i<=n; i++){ if (fix[p[i].s]==2) continue; //pirvelia i z=1; x=p[i].s; ok=0; b[1]=x; k=1; d[k].f=p[i].f; d[k].s=1; ll cr=1; okk=0; anss=n+1; while (fix[x]==0){ fix[x]=1; y=mx[x]; if (y<=x){ ok=1; break; } x=y; z++; y=c[x]; k++; d[k].f=y; d[k].s=z; while (cr<=k && d[cr].f+m<=x+okk*m){ anss=min(anss,z-d[cr].s+1); cr++; } if (x>m) okk=1,x-=m; } y=c[x]; for (int i=1; i<=k; i++){ if (d[i].f==y){ continue; } fix[e[d[i].f]]=2; } z=anss; if (z>n) continue; if (ans==-1) ans=z; ans=min(ans,z); } cout<<ans<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...