#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const ll INF = 1e18;
int T[100005];
int L[100005];
int R[100005];
ll C[100005];
vector<array<ll, 2>> adj[800005];
ll dis[200005];
bool vis[200005];
typedef pair<int, int> P;
struct SegTree {
struct Node {
set<array<int, 2>> S1, S2;
};
vector<Node> seg;
int MAX;
SegTree(int N) {
int i;
for(i=1;i<2*N;i*=2) {}
seg.resize(i);
MAX = i;
}
void Edit(int n, int a) {
n += MAX/2;
seg[n].S1.insert({L[a] + T[a], 2*a});
seg[n].S2.insert({L[a] - T[a], 2*a});
n /= 2;
while(n) {
seg[n].S1.insert({L[a] + T[a], 2*a});
seg[n].S2.insert({L[a] - T[a], 2*a});
n /= 2;
}
}
void make_edge(int s, int e, int k, int n, int ns, int ne, bool type) {
if(e<=ns||ne<=s) return;
if(s<=ns&&ne<=e) {
if(type) {
while(!seg[n].S2.empty()) {
auto m = *seg[n].S2.begin();
if(m[0] > R[k] - T[k] + 1) break;
adj[2*k+1].push_back({m[1], 0});
seg[n].S2.erase(m);
}
}
else {
while(!seg[n].S1.empty()) {
auto m = *seg[n].S1.begin();
if(m[0] > R[k] + T[k] + 1) break;
adj[2*k+1].push_back({m[1], 0});
seg[n].S1.erase(m);
}
}
return;
}
int mid = ns + ne >> 1;
make_edge(s, e, k, 2*n, ns, mid, type);
make_edge(s, e, k, 2*n+1, mid, ne, type);
}
};
vector<int> idt;
int it(int n) {
return lower_bound(idt.begin(),idt.end(),n) - idt.begin();
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, M;
cin >> N >> M;
int i, j;
for(i=1;i<=M;i++) {
cin >> T[i] >> L[i] >> R[i] >> C[i];
idt.push_back(T[i]);
}
sort(idt.begin(),idt.end());
idt.erase(unique(idt.begin(),idt.end()),idt.end());
//0 is left, 1 is right
//2i is input, 2i+1 is output
for(i=1;i<=M;i++) {
adj[2*i].push_back({2*i+1, C[i]});
if(L[i]==1) adj[0].push_back({2*i, 0});
if(R[i]==N) adj[2*i+1].push_back({1, 0});
}
/*for(i=1;i<=M;i++) {
for(j=1;j<=M;j++) {
if(i==j) continue;
if(T[i] >= T[j]) {
int t = T[i] - T[j];
if(R[j] == N || L[i] <= R[j] - t + 1) {
adj[2*j+1].push_back({2*i, 0});
}
}
if(T[i] <= T[j]) {
int t = T[j] - T[i];
if(L[i] == 1 || L[i] - 1 + t <= R[j]) {
adj[2*j+1].push_back({2*i, 0});
}
}
}
}*/
/*for(i=0;i<=2*M+1;i++) {
cout << i << " : ";
for(auto n2 : adj[i]) {
cout << "(" << n2[0] << ", " << n2[1] << ") ";
}
cout << '\n';
}*/
SegTree tree(idt.size() + 3);
int MAX = tree.MAX;
for(i=1;i<=M;i++) {
tree.Edit(it(T[i]), i);
}
for(i=0;i<=2*M+1;i++) dis[i] = INF;
dis[0] = 0;
priority_queue<P, vector<P>, greater<P>> PQ;
PQ.push(P(0, 0));
while(!PQ.empty()) {
auto k = PQ.top();
PQ.pop();
int id = k.second;
if(vis[id]) continue;
vis[id] = true;
if(id != 0 && id != 1 && id % 2 == 1) {
int x = id / 2;
tree.make_edge(0, it(T[x])+1, x, 1, 0, MAX/2, true);
tree.make_edge(it(T[x]), idt.size(), x, 1, 0, MAX/2, false);
}
for(auto n : adj[id]) {
if(dis[n[0]] > dis[id] + n[1]) {
dis[n[0]] = dis[id] + n[1];
PQ.push(P(dis[n[0]], n[0]));
}
}
}
cout << (dis[1]==INF?-1:dis[1]);
}
Compilation message
treatment.cpp: In member function 'void SegTree::make_edge(long long int, long long int, long long int, long long int, long long int, long long int, bool)':
treatment.cpp:58:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | int mid = ns + ne >> 1;
| ~~~^~~~
treatment.cpp: In function 'int main()':
treatment.cpp:73:12: warning: unused variable 'j' [-Wunused-variable]
73 | int i, j;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
382 ms |
65536 KB |
Output is correct |
2 |
Correct |
328 ms |
68684 KB |
Output is correct |
3 |
Correct |
402 ms |
72464 KB |
Output is correct |
4 |
Correct |
406 ms |
71832 KB |
Output is correct |
5 |
Correct |
313 ms |
73516 KB |
Output is correct |
6 |
Correct |
306 ms |
72712 KB |
Output is correct |
7 |
Correct |
245 ms |
73652 KB |
Output is correct |
8 |
Correct |
290 ms |
68616 KB |
Output is correct |
9 |
Correct |
203 ms |
68488 KB |
Output is correct |
10 |
Correct |
189 ms |
68420 KB |
Output is correct |
11 |
Correct |
401 ms |
76772 KB |
Output is correct |
12 |
Correct |
447 ms |
75324 KB |
Output is correct |
13 |
Correct |
390 ms |
74300 KB |
Output is correct |
14 |
Correct |
411 ms |
74444 KB |
Output is correct |
15 |
Correct |
371 ms |
73156 KB |
Output is correct |
16 |
Correct |
387 ms |
73248 KB |
Output is correct |
17 |
Correct |
368 ms |
72280 KB |
Output is correct |
18 |
Correct |
411 ms |
76064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
9 ms |
19156 KB |
Output is correct |
3 |
Correct |
10 ms |
19156 KB |
Output is correct |
4 |
Correct |
10 ms |
19072 KB |
Output is correct |
5 |
Correct |
10 ms |
19104 KB |
Output is correct |
6 |
Correct |
10 ms |
19160 KB |
Output is correct |
7 |
Correct |
11 ms |
19156 KB |
Output is correct |
8 |
Correct |
11 ms |
19156 KB |
Output is correct |
9 |
Correct |
10 ms |
19156 KB |
Output is correct |
10 |
Correct |
9 ms |
19156 KB |
Output is correct |
11 |
Correct |
10 ms |
19120 KB |
Output is correct |
12 |
Correct |
10 ms |
19092 KB |
Output is correct |
13 |
Correct |
9 ms |
19156 KB |
Output is correct |
14 |
Correct |
9 ms |
19284 KB |
Output is correct |
15 |
Correct |
10 ms |
19156 KB |
Output is correct |
16 |
Correct |
10 ms |
19156 KB |
Output is correct |
17 |
Correct |
10 ms |
19156 KB |
Output is correct |
18 |
Correct |
10 ms |
19156 KB |
Output is correct |
19 |
Correct |
9 ms |
19028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
9 ms |
19156 KB |
Output is correct |
3 |
Correct |
10 ms |
19156 KB |
Output is correct |
4 |
Correct |
10 ms |
19072 KB |
Output is correct |
5 |
Correct |
10 ms |
19104 KB |
Output is correct |
6 |
Correct |
10 ms |
19160 KB |
Output is correct |
7 |
Correct |
11 ms |
19156 KB |
Output is correct |
8 |
Correct |
11 ms |
19156 KB |
Output is correct |
9 |
Correct |
10 ms |
19156 KB |
Output is correct |
10 |
Correct |
9 ms |
19156 KB |
Output is correct |
11 |
Correct |
10 ms |
19120 KB |
Output is correct |
12 |
Correct |
10 ms |
19092 KB |
Output is correct |
13 |
Correct |
9 ms |
19156 KB |
Output is correct |
14 |
Correct |
9 ms |
19284 KB |
Output is correct |
15 |
Correct |
10 ms |
19156 KB |
Output is correct |
16 |
Correct |
10 ms |
19156 KB |
Output is correct |
17 |
Correct |
10 ms |
19156 KB |
Output is correct |
18 |
Correct |
10 ms |
19156 KB |
Output is correct |
19 |
Correct |
9 ms |
19028 KB |
Output is correct |
20 |
Correct |
52 ms |
30012 KB |
Output is correct |
21 |
Correct |
61 ms |
29932 KB |
Output is correct |
22 |
Correct |
48 ms |
26520 KB |
Output is correct |
23 |
Correct |
40 ms |
26060 KB |
Output is correct |
24 |
Correct |
64 ms |
31052 KB |
Output is correct |
25 |
Correct |
63 ms |
30944 KB |
Output is correct |
26 |
Correct |
52 ms |
31068 KB |
Output is correct |
27 |
Correct |
50 ms |
30920 KB |
Output is correct |
28 |
Correct |
61 ms |
31068 KB |
Output is correct |
29 |
Correct |
55 ms |
30792 KB |
Output is correct |
30 |
Correct |
44 ms |
29908 KB |
Output is correct |
31 |
Correct |
43 ms |
29872 KB |
Output is correct |
32 |
Correct |
66 ms |
31248 KB |
Output is correct |
33 |
Correct |
59 ms |
31196 KB |
Output is correct |
34 |
Correct |
60 ms |
29584 KB |
Output is correct |
35 |
Correct |
58 ms |
31140 KB |
Output is correct |
36 |
Correct |
71 ms |
31296 KB |
Output is correct |
37 |
Correct |
54 ms |
29508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
382 ms |
65536 KB |
Output is correct |
2 |
Correct |
328 ms |
68684 KB |
Output is correct |
3 |
Correct |
402 ms |
72464 KB |
Output is correct |
4 |
Correct |
406 ms |
71832 KB |
Output is correct |
5 |
Correct |
313 ms |
73516 KB |
Output is correct |
6 |
Correct |
306 ms |
72712 KB |
Output is correct |
7 |
Correct |
245 ms |
73652 KB |
Output is correct |
8 |
Correct |
290 ms |
68616 KB |
Output is correct |
9 |
Correct |
203 ms |
68488 KB |
Output is correct |
10 |
Correct |
189 ms |
68420 KB |
Output is correct |
11 |
Correct |
401 ms |
76772 KB |
Output is correct |
12 |
Correct |
447 ms |
75324 KB |
Output is correct |
13 |
Correct |
390 ms |
74300 KB |
Output is correct |
14 |
Correct |
411 ms |
74444 KB |
Output is correct |
15 |
Correct |
371 ms |
73156 KB |
Output is correct |
16 |
Correct |
387 ms |
73248 KB |
Output is correct |
17 |
Correct |
368 ms |
72280 KB |
Output is correct |
18 |
Correct |
411 ms |
76064 KB |
Output is correct |
19 |
Correct |
9 ms |
19028 KB |
Output is correct |
20 |
Correct |
9 ms |
19156 KB |
Output is correct |
21 |
Correct |
10 ms |
19156 KB |
Output is correct |
22 |
Correct |
10 ms |
19072 KB |
Output is correct |
23 |
Correct |
10 ms |
19104 KB |
Output is correct |
24 |
Correct |
10 ms |
19160 KB |
Output is correct |
25 |
Correct |
11 ms |
19156 KB |
Output is correct |
26 |
Correct |
11 ms |
19156 KB |
Output is correct |
27 |
Correct |
10 ms |
19156 KB |
Output is correct |
28 |
Correct |
9 ms |
19156 KB |
Output is correct |
29 |
Correct |
10 ms |
19120 KB |
Output is correct |
30 |
Correct |
10 ms |
19092 KB |
Output is correct |
31 |
Correct |
9 ms |
19156 KB |
Output is correct |
32 |
Correct |
9 ms |
19284 KB |
Output is correct |
33 |
Correct |
10 ms |
19156 KB |
Output is correct |
34 |
Correct |
10 ms |
19156 KB |
Output is correct |
35 |
Correct |
10 ms |
19156 KB |
Output is correct |
36 |
Correct |
10 ms |
19156 KB |
Output is correct |
37 |
Correct |
9 ms |
19028 KB |
Output is correct |
38 |
Correct |
52 ms |
30012 KB |
Output is correct |
39 |
Correct |
61 ms |
29932 KB |
Output is correct |
40 |
Correct |
48 ms |
26520 KB |
Output is correct |
41 |
Correct |
40 ms |
26060 KB |
Output is correct |
42 |
Correct |
64 ms |
31052 KB |
Output is correct |
43 |
Correct |
63 ms |
30944 KB |
Output is correct |
44 |
Correct |
52 ms |
31068 KB |
Output is correct |
45 |
Correct |
50 ms |
30920 KB |
Output is correct |
46 |
Correct |
61 ms |
31068 KB |
Output is correct |
47 |
Correct |
55 ms |
30792 KB |
Output is correct |
48 |
Correct |
44 ms |
29908 KB |
Output is correct |
49 |
Correct |
43 ms |
29872 KB |
Output is correct |
50 |
Correct |
66 ms |
31248 KB |
Output is correct |
51 |
Correct |
59 ms |
31196 KB |
Output is correct |
52 |
Correct |
60 ms |
29584 KB |
Output is correct |
53 |
Correct |
58 ms |
31140 KB |
Output is correct |
54 |
Correct |
71 ms |
31296 KB |
Output is correct |
55 |
Correct |
54 ms |
29508 KB |
Output is correct |
56 |
Correct |
2347 ms |
294512 KB |
Output is correct |
57 |
Correct |
2382 ms |
294264 KB |
Output is correct |
58 |
Correct |
2028 ms |
287436 KB |
Output is correct |
59 |
Correct |
2020 ms |
288824 KB |
Output is correct |
60 |
Correct |
2195 ms |
229988 KB |
Output is correct |
61 |
Correct |
2093 ms |
287564 KB |
Output is correct |
62 |
Correct |
2346 ms |
294740 KB |
Output is correct |
63 |
Correct |
1485 ms |
283424 KB |
Output is correct |
64 |
Correct |
1510 ms |
283364 KB |
Output is correct |
65 |
Correct |
522 ms |
81328 KB |
Output is correct |
66 |
Correct |
2138 ms |
225940 KB |
Output is correct |
67 |
Correct |
2415 ms |
314196 KB |
Output is correct |
68 |
Correct |
1909 ms |
315796 KB |
Output is correct |
69 |
Correct |
1657 ms |
313988 KB |
Output is correct |
70 |
Correct |
2599 ms |
314644 KB |
Output is correct |
71 |
Correct |
2017 ms |
316168 KB |
Output is correct |
72 |
Correct |
1657 ms |
314548 KB |
Output is correct |
73 |
Correct |
2541 ms |
314720 KB |
Output is correct |
74 |
Correct |
1187 ms |
281872 KB |
Output is correct |
75 |
Correct |
941 ms |
281704 KB |
Output is correct |
76 |
Correct |
2518 ms |
288952 KB |
Output is correct |
77 |
Execution timed out |
3043 ms |
315928 KB |
Time limit exceeded |
78 |
Halted |
0 ms |
0 KB |
- |