#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push back
#define pii pair<int, int>
#define pll pair<ll, int>
using namespace std;
const int N = 1e6 + 6;
const int mod = 1e9 + 7;
const int inf = 2e9 + 7;
struct node
{
int t, l, r, v;
bool operator<(const node &u) const
{
return t < u.t;
}
} d[N];
struct IT
{
int n;
vector<pii> st;
IT(int _n)
{
n = _n;
st.resize(n * 4 + 4, make_pair(inf, 0));
}
void build(int id, int l, int r, int pos, pii v)
{
if (l == r)
{
st[id] = v;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
build(id << 1, l, mid, pos, v);
else
build(id << 1 | 1, mid + 1, r, pos, v);
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
void build(int pos, pii v)
{
build(1, 1, n, pos, v);
}
pii get(int id, int l, int r, int u, int v)
{
if (u <= l && r <= v)
return st[id];
if (u > r || l > v)
return make_pair(inf, 0);
int mid = (l + r) >> 1;
return min(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
pii get(int l, int r)
{
if (l > r)
return make_pair(inf, 0);
return get(1, 1, n, l, r);
}
};
int n, m, k, t, a[N], b[N], l[N], r[N];
ll ans;
int main()
{
cin.tie(0);
cin >> n >> m;
ans = -1;
priority_queue<pll, vector<pll>, greater<pll>> pq;
for (int i = 1; i <= m; i++)
cin >> d[i].t >> d[i].l >> d[i].r >> d[i].v;
sort(d + 1, d + 1 + m);
IT L(m), R(m);
for (int i = 1; i <= m; i++)
{
--d[i].l;
//cout << d[i].t << " " << d[i].l << " " << d[i].r << " " << d[i].v << '\n';
if (i > 1 && d[i].t == d[i - 1].t)
l[i] = l[i - 1];
else
l[i] = i;
if (d[i].l == 0)
{
pq.push({d[i].v, i});
continue;
}
L.build(i, make_pair(d[i].l - d[i].t, i));
R.build(i, make_pair(d[i].l + d[i].t, i));
}
r[m] = m;
for (int i = m - 1; i > 0; i--)
{
if (d[i].t == d[i + 1].t)
r[i] = r[i + 1];
else
r[i] = i;
}
while (!pq.empty())
{
pll u = pq.top();
pq.pop();
//cout << u.se << " " << u.fi << '\n';
if (d[u.se].r == n)
{
if (ans == -1 || ans > u.fi)
ans = u.fi;
}
k = d[u.se].r - d[u.se].t;
while (true)
{
pii v = L.get(1, r[u.se]);
if (v.fi <= k)
{
L.build(v.se, make_pair(inf, 0));
R.build(v.se, make_pair(inf, 0));
pq.push({u.fi+d[v.se].v, v.se});
}
else
break;
}
k = d[u.se].r + d[u.se].t;
while (true)
{
pii v = R.get(l[u.se], m);
if (v.fi <= k)
{
R.build(v.se, make_pair(inf, 0));
L.build(v.se, make_pair(inf, 0));
pq.push({u.fi+d[v.se].v, v.se});
}
else
break;
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
12184 KB |
Output is correct |
2 |
Correct |
159 ms |
12120 KB |
Output is correct |
3 |
Correct |
159 ms |
11680 KB |
Output is correct |
4 |
Correct |
154 ms |
12244 KB |
Output is correct |
5 |
Correct |
195 ms |
14148 KB |
Output is correct |
6 |
Correct |
180 ms |
12268 KB |
Output is correct |
7 |
Correct |
167 ms |
11944 KB |
Output is correct |
8 |
Correct |
146 ms |
12020 KB |
Output is correct |
9 |
Correct |
139 ms |
11976 KB |
Output is correct |
10 |
Correct |
137 ms |
11960 KB |
Output is correct |
11 |
Correct |
213 ms |
14272 KB |
Output is correct |
12 |
Correct |
241 ms |
14228 KB |
Output is correct |
13 |
Correct |
211 ms |
14180 KB |
Output is correct |
14 |
Correct |
203 ms |
14184 KB |
Output is correct |
15 |
Correct |
222 ms |
12096 KB |
Output is correct |
16 |
Correct |
202 ms |
12096 KB |
Output is correct |
17 |
Correct |
178 ms |
11372 KB |
Output is correct |
18 |
Correct |
195 ms |
13448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
11 ms |
852 KB |
Output is correct |
21 |
Correct |
9 ms |
820 KB |
Output is correct |
22 |
Correct |
11 ms |
948 KB |
Output is correct |
23 |
Correct |
10 ms |
936 KB |
Output is correct |
24 |
Correct |
12 ms |
1092 KB |
Output is correct |
25 |
Correct |
12 ms |
948 KB |
Output is correct |
26 |
Correct |
12 ms |
972 KB |
Output is correct |
27 |
Correct |
11 ms |
960 KB |
Output is correct |
28 |
Correct |
11 ms |
1068 KB |
Output is correct |
29 |
Correct |
11 ms |
956 KB |
Output is correct |
30 |
Correct |
8 ms |
852 KB |
Output is correct |
31 |
Correct |
9 ms |
852 KB |
Output is correct |
32 |
Correct |
12 ms |
1108 KB |
Output is correct |
33 |
Correct |
12 ms |
1108 KB |
Output is correct |
34 |
Correct |
19 ms |
956 KB |
Output is correct |
35 |
Correct |
12 ms |
1108 KB |
Output is correct |
36 |
Correct |
12 ms |
1032 KB |
Output is correct |
37 |
Correct |
12 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
12184 KB |
Output is correct |
2 |
Correct |
159 ms |
12120 KB |
Output is correct |
3 |
Correct |
159 ms |
11680 KB |
Output is correct |
4 |
Correct |
154 ms |
12244 KB |
Output is correct |
5 |
Correct |
195 ms |
14148 KB |
Output is correct |
6 |
Correct |
180 ms |
12268 KB |
Output is correct |
7 |
Correct |
167 ms |
11944 KB |
Output is correct |
8 |
Correct |
146 ms |
12020 KB |
Output is correct |
9 |
Correct |
139 ms |
11976 KB |
Output is correct |
10 |
Correct |
137 ms |
11960 KB |
Output is correct |
11 |
Correct |
213 ms |
14272 KB |
Output is correct |
12 |
Correct |
241 ms |
14228 KB |
Output is correct |
13 |
Correct |
211 ms |
14180 KB |
Output is correct |
14 |
Correct |
203 ms |
14184 KB |
Output is correct |
15 |
Correct |
222 ms |
12096 KB |
Output is correct |
16 |
Correct |
202 ms |
12096 KB |
Output is correct |
17 |
Correct |
178 ms |
11372 KB |
Output is correct |
18 |
Correct |
195 ms |
13448 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
304 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
304 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
308 KB |
Output is correct |
38 |
Correct |
11 ms |
852 KB |
Output is correct |
39 |
Correct |
9 ms |
820 KB |
Output is correct |
40 |
Correct |
11 ms |
948 KB |
Output is correct |
41 |
Correct |
10 ms |
936 KB |
Output is correct |
42 |
Correct |
12 ms |
1092 KB |
Output is correct |
43 |
Correct |
12 ms |
948 KB |
Output is correct |
44 |
Correct |
12 ms |
972 KB |
Output is correct |
45 |
Correct |
11 ms |
960 KB |
Output is correct |
46 |
Correct |
11 ms |
1068 KB |
Output is correct |
47 |
Correct |
11 ms |
956 KB |
Output is correct |
48 |
Correct |
8 ms |
852 KB |
Output is correct |
49 |
Correct |
9 ms |
852 KB |
Output is correct |
50 |
Correct |
12 ms |
1108 KB |
Output is correct |
51 |
Correct |
12 ms |
1108 KB |
Output is correct |
52 |
Correct |
19 ms |
956 KB |
Output is correct |
53 |
Correct |
12 ms |
1108 KB |
Output is correct |
54 |
Correct |
12 ms |
1032 KB |
Output is correct |
55 |
Correct |
12 ms |
852 KB |
Output is correct |
56 |
Correct |
176 ms |
11904 KB |
Output is correct |
57 |
Correct |
202 ms |
13180 KB |
Output is correct |
58 |
Correct |
220 ms |
11188 KB |
Output is correct |
59 |
Correct |
201 ms |
11632 KB |
Output is correct |
60 |
Correct |
214 ms |
12536 KB |
Output is correct |
61 |
Correct |
184 ms |
11376 KB |
Output is correct |
62 |
Correct |
184 ms |
12072 KB |
Output is correct |
63 |
Correct |
196 ms |
12160 KB |
Output is correct |
64 |
Correct |
197 ms |
12072 KB |
Output is correct |
65 |
Correct |
197 ms |
12232 KB |
Output is correct |
66 |
Correct |
201 ms |
12356 KB |
Output is correct |
67 |
Correct |
266 ms |
12912 KB |
Output is correct |
68 |
Correct |
278 ms |
12872 KB |
Output is correct |
69 |
Correct |
254 ms |
12884 KB |
Output is correct |
70 |
Correct |
279 ms |
13132 KB |
Output is correct |
71 |
Correct |
267 ms |
12824 KB |
Output is correct |
72 |
Correct |
263 ms |
12880 KB |
Output is correct |
73 |
Correct |
322 ms |
12936 KB |
Output is correct |
74 |
Correct |
180 ms |
12812 KB |
Output is correct |
75 |
Correct |
181 ms |
12764 KB |
Output is correct |
76 |
Correct |
285 ms |
14656 KB |
Output is correct |
77 |
Correct |
377 ms |
15060 KB |
Output is correct |
78 |
Correct |
311 ms |
14588 KB |
Output is correct |
79 |
Correct |
284 ms |
12868 KB |
Output is correct |
80 |
Correct |
284 ms |
12408 KB |
Output is correct |
81 |
Correct |
171 ms |
12764 KB |
Output is correct |
82 |
Correct |
264 ms |
11892 KB |
Output is correct |
83 |
Correct |
261 ms |
12012 KB |
Output is correct |
84 |
Correct |
261 ms |
12008 KB |
Output is correct |
85 |
Correct |
221 ms |
12892 KB |
Output is correct |
86 |
Correct |
236 ms |
12776 KB |
Output is correct |
87 |
Correct |
291 ms |
12820 KB |
Output is correct |
88 |
Correct |
246 ms |
12828 KB |
Output is correct |
89 |
Correct |
272 ms |
12716 KB |
Output is correct |
90 |
Correct |
310 ms |
14912 KB |
Output is correct |
91 |
Correct |
268 ms |
13988 KB |
Output is correct |
92 |
Correct |
228 ms |
12748 KB |
Output is correct |
93 |
Correct |
280 ms |
12868 KB |
Output is correct |
94 |
Correct |
289 ms |
13076 KB |
Output is correct |
95 |
Correct |
272 ms |
12852 KB |
Output is correct |
96 |
Correct |
308 ms |
14964 KB |
Output is correct |
97 |
Correct |
276 ms |
14968 KB |
Output is correct |
98 |
Correct |
290 ms |
15036 KB |
Output is correct |
99 |
Correct |
273 ms |
15000 KB |
Output is correct |
100 |
Correct |
259 ms |
14928 KB |
Output is correct |
101 |
Correct |
274 ms |
14952 KB |
Output is correct |
102 |
Correct |
298 ms |
14956 KB |
Output is correct |
103 |
Correct |
259 ms |
13184 KB |
Output is correct |