#include <bits/stdc++.h>
using namespace std;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
const int MAXN = 1e5 + 5;
struct Data {
int T, L, R, C;
} a[MAXN];
int N, M, dd[MAXN];
vector <int> cp;
long long dp[MAXN];
priority_queue <pair <long long, int>, vector <pair <long long, int>>, greater <pair <long long, int>>> pq, q[MAXN];
vector <int> ver;
void update(int u, int val, int id) {
for (int v = u; v <= (int) cp.size(); v += v & -v) {
q[v].emplace(val, id);
}
}
void get(int u, int t) {
for (; u > 0; u -= u & -u) {
while(!q[u].empty() && q[u].top().first <= t) {
ver.emplace_back(q[u].top().second); q[u].pop();
}
}
}
void you_make_it(void) {
cin >> N >> M;
FOR(i, 1, M) {
cin >> a[i].T >> a[i].L >> a[i].R >> a[i].C;
cp.emplace_back(a[i].L - a[i].T);
}
sort(cp.begin(), cp.end()); cp.erase(unique(cp.begin(), cp.end()), cp.end());
memset(dp, 0x3f, sizeof dp);
FOR(i, 1, M) {
update(lower_bound(cp.begin(), cp.end(), a[i].L - a[i].T) - cp.begin() + 1, a[i].L + a[i].T, i);
}
FOR(i, 1, M) {
if(a[i].L == 1) {
dp[i] = a[i].C;
dd[i] = 1;
pq.emplace(dp[i], i);
}
}
long long ans = 1E18;
while(!pq.empty()) {
auto [du, u] = pq.top(); pq.pop();
if(du != dp[u]) continue;
if(a[u].R == N) {
minimize(ans, du);
continue;
}
ver.clear();
get(upper_bound(cp.begin(), cp.end(), a[u].R - a[u].T + 1) - cp.begin(), a[u].R + a[u].T + 1);
for (auto i : ver) {
if(!dd[i] && minimize(dp[i], dp[u] + a[i].C)) {
pq.emplace(dp[i], i);
dd[i] = 1;
}
}
}
// FOR(i, 1, M) cout << dp[i] << " \n"[i == M];
cout << (ans >= 1E18 ? -1 : ans);
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
file("treatment");
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message
treatment.cpp: In function 'int main()':
treatment.cpp:13:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treatment.cpp:90:5: note: in expansion of macro 'file'
90 | file("treatment");
| ^~~~
treatment.cpp:13:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
treatment.cpp:90:5: note: in expansion of macro 'file'
90 | file("treatment");
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
22356 KB |
Output is correct |
2 |
Correct |
110 ms |
25412 KB |
Output is correct |
3 |
Correct |
159 ms |
29464 KB |
Output is correct |
4 |
Correct |
149 ms |
28488 KB |
Output is correct |
5 |
Correct |
87 ms |
18892 KB |
Output is correct |
6 |
Correct |
97 ms |
19668 KB |
Output is correct |
7 |
Correct |
109 ms |
26112 KB |
Output is correct |
8 |
Correct |
38 ms |
15820 KB |
Output is correct |
9 |
Correct |
46 ms |
18884 KB |
Output is correct |
10 |
Correct |
53 ms |
25032 KB |
Output is correct |
11 |
Correct |
161 ms |
29636 KB |
Output is correct |
12 |
Correct |
172 ms |
29484 KB |
Output is correct |
13 |
Correct |
170 ms |
28868 KB |
Output is correct |
14 |
Correct |
174 ms |
29000 KB |
Output is correct |
15 |
Correct |
158 ms |
27228 KB |
Output is correct |
16 |
Correct |
156 ms |
26432 KB |
Output is correct |
17 |
Correct |
158 ms |
26428 KB |
Output is correct |
18 |
Correct |
172 ms |
29136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4696 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4744 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4532 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4696 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4744 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4532 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
6 ms |
5468 KB |
Output is correct |
21 |
Correct |
6 ms |
5724 KB |
Output is correct |
22 |
Correct |
6 ms |
5464 KB |
Output is correct |
23 |
Correct |
5 ms |
5520 KB |
Output is correct |
24 |
Correct |
7 ms |
5724 KB |
Output is correct |
25 |
Correct |
7 ms |
5652 KB |
Output is correct |
26 |
Correct |
6 ms |
5468 KB |
Output is correct |
27 |
Correct |
5 ms |
5468 KB |
Output is correct |
28 |
Correct |
7 ms |
5768 KB |
Output is correct |
29 |
Correct |
7 ms |
5468 KB |
Output is correct |
30 |
Correct |
4 ms |
5524 KB |
Output is correct |
31 |
Correct |
4 ms |
5468 KB |
Output is correct |
32 |
Correct |
7 ms |
5888 KB |
Output is correct |
33 |
Correct |
6 ms |
5728 KB |
Output is correct |
34 |
Correct |
6 ms |
5656 KB |
Output is correct |
35 |
Correct |
6 ms |
5724 KB |
Output is correct |
36 |
Correct |
6 ms |
5720 KB |
Output is correct |
37 |
Correct |
6 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
22356 KB |
Output is correct |
2 |
Correct |
110 ms |
25412 KB |
Output is correct |
3 |
Correct |
159 ms |
29464 KB |
Output is correct |
4 |
Correct |
149 ms |
28488 KB |
Output is correct |
5 |
Correct |
87 ms |
18892 KB |
Output is correct |
6 |
Correct |
97 ms |
19668 KB |
Output is correct |
7 |
Correct |
109 ms |
26112 KB |
Output is correct |
8 |
Correct |
38 ms |
15820 KB |
Output is correct |
9 |
Correct |
46 ms |
18884 KB |
Output is correct |
10 |
Correct |
53 ms |
25032 KB |
Output is correct |
11 |
Correct |
161 ms |
29636 KB |
Output is correct |
12 |
Correct |
172 ms |
29484 KB |
Output is correct |
13 |
Correct |
170 ms |
28868 KB |
Output is correct |
14 |
Correct |
174 ms |
29000 KB |
Output is correct |
15 |
Correct |
158 ms |
27228 KB |
Output is correct |
16 |
Correct |
156 ms |
26432 KB |
Output is correct |
17 |
Correct |
158 ms |
26428 KB |
Output is correct |
18 |
Correct |
172 ms |
29136 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4696 KB |
Output is correct |
27 |
Correct |
2 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4696 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
1 ms |
4744 KB |
Output is correct |
34 |
Correct |
1 ms |
4700 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
2 ms |
4532 KB |
Output is correct |
37 |
Correct |
1 ms |
4700 KB |
Output is correct |
38 |
Correct |
6 ms |
5468 KB |
Output is correct |
39 |
Correct |
6 ms |
5724 KB |
Output is correct |
40 |
Correct |
6 ms |
5464 KB |
Output is correct |
41 |
Correct |
5 ms |
5520 KB |
Output is correct |
42 |
Correct |
7 ms |
5724 KB |
Output is correct |
43 |
Correct |
7 ms |
5652 KB |
Output is correct |
44 |
Correct |
6 ms |
5468 KB |
Output is correct |
45 |
Correct |
5 ms |
5468 KB |
Output is correct |
46 |
Correct |
7 ms |
5768 KB |
Output is correct |
47 |
Correct |
7 ms |
5468 KB |
Output is correct |
48 |
Correct |
4 ms |
5524 KB |
Output is correct |
49 |
Correct |
4 ms |
5468 KB |
Output is correct |
50 |
Correct |
7 ms |
5888 KB |
Output is correct |
51 |
Correct |
6 ms |
5728 KB |
Output is correct |
52 |
Correct |
6 ms |
5656 KB |
Output is correct |
53 |
Correct |
6 ms |
5724 KB |
Output is correct |
54 |
Correct |
6 ms |
5720 KB |
Output is correct |
55 |
Correct |
6 ms |
5468 KB |
Output is correct |
56 |
Correct |
174 ms |
27488 KB |
Output is correct |
57 |
Correct |
136 ms |
26924 KB |
Output is correct |
58 |
Correct |
139 ms |
26824 KB |
Output is correct |
59 |
Correct |
151 ms |
27600 KB |
Output is correct |
60 |
Correct |
126 ms |
25260 KB |
Output is correct |
61 |
Correct |
137 ms |
26792 KB |
Output is correct |
62 |
Correct |
168 ms |
27532 KB |
Output is correct |
63 |
Correct |
114 ms |
25300 KB |
Output is correct |
64 |
Correct |
106 ms |
25048 KB |
Output is correct |
65 |
Correct |
147 ms |
25808 KB |
Output is correct |
66 |
Correct |
115 ms |
25292 KB |
Output is correct |
67 |
Correct |
178 ms |
27928 KB |
Output is correct |
68 |
Correct |
156 ms |
27592 KB |
Output is correct |
69 |
Correct |
132 ms |
26944 KB |
Output is correct |
70 |
Correct |
185 ms |
28368 KB |
Output is correct |
71 |
Correct |
158 ms |
27016 KB |
Output is correct |
72 |
Correct |
130 ms |
27344 KB |
Output is correct |
73 |
Correct |
171 ms |
26064 KB |
Output is correct |
74 |
Correct |
76 ms |
27088 KB |
Output is correct |
75 |
Correct |
65 ms |
26352 KB |
Output is correct |
76 |
Correct |
175 ms |
30252 KB |
Output is correct |
77 |
Correct |
166 ms |
29512 KB |
Output is correct |
78 |
Correct |
176 ms |
28872 KB |
Output is correct |
79 |
Correct |
180 ms |
27592 KB |
Output is correct |
80 |
Correct |
161 ms |
26412 KB |
Output is correct |
81 |
Correct |
89 ms |
25876 KB |
Output is correct |
82 |
Correct |
160 ms |
26640 KB |
Output is correct |
83 |
Correct |
163 ms |
26828 KB |
Output is correct |
84 |
Correct |
188 ms |
26620 KB |
Output is correct |
85 |
Correct |
146 ms |
27592 KB |
Output is correct |
86 |
Correct |
141 ms |
27348 KB |
Output is correct |
87 |
Correct |
169 ms |
27352 KB |
Output is correct |
88 |
Correct |
160 ms |
29960 KB |
Output is correct |
89 |
Correct |
141 ms |
27596 KB |
Output is correct |
90 |
Correct |
178 ms |
29756 KB |
Output is correct |
91 |
Correct |
144 ms |
27048 KB |
Output is correct |
92 |
Correct |
146 ms |
28464 KB |
Output is correct |
93 |
Correct |
181 ms |
27152 KB |
Output is correct |
94 |
Correct |
166 ms |
27644 KB |
Output is correct |
95 |
Correct |
156 ms |
27732 KB |
Output is correct |
96 |
Correct |
175 ms |
29688 KB |
Output is correct |
97 |
Correct |
170 ms |
29900 KB |
Output is correct |
98 |
Correct |
175 ms |
30564 KB |
Output is correct |
99 |
Correct |
175 ms |
29640 KB |
Output is correct |
100 |
Correct |
145 ms |
27084 KB |
Output is correct |
101 |
Correct |
178 ms |
30052 KB |
Output is correct |
102 |
Correct |
161 ms |
30408 KB |
Output is correct |
103 |
Correct |
129 ms |
27936 KB |
Output is correct |