//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<int , x>
#define endl '\n'
const ll INF = (1ll * 1e18);
int k , n , m;
vector<arr(4)> a;
vector<int> ns;
ll seg[1200001][2];
int seginf[1200001][2] , is[1000001];
int cnt = 0;
void frm(int &el){
el = int(lb(ns.bg() , ns.end() , el) - ns.bg());
}
void buildSeg(int l = 0 , int r = cnt - 1 , int i = 1){
seginf[i][0] = l , seginf[i][1] = r , seg[i][0] = INF , seg[i][1] = INF;
if(r == l) is[l] = i;
else{
int c = (r + l - 1) >> 1;
buildSeg(l , c , i << 1) , buildSeg(c + 1 , r , (i << 1) | 1);
}
}
void updSeg(int md , int i , ll val){
while(i >= 1) seg[i][md] = min(seg[i][md] , val) , i >>= 1;
}
ll getSeg(int l , int r , int md , int i){
if(seginf[i][0] >= l and seginf[i][1] <= r) return seg[i][md];
if(seginf[i][1] >= l and seginf[i][0] <= r) return min(getSeg(l , r , md , i << 1) , getSeg(l , r , md , (i << 1) | 1));
return INF;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n;
m = k + 2;
ns.pb(1);
for(int i = 0 ; i < k ; i++){
int d[4];
cin >> d[0] >> d[1] >> d[2] >> d[3];
a.pb({d[0] , d[2] , d[1] , d[3]});
ns.pb(d[0]) , ns.pb(d[1]) , ns.pb(d[2]);
ns.pb(min(n , d[1] + 1));
}
sort(ns.bg() , ns.end()) , ns.resize(int(unique(ns.bg() , ns.end()) - ns.bg()));
cnt = (int)ns.size();
for(int i = 0 ; i < k ; i++) frm(a[i][0]) , frm(a[i][1]) , frm(a[i][2]);
buildSeg();
updSeg(0 , is[0] , 0) , updSeg(1 , is[cnt - 1] , 0);
ll o = INF;
for(int i = 0 ; i < k ; i++){
o = min(o , getSeg(a[i][0] , a[i][2] , 0 , 1) + getSeg(a[i][0] , a[i][2] , 1 , 1) + (1ll * a[i][3]));
ll d1 = INF , d2 = INF;
if(a[i][0] < a[i][1]) d1 = getSeg(a[i][0] , a[i][1] - 1 , 0 , 1) + (1ll * a[i][3]);
if(a[i][1] < a[i][2]) d2 = getSeg(a[i][1] + 1 , a[i][2] , 1 , 1) + (1ll * a[i][3]);
updSeg(0 , is[a[i][1]] , d1) , updSeg(1 , is[a[i][1]] , d2);
}
if(o == INF) cout << -1;
else cout << o;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4440 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4448 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4440 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4448 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
19 ms |
7128 KB |
Output is correct |
26 |
Correct |
51 ms |
13940 KB |
Output is correct |
27 |
Correct |
167 ms |
15164 KB |
Output is correct |
28 |
Correct |
143 ms |
7912 KB |
Output is correct |
29 |
Correct |
106 ms |
14544 KB |
Output is correct |
30 |
Correct |
191 ms |
10028 KB |
Output is correct |
31 |
Correct |
234 ms |
22436 KB |
Output is correct |
32 |
Correct |
222 ms |
16288 KB |
Output is correct |
33 |
Correct |
28 ms |
13776 KB |
Output is correct |
34 |
Correct |
114 ms |
20668 KB |
Output is correct |
35 |
Correct |
153 ms |
34732 KB |
Output is correct |
36 |
Correct |
276 ms |
34640 KB |
Output is correct |
37 |
Correct |
225 ms |
34564 KB |
Output is correct |
38 |
Correct |
222 ms |
34740 KB |
Output is correct |