#include<bits/stdc++.h>
#define pb push_back
#define pli pair<ll,ll>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxn=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll n,m,t[maxn],l[maxn],r[maxn],c[maxn],d[maxn];
priority_queue<pli,vector<pli>,greater<pli>>pq;
struct node
{
ll fi,se;
node ()
{
fi=inf,se=0;
}
node operator+(const node&o)
{
node ans;
if(fi<o.fi) ans.se=se;
else ans.se=o.se;
ans.fi=min(fi,o.fi);
return ans;
}
};
struct segtree
{
node st[4*maxn];
void update(ll pos,ll v,ll id=1,ll l=1,ll r=m)
{
if(l==r)
{
st[id].fi=v;
st[id].se=l;
return;
}
ll mid=l+r>>1;
if(pos<=mid) update(pos,v,id*2,l,mid);
else update(pos,v,id*2+1,mid+1,r);
st[id]=st[id*2]+st[id*2+1];
}
node get(ll i,ll j,ll id=1,ll l=1,ll r=m)
{
if(j<l||r<i) return node();
if(i<=l&&r<=j) return st[id];
ll mid=l+r>>1;
return get(i,j,id*2,l,mid)+get(i,j,id*2+1,mid+1,r);
}
}seg[2];
pli b[maxn];
ll pos[maxn],vv[maxn];
void solve()
{
cin >> n >> m;
for(int i=1;i<=m;i++)
{
cin >> t[i] >> l[i] >> r[i] >> c[i];
b[i]={t[i],i};
}
sort(b+1,b+m+1);
for(int i=1;i<=m;i++)
{
ll id=b[i].se;
seg[0].update(i,l[id]+t[id]);
seg[1].update(i,l[id]-t[id]);
pos[id]=i;
vv[i]=id;
}
for(int i=1;i<=m;i++)
{
d[i]=inf;
if(l[i]==1)
{
d[i]=c[i];
pq.push({d[i],i});
seg[0].update(pos[i],inf);
seg[1].update(pos[i],inf);
}
}
while(!pq.empty())
{
ll u=pq.top().se;
ll W=pq.top().fi;
//cout<<u<<":\n";
pq.pop();
if(W>d[u]) continue;
if(r[u]==n) {cout << W;return;}
while(seg[0].get(pos[u]+1,m).fi<=r[u]+t[u]+1)
{
ll v=vv[seg[0].get(pos[u]+1,m).se];
d[v]=d[u]+c[v];
pq.push({d[v],v});
seg[0].update(pos[v],inf);
seg[1].update(pos[v],inf);
//cout <<"phai "<< v << '\n';
}
while(seg[1].get(1,pos[u]-1).fi<=r[u]-t[u]+1)
{
ll v=vv[seg[1].get(1,pos[u]-1).se];
d[v]=d[u]+c[v];
pq.push({d[v],v});
seg[0].update(pos[v],inf);
seg[1].update(pos[v],inf);
//cout << "trai "<<v << '\n';
}
}
cout << -1;
}
int main()
{
fastio
//freopen("c.INP","r",stdin);
//freopen("c.OUT","w",stdout);
solve();
}
Compilation message
treatment.cpp: In member function 'void segtree::update(ll, ll, ll, ll, ll)':
treatment.cpp:41:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | ll mid=l+r>>1;
| ~^~
treatment.cpp: In member function 'node segtree::get(ll, ll, ll, ll, ll)':
treatment.cpp:50:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | ll mid=l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
39568 KB |
Output is correct |
2 |
Correct |
87 ms |
39504 KB |
Output is correct |
3 |
Correct |
127 ms |
40660 KB |
Output is correct |
4 |
Correct |
125 ms |
42456 KB |
Output is correct |
5 |
Correct |
88 ms |
45004 KB |
Output is correct |
6 |
Correct |
111 ms |
42836 KB |
Output is correct |
7 |
Correct |
109 ms |
42484 KB |
Output is correct |
8 |
Correct |
47 ms |
42608 KB |
Output is correct |
9 |
Correct |
48 ms |
42680 KB |
Output is correct |
10 |
Correct |
48 ms |
42836 KB |
Output is correct |
11 |
Correct |
88 ms |
44776 KB |
Output is correct |
12 |
Correct |
101 ms |
44752 KB |
Output is correct |
13 |
Correct |
115 ms |
44752 KB |
Output is correct |
14 |
Correct |
128 ms |
44868 KB |
Output is correct |
15 |
Correct |
143 ms |
42732 KB |
Output is correct |
16 |
Correct |
144 ms |
42680 KB |
Output is correct |
17 |
Correct |
134 ms |
41812 KB |
Output is correct |
18 |
Correct |
87 ms |
43980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
39256 KB |
Output is correct |
2 |
Correct |
5 ms |
39260 KB |
Output is correct |
3 |
Correct |
5 ms |
39360 KB |
Output is correct |
4 |
Correct |
5 ms |
39260 KB |
Output is correct |
5 |
Correct |
6 ms |
39260 KB |
Output is correct |
6 |
Correct |
5 ms |
39324 KB |
Output is correct |
7 |
Correct |
5 ms |
39260 KB |
Output is correct |
8 |
Correct |
6 ms |
39260 KB |
Output is correct |
9 |
Correct |
5 ms |
39260 KB |
Output is correct |
10 |
Correct |
5 ms |
39260 KB |
Output is correct |
11 |
Correct |
5 ms |
39260 KB |
Output is correct |
12 |
Correct |
5 ms |
39260 KB |
Output is correct |
13 |
Correct |
5 ms |
39260 KB |
Output is correct |
14 |
Correct |
5 ms |
39260 KB |
Output is correct |
15 |
Correct |
6 ms |
39424 KB |
Output is correct |
16 |
Correct |
5 ms |
39428 KB |
Output is correct |
17 |
Correct |
5 ms |
39260 KB |
Output is correct |
18 |
Correct |
6 ms |
39376 KB |
Output is correct |
19 |
Correct |
6 ms |
39260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
39256 KB |
Output is correct |
2 |
Correct |
5 ms |
39260 KB |
Output is correct |
3 |
Correct |
5 ms |
39360 KB |
Output is correct |
4 |
Correct |
5 ms |
39260 KB |
Output is correct |
5 |
Correct |
6 ms |
39260 KB |
Output is correct |
6 |
Correct |
5 ms |
39324 KB |
Output is correct |
7 |
Correct |
5 ms |
39260 KB |
Output is correct |
8 |
Correct |
6 ms |
39260 KB |
Output is correct |
9 |
Correct |
5 ms |
39260 KB |
Output is correct |
10 |
Correct |
5 ms |
39260 KB |
Output is correct |
11 |
Correct |
5 ms |
39260 KB |
Output is correct |
12 |
Correct |
5 ms |
39260 KB |
Output is correct |
13 |
Correct |
5 ms |
39260 KB |
Output is correct |
14 |
Correct |
5 ms |
39260 KB |
Output is correct |
15 |
Correct |
6 ms |
39424 KB |
Output is correct |
16 |
Correct |
5 ms |
39428 KB |
Output is correct |
17 |
Correct |
5 ms |
39260 KB |
Output is correct |
18 |
Correct |
6 ms |
39376 KB |
Output is correct |
19 |
Correct |
6 ms |
39260 KB |
Output is correct |
20 |
Correct |
11 ms |
39512 KB |
Output is correct |
21 |
Correct |
10 ms |
39516 KB |
Output is correct |
22 |
Correct |
15 ms |
39516 KB |
Output is correct |
23 |
Correct |
9 ms |
39516 KB |
Output is correct |
24 |
Correct |
9 ms |
39772 KB |
Output is correct |
25 |
Correct |
11 ms |
39680 KB |
Output is correct |
26 |
Correct |
12 ms |
39440 KB |
Output is correct |
27 |
Correct |
11 ms |
39676 KB |
Output is correct |
28 |
Correct |
10 ms |
39772 KB |
Output is correct |
29 |
Correct |
10 ms |
39516 KB |
Output is correct |
30 |
Correct |
8 ms |
39612 KB |
Output is correct |
31 |
Correct |
8 ms |
39632 KB |
Output is correct |
32 |
Correct |
10 ms |
39772 KB |
Output is correct |
33 |
Correct |
10 ms |
39636 KB |
Output is correct |
34 |
Correct |
11 ms |
39440 KB |
Output is correct |
35 |
Correct |
9 ms |
39592 KB |
Output is correct |
36 |
Correct |
9 ms |
39584 KB |
Output is correct |
37 |
Correct |
10 ms |
39516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
39568 KB |
Output is correct |
2 |
Correct |
87 ms |
39504 KB |
Output is correct |
3 |
Correct |
127 ms |
40660 KB |
Output is correct |
4 |
Correct |
125 ms |
42456 KB |
Output is correct |
5 |
Correct |
88 ms |
45004 KB |
Output is correct |
6 |
Correct |
111 ms |
42836 KB |
Output is correct |
7 |
Correct |
109 ms |
42484 KB |
Output is correct |
8 |
Correct |
47 ms |
42608 KB |
Output is correct |
9 |
Correct |
48 ms |
42680 KB |
Output is correct |
10 |
Correct |
48 ms |
42836 KB |
Output is correct |
11 |
Correct |
88 ms |
44776 KB |
Output is correct |
12 |
Correct |
101 ms |
44752 KB |
Output is correct |
13 |
Correct |
115 ms |
44752 KB |
Output is correct |
14 |
Correct |
128 ms |
44868 KB |
Output is correct |
15 |
Correct |
143 ms |
42732 KB |
Output is correct |
16 |
Correct |
144 ms |
42680 KB |
Output is correct |
17 |
Correct |
134 ms |
41812 KB |
Output is correct |
18 |
Correct |
87 ms |
43980 KB |
Output is correct |
19 |
Correct |
5 ms |
39256 KB |
Output is correct |
20 |
Correct |
5 ms |
39260 KB |
Output is correct |
21 |
Correct |
5 ms |
39360 KB |
Output is correct |
22 |
Correct |
5 ms |
39260 KB |
Output is correct |
23 |
Correct |
6 ms |
39260 KB |
Output is correct |
24 |
Correct |
5 ms |
39324 KB |
Output is correct |
25 |
Correct |
5 ms |
39260 KB |
Output is correct |
26 |
Correct |
6 ms |
39260 KB |
Output is correct |
27 |
Correct |
5 ms |
39260 KB |
Output is correct |
28 |
Correct |
5 ms |
39260 KB |
Output is correct |
29 |
Correct |
5 ms |
39260 KB |
Output is correct |
30 |
Correct |
5 ms |
39260 KB |
Output is correct |
31 |
Correct |
5 ms |
39260 KB |
Output is correct |
32 |
Correct |
5 ms |
39260 KB |
Output is correct |
33 |
Correct |
6 ms |
39424 KB |
Output is correct |
34 |
Correct |
5 ms |
39428 KB |
Output is correct |
35 |
Correct |
5 ms |
39260 KB |
Output is correct |
36 |
Correct |
6 ms |
39376 KB |
Output is correct |
37 |
Correct |
6 ms |
39260 KB |
Output is correct |
38 |
Correct |
11 ms |
39512 KB |
Output is correct |
39 |
Correct |
10 ms |
39516 KB |
Output is correct |
40 |
Correct |
15 ms |
39516 KB |
Output is correct |
41 |
Correct |
9 ms |
39516 KB |
Output is correct |
42 |
Correct |
9 ms |
39772 KB |
Output is correct |
43 |
Correct |
11 ms |
39680 KB |
Output is correct |
44 |
Correct |
12 ms |
39440 KB |
Output is correct |
45 |
Correct |
11 ms |
39676 KB |
Output is correct |
46 |
Correct |
10 ms |
39772 KB |
Output is correct |
47 |
Correct |
10 ms |
39516 KB |
Output is correct |
48 |
Correct |
8 ms |
39612 KB |
Output is correct |
49 |
Correct |
8 ms |
39632 KB |
Output is correct |
50 |
Correct |
10 ms |
39772 KB |
Output is correct |
51 |
Correct |
10 ms |
39636 KB |
Output is correct |
52 |
Correct |
11 ms |
39440 KB |
Output is correct |
53 |
Correct |
9 ms |
39592 KB |
Output is correct |
54 |
Correct |
9 ms |
39584 KB |
Output is correct |
55 |
Correct |
10 ms |
39516 KB |
Output is correct |
56 |
Correct |
127 ms |
43224 KB |
Output is correct |
57 |
Correct |
114 ms |
43472 KB |
Output is correct |
58 |
Correct |
123 ms |
41904 KB |
Output is correct |
59 |
Correct |
111 ms |
42304 KB |
Output is correct |
60 |
Correct |
127 ms |
42836 KB |
Output is correct |
61 |
Correct |
115 ms |
41740 KB |
Output is correct |
62 |
Correct |
124 ms |
43152 KB |
Output is correct |
63 |
Correct |
107 ms |
42604 KB |
Output is correct |
64 |
Correct |
105 ms |
42580 KB |
Output is correct |
65 |
Correct |
148 ms |
42836 KB |
Output is correct |
66 |
Correct |
108 ms |
42860 KB |
Output is correct |
67 |
Correct |
136 ms |
43576 KB |
Output is correct |
68 |
Correct |
133 ms |
43372 KB |
Output is correct |
69 |
Correct |
139 ms |
43600 KB |
Output is correct |
70 |
Correct |
137 ms |
43604 KB |
Output is correct |
71 |
Correct |
137 ms |
43344 KB |
Output is correct |
72 |
Correct |
135 ms |
43264 KB |
Output is correct |
73 |
Correct |
139 ms |
43508 KB |
Output is correct |
74 |
Correct |
54 ms |
43380 KB |
Output is correct |
75 |
Correct |
54 ms |
43344 KB |
Output is correct |
76 |
Correct |
104 ms |
45264 KB |
Output is correct |
77 |
Correct |
96 ms |
44752 KB |
Output is correct |
78 |
Correct |
124 ms |
45032 KB |
Output is correct |
79 |
Correct |
148 ms |
43376 KB |
Output is correct |
80 |
Correct |
146 ms |
43004 KB |
Output is correct |
81 |
Correct |
58 ms |
43344 KB |
Output is correct |
82 |
Correct |
144 ms |
42356 KB |
Output is correct |
83 |
Correct |
140 ms |
42696 KB |
Output is correct |
84 |
Correct |
145 ms |
42612 KB |
Output is correct |
85 |
Correct |
113 ms |
43256 KB |
Output is correct |
86 |
Correct |
114 ms |
43244 KB |
Output is correct |
87 |
Correct |
118 ms |
43348 KB |
Output is correct |
88 |
Correct |
144 ms |
43628 KB |
Output is correct |
89 |
Correct |
120 ms |
43248 KB |
Output is correct |
90 |
Correct |
95 ms |
45520 KB |
Output is correct |
91 |
Correct |
58 ms |
43244 KB |
Output is correct |
92 |
Correct |
113 ms |
43348 KB |
Output is correct |
93 |
Correct |
134 ms |
43368 KB |
Output is correct |
94 |
Correct |
152 ms |
43556 KB |
Output is correct |
95 |
Correct |
113 ms |
43384 KB |
Output is correct |
96 |
Correct |
84 ms |
44496 KB |
Output is correct |
97 |
Correct |
104 ms |
45584 KB |
Output is correct |
98 |
Correct |
114 ms |
45592 KB |
Output is correct |
99 |
Correct |
95 ms |
45892 KB |
Output is correct |
100 |
Correct |
97 ms |
45620 KB |
Output is correct |
101 |
Correct |
109 ms |
45412 KB |
Output is correct |
102 |
Correct |
106 ms |
45520 KB |
Output is correct |
103 |
Correct |
90 ms |
43796 KB |
Output is correct |