#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define all(a) a.begin(),a.end()
#define pb push_back
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define fi first
#define se second
#define sz(a) (int)(a.size())
typedef pair<int,int> pii;
vector<ll>glo;
const int MAXN = 1e8,INF = 2e9+1;
pii t[MAXN];
int ch[2][MAXN];
int ndcnt = 0;
int nwnode(int v = -1){
ndcnt++;
int u = ndcnt;
t[u] = {-INF,0};
if(v!=-1){
t[u] = t[v];
ch[0][u] = ch[0][v];
ch[1][u] = ch[1][v];
}
return u;
}
void pull(int v){
t[v] = {-INF,0};
if(ch[0][v])t[v] = max(t[v],t[ch[0][v]]);
if(ch[1][v])t[v] = max(t[v],t[ch[1][v]]);
}
int build(int l,int r){
if(l==r){
int u = nwnode();
t[u].se = l;
return u;
}
int u = nwnode();
int tm = (l+r)/2;
ch[0][u] = build(l,tm);
ch[1][u] = build(tm+1,r);
pull(u);
return u;
}
int upd(int v,int l,int r,int pos,int val){
if(l==r){
int u = nwnode();
t[u] = {val,pos};
//cout<<l<<" "<<r<<" "<<t[u].fi<<" "<<t[u].se<<'\n';
return u;
}
int u = nwnode(v);
int tm = (l+r)/2;
if(pos <= tm)ch[0][u] = upd(ch[0][v],l,tm,pos,val);
else ch[1][u] = upd(ch[1][v],tm+1,r,pos,val);
pull(u);
//cout<<l<<" "<<r<<" "<<t[u].fi<<" "<<t[u].se<<'\n';
return u;
}
pii query(int v,int l,int r,int tl,int tr){
if(l>r)return {-INF,0};
if(l==tl && r==tr)return t[v];
int tm = (tl+tr)/2;
return max(query(ch[0][v],l,min(r,tm),tl,tm),
query(ch[1][v],max(l,tm+1),r,tm+1,tr));
}
void reset(){
for(int i=0;i<=ndcnt;i++){
t[i] = {-INF,0};
ch[0][i] = ch[1][i] = 0;
}
ndcnt = 0;
}
vector<array<int,4>>seg;
void fetch(int v,int l,int r,int tl,int tr){
//cout<<l<<" "<<r<<" "<<tl<<" "<<tr<<'\n';
if(l>r)return;
if(l==tl && r==tr){
seg.pb({v,l,r,0});
return;
}
int tm = (tl+tr)/2;
fetch(ch[0][v],l,min(r,tm),tl,tm);
fetch(ch[1][v],max(l,tm+1),r,tm+1,tr);
}
int main()
{
owo
int n,k;
cin>>n>>k;
vector<pii>p(n),y(n);
vector<int>id(n);
for(int i=0;i<n;i++)cin>>p[i].fi>>p[i].se;
sort(all(p));
for(int i=0;i<n;i++){
y[i].fi = p[i].se;
y[i].se = i;
}
sort(all(y));
map<int,int>r; //right border of fixed value of y
//cout<<"sort\n";
for(int i=0;i<n;i++){
//cout<<p[i].fi<<" "<<p[i].se<<'\n';
r[y[i].fi] = i;
id[y[i].se] = i;
}
//for(int i=0;i<n;i++)cout<<id[i]<<" ";
//cout<<'\n';
int root = build(0,n-1);
for(int i=0;i<n;i++){
int oldsz = sz(seg);
fetch(root,0,r[p[i].se],0,n-1);
for(int j=oldsz;j<sz(seg);j++)seg[j][3] = i;
root = upd(root,0,n-1,id[i],p[i].fi+p[i].se);
}
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>pq;
int m = sz(seg);
for(int i=0;i<m;i++){
pii res = t[seg[i][0]];
if(res.fi == -INF)continue;
int id = seg[i][3];
ll c = p[id].fi+p[id].se;
c-=res.fi;
pq.push({c,i});
}
for(int j=0;j<k;j++){
if(pq.empty())break;
glo.pb(pq.top().fi);
int i = pq.top().se;
pq.pop();
pii res = t[seg[i][0]];
seg[i][0] = upd(seg[i][0],seg[i][1],seg[i][2],res.se,-INF);
res = t[seg[i][0]];
if(res.fi == -INF)continue;
int id = seg[i][3];
ll c = p[id].fi+p[id].se;
c-=res.fi;
pq.push({c,i});
}
reset();
for(int i=0;i<n;i++){
if(r[p[i].se] == n-1)continue;
int oldsz = sz(seg);
fetch(root,r[p[i].se]+1,n-1,0,n-1);
for(int j=oldsz;j<sz(seg);j++)seg[j][3] = i;
root = upd(root,0,n-1,id[i],p[i].fi-p[i].se);
}
while(!pq.empty())pq.pop();
m = sz(seg);
for(int i=0;i<m;i++){
pii res = t[seg[i][0]];
if(res.fi == -INF)continue;
int id = seg[i][3];
ll c = p[id].fi-p[id].se;
c-=res.fi;
pq.push({c,i});
}
for(int j=0;j<k;j++){
if(pq.empty())break;
glo.pb(pq.top().fi);
int i = pq.top().se;
pq.pop();
pii res = t[seg[i][0]];
seg[i][0] = upd(seg[i][0],seg[i][1],seg[i][2],res.se,-INF);
res = t[seg[i][0]];
if(res.fi == -INF)continue;
int id = seg[i][3];
ll c = p[id].fi-p[id].se;
c-=res.fi;
pq.push({c,i});
}
sort(all(glo));
for(int i=0;i<k;i++)cout<<glo[i]<<'\n';
//cout<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
191 ms |
42660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
517 ms |
174576 KB |
Output is correct |
2 |
Correct |
507 ms |
174440 KB |
Output is correct |
3 |
Correct |
126 ms |
46376 KB |
Output is correct |
4 |
Correct |
357 ms |
174244 KB |
Output is correct |
5 |
Correct |
347 ms |
174412 KB |
Output is correct |
6 |
Correct |
339 ms |
174456 KB |
Output is correct |
7 |
Correct |
403 ms |
173676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1924 ms |
261816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1924 ms |
261816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
191 ms |
42660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
191 ms |
42660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |