#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vi = vector<int>;
using vll = vector<ll>;
struct project
{
int T;
int L;
int R;
int C;
int I;
};
vector<project> P;
const int maxM = 100'000;
vi query_ans;
vi visited(maxM, 0);
const ll INF = 1'000'000'000'000'000'000LL;
vll dp(maxM, INF);
struct pos
{
int i;
};
bool operator < (pos A, pos B)
{
if(dp[A.i] == dp[B.i]) return A.i < B.i;
else return dp[A.i] < dp[B.i];
}
struct segtree
{
int l;
int r;
deque<pair<int, int>> u;
deque<pair<int, int> >d;
segtree* left = NULL;
segtree* right = NULL;
segtree()
{
;
}
segtree(int L, int R)
{
l = L;
r = R;
if(l == r) return;
int m = (l+r)/2;
left = new segtree(l, m);
right = new segtree(m+1, r);
}
void addU(int I, ll Pr, ll val)
{
u.push_back({Pr, val});
if(l != r)
{
if(I <= (l+r)/2) left->addU(I, Pr, val);
else right->addU(I, Pr, val);
}
}
void getU(int L, int R, ll mxv)
{
if(R < l || r < L) return;
else if(L <= l && r <= R)
{
while(1)
{
if(u.empty()) break;
if(u[0].second > mxv) break;
if(!visited[u[0].first])
{
query_ans.push_back(u[0].first);
visited[u[0].first] = 1;
}
u.pop_front();
}
}
else
{
left->getU(L, R, mxv);
right->getU(L, R, mxv);
}
}
void addD(int I, ll Pr, ll val)
{
d.push_back({Pr, val});
if(l != r)
{
if(I <= (l+r)/2) left->addD(I, Pr, val);
else right->addD(I, Pr, val);
}
}
void getD(int L, int R, ll mxv)
{
if(R < l || r < L) return;
else if(L <= l && r <= R)
{
while(1)
{
if(d.empty()) break;
if(d[0].second > mxv) break;
if(!visited[d[0].first])
{
query_ans.push_back(d[0].first);
visited[d[0].first] = 1;
}
d.pop_front();
}
}
else
{
left->getD(L, R, mxv);
right->getD(L, R, mxv);
}
}
};
int main()
{
int N, M;
cin >> N >> M;
M = min(M, 100000);
P = vector<project>(M);
for(int i = 0; i < M; i++) cin >> P[i].T >> P[i].L >> P[i].R >> P[i].C;
sort(P.begin(), P.end(), [] (project x, project y)
{
return x.T < y.T;
});
for(int i = 0; i < M; i++) P[i].I = i;
segtree S(0, M-1);
sort(P.begin(), P.end(), [] (project x, project y)
{
return x.L + x.T <= y.L + y.T;
});
for(int i = 0; i < M; i++)
S.addU(P[i].I, P[i].I, P[i].L + P[i].T);
sort(P.begin(), P.end(), [] (project x, project y)
{
return x.L - x.T < y.L - y.T;
});
for(int i = 0; i < M; i++)
S.addD(P[i].I, P[i].I, P[i].L - P[i].T);
sort(P.begin(), P.end(), [] (project x, project y)
{
return x.I < y.I;
});
for(int i = 0; i < M; i++)
{
if(P[i].L == 1)
{
dp[i] = P[i].C;
visited[i] = 1;
}
}
set<pos> tbv;
for(int i = 0; i < M; i++) tbv.insert(pos{i});
while(!tbv.empty())
{
int u = tbv.begin()->i;
tbv.erase(tbv.begin());
// cerr << "visiting " << u << ' ' << P[u].L << ' ' << P[u].R << '\n';
if(!visited[u]) break;
query_ans.clear();
S.getU(u+1, M-1, P[u].R + P[u].T + 1);
S.getD(0, u-1, P[u].R - P[u].T + 1);
for(int v: query_ans)
{
// cerr << u << " -> " << v << '\n';
if(P[v].L <= P[u].R + 1 - abs(P[u].T - P[v].T))
{
if(dp[v] > dp[u] + P[v].C)
{
tbv.erase(pos{v});
dp[v] = dp[u] + P[v].C;
tbv.insert(pos{v});
}
}
}
}
ll final_ans = INF;
for(int i = 0; i < M; i++)
if(P[i].R == N)
final_ans = min(final_ans, dp[i]);
if(final_ans >= INF) final_ans = -1;
cout << final_ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
887 ms |
301088 KB |
Output is correct |
2 |
Correct |
674 ms |
301088 KB |
Output is correct |
3 |
Runtime error |
511 ms |
524288 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1356 KB |
Output is correct |
2 |
Correct |
2 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
1 ms |
1472 KB |
Output is correct |
6 |
Correct |
1 ms |
1484 KB |
Output is correct |
7 |
Correct |
1 ms |
1484 KB |
Output is correct |
8 |
Correct |
1 ms |
1484 KB |
Output is correct |
9 |
Correct |
1 ms |
1484 KB |
Output is correct |
10 |
Correct |
1 ms |
1484 KB |
Output is correct |
11 |
Correct |
1 ms |
1484 KB |
Output is correct |
12 |
Correct |
1 ms |
1484 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1484 KB |
Output is correct |
15 |
Correct |
1 ms |
1484 KB |
Output is correct |
16 |
Correct |
1 ms |
1484 KB |
Output is correct |
17 |
Correct |
1 ms |
1484 KB |
Output is correct |
18 |
Correct |
1 ms |
1484 KB |
Output is correct |
19 |
Correct |
1 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1356 KB |
Output is correct |
2 |
Correct |
2 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
1 ms |
1472 KB |
Output is correct |
6 |
Correct |
1 ms |
1484 KB |
Output is correct |
7 |
Correct |
1 ms |
1484 KB |
Output is correct |
8 |
Correct |
1 ms |
1484 KB |
Output is correct |
9 |
Correct |
1 ms |
1484 KB |
Output is correct |
10 |
Correct |
1 ms |
1484 KB |
Output is correct |
11 |
Correct |
1 ms |
1484 KB |
Output is correct |
12 |
Correct |
1 ms |
1484 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1484 KB |
Output is correct |
15 |
Correct |
1 ms |
1484 KB |
Output is correct |
16 |
Correct |
1 ms |
1484 KB |
Output is correct |
17 |
Correct |
1 ms |
1484 KB |
Output is correct |
18 |
Correct |
1 ms |
1484 KB |
Output is correct |
19 |
Correct |
1 ms |
1484 KB |
Output is correct |
20 |
Correct |
25 ms |
16056 KB |
Output is correct |
21 |
Correct |
23 ms |
16076 KB |
Output is correct |
22 |
Correct |
25 ms |
16004 KB |
Output is correct |
23 |
Correct |
21 ms |
16076 KB |
Output is correct |
24 |
Correct |
25 ms |
16044 KB |
Output is correct |
25 |
Correct |
27 ms |
16068 KB |
Output is correct |
26 |
Correct |
25 ms |
16076 KB |
Output is correct |
27 |
Correct |
24 ms |
16064 KB |
Output is correct |
28 |
Correct |
24 ms |
16076 KB |
Output is correct |
29 |
Correct |
24 ms |
16092 KB |
Output is correct |
30 |
Correct |
21 ms |
16036 KB |
Output is correct |
31 |
Correct |
19 ms |
16036 KB |
Output is correct |
32 |
Correct |
26 ms |
16016 KB |
Output is correct |
33 |
Correct |
27 ms |
16036 KB |
Output is correct |
34 |
Correct |
26 ms |
15980 KB |
Output is correct |
35 |
Correct |
30 ms |
16100 KB |
Output is correct |
36 |
Correct |
31 ms |
16076 KB |
Output is correct |
37 |
Correct |
29 ms |
16020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
887 ms |
301088 KB |
Output is correct |
2 |
Correct |
674 ms |
301088 KB |
Output is correct |
3 |
Runtime error |
511 ms |
524288 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |