#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>*/
template<class S,class T>
bool chmin(S &a,const T &b) {
return a>b?(a=b)==b:false;
}
template<class S,class T>
bool chmax(S &a,const T &b) {
return a<b?(a=b)==b:false;
}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e18;
const ll mod=998244353;
const ll N=1e6+5;
const ld eps=1e-9;
vector<ll> t(N*4,inf);
ll n,m;
set<ll> st;
ll get(ll l,ll r,ll v=1,ll tl=1,ll tr=st.sz){
if(tl>r || tr<l) return inf;
if(l<=tl && tr<=r)return t[v];
ll tm=(tl+tr)/2;
return min(get(l,r,v*2,tl,tm),get(l,r,v*2+1,tm+1,tr));
}
void upd(ll pos,ll val,ll v=1,ll tl=1,ll tr=st.sz){
if(tl>pos || tr<pos) return;
if(tl==tr){
chmin(t[v],val);
return;
}
ll tm=(tl+tr)/2;
upd(pos,val,v*2,tl,tm);
upd(pos,val,v*2+1,tm+1,tr);
t[v]=min(t[v*2],t[v*2+1]);
}
void solve(){
ll i,j;
cin>>n>>m;
ll t[m+5],l[m+5],r[m+5],c[m+5];
for(i=1;i<=m;i++){
cin>>t[i]>>l[i]>>r[i]>>c[i];
st.ins(l[i]);
st.ins(r[i]);
if(l[i]+1<=n)st.ins(l[i]+1);
if(l[i]-1>0)st.ins(l[i]-1);
if(r[i]+1<=n)st.ins(r[i]+1);
if(r[i]-1>0)st.ins(r[i]-1);
}
ll cnt=1;
map<ll,ll> mp;
if(*st.begin()>1 || *st.rbegin()<n){
cout<<-1<<endl;
return;
}
for(auto i : st){
mp[i]=cnt++;
}
pll v;
for(i=1;i<=m;i++){
l[i]=mp[l[i]];
r[i]=mp[r[i]];
v.pb({r[i],i});
}
sort(all(v));
for(i=0;i<m;i++){
if(l[v[i].sc]==1){
upd(v[i].fr,c[v[i].sc]);
continue;
}
upd(v[i].fr,get(l[v[i].sc]-1,v[i].fr)+c[v[i].sc]);
}
if(get(st.sz,st.sz)>=inf)cout<<-1<<endl;
else cout<<get(st.sz,st.sz)<<endl;
}
signed main(){
start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
*/
Compilation message
treatment.cpp: In function 'void solve()':
treatment.cpp:65:10: warning: unused variable 'j' [-Wunused-variable]
65 | ll i,j;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
363 ms |
83180 KB |
Output is correct |
2 |
Correct |
384 ms |
83652 KB |
Output is correct |
3 |
Correct |
142 ms |
44408 KB |
Output is correct |
4 |
Correct |
137 ms |
44484 KB |
Output is correct |
5 |
Correct |
67 ms |
39876 KB |
Output is correct |
6 |
Correct |
82 ms |
40136 KB |
Output is correct |
7 |
Correct |
107 ms |
44264 KB |
Output is correct |
8 |
Correct |
75 ms |
40008 KB |
Output is correct |
9 |
Correct |
87 ms |
40396 KB |
Output is correct |
10 |
Correct |
115 ms |
45356 KB |
Output is correct |
11 |
Correct |
438 ms |
89244 KB |
Output is correct |
12 |
Correct |
443 ms |
89004 KB |
Output is correct |
13 |
Correct |
537 ms |
105472 KB |
Output is correct |
14 |
Correct |
565 ms |
105416 KB |
Output is correct |
15 |
Correct |
523 ms |
105356 KB |
Output is correct |
16 |
Correct |
559 ms |
105724 KB |
Output is correct |
17 |
Correct |
528 ms |
105168 KB |
Output is correct |
18 |
Correct |
434 ms |
88528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
31576 KB |
Output is correct |
2 |
Incorrect |
5 ms |
31696 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
31576 KB |
Output is correct |
2 |
Incorrect |
5 ms |
31696 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
363 ms |
83180 KB |
Output is correct |
2 |
Correct |
384 ms |
83652 KB |
Output is correct |
3 |
Correct |
142 ms |
44408 KB |
Output is correct |
4 |
Correct |
137 ms |
44484 KB |
Output is correct |
5 |
Correct |
67 ms |
39876 KB |
Output is correct |
6 |
Correct |
82 ms |
40136 KB |
Output is correct |
7 |
Correct |
107 ms |
44264 KB |
Output is correct |
8 |
Correct |
75 ms |
40008 KB |
Output is correct |
9 |
Correct |
87 ms |
40396 KB |
Output is correct |
10 |
Correct |
115 ms |
45356 KB |
Output is correct |
11 |
Correct |
438 ms |
89244 KB |
Output is correct |
12 |
Correct |
443 ms |
89004 KB |
Output is correct |
13 |
Correct |
537 ms |
105472 KB |
Output is correct |
14 |
Correct |
565 ms |
105416 KB |
Output is correct |
15 |
Correct |
523 ms |
105356 KB |
Output is correct |
16 |
Correct |
559 ms |
105724 KB |
Output is correct |
17 |
Correct |
528 ms |
105168 KB |
Output is correct |
18 |
Correct |
434 ms |
88528 KB |
Output is correct |
19 |
Correct |
5 ms |
31576 KB |
Output is correct |
20 |
Incorrect |
5 ms |
31696 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |