이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MX=2e5+5;
int N,K;
int L[MX], R[MX], nxt[MX], deg[MX];
vector<pair<int,int>> ptL[MX], ptR[MX];
map<int,int> id;
vector<int> adj[MX];
int up[MX][20];
void dfs(int v) {
for(int k=1;k<20;k++) {
up[v][k]=up[up[v][k-1]][k-1];
}
for(auto u:adj[v]) {
up[u][0]=v;
dfs(u);
}
}
int calc(int l, int r) {
int v=nxt[l], res=0;
if(v==1e9) return 0;
if(R[v]>r) return 0;
for(int k=19;k>=0;k--) {
if(up[v][k]!=0 && R[up[v][k]]<=r) {
res+=1<<k;
v=up[v][k];
}
}
return res+1;
}
bool chk(pair<int,int> p, pair<int,int> q) {
if(p.second<=q.first) return false;
return true;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N>>K;
for(int i=1;i<=N;i++) {
cin>>L[i]>>R[i];
id[L[i]]=0;
id[R[i]]=0;
}
int z=0;
for(auto &[x,y]:id) {
y=++z;
}
for(int i=1;i<=N;i++) {
L[i]=id[L[i]];
R[i]=id[R[i]];
ptL[L[i]].push_back({R[i],i});
ptR[R[i]].push_back({L[i],i});
}
pair<int,int> lst={1e9,1e9};
for(int p=z;p>=1;p--) {
for(auto [r,id]:ptL[p]) {
lst=min(lst,make_pair(r,id));
}
nxt[p]=lst.second;
for(auto [l,id]:ptR[p]) {
if(lst.second!=1e9) {
adj[lst.second].push_back(id);
}
}
}
for(int i=1;i<=N;i++) {
for(auto j:adj[i]) deg[j]+=1;
}
for(int i=1;i<=N;i++) {
if(!deg[i]) {
dfs(i);
}
}
set<pair<int,int>> ranges;
vector<int> ans;
int prv=calc(1,z);
for(int i=1;i<=N;i++) {
int a=1,b=z;
auto it=ranges.lower_bound(make_pair(R[i],0));
if(it!=ranges.begin()) {
it--;
if(chk(*it,make_pair(L[i],R[i]))) continue; // intersected
a=it->second;
}
auto itt=ranges.lower_bound(make_pair(R[i],0));
if(itt!=ranges.end()) {
b=itt->first;
}
if(prv+1+calc(a,L[i])+calc(R[i],b)-calc(a,b)>=K) {
assert(L[i]<=R[i]);
prv+=1+calc(a,L[i])+calc(R[i],b)-calc(a,b);
ranges.insert({L[i],R[i]});
ans.push_back(i);
}
if(ans.size()==K) break;
}
if(ans.size()<K) {
cout<<-1<<'\n';
return 0;
}
for(auto x:ans) {
cout<<x<<'\n';
}
}
컴파일 시 표준 에러 (stderr) 메시지
event2.cpp: In function 'int main()':
event2.cpp:114:16: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
114 | if(ans.size()==K) break;
| ~~~~~~~~~~^~~
event2.cpp:117:15: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
117 | if(ans.size()<K) {
| ~~~~~~~~~~^~
# | 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... |