#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll
typedef pair<int,int> pii;
const int N=6e5+99,inf=1e10;
const pii pinf={-10*inf,-1};
int n,k,a[N],b[N],pos[N];
pii seg[N];
vector<int> ans;
vector<pii> vec;
map<int,vector<int>> mark;
void add(int x){
seg[x+n]={vec[x].F+vec[x].S,x};
for(x+=n;x>1;x>>=1){
seg[x>>1]=max(seg[x],seg[x^1]);
}
}
void del(int x){
seg[x+n]=pinf;
for(x+=n;x>1;x>>=1){
seg[x>>1]=max(seg[x],seg[x^1]);
}
}
pii get(int l,int r){
pii res=pinf;
for(l+=n,r+=n;l<r;l>>=1,r>>=1){
if(l&1) maxm(res,seg[l++]);
if(r&1) maxm(res,seg[--r]);
}
return res;
}
bool check(int val,int type=0){
fill(seg,seg+N,pinf);
ans.clear();
int cnt=0;
for(auto p : mark){
vector<int> v=p.S;
for(auto x : v) add(x);
for(auto x : v){
int p=upper_bound(all(vec),mp(vec[x].F,-inf))-vec.begin();
vector<int> op;
for(pii e=get(0,p);vec[x].F+vec[x].S-e.F<=val;e=get(0,p)){
cnt++;
op.pb(e.S);
del(e.S);
if(type==1) ans.pb(vec[x].F+vec[x].S-e.F);
if(cnt>=k) return 0;
}
for(auto id : op) add(id);
}
}
return 1;
}
vector<int> solve(){
f(i,0,n) vec.pb({a[i],b[i]});
sort(all(vec));
f(i,0,n) pos[i]=lower_bound(all(vec),mp(a[i],b[i]))-vec.begin();
f(i,0,n) mark[b[i]].pb(pos[i]);
int l=0,r=inf;
while(l+1<r){
int mid=(l+r)>>1;
if(check(mid)) l=mid;
else r=mid;
}
check(l,1);
vec.clear();
mark.clear();
while(ans.size()<k) ans.pb(r);
return ans;
}
int32_t main(){
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>n>>k;
f(i,0,n) cin>>a[i]>>b[i];
vector<int> A,B;
A=solve();
f(i,0,n) a[i]=-a[i],swap(a[i],b[i]);
B=solve();
for(auto x : B) A.pb(x);
sort(all(A));
f(i,0,k) cout<<A[i]<<" ";
}
/*
3 2
0 1
0 0
2 0
3 2
-1 0
0 0
0 2
*/
Compilation message
road_construction.cpp: In function 'std::vector<long long int> solve()':
road_construction.cpp:92:18: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
92 | while(ans.size()<k) ans.pb(r);
| ~~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2788 ms |
20452 KB |
Output is correct |
2 |
Correct |
2816 ms |
20488 KB |
Output is correct |
3 |
Correct |
1504 ms |
20560 KB |
Output is correct |
4 |
Correct |
1212 ms |
20540 KB |
Output is correct |
5 |
Correct |
2400 ms |
19468 KB |
Output is correct |
6 |
Correct |
98 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8326 ms |
52504 KB |
Output is correct |
2 |
Correct |
8363 ms |
52076 KB |
Output is correct |
3 |
Correct |
936 ms |
20244 KB |
Output is correct |
4 |
Correct |
7354 ms |
51864 KB |
Output is correct |
5 |
Correct |
8023 ms |
52124 KB |
Output is correct |
6 |
Correct |
8042 ms |
52196 KB |
Output is correct |
7 |
Correct |
7832 ms |
51348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5740 ms |
46904 KB |
Output is correct |
2 |
Correct |
4739 ms |
46904 KB |
Output is correct |
3 |
Correct |
66 ms |
9684 KB |
Output is correct |
4 |
Correct |
5149 ms |
46784 KB |
Output is correct |
5 |
Correct |
2779 ms |
21828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5740 ms |
46904 KB |
Output is correct |
2 |
Correct |
4739 ms |
46904 KB |
Output is correct |
3 |
Correct |
66 ms |
9684 KB |
Output is correct |
4 |
Correct |
5149 ms |
46784 KB |
Output is correct |
5 |
Correct |
2779 ms |
21828 KB |
Output is correct |
6 |
Correct |
6321 ms |
46836 KB |
Output is correct |
7 |
Correct |
6587 ms |
46864 KB |
Output is correct |
8 |
Correct |
62 ms |
9684 KB |
Output is correct |
9 |
Correct |
65 ms |
9728 KB |
Output is correct |
10 |
Correct |
6491 ms |
43904 KB |
Output is correct |
11 |
Correct |
5050 ms |
46916 KB |
Output is correct |
12 |
Correct |
2895 ms |
21840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2788 ms |
20452 KB |
Output is correct |
2 |
Correct |
2816 ms |
20488 KB |
Output is correct |
3 |
Correct |
1504 ms |
20560 KB |
Output is correct |
4 |
Correct |
1212 ms |
20540 KB |
Output is correct |
5 |
Correct |
2400 ms |
19468 KB |
Output is correct |
6 |
Correct |
98 ms |
9820 KB |
Output is correct |
7 |
Correct |
8412 ms |
34532 KB |
Output is correct |
8 |
Correct |
8457 ms |
34428 KB |
Output is correct |
9 |
Correct |
1230 ms |
20472 KB |
Output is correct |
10 |
Correct |
7337 ms |
33488 KB |
Output is correct |
11 |
Correct |
5987 ms |
32776 KB |
Output is correct |
12 |
Correct |
5804 ms |
25036 KB |
Output is correct |
13 |
Correct |
4686 ms |
23216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2788 ms |
20452 KB |
Output is correct |
2 |
Correct |
2816 ms |
20488 KB |
Output is correct |
3 |
Correct |
1504 ms |
20560 KB |
Output is correct |
4 |
Correct |
1212 ms |
20540 KB |
Output is correct |
5 |
Correct |
2400 ms |
19468 KB |
Output is correct |
6 |
Correct |
98 ms |
9820 KB |
Output is correct |
7 |
Correct |
8326 ms |
52504 KB |
Output is correct |
8 |
Correct |
8363 ms |
52076 KB |
Output is correct |
9 |
Correct |
936 ms |
20244 KB |
Output is correct |
10 |
Correct |
7354 ms |
51864 KB |
Output is correct |
11 |
Correct |
8023 ms |
52124 KB |
Output is correct |
12 |
Correct |
8042 ms |
52196 KB |
Output is correct |
13 |
Correct |
7832 ms |
51348 KB |
Output is correct |
14 |
Correct |
5740 ms |
46904 KB |
Output is correct |
15 |
Correct |
4739 ms |
46904 KB |
Output is correct |
16 |
Correct |
66 ms |
9684 KB |
Output is correct |
17 |
Correct |
5149 ms |
46784 KB |
Output is correct |
18 |
Correct |
2779 ms |
21828 KB |
Output is correct |
19 |
Correct |
6321 ms |
46836 KB |
Output is correct |
20 |
Correct |
6587 ms |
46864 KB |
Output is correct |
21 |
Correct |
62 ms |
9684 KB |
Output is correct |
22 |
Correct |
65 ms |
9728 KB |
Output is correct |
23 |
Correct |
6491 ms |
43904 KB |
Output is correct |
24 |
Correct |
5050 ms |
46916 KB |
Output is correct |
25 |
Correct |
2895 ms |
21840 KB |
Output is correct |
26 |
Correct |
8412 ms |
34532 KB |
Output is correct |
27 |
Correct |
8457 ms |
34428 KB |
Output is correct |
28 |
Correct |
1230 ms |
20472 KB |
Output is correct |
29 |
Correct |
7337 ms |
33488 KB |
Output is correct |
30 |
Correct |
5987 ms |
32776 KB |
Output is correct |
31 |
Correct |
5804 ms |
25036 KB |
Output is correct |
32 |
Correct |
4686 ms |
23216 KB |
Output is correct |
33 |
Execution timed out |
10033 ms |
50912 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |