/*input
5 6
2 4 3 5
1 2 2 8
3 6 5 2
4 6 4 7
2 4 3 10
3 5
2 4 3 10
1 3 1 20
2 5 4 30
*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
namespace my_template {
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
}
using namespace my_template;
const int MOD = 1000000007;
const ll INF = 1e15;
const int MX = 100101;
struct tral {
int a, b, c, d;
};
struct node {
int l, r;
ll maz;
node *left = nullptr;
node *right = nullptr;
node(int a, int b) : l(a), r(b), maz(INF) {
if (l != r) {
left = new node(l, (l + r) / 2);
right = new node((l + r) / 2 + 1, r);
}
}
void upd(int pos, ll val) {
if (l == r) {
maz = min(maz, val);
return;
}
if (pos <= (l + r) / 2) {
left->upd(pos, val);
}
else {
right->upd(pos, val);
}
maz = min(left->maz, right->maz);
}
ll get(int a, int b) {
// printf("a = %d, b = %d, l = %d, r = %d\n", a, b, l, r);
if (r < a or b < l) return INF;
else if (a <= l and r <= b) return maz;
else {
return min(left->get(a, b), right->get(a, b));
}
}
};
int K;
unordered_map<int, int> kas;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int M, N;
cin >> M >> N;
vi pts = {1, N};
vector<tral> sk(M);
for (int i = 0; i < M; ++i)
{
int a, b, c, d; cin >> a >> b >> c >> d;
sk[i] = {a, b, c, d};
pts.emplace_back(a);
pts.emplace_back(b);
pts.emplace_back(c);
}
sort(pts.begin(), pts.end());
pts.erase(unique(pts.begin(), pts.end()), pts.end());
K = (int)pts.size();
for (int i = 0; i < K; ++i)
{
kas[pts[i]] = i;
}
ll ats = INF;
node Left(0, K);
node Right(0, K);
Left.upd(kas[1], 0);
Right.upd(kas[N], 0);
for (int i = 0; i < M; ++i)
{
ll l = Left.get(kas[sk[i].a], kas[sk[i].b]);
ll r = Right.get(kas[sk[i].a], kas[sk[i].b]);
ats = min(ats, l + r + sk[i].d);
Left.upd(kas[sk[i].c], sk[i].d + l);
Right.upd(kas[sk[i].c], sk[i].d + r);
}
if (ats >= INF) printf("-1\n");
else printf("%lld\n", ats);
}
/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
1024 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
1024 KB |
Output is correct |
23 |
Correct |
2 ms |
1024 KB |
Output is correct |
24 |
Correct |
4 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
1024 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
1024 KB |
Output is correct |
23 |
Correct |
2 ms |
1024 KB |
Output is correct |
24 |
Correct |
4 ms |
1024 KB |
Output is correct |
25 |
Correct |
28 ms |
4608 KB |
Output is correct |
26 |
Correct |
120 ms |
14936 KB |
Output is correct |
27 |
Correct |
381 ms |
28048 KB |
Output is correct |
28 |
Correct |
162 ms |
7664 KB |
Output is correct |
29 |
Correct |
318 ms |
24568 KB |
Output is correct |
30 |
Correct |
242 ms |
11136 KB |
Output is correct |
31 |
Correct |
583 ms |
45420 KB |
Output is correct |
32 |
Correct |
597 ms |
35052 KB |
Output is correct |
33 |
Correct |
60 ms |
15240 KB |
Output is correct |
34 |
Correct |
302 ms |
38308 KB |
Output is correct |
35 |
Correct |
396 ms |
76092 KB |
Output is correct |
36 |
Correct |
925 ms |
76220 KB |
Output is correct |
37 |
Correct |
549 ms |
76168 KB |
Output is correct |
38 |
Correct |
544 ms |
76356 KB |
Output is correct |