이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5, kx=17, inf=2e9;
int n, k, a[nx], b[nx], pa[nx][kx], mx, vl;
struct range
{
int l, r;
range(int l, int r): l(l), r(r){}
bool operator< (const range &o) const
{
if (r==o.r) return l<o.l;
return r<o.r;
}
};
vector<range> v, cur;
int query(int u, int limit)
{
int res=0;
if (limit==inf) limit--;
for (int i=kx-1; i>=0; i--) if (v[pa[u][i]].r<=limit) res+=(1<<i), u=pa[u][i];
return res;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k;
v.push_back(range(0, 0));
for (int i=1; i<=n; i++) cin>>a[i]>>b[i], v.push_back(range(a[i], b[i])), pa[i][0]=n+1;
v.push_back(range({inf, inf}));
sort(v.begin(), v.end());
int lst=0;
for (int i=1; i<=n+1; i++)
{
mx=max(mx, v[i].l);
while (lst<=n+1&&v[lst].r<=mx) pa[lst++][0]=i;
}
for (int i=1; i<kx; i++) for (int j=0; j<=n+1; j++) pa[j][i]=pa[pa[j][i-1]][i-1];
/*
for (int i=0; i<=n+1; i++)
{
cout<<"debug "<<i<<' '<<v[i].l<<' '<<v[i].r<<':';
for (int j=0; j<=2; j++) cout<<pa[i][j]<<' ';
cout<<'\n';
}
*/
vl=query(0, inf);
if (vl<k) return cout<<-1, 0;
cur.push_back(range(0, 0));
for (int i=1; i<=n; i++)
{
if (!k) continue;
auto itr=upper_bound(cur.begin(), cur.end(), range(a[i], b[i]));
if (itr!=cur.end()&&itr->l<b[i]) continue;
if (prev(itr)->r>a[i]) continue;
int tmp=vl, pidx=lower_bound(v.begin(), v.end(), *prev(itr))-v.begin(), cidx=lower_bound(v.begin(), v.end(), range(a[i], b[i]))-v.begin();
tmp-=query(pidx, itr->l);
tmp+=query(pidx, a[i]);
tmp+=query(cidx, itr->l);
if (tmp>=k-1)
{
k--;
cur.push_back(range(a[i], b[i]));
vl=tmp;
cout<<i<<'\n';
}
}
}
# | 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... |