/* I can do this all day */
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;
const int N = 3e5 + 10;
const int LOG = 20;
const ll mod = 1e9 + 7;
const ll inf = 2e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;
ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }
mt19937 rng(time(nullptr));
ll dis[2][N], seg[2][N << 2];
int LEN, n, L[N], R[N], C[N], cost[N];
vector < int > cm;
inline int lwr(int x)
{
return lower_bound(all(cm), x) - cm.begin();
}
void upd(int id, int p, ll x, int v = 1, int tl = 0, int tr = SZ(cm) - 1)
{
if(tl == tr)
{
seg[id][v] = min(seg[id][v], x);
return;
}
int mid = (tl + tr) >> 1;
if(p <= mid) upd(id, p, x, v << 1, tl, mid);
else upd(id, p, x, v << 1 | 1, mid + 1, tr);
seg[id][v] = min(seg[id][v << 1], seg[id][v << 1 | 1]);
}
ll get(int id, int l, int r, int v = 1, int tl = 0, int tr = SZ(cm) - 1)
{
if(l > tr || r < tl || l > r) return inf;
if(l <= tl && tr <= r) return seg[id][v];
int mid = (tl + tr) >> 1;
return min(get(id, l, r, v << 1, tl, mid), get(id, l, r, v << 1 | 1, mid + 1, tr));
}
int main()
{
fast_io;
for(int j = 0; j < 2; j ++) for(int i = 0; i < N << 2; i ++) seg[j][i] = inf;
cin >> n >> LEN;
cm.push_back(0);
cm.push_back(LEN);
for(int i = 0; i <= n + 1; i ++) dis[0][i] = dis[1][i] = inf;
for(int i = 1; i <= n; i ++)
{
cin >> L[i] >> R[i] >> C[i] >> cost[i];
cm.push_back(L[i]);
cm.push_back(R[i]);
cm.push_back(C[i]);
if(L[i] == 1)
{
dis[0][i] = 0;
}
if(R[i] == LEN)
{
dis[1][i] = 0;
}
}
sort(all(cm));
cm.resize(unique(all(cm)) - cm.begin());
ll tot = inf;
for(int i = 1; i <= n; i ++)
{
L[i] = lwr(L[i]);
R[i] = lwr(R[i]);
C[i] = lwr(C[i]);
///printf("i = %d L = %d R = %d C = %d\n", i, L[i], R[i], C[i]);
dis[0][i] = min(dis[0][i], get(0, L[i], R[i])) + cost[i];
dis[1][i] = min(dis[1][i], get(1, L[i], R[i])) + cost[i];
///printf("i = %d dis0 = %lld dis1 = %lld cost = %d\n", i, dis[0][i], dis[1][i], cost[i]);
tot = min(tot, dis[0][i] + dis[1][i] - cost[i]);
upd(0, C[i], dis[0][i]);
upd(1, C[i], dis[1][i]);
}
if(tot >= inf / 2) tot = -1;
cout << tot;
return 0;
}
/* check corner case(n = 1?), watch for negetive index or overflow */
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19100 KB |
Output is correct |
2 |
Correct |
9 ms |
19140 KB |
Output is correct |
3 |
Correct |
10 ms |
19104 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19116 KB |
Output is correct |
6 |
Correct |
10 ms |
19020 KB |
Output is correct |
7 |
Correct |
8 ms |
19148 KB |
Output is correct |
8 |
Correct |
8 ms |
19020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19100 KB |
Output is correct |
2 |
Correct |
9 ms |
19140 KB |
Output is correct |
3 |
Correct |
10 ms |
19104 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19116 KB |
Output is correct |
6 |
Correct |
10 ms |
19020 KB |
Output is correct |
7 |
Correct |
8 ms |
19148 KB |
Output is correct |
8 |
Correct |
8 ms |
19020 KB |
Output is correct |
9 |
Correct |
8 ms |
19132 KB |
Output is correct |
10 |
Correct |
8 ms |
19084 KB |
Output is correct |
11 |
Correct |
8 ms |
19128 KB |
Output is correct |
12 |
Correct |
8 ms |
19148 KB |
Output is correct |
13 |
Correct |
8 ms |
19148 KB |
Output is correct |
14 |
Correct |
8 ms |
19128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19100 KB |
Output is correct |
2 |
Correct |
9 ms |
19140 KB |
Output is correct |
3 |
Correct |
10 ms |
19104 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19116 KB |
Output is correct |
6 |
Correct |
10 ms |
19020 KB |
Output is correct |
7 |
Correct |
8 ms |
19148 KB |
Output is correct |
8 |
Correct |
8 ms |
19020 KB |
Output is correct |
9 |
Correct |
8 ms |
19132 KB |
Output is correct |
10 |
Correct |
8 ms |
19084 KB |
Output is correct |
11 |
Correct |
8 ms |
19128 KB |
Output is correct |
12 |
Correct |
8 ms |
19148 KB |
Output is correct |
13 |
Correct |
8 ms |
19148 KB |
Output is correct |
14 |
Correct |
8 ms |
19128 KB |
Output is correct |
15 |
Correct |
7 ms |
19148 KB |
Output is correct |
16 |
Correct |
8 ms |
19092 KB |
Output is correct |
17 |
Correct |
9 ms |
19148 KB |
Output is correct |
18 |
Correct |
9 ms |
19148 KB |
Output is correct |
19 |
Correct |
9 ms |
19156 KB |
Output is correct |
20 |
Correct |
9 ms |
19128 KB |
Output is correct |
21 |
Correct |
8 ms |
19116 KB |
Output is correct |
22 |
Correct |
10 ms |
19148 KB |
Output is correct |
23 |
Correct |
9 ms |
19148 KB |
Output is correct |
24 |
Correct |
12 ms |
19148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19100 KB |
Output is correct |
2 |
Correct |
9 ms |
19140 KB |
Output is correct |
3 |
Correct |
10 ms |
19104 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19116 KB |
Output is correct |
6 |
Correct |
10 ms |
19020 KB |
Output is correct |
7 |
Correct |
8 ms |
19148 KB |
Output is correct |
8 |
Correct |
8 ms |
19020 KB |
Output is correct |
9 |
Correct |
8 ms |
19132 KB |
Output is correct |
10 |
Correct |
8 ms |
19084 KB |
Output is correct |
11 |
Correct |
8 ms |
19128 KB |
Output is correct |
12 |
Correct |
8 ms |
19148 KB |
Output is correct |
13 |
Correct |
8 ms |
19148 KB |
Output is correct |
14 |
Correct |
8 ms |
19128 KB |
Output is correct |
15 |
Correct |
7 ms |
19148 KB |
Output is correct |
16 |
Correct |
8 ms |
19092 KB |
Output is correct |
17 |
Correct |
9 ms |
19148 KB |
Output is correct |
18 |
Correct |
9 ms |
19148 KB |
Output is correct |
19 |
Correct |
9 ms |
19156 KB |
Output is correct |
20 |
Correct |
9 ms |
19128 KB |
Output is correct |
21 |
Correct |
8 ms |
19116 KB |
Output is correct |
22 |
Correct |
10 ms |
19148 KB |
Output is correct |
23 |
Correct |
9 ms |
19148 KB |
Output is correct |
24 |
Correct |
12 ms |
19148 KB |
Output is correct |
25 |
Correct |
25 ms |
19988 KB |
Output is correct |
26 |
Correct |
52 ms |
21312 KB |
Output is correct |
27 |
Correct |
144 ms |
24768 KB |
Output is correct |
28 |
Correct |
127 ms |
27812 KB |
Output is correct |
29 |
Correct |
100 ms |
23620 KB |
Output is correct |
30 |
Correct |
160 ms |
27724 KB |
Output is correct |
31 |
Correct |
224 ms |
27760 KB |
Output is correct |
32 |
Correct |
195 ms |
27812 KB |
Output is correct |
33 |
Correct |
34 ms |
20688 KB |
Output is correct |
34 |
Correct |
98 ms |
23596 KB |
Output is correct |
35 |
Correct |
141 ms |
27720 KB |
Output is correct |
36 |
Correct |
234 ms |
27708 KB |
Output is correct |
37 |
Correct |
204 ms |
27840 KB |
Output is correct |
38 |
Correct |
195 ms |
27816 KB |
Output is correct |