#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,k,nxL[100005][18],nxR[100005][18];
const int mx=200010;
pii seg[100005];
set<pii> range;
map<int,int> mp;
pii fen1[200015], fen2[200015];
int find1(int x) {
int ma=-INT_MAX, ans;
while (x) {
if (fen1[x].first && ma<=fen1[x].first){
ma=fen1[x].first;
ans=fen1[x].second;
}
x-=x&-x;
}
return ans;
}
void update1(int x, int val, int idx) {
while (x<=mx+2) {
if (fen1[x].first==0 || val>fen1[x].first) {
fen1[x]=pii(val,idx);
}
x+=x&-x;
}
}
int find2(int x) {
int mi=INT_MAX, ans;
while (x) {
if (fen2[x].first && fen2[x].first<=mi) {
mi=fen2[x].first;
ans=fen2[x].second;
}
x-=x&-x;
}
return ans;
}
void update2(int x, int val, int idx) {
while (x<=mx+2) {
if (fen2[x].first==0 || fen2[x].first>val) fen2[x]=pii(val,idx);
x+=x&-x;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie();
cin>>n>>k;
seg[0]=pii(-INT_MAX,-INT_MAX);
seg[n+1]=pii(INT_MAX,INT_MAX);
mp[-INT_MAX]=0; mp[INT_MAX]=0;
for (int i=0; i<18; ++i) nxL[0][i]=0, nxR[n+1][i]=n+1;
for (int i=1; i<=n; ++i) {
cin>>seg[i].first>>seg[i].second;
mp[seg[i].first]=0;
mp[seg[i].second]=0;
}
int cnt=0;
for (auto s : mp) mp[s.first]=++cnt;
//find L
for (int i=0; i<=n; ++i) update1(mp[seg[i].second],seg[i].first,i);
for (int i=n; i>0; --i) {
nxL[i][0]=find1(mp[seg[i].first]);
for (int j=1; j<18; ++j) nxL[i][j]=nxL[nxL[i][j-1]][j-1];
}
//find R
for (int i=1; i<=n+1; ++i) update2(mx-mp[seg[i].first],seg[i].second,i);
for (int i=n; i>0; --i) {
nxR[i][0]=find2(mx-mp[seg[i].second]);
for (int j=1; j<18; ++j) nxR[i][j]=nxR[nxR[i][j-1]][j-1];
}
vector<int> ans;
range.insert(pii(1,1e9));
for (int i=1; i<=n; ++i) {
auto it=range.upper_bound(pii(seg[i].first,INT_MAX));
if (it==range.begin()) continue;
--it;
if ((*it).second<seg[i].second) continue;
int l=(*it).first, r=(*it).second, cnt=0;
int idx=i;
for (int j=17; j>=0; --j) {
if (seg[nxL[idx][j]].first>=l) idx=nxL[idx][j], cnt+=(1<<j);
}
idx=i;
for (int j=17; j>=0; --j) {
if (seg[nxR[idx][j]].second<=r) idx=nxR[idx][j], cnt+=(1<<j);
}
if (cnt>=k-ans.size()-1) {
ans.push_back(i);
range.erase(it);
if (seg[i].first!=l) range.insert(pii(l,seg[i].first));
if (seg[i].second!=r) range.insert(pii(seg[i].second,r));
}
if (ans.size()==k) {
for (auto s : ans) cout<<s<<"\n";
return 0;
}
}
cout<<-1;
}
Compilation message
event2.cpp: In function 'int main()':
event2.cpp:105:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | if (cnt>=k-ans.size()-1) {
| ~~~^~~~~~~~~~~~~~~~
event2.cpp:112:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
112 | if (ans.size()==k) {
| ~~~~~~~~~~^~~
event2.cpp: In function 'int find1(int)':
event2.cpp:21:12: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
21 | return ans;
| ^~~
event2.cpp: In function 'int find2(int)':
event2.cpp:42:12: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
42 | return ans;
| ^~~
event2.cpp: In function 'int main()':
event2.cpp:81:18: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
81 | nxR[i][0]=find2(mx-mp[seg[i].second]);
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
event2.cpp:74:18: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
74 | nxL[i][0]=find1(mp[seg[i].first]);
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
141 ms |
34924 KB |
Output is correct |
5 |
Correct |
148 ms |
34648 KB |
Output is correct |
6 |
Correct |
143 ms |
34316 KB |
Output is correct |
7 |
Correct |
140 ms |
33796 KB |
Output is correct |
8 |
Correct |
139 ms |
34816 KB |
Output is correct |
9 |
Correct |
142 ms |
34624 KB |
Output is correct |
10 |
Correct |
135 ms |
34372 KB |
Output is correct |
11 |
Correct |
130 ms |
33872 KB |
Output is correct |
12 |
Correct |
160 ms |
32876 KB |
Output is correct |
13 |
Correct |
128 ms |
32768 KB |
Output is correct |
14 |
Correct |
125 ms |
32584 KB |
Output is correct |
15 |
Correct |
122 ms |
32248 KB |
Output is correct |
16 |
Correct |
113 ms |
30364 KB |
Output is correct |
17 |
Correct |
111 ms |
30200 KB |
Output is correct |
18 |
Correct |
110 ms |
30148 KB |
Output is correct |
19 |
Correct |
112 ms |
29716 KB |
Output is correct |
20 |
Correct |
139 ms |
29624 KB |
Output is correct |
21 |
Correct |
114 ms |
29692 KB |
Output is correct |
22 |
Correct |
118 ms |
29604 KB |
Output is correct |
23 |
Correct |
123 ms |
29696 KB |
Output is correct |
24 |
Correct |
119 ms |
29604 KB |
Output is correct |
25 |
Correct |
137 ms |
29476 KB |
Output is correct |
26 |
Correct |
139 ms |
29576 KB |
Output is correct |
27 |
Correct |
161 ms |
29580 KB |
Output is correct |
28 |
Correct |
120 ms |
29648 KB |
Output is correct |
29 |
Correct |
106 ms |
29592 KB |
Output is correct |
30 |
Correct |
109 ms |
29592 KB |
Output is correct |
31 |
Correct |
113 ms |
29592 KB |
Output is correct |
32 |
Correct |
158 ms |
29644 KB |
Output is correct |
33 |
Correct |
144 ms |
29496 KB |
Output is correct |
34 |
Correct |
119 ms |
31608 KB |
Output is correct |
35 |
Correct |
120 ms |
30952 KB |
Output is correct |
36 |
Correct |
105 ms |
30288 KB |
Output is correct |
37 |
Correct |
153 ms |
32444 KB |
Output is correct |
38 |
Correct |
162 ms |
32336 KB |
Output is correct |
39 |
Correct |
151 ms |
32156 KB |
Output is correct |
40 |
Correct |
149 ms |
32152 KB |
Output is correct |
41 |
Correct |
149 ms |
31984 KB |
Output is correct |
42 |
Correct |
116 ms |
29588 KB |
Output is correct |
43 |
Correct |
125 ms |
32292 KB |
Output is correct |
44 |
Correct |
126 ms |
32216 KB |
Output is correct |
45 |
Correct |
122 ms |
32100 KB |
Output is correct |
46 |
Correct |
122 ms |
31980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
141 ms |
34924 KB |
Output is correct |
5 |
Correct |
148 ms |
34648 KB |
Output is correct |
6 |
Correct |
143 ms |
34316 KB |
Output is correct |
7 |
Correct |
140 ms |
33796 KB |
Output is correct |
8 |
Correct |
139 ms |
34816 KB |
Output is correct |
9 |
Correct |
142 ms |
34624 KB |
Output is correct |
10 |
Correct |
135 ms |
34372 KB |
Output is correct |
11 |
Correct |
130 ms |
33872 KB |
Output is correct |
12 |
Correct |
160 ms |
32876 KB |
Output is correct |
13 |
Correct |
128 ms |
32768 KB |
Output is correct |
14 |
Correct |
125 ms |
32584 KB |
Output is correct |
15 |
Correct |
122 ms |
32248 KB |
Output is correct |
16 |
Correct |
113 ms |
30364 KB |
Output is correct |
17 |
Correct |
111 ms |
30200 KB |
Output is correct |
18 |
Correct |
110 ms |
30148 KB |
Output is correct |
19 |
Correct |
112 ms |
29716 KB |
Output is correct |
20 |
Correct |
139 ms |
29624 KB |
Output is correct |
21 |
Correct |
114 ms |
29692 KB |
Output is correct |
22 |
Correct |
118 ms |
29604 KB |
Output is correct |
23 |
Correct |
123 ms |
29696 KB |
Output is correct |
24 |
Correct |
119 ms |
29604 KB |
Output is correct |
25 |
Correct |
137 ms |
29476 KB |
Output is correct |
26 |
Correct |
139 ms |
29576 KB |
Output is correct |
27 |
Correct |
161 ms |
29580 KB |
Output is correct |
28 |
Correct |
120 ms |
29648 KB |
Output is correct |
29 |
Correct |
106 ms |
29592 KB |
Output is correct |
30 |
Correct |
109 ms |
29592 KB |
Output is correct |
31 |
Correct |
113 ms |
29592 KB |
Output is correct |
32 |
Correct |
158 ms |
29644 KB |
Output is correct |
33 |
Correct |
144 ms |
29496 KB |
Output is correct |
34 |
Correct |
119 ms |
31608 KB |
Output is correct |
35 |
Correct |
120 ms |
30952 KB |
Output is correct |
36 |
Correct |
105 ms |
30288 KB |
Output is correct |
37 |
Correct |
153 ms |
32444 KB |
Output is correct |
38 |
Correct |
162 ms |
32336 KB |
Output is correct |
39 |
Correct |
151 ms |
32156 KB |
Output is correct |
40 |
Correct |
149 ms |
32152 KB |
Output is correct |
41 |
Correct |
149 ms |
31984 KB |
Output is correct |
42 |
Correct |
116 ms |
29588 KB |
Output is correct |
43 |
Correct |
125 ms |
32292 KB |
Output is correct |
44 |
Correct |
126 ms |
32216 KB |
Output is correct |
45 |
Correct |
122 ms |
32100 KB |
Output is correct |
46 |
Correct |
122 ms |
31980 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
336 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
54 |
Halted |
0 ms |
0 KB |
- |