#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
ll T[100005];
ll L[100005];
ll R[100005];
ll C[100005];
vector<array<ll, 2>> adj[800005];
ll dis[200005];
bool vis[200005];
typedef pair<ll, ll> P;
struct SegTree {
struct Node {
vector<array<ll, 2>> S1, S2;
int p1, p2;
Node() : p1(0), p2(0) {}
};
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.push_back({L[a] + T[a], 2*a});
seg[n].S2.push_back({L[a] - T[a], 2*a});
}
void cons() {
for(int i=MAX/2;i<MAX;i++) {
sort(seg[i].S1.begin(), seg[i].S1.end());
sort(seg[i].S2.begin(), seg[i].S2.end());
}
for(int i = MAX/2-1;i>=1;i--) {
seg[i].S1.resize(seg[2*i].S1.size() + seg[2*i+1].S1.size());
merge(seg[2*i].S1.begin(),seg[2*i].S1.end(),seg[2*i+1].S1.begin(),seg[2*i+1].S1.end(),seg[i].S1.begin());
seg[i].S2.resize(seg[2*i].S2.size() + seg[2*i+1].S2.size());
merge(seg[2*i].S2.begin(),seg[2*i].S2.end(),seg[2*i+1].S2.begin(),seg[2*i+1].S2.end(),seg[i].S2.begin());
}
}
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].p2 < seg[n].S2.size()) {
auto m = seg[n].S2[seg[n].p2];
if(m[0]>R[k]-T[k]+1) break;
adj[2*k+1].push_back({m[1], 0});
seg[n].p2++;
}
}
else {
while(seg[n].p1 < seg[n].S1.size()) {
auto m = seg[n].S1[seg[n].p1];
if(m[0]>R[k]+T[k]+1) break;
adj[2*k+1].push_back({m[1], 0});
seg[n].p1++;
}
}
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<ll> 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);
}
tree.cons();
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(int, int, int, int, int, int, bool)':
treatment.cpp:48:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | while(seg[n].p2 < seg[n].S2.size()) {
treatment.cpp:56:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | while(seg[n].p1 < seg[n].S1.size()) {
treatment.cpp:65:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | int mid = ns + ne >> 1;
| ~~~^~~~
treatment.cpp: In function 'int main()':
treatment.cpp:80:12: warning: unused variable 'j' [-Wunused-variable]
80 | int i, j;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
41988 KB |
Output is correct |
2 |
Correct |
93 ms |
39224 KB |
Output is correct |
3 |
Correct |
137 ms |
43452 KB |
Output is correct |
4 |
Correct |
125 ms |
42852 KB |
Output is correct |
5 |
Correct |
109 ms |
42220 KB |
Output is correct |
6 |
Correct |
110 ms |
41476 KB |
Output is correct |
7 |
Correct |
119 ms |
42600 KB |
Output is correct |
8 |
Correct |
75 ms |
37480 KB |
Output is correct |
9 |
Correct |
72 ms |
37380 KB |
Output is correct |
10 |
Correct |
69 ms |
37252 KB |
Output is correct |
11 |
Correct |
131 ms |
45400 KB |
Output is correct |
12 |
Correct |
137 ms |
46012 KB |
Output is correct |
13 |
Correct |
127 ms |
43148 KB |
Output is correct |
14 |
Correct |
148 ms |
43192 KB |
Output is correct |
15 |
Correct |
141 ms |
41852 KB |
Output is correct |
16 |
Correct |
130 ms |
41988 KB |
Output is correct |
17 |
Correct |
122 ms |
41776 KB |
Output is correct |
18 |
Correct |
124 ms |
45336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19096 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
11 ms |
19156 KB |
Output is correct |
4 |
Correct |
10 ms |
19080 KB |
Output is correct |
5 |
Correct |
10 ms |
19028 KB |
Output is correct |
6 |
Correct |
10 ms |
19152 KB |
Output is correct |
7 |
Correct |
12 ms |
19112 KB |
Output is correct |
8 |
Correct |
10 ms |
19104 KB |
Output is correct |
9 |
Correct |
10 ms |
19072 KB |
Output is correct |
10 |
Correct |
10 ms |
19100 KB |
Output is correct |
11 |
Correct |
10 ms |
19028 KB |
Output is correct |
12 |
Correct |
10 ms |
19156 KB |
Output is correct |
13 |
Correct |
11 ms |
19156 KB |
Output is correct |
14 |
Correct |
10 ms |
19156 KB |
Output is correct |
15 |
Correct |
10 ms |
19028 KB |
Output is correct |
16 |
Correct |
10 ms |
19028 KB |
Output is correct |
17 |
Correct |
9 ms |
19028 KB |
Output is correct |
18 |
Correct |
10 ms |
19152 KB |
Output is correct |
19 |
Correct |
9 ms |
19028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19096 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
11 ms |
19156 KB |
Output is correct |
4 |
Correct |
10 ms |
19080 KB |
Output is correct |
5 |
Correct |
10 ms |
19028 KB |
Output is correct |
6 |
Correct |
10 ms |
19152 KB |
Output is correct |
7 |
Correct |
12 ms |
19112 KB |
Output is correct |
8 |
Correct |
10 ms |
19104 KB |
Output is correct |
9 |
Correct |
10 ms |
19072 KB |
Output is correct |
10 |
Correct |
10 ms |
19100 KB |
Output is correct |
11 |
Correct |
10 ms |
19028 KB |
Output is correct |
12 |
Correct |
10 ms |
19156 KB |
Output is correct |
13 |
Correct |
11 ms |
19156 KB |
Output is correct |
14 |
Correct |
10 ms |
19156 KB |
Output is correct |
15 |
Correct |
10 ms |
19028 KB |
Output is correct |
16 |
Correct |
10 ms |
19028 KB |
Output is correct |
17 |
Correct |
9 ms |
19028 KB |
Output is correct |
18 |
Correct |
10 ms |
19152 KB |
Output is correct |
19 |
Correct |
9 ms |
19028 KB |
Output is correct |
20 |
Correct |
19 ms |
23876 KB |
Output is correct |
21 |
Correct |
20 ms |
23888 KB |
Output is correct |
22 |
Correct |
23 ms |
21996 KB |
Output is correct |
23 |
Correct |
20 ms |
21592 KB |
Output is correct |
24 |
Correct |
24 ms |
24276 KB |
Output is correct |
25 |
Correct |
23 ms |
24268 KB |
Output is correct |
26 |
Correct |
22 ms |
24220 KB |
Output is correct |
27 |
Correct |
23 ms |
24132 KB |
Output is correct |
28 |
Correct |
22 ms |
24420 KB |
Output is correct |
29 |
Correct |
21 ms |
24044 KB |
Output is correct |
30 |
Correct |
15 ms |
22996 KB |
Output is correct |
31 |
Correct |
15 ms |
22976 KB |
Output is correct |
32 |
Correct |
22 ms |
24484 KB |
Output is correct |
33 |
Correct |
22 ms |
24476 KB |
Output is correct |
34 |
Correct |
24 ms |
23568 KB |
Output is correct |
35 |
Correct |
26 ms |
24396 KB |
Output is correct |
36 |
Correct |
23 ms |
24528 KB |
Output is correct |
37 |
Correct |
27 ms |
23676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
41988 KB |
Output is correct |
2 |
Correct |
93 ms |
39224 KB |
Output is correct |
3 |
Correct |
137 ms |
43452 KB |
Output is correct |
4 |
Correct |
125 ms |
42852 KB |
Output is correct |
5 |
Correct |
109 ms |
42220 KB |
Output is correct |
6 |
Correct |
110 ms |
41476 KB |
Output is correct |
7 |
Correct |
119 ms |
42600 KB |
Output is correct |
8 |
Correct |
75 ms |
37480 KB |
Output is correct |
9 |
Correct |
72 ms |
37380 KB |
Output is correct |
10 |
Correct |
69 ms |
37252 KB |
Output is correct |
11 |
Correct |
131 ms |
45400 KB |
Output is correct |
12 |
Correct |
137 ms |
46012 KB |
Output is correct |
13 |
Correct |
127 ms |
43148 KB |
Output is correct |
14 |
Correct |
148 ms |
43192 KB |
Output is correct |
15 |
Correct |
141 ms |
41852 KB |
Output is correct |
16 |
Correct |
130 ms |
41988 KB |
Output is correct |
17 |
Correct |
122 ms |
41776 KB |
Output is correct |
18 |
Correct |
124 ms |
45336 KB |
Output is correct |
19 |
Correct |
9 ms |
19096 KB |
Output is correct |
20 |
Correct |
10 ms |
19028 KB |
Output is correct |
21 |
Correct |
11 ms |
19156 KB |
Output is correct |
22 |
Correct |
10 ms |
19080 KB |
Output is correct |
23 |
Correct |
10 ms |
19028 KB |
Output is correct |
24 |
Correct |
10 ms |
19152 KB |
Output is correct |
25 |
Correct |
12 ms |
19112 KB |
Output is correct |
26 |
Correct |
10 ms |
19104 KB |
Output is correct |
27 |
Correct |
10 ms |
19072 KB |
Output is correct |
28 |
Correct |
10 ms |
19100 KB |
Output is correct |
29 |
Correct |
10 ms |
19028 KB |
Output is correct |
30 |
Correct |
10 ms |
19156 KB |
Output is correct |
31 |
Correct |
11 ms |
19156 KB |
Output is correct |
32 |
Correct |
10 ms |
19156 KB |
Output is correct |
33 |
Correct |
10 ms |
19028 KB |
Output is correct |
34 |
Correct |
10 ms |
19028 KB |
Output is correct |
35 |
Correct |
9 ms |
19028 KB |
Output is correct |
36 |
Correct |
10 ms |
19152 KB |
Output is correct |
37 |
Correct |
9 ms |
19028 KB |
Output is correct |
38 |
Correct |
19 ms |
23876 KB |
Output is correct |
39 |
Correct |
20 ms |
23888 KB |
Output is correct |
40 |
Correct |
23 ms |
21996 KB |
Output is correct |
41 |
Correct |
20 ms |
21592 KB |
Output is correct |
42 |
Correct |
24 ms |
24276 KB |
Output is correct |
43 |
Correct |
23 ms |
24268 KB |
Output is correct |
44 |
Correct |
22 ms |
24220 KB |
Output is correct |
45 |
Correct |
23 ms |
24132 KB |
Output is correct |
46 |
Correct |
22 ms |
24420 KB |
Output is correct |
47 |
Correct |
21 ms |
24044 KB |
Output is correct |
48 |
Correct |
15 ms |
22996 KB |
Output is correct |
49 |
Correct |
15 ms |
22976 KB |
Output is correct |
50 |
Correct |
22 ms |
24484 KB |
Output is correct |
51 |
Correct |
22 ms |
24476 KB |
Output is correct |
52 |
Correct |
24 ms |
23568 KB |
Output is correct |
53 |
Correct |
26 ms |
24396 KB |
Output is correct |
54 |
Correct |
23 ms |
24528 KB |
Output is correct |
55 |
Correct |
27 ms |
23676 KB |
Output is correct |
56 |
Correct |
341 ms |
133292 KB |
Output is correct |
57 |
Correct |
336 ms |
133412 KB |
Output is correct |
58 |
Correct |
314 ms |
126184 KB |
Output is correct |
59 |
Correct |
315 ms |
126128 KB |
Output is correct |
60 |
Correct |
271 ms |
91412 KB |
Output is correct |
61 |
Correct |
340 ms |
126108 KB |
Output is correct |
62 |
Correct |
372 ms |
133284 KB |
Output is correct |
63 |
Correct |
269 ms |
113376 KB |
Output is correct |
64 |
Correct |
262 ms |
113256 KB |
Output is correct |
65 |
Correct |
132 ms |
48344 KB |
Output is correct |
66 |
Correct |
214 ms |
86144 KB |
Output is correct |
67 |
Correct |
426 ms |
141160 KB |
Output is correct |
68 |
Correct |
389 ms |
140804 KB |
Output is correct |
69 |
Correct |
337 ms |
137920 KB |
Output is correct |
70 |
Correct |
442 ms |
141460 KB |
Output is correct |
71 |
Correct |
399 ms |
141012 KB |
Output is correct |
72 |
Correct |
336 ms |
138956 KB |
Output is correct |
73 |
Correct |
453 ms |
141536 KB |
Output is correct |
74 |
Correct |
131 ms |
104928 KB |
Output is correct |
75 |
Correct |
143 ms |
104688 KB |
Output is correct |
76 |
Correct |
372 ms |
127752 KB |
Output is correct |
77 |
Correct |
446 ms |
143024 KB |
Output is correct |
78 |
Correct |
427 ms |
127492 KB |
Output is correct |
79 |
Correct |
480 ms |
141652 KB |
Output is correct |
80 |
Correct |
461 ms |
131256 KB |
Output is correct |
81 |
Correct |
159 ms |
108696 KB |
Output is correct |
82 |
Correct |
431 ms |
131004 KB |
Output is correct |
83 |
Correct |
477 ms |
146120 KB |
Output is correct |
84 |
Correct |
476 ms |
144508 KB |
Output is correct |
85 |
Correct |
354 ms |
130484 KB |
Output is correct |
86 |
Correct |
329 ms |
130456 KB |
Output is correct |
87 |
Correct |
367 ms |
134088 KB |
Output is correct |
88 |
Correct |
401 ms |
142472 KB |
Output is correct |
89 |
Correct |
339 ms |
134524 KB |
Output is correct |
90 |
Correct |
456 ms |
143708 KB |
Output is correct |
91 |
Correct |
385 ms |
136912 KB |
Output is correct |
92 |
Correct |
342 ms |
130284 KB |
Output is correct |
93 |
Correct |
487 ms |
144880 KB |
Output is correct |
94 |
Correct |
517 ms |
145084 KB |
Output is correct |
95 |
Correct |
453 ms |
142060 KB |
Output is correct |
96 |
Correct |
464 ms |
143764 KB |
Output is correct |
97 |
Correct |
429 ms |
145032 KB |
Output is correct |
98 |
Correct |
438 ms |
145724 KB |
Output is correct |
99 |
Correct |
466 ms |
145296 KB |
Output is correct |
100 |
Correct |
426 ms |
145832 KB |
Output is correct |
101 |
Correct |
427 ms |
145848 KB |
Output is correct |
102 |
Correct |
414 ms |
152144 KB |
Output is correct |
103 |
Correct |
302 ms |
125460 KB |
Output is correct |