#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 100005;
int n,m;
struct item {
int t,l,r,c;
} T[MAXN];
vector<pi> vec;
map<int,multiset<int>> S; // {right diag, cost}
priority_queue<pi,vector<pi>,greater<pi>> pq; // {right diagonal, index}
struct node {
int s,e,m,val;
node *l=nullptr, *r=nullptr;
node(int _s, int _e) {
s=_s;e=_e;
m=floor(((double)s+e)/2);
val=oo;
}
int query(int x, int y) {
if(s==x&&e==y)return val;
if(x>m) return (r?r->query(x,y):oo);
if(y<=m) return (l?l->query(x,y):oo);
else return min(l?l->query(x,m):oo, r?r->query(m+1,y):oo);
}
void update(int x, int nval){
if(s==e){
val=nval;
return;
}
if(x>m){
if(r==nullptr)r=new node(m+1,e);
r->update(x,nval);
} else {
if(l==nullptr)l=new node(s,m);
l->update(x,nval);
}
val=min(l?l->val:oo,r?r->val:oo);
}
}*seg=new node(-2*inf,2*inf);
int dp[MAXN];
int ans=oo;
int32_t main()
{
IAMSPEED
cin >> n >> m;
FOR(i,1,m) cin >> T[i].t >> T[i].l >> T[i].r >> T[i].c;
FOR(i,1,m)T[i].r++;
FOR(i,1,m) {
if(T[i].l == 1)vec.pb({-inf-T[i].t,i});
else vec.pb({T[i].l-T[i].t,i});
}
sort(all(vec));
for(auto &x:vec){
db(x.s);
while(pq.size() && pq.top().f<x.f) {
pi cur=pq.top(); pq.pop();
db2(cur.f,cur.s);
int idx=cur.s;
int rr=T[idx].r+T[idx].t;
S[rr].erase(S[rr].find(dp[idx]));
if(S[rr].size())seg->update(rr,*S[rr].begin());
else seg->update(rr,oo);
}
int sz=T[x.s].r - T[x.s].l;
int l0=T[x.s].l, l1=T[x.s].l+sz;
int t0=T[x.s].t, t1=t0+sz;
int r0=T[x.s].r, r1=T[x.s].r-sz;
// remove the expiring ones.
int &cost = dp[x.s];
cost = T[x.s].c;
if(T[x.s].l != 1) { // cannot start from this segment.
int L=l0+t0;
int R=l1+t1;
cost += seg->query(L,R); // the downwards diagonal
}
if(T[x.s].r==n+1)chmin(ans,cost);
db3(r0,t0,cost);
S[r0+t0].insert(cost);
seg->update(r0+t0,*S[r0+t0].begin());
pq.push({r0-t0, x.s});
}
if(ans!=oo)cout<<ans;
else cout<<-1;
return (0-0) ;
}
Compilation message
treatment.cpp: In function 'int32_t main()':
treatment.cpp:109:20: warning: unused variable 'r1' [-Wunused-variable]
109 | int r0=T[x.s].r, r1=T[x.s].r-sz;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
252 ms |
113804 KB |
Output is correct |
2 |
Correct |
254 ms |
113700 KB |
Output is correct |
3 |
Correct |
181 ms |
20464 KB |
Output is correct |
4 |
Correct |
168 ms |
20544 KB |
Output is correct |
5 |
Correct |
110 ms |
9092 KB |
Output is correct |
6 |
Correct |
118 ms |
10264 KB |
Output is correct |
7 |
Correct |
142 ms |
21400 KB |
Output is correct |
8 |
Correct |
113 ms |
9128 KB |
Output is correct |
9 |
Correct |
118 ms |
10376 KB |
Output is correct |
10 |
Correct |
133 ms |
21452 KB |
Output is correct |
11 |
Correct |
293 ms |
64148 KB |
Output is correct |
12 |
Correct |
310 ms |
64064 KB |
Output is correct |
13 |
Correct |
285 ms |
108184 KB |
Output is correct |
14 |
Correct |
296 ms |
108148 KB |
Output is correct |
15 |
Correct |
269 ms |
108480 KB |
Output is correct |
16 |
Correct |
274 ms |
108460 KB |
Output is correct |
17 |
Correct |
274 ms |
107812 KB |
Output is correct |
18 |
Correct |
262 ms |
63112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
252 ms |
113804 KB |
Output is correct |
2 |
Correct |
254 ms |
113700 KB |
Output is correct |
3 |
Correct |
181 ms |
20464 KB |
Output is correct |
4 |
Correct |
168 ms |
20544 KB |
Output is correct |
5 |
Correct |
110 ms |
9092 KB |
Output is correct |
6 |
Correct |
118 ms |
10264 KB |
Output is correct |
7 |
Correct |
142 ms |
21400 KB |
Output is correct |
8 |
Correct |
113 ms |
9128 KB |
Output is correct |
9 |
Correct |
118 ms |
10376 KB |
Output is correct |
10 |
Correct |
133 ms |
21452 KB |
Output is correct |
11 |
Correct |
293 ms |
64148 KB |
Output is correct |
12 |
Correct |
310 ms |
64064 KB |
Output is correct |
13 |
Correct |
285 ms |
108184 KB |
Output is correct |
14 |
Correct |
296 ms |
108148 KB |
Output is correct |
15 |
Correct |
269 ms |
108480 KB |
Output is correct |
16 |
Correct |
274 ms |
108460 KB |
Output is correct |
17 |
Correct |
274 ms |
107812 KB |
Output is correct |
18 |
Correct |
262 ms |
63112 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |