#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
int N, M;
#define MAX 100005
struct Info{
int l, r, m, c;
Info(){}
Info(int _l, int _r, int _m, int _c): l(_l), r(_r), m(_m), c(_c){}
friend istream &operator >> (istream& cin, Info& x){
cin >> x.l >> x.r >> x.m >> x.c;
return cin;
}
} A[MAX];
vector<int> comp;
int find(int x){
return lower_bound(comp.begin(), comp.end(), x) - comp.begin() + 1;
}
int l[MAX], r[MAX];
namespace SUBTASK_3{
bool check(void){
return (M <= 2000);
}
void main(void){
memset(l, 0x3f, (N + 1) * sizeof(int));
memset(r, 0x3f, (N + 1) * sizeof(int));
for (int i = 1; i <= N; ++i){
if (A[i].l == 1) l[i] = A[i].c;
if (A[i].r == M) r[i] = A[i].c;
for (int j = 1; j < i; ++j){
if (A[i].l <= A[j].m && A[j].m <= A[i].r){
minimize(l[i], l[j] + A[i].c);
}
if (A[i].l <= A[j].m && A[j].m <= A[i].r){
minimize(r[i], r[j] + A[i].c);
}
}
}
int ans = LINF;
for (int i = 1; i <= N; ++i){
minimize(ans, l[i] + r[i] - A[i].c);
}
cout << (ans >= 1e15 ? -1 : ans);
}
}
namespace SUBTASK_5{
struct SegmentTree{
vector<int> st;
int n;
SegmentTree(int _n = 0){
init(_n);
}
void init(int _n){
st.clear();
this -> n = _n;
st.resize((n << 2) + 5, LINF);
}
void upd(int p, int val){
for (minimize(st[p += n], val); p > 0; p >>= 1){
st[p >> 1] = min(st[p], st[p ^ 1]);
}
}
int query(int l, int r){
int res = LINF;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1){
if (l & 1) minimize(res, st[l++]);
if (r & 1) minimize(res, st[--r]);
}
return res;
}
};
void main(void){
memset(l, 0x3f, (N + 1) * sizeof(int));
memset(r, 0x3f, (N + 1) * sizeof(int));
#define SZ(a) (int)a.size()
SegmentTree myit(M + 1);
for (int i = 1; i <= N; ++i){
if (A[i].l == 1) l[i] = A[i].c;
minimize(l[i], myit.query(A[i].l, A[i].r) + A[i].c);
myit.upd(A[i].m, l[i]);
}
myit.init(SZ(comp));
for (int i = 1; i <= N; ++i){
if (A[i].r == M) r[i] = A[i].c;
minimize(r[i], myit.query(A[i].l, A[i].r) + A[i].c);
myit.upd(A[i].m, r[i]);
}
int ans = LINF;
for (int i = 1; i <= N; ++i){
minimize(ans, l[i] + r[i] - A[i].c);
}
cout << (ans >= 1e15 ? -1 : ans);
}
}
void process(void){
cin >> N >> M;
for (int i = 1; i <= N; ++i){
cin >> A[i];
comp.push_back(A[i].l);
comp.push_back(A[i].r);
comp.push_back(A[i].m);
}
if(SUBTASK_3 :: check()) {
SUBTASK_3 :: main(); return;
}
comp.push_back(1);
comp.push_back(M);
sort(comp.begin(), comp.end());
comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
for (int i = 1; i <= N; ++i){
A[i].l = find(A[i].l);
A[i].r = find(A[i].r);
A[i].m = find(A[i].m);
}
M = find(M);
SUBTASK_5 :: main();
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
500 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
500 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
9 ms |
1684 KB |
Output is correct |
26 |
Correct |
26 ms |
4056 KB |
Output is correct |
27 |
Correct |
75 ms |
8648 KB |
Output is correct |
28 |
Correct |
67 ms |
7456 KB |
Output is correct |
29 |
Correct |
53 ms |
6984 KB |
Output is correct |
30 |
Correct |
85 ms |
7868 KB |
Output is correct |
31 |
Correct |
119 ms |
12728 KB |
Output is correct |
32 |
Correct |
109 ms |
11188 KB |
Output is correct |
33 |
Correct |
14 ms |
5084 KB |
Output is correct |
34 |
Correct |
52 ms |
8820 KB |
Output is correct |
35 |
Correct |
67 ms |
16840 KB |
Output is correct |
36 |
Correct |
114 ms |
16832 KB |
Output is correct |
37 |
Correct |
94 ms |
16832 KB |
Output is correct |
38 |
Correct |
94 ms |
16828 KB |
Output is correct |