#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
struct seg {
struct node {
int val=1000000000000000000;
int c1=-1;
int c2=-1;
};
vector<node> tree{(node){1000000000000000000, -1, -1}};
void update(int v, int rl, int rr, int pos, int x) {
if (rl == rr) {
tree[v].val = x;
return;
}
int rm = (rl+rr)/2;
if (pos <= rm) {
if (tree[v].c1 == -1) {
tree[v].c1=tree.size();
tree.push_back(node());
}
update(tree[v].c1, rl, rm, pos, x);
}
else {
if (tree[v].c2 == -1) {
tree[v].c2=tree.size();
tree.push_back(node());
}
update(tree[v].c2, rm+1, rr, pos, x);
}
int v1 = 1e18;
int v2 = 1e18;
if (tree[v].c1 != -1) v1 = tree[tree[v].c1].val;
if (tree[v].c2 != -1) v2 = tree[tree[v].c2].val;
tree[v].val = min(v1, v2);
}
int query(int v, int rl, int rr, int ql, int qr) {
if (ql > qr) return 1e18;
if (ql == rl && qr == rr) {return tree[v].val;}
int rm = (rl + rr) / 2;
int v1 = 1e18;
int v2 = 1e18;
if (tree[v].c1 != -1) v1 = query(tree[v].c1, rl, rm, ql, min(qr, rm));
if (tree[v].c2 != -1) v2 = query(tree[v].c2, rm+1, rr, max(ql, rm+1), qr);
return min(v1, v2);
}
};
long long min_distance(std::vector<signed> x, std::vector<signed> h, std::vector<signed> l, std::vector<signed> r, std::vector<signed> y, signed s, signed g) {
if (x.size() == 1) return 0;
deque<tuple<int, int, bool>> Oevents;
deque<tuple<int, bool, int>> events;
for (int i = 0; i<l.size(); i++) {
Oevents.push_back({l[i], y[i], 1});
Oevents.push_back({r[i], y[i], 0});
}
sort(Oevents.begin(), Oevents.end());
vector<bool> skip(Oevents.size());
for (int i = 0; i<Oevents.size()-1; i++) {
if (get<0>(Oevents[i]) == get<0>(Oevents[i+1]) && get<1>(Oevents[i]) == get<1>(Oevents[i+1])&& get<2>(Oevents[i]) != get<2>(Oevents[i+1])) {
skip[i] = 1;
skip[i+1] = 1;
}
}
for (int i = 0; i<Oevents.size(); i++) {
if (skip[i]) continue;
if (!get<2>(Oevents[i])) {
events.push_back({get<0>(Oevents[i])+1, get<2>(Oevents[i]), get<1>(Oevents[i])});
}
else events.push_back({get<0>(Oevents[i]), get<2>(Oevents[i]), get<1>(Oevents[i])});
}
sort(events.begin(), events.end());
seg tree1, tree2;
for (int i = 0; i<x.size(); i++) {
while(events.size() && get<0>(events[0]) == i) {
if (!get<1>(events[0])) {
tree1.update(0, 0, 1e9, get<2>(events[0]), 1e18);
tree2.update(0, 0, 1e9, get<2>(events[0]), 1e18);
// cerr << "r " << get<2>(events[0]) << "\n";
}
else {
int val = 1e18;
if (i == 0) val = get<2>(events[0]);
// cerr << get<2>(events[0]) << " ";
val = min(val, tree1.query(0, 0, 1e9, get<2>(events[0]), 1e9)-get<2>(events[0]));
val = min(val, tree2.query(0, 0, 1e9, 0, min(get<2>(events[0]), (int)1e9))+get<2>(events[0])-(int)1e9);
// cerr << val << "\n";
tree1.update(0, 0, 1e9, get<2>(events[0]), val+get<2>(events[0]));
tree2.update(0, 0, 1e9, get<2>(events[0]), val+1e9-get<2>(events[0]));
}
events.pop_front();
}
}
if (tree1.query(0, 0, 1e9, 0, 1e9) + x.back() - x[0] > 1e17) return -1;
return tree1.query(0, 0, 1e9, 0, 1e9) + x.back() - x[0];
}
Compilation message
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:72:19: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i<l.size(); i++) {
| ~^~~~~~~~~
walk.cpp:81:19: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::deque<std::tuple<long int, long int, bool> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int i = 0; i<Oevents.size()-1; i++) {
| ~^~~~~~~~~~~~~~~~~
walk.cpp:90:19: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::deque<std::tuple<long int, long int, bool> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (int i = 0; i<Oevents.size(); i++) {
| ~^~~~~~~~~~~~~~~
walk.cpp:105:19: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (int i = 0; i<x.size(); i++) {
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
3980 KB |
Output is correct |
2 |
Correct |
453 ms |
86340 KB |
Output is correct |
3 |
Correct |
466 ms |
86508 KB |
Output is correct |
4 |
Correct |
441 ms |
87908 KB |
Output is correct |
5 |
Correct |
470 ms |
87996 KB |
Output is correct |
6 |
Correct |
457 ms |
87980 KB |
Output is correct |
7 |
Correct |
192 ms |
45088 KB |
Output is correct |
8 |
Correct |
380 ms |
87952 KB |
Output is correct |
9 |
Correct |
404 ms |
87924 KB |
Output is correct |
10 |
Correct |
178 ms |
18732 KB |
Output is correct |
11 |
Correct |
8 ms |
1208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
3980 KB |
Output is correct |
2 |
Correct |
453 ms |
86340 KB |
Output is correct |
3 |
Correct |
466 ms |
86508 KB |
Output is correct |
4 |
Correct |
441 ms |
87908 KB |
Output is correct |
5 |
Correct |
470 ms |
87996 KB |
Output is correct |
6 |
Correct |
457 ms |
87980 KB |
Output is correct |
7 |
Correct |
192 ms |
45088 KB |
Output is correct |
8 |
Correct |
380 ms |
87952 KB |
Output is correct |
9 |
Correct |
404 ms |
87924 KB |
Output is correct |
10 |
Correct |
178 ms |
18732 KB |
Output is correct |
11 |
Correct |
8 ms |
1208 KB |
Output is correct |
12 |
Correct |
425 ms |
86580 KB |
Output is correct |
13 |
Correct |
436 ms |
87904 KB |
Output is correct |
14 |
Correct |
497 ms |
87904 KB |
Output is correct |
15 |
Correct |
275 ms |
50976 KB |
Output is correct |
16 |
Correct |
286 ms |
38444 KB |
Output is correct |
17 |
Correct |
252 ms |
38524 KB |
Output is correct |
18 |
Correct |
254 ms |
50944 KB |
Output is correct |
19 |
Correct |
246 ms |
38532 KB |
Output is correct |
20 |
Correct |
210 ms |
44944 KB |
Output is correct |
21 |
Correct |
19 ms |
3280 KB |
Output is correct |
22 |
Correct |
265 ms |
59656 KB |
Output is correct |
23 |
Correct |
273 ms |
61112 KB |
Output is correct |
24 |
Correct |
287 ms |
86656 KB |
Output is correct |
25 |
Correct |
283 ms |
86024 KB |
Output is correct |
26 |
Correct |
330 ms |
86820 KB |
Output is correct |
27 |
Correct |
428 ms |
87968 KB |
Output is correct |
28 |
Correct |
411 ms |
87980 KB |
Output is correct |
29 |
Correct |
408 ms |
87900 KB |
Output is correct |
30 |
Correct |
191 ms |
45048 KB |
Output is correct |
31 |
Correct |
346 ms |
88004 KB |
Output is correct |
32 |
Correct |
164 ms |
16244 KB |
Output is correct |
33 |
Correct |
177 ms |
16444 KB |
Output is correct |
34 |
Correct |
169 ms |
22132 KB |
Output is correct |
35 |
Correct |
203 ms |
35492 KB |
Output is correct |
36 |
Correct |
190 ms |
35120 KB |
Output is correct |
37 |
Correct |
156 ms |
16344 KB |
Output is correct |
38 |
Correct |
166 ms |
17108 KB |
Output is correct |
39 |
Correct |
182 ms |
26164 KB |
Output is correct |
40 |
Correct |
171 ms |
16812 KB |
Output is correct |
41 |
Correct |
153 ms |
15788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |