//Self-control leads to consistency.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 100000000000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define EPS 0.000001
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
ll m,n,price[100001],seg[400001],dp[100001],pos[100001],dp1[100001];
dos ran[100001];
void upd(ll p,ll l,ll r,ll in,ll val){
if(l==r && l==in){
seg[p]=val;
return;
}
ll med=(l+r)>>1;
if(in<=med)
upd(p*2,l,med,in,val);
else upd(p*2+1,med+1,r,in,val);
seg[p]=min(seg[p*2],seg[p*2+1]);
}
ll query(ll p,ll l,ll r,ll l1,ll r1){
if(l>r || l>r1 || l1>r)return INF;
if(l>=l1 && r<=r1)return seg[p];
ll med=(l+r)>>1;
return min(query(p*2,l,med,l1,r1),query(p*2+1,med+1,r,l1,r1));
}
int main()
{
flash;
cin>>m>>n;
set<dos> in;
for (ll i = 0; i < m; ++i)
{
ll a,b,c,d;
cin>>a>>b>>c>>d;
a--,b--,c--;
ran[i]={a,b};
in.insert({c,i});
price[i]=d;
}
ll co=0;
for(auto it:in){
pos[it.second]=co++;
}
for (ll i = 0; i < m; ++i)
{
dp[i]=INF;
upd(1,0,m-1,pos[i],dp[i]);
}
for (ll i = 0; i < m; ++i)
{
if(ran[i].first==0)dp[i]=price[i];
else{
auto u=in.lower_bound({ran[i].first,-INF});
auto v=in.lower_bound({ran[i].second+1,-INF});
if(v!=in.begin() && u!=in.end()){
v--;
dp[i]=price[i]+query(1,0,m-1,pos[(*u).second],pos[(*v).second]);
}
}
upd(1,0,m-1,pos[i],dp[i]);
}
for (ll i = 0; i < m; ++i)
{
dp1[i]=INF;
upd(1,0,m-1,pos[i],dp1[i]);
}
for (ll i = 0; i < m; ++i)
{
if(ran[i].second==n-1)dp1[i]=price[i];
else{
auto u=in.lower_bound({ran[i].first,-INF});
auto v=in.lower_bound({ran[i].second+1,-INF});
if(v!=in.begin() && u!=in.end()){
v--;
dp1[i]=price[i]+query(1,0,m-1,pos[(*u).second],pos[(*v).second]);
}
}
upd(1,0,m-1,pos[i],dp1[i]);
}
ll ans=INF;
for (ll i = 0; i < m; ++i)
{
ans=min(ans,dp[i]+dp1[i]-price[i]);
}
if(ans==INF)cout<<-1;
else cout<<ans;
return 0;
}
Compilation message
pinball.cpp:5:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
pinball.cpp:6:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("unroll-loops")
pinball.cpp: In function 'void setIO(std::__cxx11::string)':
pinball.cpp:28:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen((s+".in").c_str(),"r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:29:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen((s+".out").c_str(),"w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
26 ms |
2048 KB |
Output is correct |
26 |
Correct |
81 ms |
4600 KB |
Output is correct |
27 |
Correct |
304 ms |
12644 KB |
Output is correct |
28 |
Correct |
401 ms |
17144 KB |
Output is correct |
29 |
Correct |
198 ms |
8776 KB |
Output is correct |
30 |
Correct |
492 ms |
17332 KB |
Output is correct |
31 |
Correct |
496 ms |
17400 KB |
Output is correct |
32 |
Correct |
505 ms |
17272 KB |
Output is correct |
33 |
Correct |
45 ms |
3704 KB |
Output is correct |
34 |
Correct |
162 ms |
8824 KB |
Output is correct |
35 |
Correct |
231 ms |
17272 KB |
Output is correct |
36 |
Correct |
502 ms |
17272 KB |
Output is correct |
37 |
Correct |
431 ms |
17272 KB |
Output is correct |
38 |
Correct |
414 ms |
17272 KB |
Output is correct |