#include "bits/stdc++.h"
using namespace std;
#define ar array
#define int long long
const int N = 3e5 + 5;
struct ST{
int tree[N<<2];
void sett(int i, int v, int lx = 0, int rx = N, int x = 1){
if(lx == rx) { tree[x] = min(tree[x], v); return; }
int m = (lx + rx) >> 1;
if(i <= m) sett(i, v, lx, m, x<<1);
else sett(i, v, m+1, rx, x<<1|1);
tree[x] = min(tree[x<<1], tree[x<<1|1]);
}
int get(int l, int r, int lx = 0, int rx = N, int x = 1){
if(lx > r || rx < l) return 1e15;
if(lx >= l && rx <= r) return tree[x];
int m = (lx + rx) >> 1;
return min(get(l, r, lx, m, x<<1), get(l, r, m+1, rx, x<<1|1));
}
}pref, suff;
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin>>n>>m;
vector<ar<int, 4>> a(n);
vector<int> p(n); iota(p.begin(), p.end(), 0);
for(int i=0;i<n;i++){
cin>>a[i][0]>>a[i][1]>>a[i][2]>>a[i][3];
}
sort(p.begin(), p.end(), [&](int i, int j){
return (a[i][0] < a[j][0]);
});
int r = 1;
for(auto i : p){
if(r < a[i][0]){
cout<<-1<<"\n"; return 0;
} r = max(r, a[i][1]);
} if(r < m) { cout<<-1<<"\n"; return 0; }
vector<ar<int, 2>> pp;
for(int i=0;i<n;i++){
pp.push_back({i, 0});
pp.push_back({i, 1});
pp.push_back({i, 2});
}
sort(pp.begin(), pp.end(), [&](auto& i, auto& j){
return (a[i[0]][i[1]] < a[j[0]][j[1]]);
});
int last = 0;
for(int i=0;i<n*3;){ int j = i;
while(j < n*3 && a[pp[i][0]][pp[i][1]] == a[pp[j][0]][pp[j][1]]) j++;
while(i < j){
a[pp[i][0]][pp[i][1]] = last;
i++;
} last++;
}
memset(pref.tree, 127, sizeof pref.tree);
memset(suff.tree, 127, sizeof suff.tree);
int res = 1e15;
for(int i=0;i<n;i++){
int l = a[i][0], r = a[i][1], c = a[i][2], d = a[i][3];
int pp = pref.get(l, r);
if(l == 0) pp = 0;
pref.sett(c, pp + d);
int ss = suff.get(l, r);
if(r == last - 1) ss = 0;
suff.sett(c, ss + d);
res = min(res, pp + ss + d);
}
if(res == 1e15) cout<<-1<<"\n";
else cout<<res<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19008 KB |
Output is correct |
2 |
Correct |
7 ms |
19020 KB |
Output is correct |
3 |
Correct |
9 ms |
19008 KB |
Output is correct |
4 |
Correct |
8 ms |
19012 KB |
Output is correct |
5 |
Correct |
8 ms |
19020 KB |
Output is correct |
6 |
Correct |
8 ms |
19020 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
8 ms |
19020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19008 KB |
Output is correct |
2 |
Correct |
7 ms |
19020 KB |
Output is correct |
3 |
Correct |
9 ms |
19008 KB |
Output is correct |
4 |
Correct |
8 ms |
19012 KB |
Output is correct |
5 |
Correct |
8 ms |
19020 KB |
Output is correct |
6 |
Correct |
8 ms |
19020 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
8 ms |
19020 KB |
Output is correct |
9 |
Correct |
8 ms |
19148 KB |
Output is correct |
10 |
Correct |
7 ms |
19148 KB |
Output is correct |
11 |
Correct |
7 ms |
19144 KB |
Output is correct |
12 |
Correct |
8 ms |
19152 KB |
Output is correct |
13 |
Correct |
7 ms |
19148 KB |
Output is correct |
14 |
Correct |
9 ms |
19032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19008 KB |
Output is correct |
2 |
Correct |
7 ms |
19020 KB |
Output is correct |
3 |
Correct |
9 ms |
19008 KB |
Output is correct |
4 |
Correct |
8 ms |
19012 KB |
Output is correct |
5 |
Correct |
8 ms |
19020 KB |
Output is correct |
6 |
Correct |
8 ms |
19020 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
8 ms |
19020 KB |
Output is correct |
9 |
Correct |
8 ms |
19148 KB |
Output is correct |
10 |
Correct |
7 ms |
19148 KB |
Output is correct |
11 |
Correct |
7 ms |
19144 KB |
Output is correct |
12 |
Correct |
8 ms |
19152 KB |
Output is correct |
13 |
Correct |
7 ms |
19148 KB |
Output is correct |
14 |
Correct |
9 ms |
19032 KB |
Output is correct |
15 |
Correct |
7 ms |
19020 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
9 ms |
19272 KB |
Output is correct |
18 |
Correct |
8 ms |
19284 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
9 ms |
19276 KB |
Output is correct |
21 |
Correct |
8 ms |
19132 KB |
Output is correct |
22 |
Correct |
9 ms |
19200 KB |
Output is correct |
23 |
Correct |
9 ms |
19188 KB |
Output is correct |
24 |
Correct |
9 ms |
19280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19008 KB |
Output is correct |
2 |
Correct |
7 ms |
19020 KB |
Output is correct |
3 |
Correct |
9 ms |
19008 KB |
Output is correct |
4 |
Correct |
8 ms |
19012 KB |
Output is correct |
5 |
Correct |
8 ms |
19020 KB |
Output is correct |
6 |
Correct |
8 ms |
19020 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
8 ms |
19020 KB |
Output is correct |
9 |
Correct |
8 ms |
19148 KB |
Output is correct |
10 |
Correct |
7 ms |
19148 KB |
Output is correct |
11 |
Correct |
7 ms |
19144 KB |
Output is correct |
12 |
Correct |
8 ms |
19152 KB |
Output is correct |
13 |
Correct |
7 ms |
19148 KB |
Output is correct |
14 |
Correct |
9 ms |
19032 KB |
Output is correct |
15 |
Correct |
7 ms |
19020 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
9 ms |
19272 KB |
Output is correct |
18 |
Correct |
8 ms |
19284 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
9 ms |
19276 KB |
Output is correct |
21 |
Correct |
8 ms |
19132 KB |
Output is correct |
22 |
Correct |
9 ms |
19200 KB |
Output is correct |
23 |
Correct |
9 ms |
19188 KB |
Output is correct |
24 |
Correct |
9 ms |
19280 KB |
Output is correct |
25 |
Correct |
23 ms |
20300 KB |
Output is correct |
26 |
Correct |
51 ms |
22196 KB |
Output is correct |
27 |
Correct |
130 ms |
27740 KB |
Output is correct |
28 |
Correct |
142 ms |
31588 KB |
Output is correct |
29 |
Correct |
94 ms |
25416 KB |
Output is correct |
30 |
Correct |
158 ms |
31656 KB |
Output is correct |
31 |
Correct |
202 ms |
31664 KB |
Output is correct |
32 |
Correct |
180 ms |
31704 KB |
Output is correct |
33 |
Correct |
30 ms |
21584 KB |
Output is correct |
34 |
Correct |
88 ms |
25420 KB |
Output is correct |
35 |
Correct |
127 ms |
31648 KB |
Output is correct |
36 |
Correct |
230 ms |
31664 KB |
Output is correct |
37 |
Correct |
191 ms |
31656 KB |
Output is correct |
38 |
Correct |
185 ms |
31604 KB |
Output is correct |