//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;
map<int , int> cmp;
ll seg[1200001][2];
int seginf[1200001][2] , is[300000];
int cnt = 0;
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(d[0]) , ns.pb(d[1]) , ns.pb(d[2]);
}
sort(ns.bg() , ns.end());
for(int i = 0 ; i < (3 * k) ; i++) if(i == 0 or ns[i] != ns[i - 1]) cmp[ns[i]] = cnt++;
for(auto &el : a) el[0] = cmp[el[0]] , el[1] = cmp[el[1]] , el[2] = cmp[el[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 |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4864 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 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 |
4864 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 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 |
4864 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 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 |
4864 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |