// And you curse yourself for things you never done
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, int> pli;
const int maxn = 1e5 + 10;
const ll inf = 1e18 + 10;
int L[maxn], R[maxn], T[maxn], C[maxn];
const int MAXN2 = maxn * 40;
int VERTID, m;
vector<int> v[MAXN2];
bool mark[MAXN2];
ll dis[MAXN2];
void add_edge(int a, int b){
v[a].PB(b);
}
struct Segment{
vector<pii> v[4 * maxn];
vector<int> L[4 * maxn], R[4 * maxn];
function<bool(int, int)> f1, f2, f3; // tartib dim1 // tartib dim2 // sould I connect?
int arr[maxn];
void restart(function<bool(int, int)> _f1, function<bool(int, int)> _f2, function<bool(int, int)> _f3){
f1 = _f1, f2 = _f2, f3 = _f3;
for(int i = 0; i < 4 * maxn; i++)
v[i].clear(), L[i].clear(), R[i].clear();
iota(arr, arr + m, 0);
sort(arr, arr + m, f1);
build();
}
void build(int l = 0, int r = m, int id = 1){
if(r-l == 1){
v[id].PB({arr[l], VERTID++});
}
else{
int mid = (l+r) >> 1;
build(l, mid, 2*id);
build(mid, r, 2*id+1);
int pt1 = 0, pt2 = 0;
while(pt1 + pt2 < sz(v[2*id]) + sz(v[2*id+1])){
L[id].PB(pt1), R[id].PB(pt2);
if(pt2 == sz(v[2*id+1]) || (pt1 != sz(v[2*id]) && f2(v[2*id][pt1].F, v[2*id+1][pt2].F)))
v[id].PB({v[2*id][pt1++].F, VERTID++});
else
v[id].PB({v[2*id+1][pt2++].F, VERTID++});
}
}
for(int i = 0; i < sz(v[id])-1; i++)
add_edge(v[id][i].S, v[id][i+1].S);
for(pii p : v[id])
add_edge(p.S, p.F);
}
void add(int x, int l = 0, int r = m, int id = 1, int pos = -1){
if(id == 1){
int L = -1, R = m;
while(R-L > 1){
int mid = (L+R) >> 1;
if(f2(x, v[1][mid].F))
R = mid;
else
L = mid;
}
pos = R;
}
if(pos >= sz(v[id]) || f3(x, arr[r-1]) == 0)
return;
if(f3(x, arr[l])){
add_edge(x, v[id][pos].S);
return;
}
int mid = (l+r) >> 1;
add(x, l, mid, 2*id, L[id][pos]);
add(x, mid, r, 2*id+1, R[id][pos]);
}
};Segment seg;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
int n;
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> T[i] >> L[i] >> R[i] >> C[i];
}
VERTID = m;
int S = VERTID++, E = VERTID++;
for(int i = 0; i < m; i++){
if(L[i] == 1)
add_edge(S, i);
if(R[i] == n)
add_edge(i, E);
}
{
auto A = [](int i){ return R[i] + T[i]; };
auto B = [](int i){ return +T[i] + L[i] -1; };
seg.restart([&](int i, int j){ return B(i) > B(j); }, [](int i, int j){ return T[i] <= T[j]; }, [&](int x, int S){ return A(x) >= B(S); });
for(int i = 0; i < m; i++)
seg.add(i);
}
{
auto A = [](int i){ return R[i] - T[i]; };
auto B = [](int i){ return -T[i] + L[i] -1; };
seg.restart([&](int i, int j){ return B(i) > B(j); }, [](int i, int j){ return T[i] >= T[j]; }, [&](int x, int S){ return A(x) >= B(S); });
for(int i = 0; i < m; i++)
seg.add(i);
}
priority_queue<pli, vector<pli>, greater<pli> > pq;
fill(dis, dis + VERTID, inf);
pq.push({0, S});
dis[S] = 0;
while(sz(pq)){
int u = pq.top().S;
pq.pop();
if(mark[u])
continue;
mark[u] = 1;
for(int y : v[u]){
ll x = dis[u] + (y < m ? C[y] : 0);
if(dis[y] > x)
dis[y] = x, pq.push({dis[y], y});
}
}
ll ans = dis[E];
if(ans == inf)
ans = -1;
return cout << ans << endl, 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1545 ms |
318764 KB |
Output is correct |
2 |
Correct |
1485 ms |
319812 KB |
Output is correct |
3 |
Correct |
1366 ms |
317348 KB |
Output is correct |
4 |
Correct |
1280 ms |
317364 KB |
Output is correct |
5 |
Correct |
1324 ms |
321756 KB |
Output is correct |
6 |
Correct |
1309 ms |
321688 KB |
Output is correct |
7 |
Correct |
1286 ms |
321640 KB |
Output is correct |
8 |
Correct |
1127 ms |
317916 KB |
Output is correct |
9 |
Correct |
1293 ms |
318312 KB |
Output is correct |
10 |
Correct |
1188 ms |
318184 KB |
Output is correct |
11 |
Correct |
1406 ms |
320116 KB |
Output is correct |
12 |
Correct |
1449 ms |
320240 KB |
Output is correct |
13 |
Correct |
1765 ms |
323272 KB |
Output is correct |
14 |
Correct |
1805 ms |
323356 KB |
Output is correct |
15 |
Correct |
1673 ms |
321884 KB |
Output is correct |
16 |
Correct |
1660 ms |
321796 KB |
Output is correct |
17 |
Correct |
1592 ms |
320968 KB |
Output is correct |
18 |
Correct |
1403 ms |
319460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
122488 KB |
Output is correct |
2 |
Correct |
93 ms |
122616 KB |
Output is correct |
3 |
Correct |
85 ms |
122488 KB |
Output is correct |
4 |
Correct |
84 ms |
122488 KB |
Output is correct |
5 |
Correct |
86 ms |
122480 KB |
Output is correct |
6 |
Correct |
85 ms |
122488 KB |
Output is correct |
7 |
Correct |
87 ms |
122492 KB |
Output is correct |
8 |
Correct |
90 ms |
122488 KB |
Output is correct |
9 |
Correct |
91 ms |
122488 KB |
Output is correct |
10 |
Correct |
91 ms |
122508 KB |
Output is correct |
11 |
Correct |
87 ms |
122644 KB |
Output is correct |
12 |
Correct |
93 ms |
122488 KB |
Output is correct |
13 |
Correct |
87 ms |
122488 KB |
Output is correct |
14 |
Correct |
90 ms |
122488 KB |
Output is correct |
15 |
Correct |
85 ms |
122488 KB |
Output is correct |
16 |
Correct |
85 ms |
122488 KB |
Output is correct |
17 |
Correct |
86 ms |
122488 KB |
Output is correct |
18 |
Correct |
91 ms |
122492 KB |
Output is correct |
19 |
Correct |
86 ms |
122488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
122488 KB |
Output is correct |
2 |
Correct |
93 ms |
122616 KB |
Output is correct |
3 |
Correct |
85 ms |
122488 KB |
Output is correct |
4 |
Correct |
84 ms |
122488 KB |
Output is correct |
5 |
Correct |
86 ms |
122480 KB |
Output is correct |
6 |
Correct |
85 ms |
122488 KB |
Output is correct |
7 |
Correct |
87 ms |
122492 KB |
Output is correct |
8 |
Correct |
90 ms |
122488 KB |
Output is correct |
9 |
Correct |
91 ms |
122488 KB |
Output is correct |
10 |
Correct |
91 ms |
122508 KB |
Output is correct |
11 |
Correct |
87 ms |
122644 KB |
Output is correct |
12 |
Correct |
93 ms |
122488 KB |
Output is correct |
13 |
Correct |
87 ms |
122488 KB |
Output is correct |
14 |
Correct |
90 ms |
122488 KB |
Output is correct |
15 |
Correct |
85 ms |
122488 KB |
Output is correct |
16 |
Correct |
85 ms |
122488 KB |
Output is correct |
17 |
Correct |
86 ms |
122488 KB |
Output is correct |
18 |
Correct |
91 ms |
122492 KB |
Output is correct |
19 |
Correct |
86 ms |
122488 KB |
Output is correct |
20 |
Correct |
128 ms |
130300 KB |
Output is correct |
21 |
Correct |
125 ms |
130296 KB |
Output is correct |
22 |
Correct |
128 ms |
130180 KB |
Output is correct |
23 |
Correct |
127 ms |
130168 KB |
Output is correct |
24 |
Correct |
131 ms |
130428 KB |
Output is correct |
25 |
Correct |
129 ms |
130288 KB |
Output is correct |
26 |
Correct |
130 ms |
130264 KB |
Output is correct |
27 |
Correct |
127 ms |
130296 KB |
Output is correct |
28 |
Correct |
136 ms |
130424 KB |
Output is correct |
29 |
Correct |
133 ms |
130296 KB |
Output is correct |
30 |
Correct |
125 ms |
130168 KB |
Output is correct |
31 |
Correct |
121 ms |
130200 KB |
Output is correct |
32 |
Correct |
139 ms |
130552 KB |
Output is correct |
33 |
Correct |
146 ms |
130620 KB |
Output is correct |
34 |
Correct |
130 ms |
130284 KB |
Output is correct |
35 |
Correct |
135 ms |
130552 KB |
Output is correct |
36 |
Correct |
134 ms |
130552 KB |
Output is correct |
37 |
Correct |
135 ms |
130296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1545 ms |
318764 KB |
Output is correct |
2 |
Correct |
1485 ms |
319812 KB |
Output is correct |
3 |
Correct |
1366 ms |
317348 KB |
Output is correct |
4 |
Correct |
1280 ms |
317364 KB |
Output is correct |
5 |
Correct |
1324 ms |
321756 KB |
Output is correct |
6 |
Correct |
1309 ms |
321688 KB |
Output is correct |
7 |
Correct |
1286 ms |
321640 KB |
Output is correct |
8 |
Correct |
1127 ms |
317916 KB |
Output is correct |
9 |
Correct |
1293 ms |
318312 KB |
Output is correct |
10 |
Correct |
1188 ms |
318184 KB |
Output is correct |
11 |
Correct |
1406 ms |
320116 KB |
Output is correct |
12 |
Correct |
1449 ms |
320240 KB |
Output is correct |
13 |
Correct |
1765 ms |
323272 KB |
Output is correct |
14 |
Correct |
1805 ms |
323356 KB |
Output is correct |
15 |
Correct |
1673 ms |
321884 KB |
Output is correct |
16 |
Correct |
1660 ms |
321796 KB |
Output is correct |
17 |
Correct |
1592 ms |
320968 KB |
Output is correct |
18 |
Correct |
1403 ms |
319460 KB |
Output is correct |
19 |
Correct |
85 ms |
122488 KB |
Output is correct |
20 |
Correct |
93 ms |
122616 KB |
Output is correct |
21 |
Correct |
85 ms |
122488 KB |
Output is correct |
22 |
Correct |
84 ms |
122488 KB |
Output is correct |
23 |
Correct |
86 ms |
122480 KB |
Output is correct |
24 |
Correct |
85 ms |
122488 KB |
Output is correct |
25 |
Correct |
87 ms |
122492 KB |
Output is correct |
26 |
Correct |
90 ms |
122488 KB |
Output is correct |
27 |
Correct |
91 ms |
122488 KB |
Output is correct |
28 |
Correct |
91 ms |
122508 KB |
Output is correct |
29 |
Correct |
87 ms |
122644 KB |
Output is correct |
30 |
Correct |
93 ms |
122488 KB |
Output is correct |
31 |
Correct |
87 ms |
122488 KB |
Output is correct |
32 |
Correct |
90 ms |
122488 KB |
Output is correct |
33 |
Correct |
85 ms |
122488 KB |
Output is correct |
34 |
Correct |
85 ms |
122488 KB |
Output is correct |
35 |
Correct |
86 ms |
122488 KB |
Output is correct |
36 |
Correct |
91 ms |
122492 KB |
Output is correct |
37 |
Correct |
86 ms |
122488 KB |
Output is correct |
38 |
Correct |
128 ms |
130300 KB |
Output is correct |
39 |
Correct |
125 ms |
130296 KB |
Output is correct |
40 |
Correct |
128 ms |
130180 KB |
Output is correct |
41 |
Correct |
127 ms |
130168 KB |
Output is correct |
42 |
Correct |
131 ms |
130428 KB |
Output is correct |
43 |
Correct |
129 ms |
130288 KB |
Output is correct |
44 |
Correct |
130 ms |
130264 KB |
Output is correct |
45 |
Correct |
127 ms |
130296 KB |
Output is correct |
46 |
Correct |
136 ms |
130424 KB |
Output is correct |
47 |
Correct |
133 ms |
130296 KB |
Output is correct |
48 |
Correct |
125 ms |
130168 KB |
Output is correct |
49 |
Correct |
121 ms |
130200 KB |
Output is correct |
50 |
Correct |
139 ms |
130552 KB |
Output is correct |
51 |
Correct |
146 ms |
130620 KB |
Output is correct |
52 |
Correct |
130 ms |
130284 KB |
Output is correct |
53 |
Correct |
135 ms |
130552 KB |
Output is correct |
54 |
Correct |
134 ms |
130552 KB |
Output is correct |
55 |
Correct |
135 ms |
130296 KB |
Output is correct |
56 |
Correct |
1593 ms |
319632 KB |
Output is correct |
57 |
Correct |
1691 ms |
320236 KB |
Output is correct |
58 |
Correct |
1832 ms |
317796 KB |
Output is correct |
59 |
Correct |
1796 ms |
318056 KB |
Output is correct |
60 |
Correct |
1798 ms |
316048 KB |
Output is correct |
61 |
Correct |
1794 ms |
317904 KB |
Output is correct |
62 |
Correct |
1695 ms |
319448 KB |
Output is correct |
63 |
Correct |
1631 ms |
313448 KB |
Output is correct |
64 |
Correct |
1614 ms |
313392 KB |
Output is correct |
65 |
Correct |
1623 ms |
322008 KB |
Output is correct |
66 |
Correct |
1786 ms |
316148 KB |
Output is correct |
67 |
Correct |
1822 ms |
321996 KB |
Output is correct |
68 |
Correct |
1576 ms |
321512 KB |
Output is correct |
69 |
Correct |
1363 ms |
321392 KB |
Output is correct |
70 |
Correct |
1917 ms |
322024 KB |
Output is correct |
71 |
Correct |
1647 ms |
321696 KB |
Output is correct |
72 |
Correct |
1370 ms |
321192 KB |
Output is correct |
73 |
Correct |
2000 ms |
321680 KB |
Output is correct |
74 |
Correct |
1284 ms |
318720 KB |
Output is correct |
75 |
Correct |
1148 ms |
317856 KB |
Output is correct |
76 |
Correct |
1847 ms |
326500 KB |
Output is correct |
77 |
Correct |
2399 ms |
326600 KB |
Output is correct |
78 |
Correct |
2658 ms |
323500 KB |
Output is correct |
79 |
Correct |
2092 ms |
321776 KB |
Output is correct |
80 |
Correct |
2001 ms |
321604 KB |
Output is correct |
81 |
Correct |
1772 ms |
318960 KB |
Output is correct |
82 |
Correct |
2152 ms |
321124 KB |
Output is correct |
83 |
Correct |
2017 ms |
321096 KB |
Output is correct |
84 |
Correct |
2150 ms |
321204 KB |
Output is correct |
85 |
Correct |
1875 ms |
321360 KB |
Output is correct |
86 |
Correct |
1940 ms |
321312 KB |
Output is correct |
87 |
Correct |
1906 ms |
321344 KB |
Output is correct |
88 |
Correct |
1848 ms |
320212 KB |
Output is correct |
89 |
Correct |
1875 ms |
321324 KB |
Output is correct |
90 |
Correct |
2125 ms |
324036 KB |
Output is correct |
91 |
Correct |
2081 ms |
322636 KB |
Output is correct |
92 |
Correct |
1969 ms |
321212 KB |
Output is correct |
93 |
Correct |
2174 ms |
321780 KB |
Output is correct |
94 |
Correct |
1850 ms |
320812 KB |
Output is correct |
95 |
Correct |
2088 ms |
321540 KB |
Output is correct |
96 |
Correct |
2170 ms |
323756 KB |
Output is correct |
97 |
Correct |
2170 ms |
324524 KB |
Output is correct |
98 |
Correct |
2105 ms |
324704 KB |
Output is correct |
99 |
Correct |
2074 ms |
324576 KB |
Output is correct |
100 |
Correct |
1613 ms |
319860 KB |
Output is correct |
101 |
Correct |
2169 ms |
324612 KB |
Output is correct |
102 |
Correct |
1699 ms |
324900 KB |
Output is correct |
103 |
Correct |
1987 ms |
321560 KB |
Output is correct |