#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<pii,int> ipii;
typedef pair<pii,pii> ipiii;
const int MAXN = 3010;
const int SQRT = 300;
const int MAXA = 50;
const int LOG = 20;
const int INF = 1e18+10;
const ld EPS = 1e-12;
const int MOD = 1e9+7;
int sum(int a, int b){ return (a+b)%MOD; }
void chsum(int &a, int b){ a = (a+b)%MOD; }
void chmn(int &a, int b){ a = min(a, b); }
void chmx(int &a, int b){ a = max(a, b); }
int n, m, le[MAXN], ri[MAXN], c[MAXN], w[MAXN];
int dp[MAXN][MAXN];
struct seg {
int st[4*MAXN];
void bd(int id,int l,int r){
if(l==r){ st[id] = INF; return; }
bd(lf,l,md); bd(rg,md+1,r);
st[id] = min(st[lf], st[rg]);
}
int que(int id, int l, int r, int x, int y){
if(x<=l && r<=y) return st[id];
if(r<x || y<l) return INF;
return min(que(lf,l,md,x,y), que(rg,md+1,r,x,y));
}
int upd(int id,int l, int r,int x,int p){
if(l==r && l==x){
if(st[id] < p) return st[id];
return st[id] = p;
}
if(r<x || x<l) return st[id];
return st[id] = min(upd(lf,l,md,x,p), upd(rg,md+1,r,x,p));
}
} LE, RI;
signed main(){
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
vector <int> cc; cc.pb(-1);
cin>>n>>m;
for(int i=1; i<=n; i++){
cin>>le[i]>>ri[i]>>c[i]>>w[i];
cc.pb(le[i]); cc.pb(ri[i]); cc.pb(c[i]);
}
sort(cc.begin(), cc.end());
cc.resize(unique(cc.begin(), cc.end()) - cc.begin());
// for(int i=1; i<=n; i++){
// le[i] = lower_bound(cc.begin(), cc.end(), le[i]) - cc.begin();
// ri[i] = lower_bound(cc.begin(), cc.end(), ri[i]) - cc.begin();
// c[i] = lower_bound(cc.begin(), cc.end(), c[i]) - cc.begin();
// }
// m = cc.size()-1;
LE.bd(1,1,m); LE.upd(1,1,m,1,0);
RI.bd(1,1,m); RI.upd(1,1,m,m,0);
int ANS = INF;
for(int i=1; i<=n; i++){
LE.upd(1,1,m,c[i], LE.que(1,1,m,le[i],c[i])+w[i]);
RI.upd(1,1,m,c[i], RI.que(1,1,m,c[i],ri[i])+w[i]);
int te = LE.que(1,1,m,le[i],ri[i]) + RI.que(1,1,m,le[i],ri[i]);
te += w[i];
chmn(ANS, te);
// cout << dp[2][5] << ' ' << i<< " p\n";
}
cout << (ANS==INF ? -1 : ANS) << '\n';
}
# | 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... |