// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#define debug(...) ((void)0)
#endif
struct Lisan: vector<int> {
void done() { sort(begin(), end()); erase(unique(begin(), end()), end()); }
int operator () (const int& v) const { return std::lower_bound(begin(), end(), v) - begin(); }
int upper_bound (const int& v) const { return std::upper_bound(begin(), end(), v) - begin(); }
};
struct Treatment {
int T, L, R, C, x, y;
Treatment (const int& _T, const int& _L, const int& _R, const int& _C):
T(_T), L(_L), R(_R), C(_C), x(L + T), y(L - T) {}
};
struct SegmentTree {
int n; vector<vector<pair<int, int>>> val;
SegmentTree (const int& _n): n(_n), val(4 << __lg(n)) {}
void insert(int i, int l, int r, int p, pair<int, int> v) {
if (l + 1 == r) {
val[i].push_back(v);
return;
}
int m = (l + r) / 2;
if (p < m) insert(i * 2, l, m, p, v);
else insert(i * 2 + 1, m, r, p, v);
}
void insert(int x, int y, int i) { insert(1, 0, n, x, pair<int, int>(y, i)); }
void build(int i, int l, int r) {
if (l + 1 == r) {
sort(begin(val[i]), end(val[i]), greater<pair<int, int>>());
return;
}
int m = (l + r) / 2;
build(i * 2, l, m);
build(i*2+1, m, r);
val[i].reserve(val[i * 2].size() + val[i*2+1].size());
merge(begin(val[i * 2]), end(val[i * 2]), begin(val[i*2+1]), end(val[i*2+1]), back_inserter(val[i]), greater<pair<int, int>>());
}
void build() { build(1, 0, n); }
void collect(int i, int l, int r, int x, int y, vector<int>& ids) {
debug(i, l, r, x, y, val[i]);
if (x <= l) return;
if (r <= x) {
while (!val[i].empty()) {
auto [yi, id] = val[i].back();
if (yi <= y) {
ids.emplace_back(id);
val[i].pop_back();
} else break;
}
return;
}
int m = (l + r) / 2;
collect(i * 2, l, m, x, y, ids);
if (m < x) collect(i * 2 + 1, m, r, x, y, ids);
}
void collect(int x, int y, vector<int>& ids) { collect(1, 0, n, x, y, ids); }
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int N, M;
cin >> N >> M;
vector<Treatment> ts;
Lisan lisan;
ts.reserve(M);
lisan.reserve(M);
for (int T, L, R, C, i = 0; i < M; ++i) {
cin >> T >> L >> R >> C;
ts.emplace_back(T, L, R, C);
lisan.emplace_back(ts.back().x);
}
lisan.done();
debug(lisan.size(), lisan);
SegmentTree sgt(lisan.size());
for (int i = 0; i < M; ++i) {
const auto& [T, L, R, C, x, y] = ts[i];
sgt.insert(lisan(x), y, i);
debug(T, L, R, lisan(x), y, i);
}
sgt.build();
vector<long long> dis(M, LLONG_MAX);
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
for (int i = 0; i < M; ++i) {
if (ts[i].L == 1) {
dis[i] = ts[i].C;
pq.emplace(dis[i], i);
}
}
vector<int> vs;
vs.reserve(M);
while (!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
assert(d == dis[u]);
int x = ts[u].R + ts[u].T + 1;
int y = ts[u].R - ts[u].T + 1;
debug(u, x, y);
sgt.collect(lisan.upper_bound(x), y, vs);
debug(vs);
for (const int& v: vs) {
if (d + ts[v].C < dis[v]) {
dis[v] = d + ts[v].C;
pq.emplace(dis[v], v);
}
}
vs.clear();
}
long long ans = LLONG_MAX;
for (int i = 0; i < M; ++i) {
if (ts[i].R == N) {
ans = min(ans, dis[i]);
}
}
if (ans == LLONG_MAX) ans = -1;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
31024 KB |
Output is correct |
2 |
Correct |
122 ms |
30712 KB |
Output is correct |
3 |
Correct |
121 ms |
25264 KB |
Output is correct |
4 |
Correct |
130 ms |
24860 KB |
Output is correct |
5 |
Correct |
75 ms |
15416 KB |
Output is correct |
6 |
Correct |
77 ms |
16400 KB |
Output is correct |
7 |
Correct |
82 ms |
20040 KB |
Output is correct |
8 |
Correct |
68 ms |
12996 KB |
Output is correct |
9 |
Correct |
66 ms |
15836 KB |
Output is correct |
10 |
Correct |
64 ms |
19644 KB |
Output is correct |
11 |
Correct |
158 ms |
33024 KB |
Output is correct |
12 |
Correct |
175 ms |
33028 KB |
Output is correct |
13 |
Correct |
165 ms |
32900 KB |
Output is correct |
14 |
Correct |
167 ms |
32968 KB |
Output is correct |
15 |
Correct |
147 ms |
30924 KB |
Output is correct |
16 |
Correct |
173 ms |
30912 KB |
Output is correct |
17 |
Correct |
137 ms |
30112 KB |
Output is correct |
18 |
Correct |
142 ms |
32296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
7 ms |
1736 KB |
Output is correct |
21 |
Correct |
6 ms |
1748 KB |
Output is correct |
22 |
Correct |
6 ms |
1736 KB |
Output is correct |
23 |
Correct |
6 ms |
1748 KB |
Output is correct |
24 |
Correct |
7 ms |
1876 KB |
Output is correct |
25 |
Correct |
7 ms |
1748 KB |
Output is correct |
26 |
Correct |
6 ms |
1740 KB |
Output is correct |
27 |
Correct |
6 ms |
1768 KB |
Output is correct |
28 |
Correct |
7 ms |
1876 KB |
Output is correct |
29 |
Correct |
6 ms |
1748 KB |
Output is correct |
30 |
Correct |
5 ms |
1748 KB |
Output is correct |
31 |
Correct |
5 ms |
1748 KB |
Output is correct |
32 |
Correct |
8 ms |
1984 KB |
Output is correct |
33 |
Correct |
9 ms |
2004 KB |
Output is correct |
34 |
Correct |
8 ms |
1748 KB |
Output is correct |
35 |
Correct |
9 ms |
1852 KB |
Output is correct |
36 |
Correct |
6 ms |
1968 KB |
Output is correct |
37 |
Correct |
6 ms |
1732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
31024 KB |
Output is correct |
2 |
Correct |
122 ms |
30712 KB |
Output is correct |
3 |
Correct |
121 ms |
25264 KB |
Output is correct |
4 |
Correct |
130 ms |
24860 KB |
Output is correct |
5 |
Correct |
75 ms |
15416 KB |
Output is correct |
6 |
Correct |
77 ms |
16400 KB |
Output is correct |
7 |
Correct |
82 ms |
20040 KB |
Output is correct |
8 |
Correct |
68 ms |
12996 KB |
Output is correct |
9 |
Correct |
66 ms |
15836 KB |
Output is correct |
10 |
Correct |
64 ms |
19644 KB |
Output is correct |
11 |
Correct |
158 ms |
33024 KB |
Output is correct |
12 |
Correct |
175 ms |
33028 KB |
Output is correct |
13 |
Correct |
165 ms |
32900 KB |
Output is correct |
14 |
Correct |
167 ms |
32968 KB |
Output is correct |
15 |
Correct |
147 ms |
30924 KB |
Output is correct |
16 |
Correct |
173 ms |
30912 KB |
Output is correct |
17 |
Correct |
137 ms |
30112 KB |
Output is correct |
18 |
Correct |
142 ms |
32296 KB |
Output is correct |
19 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
320 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
320 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
7 ms |
1736 KB |
Output is correct |
39 |
Correct |
6 ms |
1748 KB |
Output is correct |
40 |
Correct |
6 ms |
1736 KB |
Output is correct |
41 |
Correct |
6 ms |
1748 KB |
Output is correct |
42 |
Correct |
7 ms |
1876 KB |
Output is correct |
43 |
Correct |
7 ms |
1748 KB |
Output is correct |
44 |
Correct |
6 ms |
1740 KB |
Output is correct |
45 |
Correct |
6 ms |
1768 KB |
Output is correct |
46 |
Correct |
7 ms |
1876 KB |
Output is correct |
47 |
Correct |
6 ms |
1748 KB |
Output is correct |
48 |
Correct |
5 ms |
1748 KB |
Output is correct |
49 |
Correct |
5 ms |
1748 KB |
Output is correct |
50 |
Correct |
8 ms |
1984 KB |
Output is correct |
51 |
Correct |
9 ms |
2004 KB |
Output is correct |
52 |
Correct |
8 ms |
1748 KB |
Output is correct |
53 |
Correct |
9 ms |
1852 KB |
Output is correct |
54 |
Correct |
6 ms |
1968 KB |
Output is correct |
55 |
Correct |
6 ms |
1732 KB |
Output is correct |
56 |
Correct |
141 ms |
31300 KB |
Output is correct |
57 |
Correct |
146 ms |
30316 KB |
Output is correct |
58 |
Correct |
149 ms |
29932 KB |
Output is correct |
59 |
Correct |
131 ms |
30300 KB |
Output is correct |
60 |
Correct |
154 ms |
31352 KB |
Output is correct |
61 |
Correct |
157 ms |
29952 KB |
Output is correct |
62 |
Correct |
147 ms |
31356 KB |
Output is correct |
63 |
Correct |
130 ms |
31016 KB |
Output is correct |
64 |
Correct |
126 ms |
31060 KB |
Output is correct |
65 |
Correct |
155 ms |
31180 KB |
Output is correct |
66 |
Correct |
148 ms |
30808 KB |
Output is correct |
67 |
Correct |
173 ms |
31636 KB |
Output is correct |
68 |
Correct |
192 ms |
31408 KB |
Output is correct |
69 |
Correct |
138 ms |
31440 KB |
Output is correct |
70 |
Correct |
183 ms |
31672 KB |
Output is correct |
71 |
Correct |
169 ms |
31524 KB |
Output is correct |
72 |
Correct |
135 ms |
31548 KB |
Output is correct |
73 |
Correct |
197 ms |
31684 KB |
Output is correct |
74 |
Correct |
101 ms |
31308 KB |
Output is correct |
75 |
Correct |
101 ms |
31348 KB |
Output is correct |
76 |
Correct |
183 ms |
33492 KB |
Output is correct |
77 |
Correct |
183 ms |
33800 KB |
Output is correct |
78 |
Correct |
223 ms |
33228 KB |
Output is correct |
79 |
Correct |
184 ms |
31544 KB |
Output is correct |
80 |
Correct |
151 ms |
31280 KB |
Output is correct |
81 |
Correct |
119 ms |
31280 KB |
Output is correct |
82 |
Correct |
151 ms |
30768 KB |
Output is correct |
83 |
Correct |
159 ms |
30900 KB |
Output is correct |
84 |
Correct |
186 ms |
30552 KB |
Output is correct |
85 |
Correct |
154 ms |
31392 KB |
Output is correct |
86 |
Correct |
142 ms |
31356 KB |
Output is correct |
87 |
Correct |
152 ms |
31616 KB |
Output is correct |
88 |
Correct |
147 ms |
30408 KB |
Output is correct |
89 |
Correct |
143 ms |
31308 KB |
Output is correct |
90 |
Correct |
193 ms |
33792 KB |
Output is correct |
91 |
Correct |
164 ms |
32680 KB |
Output is correct |
92 |
Correct |
144 ms |
31300 KB |
Output is correct |
93 |
Correct |
188 ms |
31436 KB |
Output is correct |
94 |
Correct |
139 ms |
26528 KB |
Output is correct |
95 |
Correct |
162 ms |
31452 KB |
Output is correct |
96 |
Correct |
198 ms |
33540 KB |
Output is correct |
97 |
Correct |
186 ms |
33620 KB |
Output is correct |
98 |
Correct |
195 ms |
33588 KB |
Output is correct |
99 |
Correct |
209 ms |
33696 KB |
Output is correct |
100 |
Correct |
149 ms |
28352 KB |
Output is correct |
101 |
Correct |
213 ms |
33600 KB |
Output is correct |
102 |
Correct |
166 ms |
33932 KB |
Output is correct |
103 |
Correct |
147 ms |
31860 KB |
Output is correct |