This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |