#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,int>
#define f first
#define s second
using namespace std;
const int mxn = 2e6+5;
const ll inf = 1e15;
pii PP = {inf, -1};
struct node
{
node *left,*right;
pii val;
node(pii _v = PP, node *l = NULL, node *r = NULL) : val(_v), left(l), right(r){};
node *update(int l,int r,int p,ll v) {
if(l > p || r < p) return this;
if(l == r) {
return new node({v, l});
}
int mid = l + r >> 1;
node *ret = new node();
if(left == NULL) left = new node();
if(right == NULL) right = new node();
ret-> left = left-> update(l, mid, p, v);
ret-> right = right-> update(mid + 1, r, p, v);
ret-> val = min(ret-> left -> val, ret-> right -> val);
return ret;
}
pii query(int l,int r, int s, int t) {
if(l > t || r < s) return {inf,-1};
if(l >= s && r <= t) return val;
int mid = l + r >> 1;
if(left == NULL) left = new node();
if(right == NULL) right = new node();
return min(left-> query(l, mid, s, t), right-> query(mid + 1, r, s, t));
}
}*root1[mxn], *root2[mxn];
int pos[mxn], inv[mxn];
int id1[mxn], id2[mxn];
int n,k;
vector<pair<int,int>> pnt;
// void build(int a*){
// root[0]= new node(0,new node(),new node());
// for(int i=1;i<=n;i++) root[i]=root[i-1]->update(1,mxn,a[i]);
// }
int main() {
cin >> n >> k;
root1[0] = new node(PP, new node(), new node());
root2[0] = new node(PP, new node(), new node());
vector<pair<int,int>> vv;
for(int i = 1; i <= n; i++) {
int x,y;
assert(scanf("%d %d",&x,&y));
vv.emplace_back(y, x);
pnt.emplace_back(x,y);
}
sort(pnt.begin(), pnt.end());
pnt.insert(pnt.begin(), {0,0});
sort(vv.begin(),vv.end());
priority_queue< tuple<ll,int, int, int> > pq;
for(int i = 1; i <= n; i++) {
auto [x,y] = pnt[i];
pos[i] = lower_bound(vv.begin(), vv.end(), make_pair(y, x)) - vv.begin() + 1;
inv[pos[i]] = i;
pii ed1 = root1[i - 1]-> query(1, n, pos[i], n);
pii ed2 = root2[i - 1]-> query(1, n, 1, pos[i] - 1);
pq.push({-(ed1.f + x - y), pos[i], ed1.s, 1});
pq.push({-(ed2.f + x + y), pos[i], ed2.s, -1});
// cout<<x<<" "<<y<<" "<<pos[i]<<" "<<ed1.s<<" "<<ed2.s<<" ==== "<<i<<endl;
root1[i] = root1[i - 1]-> update(1, n, pos[i], y - x);
root2[i] = root2[i - 1]-> update(1, n, pos[i], - y - x);
id1[i] = i;
id2[i] = i;
}
while(k--) {
auto [dist, u, v, tp] = pq.top();
pq.pop();
dist = -dist;
u = inv[u];
v = inv[v];
printf("%lld\n", dist);
if(tp == 1) {
root1[u] = root1[u]-> update(1, n, pos[v], inf);
pii ed = root1[u]-> query(1, n, pos[u], n);
auto [x,y] = pnt[u];
pq.push({-(ed.f + x - y), pos[u], ed.s, 1});
} else {
root2[u] = root2[u]-> update(1, n, pos[v], inf);
pii ed = root2[u]-> query(1, n, 1, pos[u] - 1);
auto [x,y] = pnt[u];
pq.push({-(ed.f + x + y), pos[u], ed.s, -1});
}
}
}
Compilation message
road_construction.cpp: In constructor 'node::node(std::pair<long long int, int>, node*, node*)':
road_construction.cpp:13:9: warning: 'node::val' will be initialized after [-Wreorder]
13 | pii val;
| ^~~
road_construction.cpp:12:11: warning: 'node* node::left' [-Wreorder]
12 | node *left,*right;
| ^~~~
road_construction.cpp:14:5: warning: when initialized here [-Wreorder]
14 | node(pii _v = PP, node *l = NULL, node *r = NULL) : val(_v), left(l), right(r){};
| ^~~~
road_construction.cpp: In member function 'node* node::update(int, int, int, long long int)':
road_construction.cpp:22:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int mid = l + r >> 1;
| ~~^~~
road_construction.cpp: In member function 'std::pair<long long int, int> node::query(int, int, int, int)':
road_construction.cpp:36:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
450 ms |
133024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1494 ms |
742568 KB |
Output is correct |
2 |
Correct |
1479 ms |
742732 KB |
Output is correct |
3 |
Correct |
287 ms |
127552 KB |
Output is correct |
4 |
Correct |
1159 ms |
742380 KB |
Output is correct |
5 |
Correct |
1223 ms |
742548 KB |
Output is correct |
6 |
Correct |
1155 ms |
742552 KB |
Output is correct |
7 |
Correct |
1235 ms |
742112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1179 ms |
520888 KB |
Output is correct |
2 |
Correct |
1242 ms |
520872 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
787 ms |
518820 KB |
Output is correct |
5 |
Correct |
964 ms |
521312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1179 ms |
520888 KB |
Output is correct |
2 |
Correct |
1242 ms |
520872 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
787 ms |
518820 KB |
Output is correct |
5 |
Correct |
964 ms |
521312 KB |
Output is correct |
6 |
Incorrect |
1251 ms |
520880 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
450 ms |
133024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
450 ms |
133024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |