//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[300001];
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){
// cout << seg[0][3] << "$\n";
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;
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(max(1 , d[0] - 1)) , 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]);
// cout << i << " : " << d1 << " " << d2 << "!\n";
updSeg(0 , is[a[i][1]] , d1) , updSeg(1 , is[a[i][1]] , d2);
}
if(o == INF) cout << -1;
else cout << o;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4560 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 |
4444 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4560 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 |
4444 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 |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4560 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 |
4444 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 |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 KB |
Output is correct |
15 |
Correct |
2 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
3 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
3 ms |
4952 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
3 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4560 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 |
4444 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 |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 KB |
Output is correct |
15 |
Correct |
2 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
3 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
3 ms |
4952 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
3 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
2 ms |
4700 KB |
Output is correct |
25 |
Correct |
19 ms |
5536 KB |
Output is correct |
26 |
Correct |
58 ms |
12744 KB |
Output is correct |
27 |
Correct |
170 ms |
22192 KB |
Output is correct |
28 |
Correct |
141 ms |
12108 KB |
Output is correct |
29 |
Correct |
110 ms |
14780 KB |
Output is correct |
30 |
Correct |
188 ms |
12272 KB |
Output is correct |
31 |
Correct |
258 ms |
24756 KB |
Output is correct |
32 |
Correct |
229 ms |
24496 KB |
Output is correct |
33 |
Correct |
30 ms |
12240 KB |
Output is correct |
34 |
Correct |
109 ms |
20924 KB |
Output is correct |
35 |
Incorrect |
85 ms |
36856 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |