#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll nx=250005, inf=1e9;
ll n, k, res=1e18, idxx;
pair<ll, ll> p[nx];
map<ll, vector<ll>> mp;
priority_queue<tuple<ll, ll, ll, ll>, vector<tuple<ll, ll, ll, ll>>, greater<tuple<ll, ll, ll, ll>>> pq;
struct persist
{
struct node
{
pair<ll, ll> mn;
node *l, *r;
node(pair<ll, ll> mn={1e18, 0}): mn(mn), l(0), r(0){}
};
typedef node* pnode;
pnode rt[nx], srt[nx];
void update(int l, int r, pnode &k, pnode t, int idx, ll vl)
{
//printf("update %d %d\n", l, r);
if (t) k=new node(*t);
else k=new node();
if (l==r) return k->mn={vl, idx}, void();
ll md=l+(r-l)/2;
if (idx<=md) update(l, md, k->l, t?t->l:0, idx, vl);
else update(md+1, r, k->r, t?t->r:0, idx, vl);
k->mn=min((k->l)?k->l->mn:make_pair((ll)1e18, (ll)0), (k->r)?k->r->mn:make_pair((ll)1e18, (ll)0));
}
pair<ll, ll> query(int l, int r, pnode k, int ql, int qr)
{
if (r<ql||qr<l|!k||qr<ql) return {1e18, 0};
if (ql<=l&&r<=qr) return k->mn;
ll md=l+(r-l)/2;
return min(query(l, md, k->l, ql, qr), query(md+1, r, k->r, ql, qr));
}
} up, dn;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k;
for (int i=1; i<=n; i++) cin>>p[i].first>>p[i].second;
sort(p+1, p+n+1);
for (int i=1; i<=n; i++)
{
up.srt[i]=up.rt[i];
dn.srt[i]=dn.rt[i];
auto upq=up.query(-inf, inf, up.srt[i], p[i].second, inf);
upq.first+=p[i].first-p[i].second;
auto dnq=dn.query(-inf, inf, dn.srt[i], -inf, p[i].second-1);
dnq.first+=p[i].first+p[i].second;
auto tmp=min(upq, dnq);
//printf("here %d %lld %lld\n", i, tmp.first, tmp.second);
up.update(-inf, inf, up.rt[i+1], up.rt[i], p[i].second, -p[i].first+p[i].second);
dn.update(-inf, inf, dn.rt[i+1], dn.rt[i], p[i].second, -p[i].first-p[i].second);
if (tmp.first<1e10)
{
if (tmp.second>=p[i].second) up.update(-inf, inf, up.srt[i], up.srt[i], tmp.second, 1e18);
else dn.update(-inf, inf, dn.srt[i], dn.srt[i], tmp.second, 1e18);
idxx=mp[tmp.second].back();
pq.push({tmp.first, i, tmp.second, idxx});
}
mp[p[i].second].push_back(p[i].first);
}
for (int t=0; t<k; t++)
{
auto [w, idx, idxy, idxx]=pq.top();
pq.pop();
cout<<w<<'\n';
auto itr=lower_bound(mp[idxy].begin(), mp[idxy].end(), idxx);
if (itr!=mp[idxy].begin()) pq.push({w+*itr-*prev(itr), idx, idxy, *prev(itr)});
auto upq=up.query(-inf, inf, up.srt[idx], p[idx].second, inf);
upq.first+=+p[idx].first-p[idx].second;
auto dnq=dn.query(-inf, inf, dn.srt[idx], -inf, p[idx].second-1);
dnq.first+=+p[idx].first+p[idx].second;
auto tmp=min(upq, dnq);
if (tmp.first<1e10)
{
if (tmp.second>=p[idx].second) up.update(-inf, inf, up.srt[idx], up.srt[idx], tmp.second, 1e18);
else dn.update(-inf, inf, dn.srt[idx], dn.srt[idx], tmp.second, 1e18);
idxx=p[idx].first-(tmp.first-abs(p[idx].second-tmp.second));
pq.push({tmp.first, idx, tmp.second, idxx});
}
}
}
Compilation message
road_construction.cpp: In member function 'std::pair<long long int, long long int> persist::query(int, int, persist::pnode, int, int)':
road_construction.cpp:37:21: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
37 | if (r<ql||qr<l|!k||qr<ql) return {1e18, 0};
| ~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
649 ms |
388712 KB |
Output is correct |
2 |
Correct |
646 ms |
388692 KB |
Output is correct |
3 |
Correct |
653 ms |
385704 KB |
Output is correct |
4 |
Correct |
439 ms |
250044 KB |
Output is correct |
5 |
Correct |
711 ms |
387372 KB |
Output is correct |
6 |
Correct |
9 ms |
14192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1635 ms |
1115012 KB |
Output is correct |
2 |
Correct |
1591 ms |
1118548 KB |
Output is correct |
3 |
Correct |
173 ms |
12112 KB |
Output is correct |
4 |
Correct |
1069 ms |
1118180 KB |
Output is correct |
5 |
Correct |
1249 ms |
1118388 KB |
Output is correct |
6 |
Correct |
1301 ms |
1118128 KB |
Output is correct |
7 |
Correct |
1306 ms |
1117748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1865 ms |
1174940 KB |
Output is correct |
2 |
Correct |
1855 ms |
1174704 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
919 ms |
1115736 KB |
Output is correct |
5 |
Correct |
1194 ms |
1149412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1865 ms |
1174940 KB |
Output is correct |
2 |
Correct |
1855 ms |
1174704 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
919 ms |
1115736 KB |
Output is correct |
5 |
Correct |
1194 ms |
1149412 KB |
Output is correct |
6 |
Correct |
1840 ms |
1174760 KB |
Output is correct |
7 |
Correct |
1886 ms |
1177000 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1782 ms |
1173720 KB |
Output is correct |
11 |
Correct |
872 ms |
1116864 KB |
Output is correct |
12 |
Correct |
1092 ms |
1151852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
649 ms |
388712 KB |
Output is correct |
2 |
Correct |
646 ms |
388692 KB |
Output is correct |
3 |
Correct |
653 ms |
385704 KB |
Output is correct |
4 |
Correct |
439 ms |
250044 KB |
Output is correct |
5 |
Correct |
711 ms |
387372 KB |
Output is correct |
6 |
Correct |
9 ms |
14192 KB |
Output is correct |
7 |
Correct |
2159 ms |
852380 KB |
Output is correct |
8 |
Correct |
2213 ms |
852440 KB |
Output is correct |
9 |
Correct |
450 ms |
257484 KB |
Output is correct |
10 |
Correct |
1350 ms |
849588 KB |
Output is correct |
11 |
Correct |
1561 ms |
851628 KB |
Output is correct |
12 |
Correct |
1008 ms |
850616 KB |
Output is correct |
13 |
Correct |
1087 ms |
850200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
649 ms |
388712 KB |
Output is correct |
2 |
Correct |
646 ms |
388692 KB |
Output is correct |
3 |
Correct |
653 ms |
385704 KB |
Output is correct |
4 |
Correct |
439 ms |
250044 KB |
Output is correct |
5 |
Correct |
711 ms |
387372 KB |
Output is correct |
6 |
Correct |
9 ms |
14192 KB |
Output is correct |
7 |
Correct |
1635 ms |
1115012 KB |
Output is correct |
8 |
Correct |
1591 ms |
1118548 KB |
Output is correct |
9 |
Correct |
173 ms |
12112 KB |
Output is correct |
10 |
Correct |
1069 ms |
1118180 KB |
Output is correct |
11 |
Correct |
1249 ms |
1118388 KB |
Output is correct |
12 |
Correct |
1301 ms |
1118128 KB |
Output is correct |
13 |
Correct |
1306 ms |
1117748 KB |
Output is correct |
14 |
Correct |
1865 ms |
1174940 KB |
Output is correct |
15 |
Correct |
1855 ms |
1174704 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
919 ms |
1115736 KB |
Output is correct |
18 |
Correct |
1194 ms |
1149412 KB |
Output is correct |
19 |
Correct |
1840 ms |
1174760 KB |
Output is correct |
20 |
Correct |
1886 ms |
1177000 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1782 ms |
1173720 KB |
Output is correct |
24 |
Correct |
872 ms |
1116864 KB |
Output is correct |
25 |
Correct |
1092 ms |
1151852 KB |
Output is correct |
26 |
Correct |
2159 ms |
852380 KB |
Output is correct |
27 |
Correct |
2213 ms |
852440 KB |
Output is correct |
28 |
Correct |
450 ms |
257484 KB |
Output is correct |
29 |
Correct |
1350 ms |
849588 KB |
Output is correct |
30 |
Correct |
1561 ms |
851628 KB |
Output is correct |
31 |
Correct |
1008 ms |
850616 KB |
Output is correct |
32 |
Correct |
1087 ms |
850200 KB |
Output is correct |
33 |
Correct |
3399 ms |
1553936 KB |
Output is correct |
34 |
Correct |
3447 ms |
1553840 KB |
Output is correct |
35 |
Correct |
2319 ms |
1544368 KB |
Output is correct |
36 |
Correct |
1641 ms |
1536596 KB |
Output is correct |
37 |
Correct |
1655 ms |
1537168 KB |
Output is correct |
38 |
Correct |
1859 ms |
1536708 KB |
Output is correct |