#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (b > a ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (b < a ? a = b, true : false);}
const int N = 2e5 + 5, INF = 2e9;
const ll MAX_DP = 1e16;
struct Node{
int l, r; ll dp;
Node(int l = INF, int r = -INF, ll dp = MAX_DP): l(l), r(r), dp(dp) {}
bool operator < (const Node& q) const{
return make_pair(l, dp) < make_pair(q.l, q.dp);
}
bool operator > (const Node& q) const{
return make_pair(r, -dp) > make_pair(q.r, -q.dp);
}
};
struct SegTree{
vector<Node> st[2]; int trsz;
SegTree(int n = 0): trsz(n) {
st[0].resize((n << 2) | 1);
st[1].resize((n << 2) | 1);
}
void update(int id, int l, int r, int x, Node dpL, Node dpR){
if(l == r){
ckmn(st[0][id], dpL);
ckmx(st[1][id], dpR);
}
else{
int m = l+r>>1;
if(x <= m)
update(id << 1, l, m, x, dpL, dpR);
else update(id << 1|1, m+1, r, x, dpL, dpR);
ckmn(st[0][id], dpL);
ckmx(st[1][id], dpR);
}
}
Node query(int id, int l, int r, int u, int v, int t){
if(l > v || r < u) return Node();
if(l >= u && r <= v) return st[t][id];
int m = l+r>>1;
Node ans = Node();
if(!t) ckmn(ans, query(id << 1, l, m, u, v, t)), ckmn(ans, query(id << 1|1, m+1, r, u, v, t));
else ckmx(ans, query(id << 1, l, m, u, v, t)), ckmx(ans, query(id << 1|1, m+1, r, u, v, t));
return ans;
}
void update(int x, Node dpL, Node dpR){
update(1, 1, trsz, x, dpL, dpR);
}
Node query(int l, int r, int t){
return query(1, 1, trsz, l, r, t);
}
};
int n, m, L[N], R[N], c[N], W[N];
void Main()
{
cin >> n >> m;
FOR(i, 1, n) cin >> L[i] >> R[i] >> c[i] >> W[i];
vi comp(1, -1);
FOR(i, 1, n){
comp.eb(L[i]), comp.eb(R[i]), comp.eb(c[i]);
}
comp.eb(1), comp.eb(m);
sort(all(comp)), compact(comp);
FOR(i, 1, n){
L[i] = lwrbound(comp, L[i]);
R[i] = lwrbound(comp, R[i]);
c[i] = lwrbound(comp, c[i]);
}
m = lwrbound(comp, m);
SegTree tr(sz(comp));
ll ans = MAX_DP;
FOR(i, 1, n){
Node curdp1 = tr.query(L[i], R[i], 0);
Node curdp2 = tr.query(L[i], R[i], 1);
Node curdpi = Node(L[i], R[i], 0);
ckmn(curdp1, curdpi), ckmx(curdp2, curdpi);
if(curdp1.l == 1 && curdp2.r == m){
ckmn(ans, curdp1.dp + curdp2.dp + W[i]);
}
curdp1.dp += W[i], curdp2.dp += W[i];
tr.update(c[i], curdp1, curdp2);
}
cout << (ans == MAX_DP ? -1 : ans) << el;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
Compilation message (stderr)
pinball.cpp: In function 'int32_t main()':
pinball.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |