#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
//consider the case where we only need k smallest from two different set of points
//if can do this in some reasonable time then apply zs bit trick
//+log factor
//need <= log^3 maybe?
//ok two set need log^2(?)
#define all(a) a.begin(),a.end()
#define pb push_back
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
const int MAXN = 2000000;
vector<ll>glo;
multiset<int,greater<int>>fen[MAXN];
multiset<int>::iterator it[MAXN];
vector<int>touch;
void upd(int i,int n,int v){
if(i<=0)return;
for(;i<=n;i+=i&(-i))fen[i].insert(v);
}
void query(int i){
while(i>0){
touch.pb(i);
i-=i&(-i);
}
}
#define fi first
#define se second
typedef pair<int,int> pii;
int main()
{
int n,k;
cin>>n>>k;
vector<array<int,2>>p(n);
vector<int>cy(n);
for(int i=0;i<n;i++)cin>>p[i][0]>>p[i][1];
sort(all(p));
vector<int>crd;
for(int i=0;i<n;i++)crd.push_back(p[i][1]);
sort(all(crd));
crd.resize(unique(all(crd)) - crd.begin());
for(int i=0;i<n;i++)cy[i] = lower_bound(all(crd),p[i][1]) - crd.begin()+1;
int m = crd.size();
priority_queue<pii,vector<pii>,greater<pii>>pq;
for(int i=0;i<n;i++){
touch.clear();
query(cy[i]);
int s = touch.size();
for(int j=0;j<s;j++){
int x = touch[j];
if(fen[x].empty())continue;
it[x] = fen[x].begin();
pq.push({*it[x],x});
}
for(int cnt=0;cnt<k;cnt++){
if(pq.empty())break;
pii v = pq.top();
pq.pop();
ll c = p[i][0] + p[i][1];
c-=v.fi;
glo.pb(c);
it[v.se]++;
if(it[v.se] == fen[v.se].end())continue;
pq.push({*it[v.se],v.se});
}
upd(cy[i],m,p[i][0]+p[i][1]);
}
//for(auto x:glo)cout<<x<<" ";
//cout<<'\n';
//cout<<ans<<'\n';
//y(i) < y(j) case
//we use x(i) - y(i) - (x(j)-y(j))
for(int i=1;i<=m;i++)fen[i].clear(); //i need to do the opposite directly
for(int i=n-1;i>=0;i--){
touch.clear();
query(cy[i]-1);
int s = touch.size();
for(int j=0;j<s;j++){
int x = touch[j];
if(fen[x].empty())continue;
it[x] = fen[x].begin();
pq.push({*it[x],x});
}
for(int cnt=0;cnt<k;cnt++){
if(pq.empty())break;
pii v = pq.top();
pq.pop();
ll c = p[i][1] - p[i][0];
c-=v.fi;
glo.pb(c);
it[v.se]++;
if(it[v.se] == fen[v.se].end())continue;
pq.push({*it[v.se],v.se});
}
upd(cy[i],m,p[i][1]-p[i][0]);
}
sort(all(glo));
for(int i=0;i<k;i++)cout<<glo[i]<<'\n';
//cout<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
116604 KB |
Output is correct |
2 |
Correct |
169 ms |
116620 KB |
Output is correct |
3 |
Correct |
108 ms |
114716 KB |
Output is correct |
4 |
Correct |
99 ms |
114724 KB |
Output is correct |
5 |
Correct |
149 ms |
115492 KB |
Output is correct |
6 |
Correct |
84 ms |
114164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4591 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4062 ms |
257676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4062 ms |
257676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
116604 KB |
Output is correct |
2 |
Correct |
169 ms |
116620 KB |
Output is correct |
3 |
Correct |
108 ms |
114716 KB |
Output is correct |
4 |
Correct |
99 ms |
114724 KB |
Output is correct |
5 |
Correct |
149 ms |
115492 KB |
Output is correct |
6 |
Correct |
84 ms |
114164 KB |
Output is correct |
7 |
Runtime error |
8803 ms |
2097152 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
116604 KB |
Output is correct |
2 |
Correct |
169 ms |
116620 KB |
Output is correct |
3 |
Correct |
108 ms |
114716 KB |
Output is correct |
4 |
Correct |
99 ms |
114724 KB |
Output is correct |
5 |
Correct |
149 ms |
115492 KB |
Output is correct |
6 |
Correct |
84 ms |
114164 KB |
Output is correct |
7 |
Runtime error |
4591 ms |
2097152 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |