#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;
map<int,set<pi>> S0;
map<int,set<pi>> S1;
struct node {
int s,e,m;
pi val;
node *l=nullptr, *r=nullptr;
node (int _s, int _e) {
s=_s;e=_e;m=(s+e)/2;
val={oo,0};
}
void update(int x, pi 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:pi(oo,0),r?r->val:pi(oo,0));
}
pi query(int x, int y) {
if(s==x&&e==y)return val;
if(x>m) {
if(r==nullptr)r=new node(m+1,e);
return r->query(x,y);
} else if(y<=m) {
if(l==nullptr)l=new node(s,m);
return l->query(x,y);
} else {
if(l==nullptr)l=new node(s,m);
if(r==nullptr)r=new node(m+1,e);
return min(l->query(x,m),r->query(m+1,y));
}
}
} *roots[2];
int T[MAXN];
int L[MAXN];
int R[MAXN];
int C[MAXN];
int dist[MAXN];
int32_t main()
{
IAMSPEED
int n,m;
roots[0]=new node(0,inf);
roots[1]=new node(0,inf);
cin >> n >> m;
priority_queue<pi,vector<pi>,greater<pi>> pq;
FOR(i,1,m)dist[i]=oo;
FOR(i,1,m){
cin >> T[i] >> L[i] >> R[i] >> C[i];
++R[i];
if(L[i]==1) {
dist[i]=C[i];
pq.push({C[i],i});
}
}
FOR(i,1,m) {
if(L[i]==1)continue;
int plus=L[i]+T[i];
S0[T[i]].insert({plus,i});
int minus=L[i]-T[i];
S1[T[i]].insert({minus,i});
}
for(auto t:S0) {
roots[0]->update(t.f,*t.s.begin());
}
for(auto t:S1) {
roots[1]->update(t.f,*t.s.begin());
}
while(pq.size()) {
pi cur=pq.top(); pq.pop();
int x=cur.s;
int d=cur.f;
db3(x,d,dist[x]);
if(dist[x]!=d)continue;
if(R[x]==n+1){
cout<<d<<'\n';exit(0);
}
for(pi res=roots[0]->query(T[x],inf);res.f!=oo;res=roots[0]->query(T[x],inf)){
int val = res.f;
int idx = res.s;
if(val<=R[x]+T[x]){
db2(idx,val);
dist[idx]=dist[x]+C[idx];
pq.push({dist[idx],idx});
S0[T[idx]].erase({L[idx]+T[idx],idx});
if(S0[T[idx]].size())roots[0]->update(T[idx],*S0[T[idx]].begin());
else roots[0]->update(T[idx],{oo,oo});
S1[T[idx]].erase({L[idx]-T[idx],idx});
if(S1[T[idx]].size())roots[1]->update(T[idx],*S1[T[idx]].begin());
else roots[1]->update(T[idx],{oo,oo});
} else {
break;
}
}
reach
for(pi res=roots[1]->query(0,T[x]);res.f!=oo;res=roots[1]->query(0,T[x])){
int val = res.f;
int idx = res.s;
if(val<=R[x]-T[x]){
db2(idx,val);
dist[idx]=dist[x]+C[idx];
pq.push({dist[idx],idx});
S0[T[idx]].erase({L[idx]+T[idx],idx});
if(S0[T[idx]].size())roots[0]->update(T[idx],*S0[T[idx]].begin());
else roots[0]->update(T[idx],{oo,oo});
S1[T[idx]].erase({L[idx]-T[idx],idx});
if(S1[T[idx]].size())roots[1]->update(T[idx],*S1[T[idx]].begin());
else roots[1]->update(T[idx],{oo,oo});
} else {
break;
}
}
}
cout<<-1<<'\n';
return (0-0) ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
26096 KB |
Output is correct |
2 |
Correct |
179 ms |
26288 KB |
Output is correct |
3 |
Correct |
180 ms |
21572 KB |
Output is correct |
4 |
Correct |
170 ms |
20932 KB |
Output is correct |
5 |
Correct |
163 ms |
26056 KB |
Output is correct |
6 |
Correct |
182 ms |
26088 KB |
Output is correct |
7 |
Correct |
186 ms |
26104 KB |
Output is correct |
8 |
Correct |
107 ms |
25928 KB |
Output is correct |
9 |
Correct |
104 ms |
26036 KB |
Output is correct |
10 |
Correct |
93 ms |
26100 KB |
Output is correct |
11 |
Correct |
200 ms |
26072 KB |
Output is correct |
12 |
Correct |
206 ms |
26144 KB |
Output is correct |
13 |
Correct |
231 ms |
26108 KB |
Output is correct |
14 |
Correct |
264 ms |
26060 KB |
Output is correct |
15 |
Correct |
239 ms |
26036 KB |
Output is correct |
16 |
Correct |
256 ms |
26060 KB |
Output is correct |
17 |
Correct |
234 ms |
25348 KB |
Output is correct |
18 |
Correct |
209 ms |
25468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 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 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
11 ms |
2188 KB |
Output is correct |
21 |
Correct |
11 ms |
2264 KB |
Output is correct |
22 |
Correct |
16 ms |
3324 KB |
Output is correct |
23 |
Correct |
12 ms |
3412 KB |
Output is correct |
24 |
Correct |
22 ms |
11948 KB |
Output is correct |
25 |
Correct |
33 ms |
14616 KB |
Output is correct |
26 |
Correct |
28 ms |
10052 KB |
Output is correct |
27 |
Correct |
24 ms |
6800 KB |
Output is correct |
28 |
Correct |
26 ms |
12236 KB |
Output is correct |
29 |
Correct |
31 ms |
14164 KB |
Output is correct |
30 |
Correct |
14 ms |
8788 KB |
Output is correct |
31 |
Correct |
11 ms |
6172 KB |
Output is correct |
32 |
Correct |
28 ms |
14124 KB |
Output is correct |
33 |
Correct |
25 ms |
7844 KB |
Output is correct |
34 |
Correct |
17 ms |
2900 KB |
Output is correct |
35 |
Correct |
26 ms |
14052 KB |
Output is correct |
36 |
Correct |
23 ms |
7900 KB |
Output is correct |
37 |
Correct |
18 ms |
2888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
26096 KB |
Output is correct |
2 |
Correct |
179 ms |
26288 KB |
Output is correct |
3 |
Correct |
180 ms |
21572 KB |
Output is correct |
4 |
Correct |
170 ms |
20932 KB |
Output is correct |
5 |
Correct |
163 ms |
26056 KB |
Output is correct |
6 |
Correct |
182 ms |
26088 KB |
Output is correct |
7 |
Correct |
186 ms |
26104 KB |
Output is correct |
8 |
Correct |
107 ms |
25928 KB |
Output is correct |
9 |
Correct |
104 ms |
26036 KB |
Output is correct |
10 |
Correct |
93 ms |
26100 KB |
Output is correct |
11 |
Correct |
200 ms |
26072 KB |
Output is correct |
12 |
Correct |
206 ms |
26144 KB |
Output is correct |
13 |
Correct |
231 ms |
26108 KB |
Output is correct |
14 |
Correct |
264 ms |
26060 KB |
Output is correct |
15 |
Correct |
239 ms |
26036 KB |
Output is correct |
16 |
Correct |
256 ms |
26060 KB |
Output is correct |
17 |
Correct |
234 ms |
25348 KB |
Output is correct |
18 |
Correct |
209 ms |
25468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
11 ms |
2188 KB |
Output is correct |
39 |
Correct |
11 ms |
2264 KB |
Output is correct |
40 |
Correct |
16 ms |
3324 KB |
Output is correct |
41 |
Correct |
12 ms |
3412 KB |
Output is correct |
42 |
Correct |
22 ms |
11948 KB |
Output is correct |
43 |
Correct |
33 ms |
14616 KB |
Output is correct |
44 |
Correct |
28 ms |
10052 KB |
Output is correct |
45 |
Correct |
24 ms |
6800 KB |
Output is correct |
46 |
Correct |
26 ms |
12236 KB |
Output is correct |
47 |
Correct |
31 ms |
14164 KB |
Output is correct |
48 |
Correct |
14 ms |
8788 KB |
Output is correct |
49 |
Correct |
11 ms |
6172 KB |
Output is correct |
50 |
Correct |
28 ms |
14124 KB |
Output is correct |
51 |
Correct |
25 ms |
7844 KB |
Output is correct |
52 |
Correct |
17 ms |
2900 KB |
Output is correct |
53 |
Correct |
26 ms |
14052 KB |
Output is correct |
54 |
Correct |
23 ms |
7900 KB |
Output is correct |
55 |
Correct |
18 ms |
2888 KB |
Output is correct |
56 |
Correct |
329 ms |
39076 KB |
Output is correct |
57 |
Correct |
318 ms |
39376 KB |
Output is correct |
58 |
Correct |
351 ms |
40888 KB |
Output is correct |
59 |
Correct |
363 ms |
41316 KB |
Output is correct |
60 |
Correct |
444 ms |
53068 KB |
Output is correct |
61 |
Correct |
377 ms |
40900 KB |
Output is correct |
62 |
Correct |
339 ms |
39056 KB |
Output is correct |
63 |
Correct |
640 ms |
287384 KB |
Output is correct |
64 |
Correct |
691 ms |
287320 KB |
Output is correct |
65 |
Correct |
230 ms |
23188 KB |
Output is correct |
66 |
Correct |
399 ms |
53080 KB |
Output is correct |
67 |
Correct |
975 ms |
249176 KB |
Output is correct |
68 |
Correct |
657 ms |
166476 KB |
Output is correct |
69 |
Correct |
505 ms |
86148 KB |
Output is correct |
70 |
Correct |
952 ms |
267044 KB |
Output is correct |
71 |
Correct |
877 ms |
255060 KB |
Output is correct |
72 |
Correct |
675 ms |
174580 KB |
Output is correct |
73 |
Correct |
990 ms |
266228 KB |
Output is correct |
74 |
Correct |
337 ms |
207324 KB |
Output is correct |
75 |
Correct |
275 ms |
158720 KB |
Output is correct |
76 |
Correct |
494 ms |
53844 KB |
Output is correct |
77 |
Correct |
637 ms |
221256 KB |
Output is correct |
78 |
Correct |
616 ms |
53860 KB |
Output is correct |
79 |
Correct |
1078 ms |
270372 KB |
Output is correct |
80 |
Correct |
675 ms |
51800 KB |
Output is correct |
81 |
Correct |
374 ms |
220372 KB |
Output is correct |
82 |
Correct |
697 ms |
51044 KB |
Output is correct |
83 |
Correct |
934 ms |
146964 KB |
Output is correct |
84 |
Correct |
998 ms |
268968 KB |
Output is correct |
85 |
Correct |
718 ms |
257284 KB |
Output is correct |
86 |
Correct |
692 ms |
257564 KB |
Output is correct |
87 |
Correct |
706 ms |
243376 KB |
Output is correct |
88 |
Correct |
664 ms |
152748 KB |
Output is correct |
89 |
Correct |
702 ms |
234160 KB |
Output is correct |
90 |
Correct |
626 ms |
206196 KB |
Output is correct |
91 |
Correct |
362 ms |
219976 KB |
Output is correct |
92 |
Correct |
706 ms |
253136 KB |
Output is correct |
93 |
Correct |
876 ms |
264108 KB |
Output is correct |
94 |
Correct |
818 ms |
175728 KB |
Output is correct |
95 |
Correct |
723 ms |
254068 KB |
Output is correct |
96 |
Correct |
582 ms |
221404 KB |
Output is correct |
97 |
Correct |
708 ms |
222344 KB |
Output is correct |
98 |
Correct |
720 ms |
222256 KB |
Output is correct |
99 |
Correct |
657 ms |
222468 KB |
Output is correct |
100 |
Correct |
561 ms |
192168 KB |
Output is correct |
101 |
Correct |
729 ms |
222400 KB |
Output is correct |
102 |
Correct |
754 ms |
222308 KB |
Output is correct |
103 |
Correct |
650 ms |
240160 KB |
Output is correct |