#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int n,duz;
int br = 0;
map<int,int> poj;
ll INF=1e8;
struct prep{
int l,r,x;
ll w;
}niz[100005];
ll dp[100005];
ll sejv[100005];
ll seg[4*300005];
void update(int node, int l, int r, int poz, ll val){
if(l == r){
seg[node] = val;
return;
}
int mid = (l + r) / 2;
if(poz <= mid)update(node*2,l,mid,poz,val);
else update(node*2+1,mid+1,r,poz,val);
seg[node] = min(seg[node * 2 + 1], seg[node * 2]);
}
ll kveri(int node, int l, int r, int levo, int desno){
if(levo > r || l > desno)return INF;
if(l >= levo && r <= desno)return seg[node];
int mid = (l + r) / 2;
return min(kveri(node*2,l,mid,levo,desno), kveri(node*2+1,mid+1,r,levo,desno));
}
void radi(){
ff(i,1,n){
dp[i] = INF;
if(niz[i].l == 1)dp[i] = niz[i].w;
else{
ff(j,1,i - 1){
if(niz[j].x >= niz[i].l && niz[j].x <= niz[i].r)dp[i] = min(dp[i], niz[i].w + dp[j]);
}
}
}
// update(1,1,duz,1,0);
// ff(i,2,duz)update(1,1,duz,i,INF);
// ff(i,1,n){
// dp[i] = kveri(1,1,duz,niz[i].l,niz[i].r) + niz[i].w;
// update(1,1,duz,niz[i].x,min(kveri(1,1,duz,niz[i].l,niz[i].r)+niz[i].w,kveri(1,1,duz,niz[i].x,niz[i].x)));
//
// }
}
void radi2(){
ff(i,1,n){
dp[i] = INF;
if(niz[i].r == duz)dp[i] = niz[i].w;
else{
ff(j,1,i - 1){
if(niz[j].x >= niz[i].l && niz[j].x <= niz[i].r)dp[i] = min(dp[i], niz[i].w + dp[j]);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
INF *= INF;
cin >> n >> duz;
vector<int> svi;
ll sm = 0;
ff(i,1,n){
cin >> niz[i].l >> niz[i].r >> niz[i].x >> niz[i].w;
svi.pb(niz[i].l);
svi.pb(niz[i].r);
svi.pb(niz[i].x);
sm += niz[i].w;
}
svi.pb(duz);
sort(svi.begin(),svi.end());
int lst = 0;
int br = 0;
for(auto c:svi){
if(c == lst)continue;
br++;
poj[c] = br;
lst = c;
}
duz = poj[duz];
ff(i,1,n)niz[i] = {poj[niz[i].l], poj[niz[i].r], poj[niz[i].x], niz[i].w};
radi();
ff(i,1,n)sejv[i] = dp[i];
radi2();
// ff(i,1,n){
// niz[i].l = duz-niz[i].l+1;
// niz[i].r = duz-niz[i].r+1;
// niz[i].x = duz-niz[i].x+1;
// swap(niz[i].r,niz[i].l);
// }
// radi();
ll ans = INF;
ff(i,1,n){
ans = min(ans, dp[i] + sejv[i] - niz[i].w);
}
if(ans <= sm)cout << ans << "\n";
else cout << -1 << "\n";
return 0;
}
Compilation message
pinball.cpp: In function 'void radi()':
pinball.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
pinball.cpp:58:5: note: in expansion of macro 'ff'
58 | ff(i,1,n){
| ^~
pinball.cpp:6:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
pinball.cpp:62:13: note: in expansion of macro 'ff'
62 | ff(j,1,i - 1){
| ^~
pinball.cpp: In function 'void radi2()':
pinball.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
pinball.cpp:77:5: note: in expansion of macro 'ff'
77 | ff(i,1,n){
| ^~
pinball.cpp:6:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
pinball.cpp:81:13: note: in expansion of macro 'ff'
81 | ff(j,1,i - 1){
| ^~
pinball.cpp: In function 'int main()':
pinball.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
pinball.cpp:96:5: note: in expansion of macro 'ff'
96 | ff(i,1,n){
| ^~
pinball.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
pinball.cpp:115:5: note: in expansion of macro 'ff'
115 | ff(i,1,n)niz[i] = {poj[niz[i].l], poj[niz[i].r], poj[niz[i].x], niz[i].w};
| ^~
pinball.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
pinball.cpp:117:5: note: in expansion of macro 'ff'
117 | ff(i,1,n)sejv[i] = dp[i];
| ^~
pinball.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
pinball.cpp:127:5: note: in expansion of macro 'ff'
127 | ff(i,1,n){
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |