#include <bits/stdc++.h> // pA
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const int N = 3e5+5;
const ll INF = 1e18;
ll Seg[N<<2];
pll p[N];
vector<ll> com;
inline void build(ll L, ll R, ll v)
{
Seg[v] = INF;
if (L != R) build(L, (L+R)>>1, v<<1), build(((L+R)>>1)+1, R, v<<1|1);
}
inline ll query(ll L, ll R, ll ql, ll qr, ll v)
{
if (ql > qr) return INF;
if (ql <= L && R <= qr) return Seg[v];
int mid((L+R)>>1);
if (qr <= mid) return query(L, mid, ql, qr, v<<1);
if (ql > mid) return query(mid+1, R, ql, qr, v<<1|1);
return min(query(L, mid, ql, mid, v<<1), query(mid+1, R, mid+1, qr, v<<1|1));
}
inline void update(ll L, ll R, ll v, ll pos, ll val)
{
if (L == R) { Seg[v] = val; return; }
int mid((L+R)>>1);
if (pos <= mid) update(L, mid, v<<1, pos, val);
else update(mid+1, R, v<<1|1, pos, val);
Seg[v] = min(Seg[v<<1], Seg[v<<1|1]);
}
int main(void)
{ IO
ll n, i, Q, j, mn=INF, tmp, ttmp;
cin >> n >> Q;
if (Q != 1) assert(0);
for (i=1; i <= n; ++i)
cin >> p[i].F >> p[i].S, com.pb(p[i].S);
sort(p+1, p+n+1);
sort(all(com));
com.resize(unique(all(com))-com.begin());
//for (i=1; i <= n; ++i) debug(p[i].F), debug(p[i].S), endl;
build(1, com.size(), 1);
for (i=1; i <= n; ++i)
{
tmp = lower_bound(all(com), p[i].S) - com.begin() + 1;
ttmp = query(1, com.size(), 1, tmp, 1);
mn = min(mn, p[i].F+p[i].S + ttmp);
update(1, com.size(), 1, tmp, -p[i].F-p[i].S);
}
build(1, com.size(), 1);
for (i=1; i <= n; ++i)
{
tmp = lower_bound(all(com), p[i].S) - com.begin() + 1;
ttmp = query(1, com.size(), tmp+1, com.size(), 1);
mn = min(mn, p[i].F-p[i].S + ttmp);
update(1, com.size(), 1, tmp, -p[i].F+p[i].S);
}
cout << mn << '\n';
return 0;
}
Compilation message
road_construction.cpp: In function 'int main()':
road_construction.cpp:47:17: warning: unused variable 'j' [-Wunused-variable]
47 | ll n, i, Q, j, mn=INF, tmp, ttmp;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
12796 KB |
Output is correct |
2 |
Correct |
272 ms |
18048 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
54 ms |
11772 KB |
Output is correct |
5 |
Correct |
116 ms |
14024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
12796 KB |
Output is correct |
2 |
Correct |
272 ms |
18048 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
54 ms |
11772 KB |
Output is correct |
5 |
Correct |
116 ms |
14024 KB |
Output is correct |
6 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 6 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |