#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int N = 1e5 + 1;
const int inf = 1e15;
int m;
struct SegTree{
int T[N * 4];
SegTree(){
for ( int i = 0; i < N * 4; i++ ){
T[i] = inf;
}
}
void upd(int v, int l, int r, int pos, int vl){
if ( l == r ){
T[v] = vl;
return;
}
int md = (l + r) >> 1;
if ( pos <= md ){
upd(v * 2, l, md, pos, vl);
} else{
upd(v * 2 + 1, md + 1, r, pos, vl);
}
T[v] = min(T[v * 2], T[v * 2 + 1]);
}
void upd(int pos, int vl){
upd(1, 0, m - 1, pos, vl);
}
int get(int v, int l, int r, int tl, int tr){
if ( l > tr or r < tl ){
return inf;
}
if ( tl <= l && tr >= r ){
return T[v];
}
int md = (l + r) >> 1;
return min(get(v * 2, l, md, tl, tr), get(v * 2 + 1, md + 1, r, tl, tr));
}
int get(int l, int r){
return get(1, 0, m - 1, l, r);
}
} seg;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n >> m;
vector <int> T(m), L(m), R(m), C(m);
for ( int i = 0; i < m; i++ ){
cin >> T[i] >> L[i] >> R[i] >> C[i];
}
if ( *max_element(all(T)) == 1 ){
vector <int> p(m);
iota(all(p), 0);
sort(all(p), [&](int &u, int &v){
return R[u] < R[v];
});
vector <int> dp(m, inf);
int opt = inf;
for ( int i = 0; i < m; i++ ){
int j = p[i];
if ( L[j] == 1 ){
dp[i] = C[j];
} else{
int l = 0, r = i;
while ( l < r ){
int md = (l + r) >> 1;
if ( R[p[md]] + 1 >= L[j] ){
r = md;
} else l = md + 1;
}
chmin(dp[i], seg.get(l, i) + C[j]);
}
seg.upd(i, dp[i]);
if ( R[j] == n ){
chmin(opt, dp[i]);
}
}
cout << (opt == inf ? -1 : opt);
return 0;
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
8272 KB |
Output is correct |
2 |
Correct |
53 ms |
11456 KB |
Output is correct |
3 |
Correct |
62 ms |
10300 KB |
Output is correct |
4 |
Correct |
64 ms |
10320 KB |
Output is correct |
5 |
Correct |
54 ms |
11372 KB |
Output is correct |
6 |
Correct |
60 ms |
11348 KB |
Output is correct |
7 |
Correct |
70 ms |
11344 KB |
Output is correct |
8 |
Correct |
58 ms |
11380 KB |
Output is correct |
9 |
Correct |
56 ms |
11324 KB |
Output is correct |
10 |
Correct |
68 ms |
11348 KB |
Output is correct |
11 |
Correct |
85 ms |
11344 KB |
Output is correct |
12 |
Correct |
85 ms |
11600 KB |
Output is correct |
13 |
Correct |
69 ms |
11380 KB |
Output is correct |
14 |
Correct |
72 ms |
11344 KB |
Output is correct |
15 |
Correct |
78 ms |
11196 KB |
Output is correct |
16 |
Correct |
62 ms |
11344 KB |
Output is correct |
17 |
Correct |
57 ms |
10624 KB |
Output is correct |
18 |
Correct |
82 ms |
10684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
8272 KB |
Output is correct |
2 |
Correct |
53 ms |
11456 KB |
Output is correct |
3 |
Correct |
62 ms |
10300 KB |
Output is correct |
4 |
Correct |
64 ms |
10320 KB |
Output is correct |
5 |
Correct |
54 ms |
11372 KB |
Output is correct |
6 |
Correct |
60 ms |
11348 KB |
Output is correct |
7 |
Correct |
70 ms |
11344 KB |
Output is correct |
8 |
Correct |
58 ms |
11380 KB |
Output is correct |
9 |
Correct |
56 ms |
11324 KB |
Output is correct |
10 |
Correct |
68 ms |
11348 KB |
Output is correct |
11 |
Correct |
85 ms |
11344 KB |
Output is correct |
12 |
Correct |
85 ms |
11600 KB |
Output is correct |
13 |
Correct |
69 ms |
11380 KB |
Output is correct |
14 |
Correct |
72 ms |
11344 KB |
Output is correct |
15 |
Correct |
78 ms |
11196 KB |
Output is correct |
16 |
Correct |
62 ms |
11344 KB |
Output is correct |
17 |
Correct |
57 ms |
10624 KB |
Output is correct |
18 |
Correct |
82 ms |
10684 KB |
Output is correct |
19 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |