//Sylwia Sapkowska
#include <bits/stdc++.h>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
const int oo = 1e18, K = 30;
struct segtree{
vector<pair<int, int>>tab;
int size = 1;
segtree(int n, vector<int>&a){
while (size < n) size*=2;
tab.assign(2*size, {oo, oo});
for (int i = 0; i<n; i++) tab[i+size] = {a[i], i};
for (int i = size-1; i>=1; i--) tab[i] = min(tab[2*i], tab[2*i+1]);
}
pair<int, int>query(int l, int r){
l = l+size-1;
r = r+size+1;
pair<int, int> ans = {oo, oo};
while (r-l>1){
if (!(l&1)) ans = min(ans, tab[l+1]);
if (r&1) ans = min(ans, tab[r-1]);
l/=2;r/=2;
}
return ans;
};
void update(int x, int v){
x += size;
tab[x] = {v, v};
while (x){
x/=2;
tab[x] = min(tab[2*x], tab[2*x+1]);
}
}
};
struct project{
int t, l, r, c;
pair<int, int>L, R;
void read(){
cin >> t >> l >> r >> c;r++;
L = {l-t, l+t};
R = {r-t, r+t};
// debug(L, R);
}
void d(){
cerr << t << " " << l << " " << r << " " << c << "\n";
debug(L, R);
cerr << "\n";
}
};
void solve(){
int n, m; cin >> n >> m;
vector<project>a(m);
for (int i = 0; i<m; i++) a[i].read();
sort(a.begin(), a.end(), [](auto x, auto y){return x.L.first < y.L.first;});
// for (int i = 0; i<m; i++){
// debug(a[i].L, a[i].R);
// }
vector<int>dist(m, oo);
vector<int>b(m);
for (int i = 0; i<m; i++) b[i] = a[i].L.second;
segtree seg(m+1, b);
set<pair<int, int>>s;
for (int i = 0; i<m; i++){
if (a[i].l == 1){
dist[i] = a[i].c;
s.insert({a[i].c, i});
seg.update(i, oo);
}
}
while (s.size()){
int v = s.begin()->second;
int d = s.begin()->first;
s.erase(s.begin());
int l = 0, r = m-1, idx = -1;
while (r>=l){
int mid = (l+r)/2;
if (a[v].R.first >= a[mid].L.first){
idx = mid;
l = mid+1;
} else {
r = mid-1;
}
}
// debug(v, dist[v], idx);
if (idx == -1) continue;
while (1){
pair<int, int>curr = seg.query(0, idx);
// debug(curr);
if (curr.first <= a[v].R.second){
dist[curr.second] = d + a[curr.second].c;
s.insert({dist[curr.second], curr.second});
seg.update(curr.second, oo);
} else break;
}
// for (int x = 0; x<m; x++){
// if (a[x].L.first <= a[v].R.first && a[x].L.second <= a[v].R.second){
// if (dist[x] > d + a[x].c){
// assert(dist[x] == oo);
// dist[x] = d+a[x].c;
// s.insert({dist[x], x});
// }
// }
// }
}
// debug(dist);
int ans = oo;
for (int i = 0; i<m; i++){
// debug(a[i].r, dist[i]);
if (a[i].r == n+1){
// debug(i, dist[i]);
ans = min(ans, dist[i]);
}
}
if (ans == oo) cout << "-1\n";
else cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
12252 KB |
Output is correct |
2 |
Correct |
65 ms |
15436 KB |
Output is correct |
3 |
Correct |
128 ms |
15960 KB |
Output is correct |
4 |
Correct |
120 ms |
15960 KB |
Output is correct |
5 |
Correct |
152 ms |
21308 KB |
Output is correct |
6 |
Correct |
123 ms |
15772 KB |
Output is correct |
7 |
Correct |
125 ms |
15368 KB |
Output is correct |
8 |
Correct |
50 ms |
15400 KB |
Output is correct |
9 |
Correct |
54 ms |
15412 KB |
Output is correct |
10 |
Correct |
58 ms |
15268 KB |
Output is correct |
11 |
Correct |
133 ms |
21680 KB |
Output is correct |
12 |
Correct |
150 ms |
21676 KB |
Output is correct |
13 |
Correct |
204 ms |
19860 KB |
Output is correct |
14 |
Correct |
175 ms |
19756 KB |
Output is correct |
15 |
Correct |
108 ms |
15436 KB |
Output is correct |
16 |
Correct |
144 ms |
15452 KB |
Output is correct |
17 |
Correct |
119 ms |
14632 KB |
Output is correct |
18 |
Correct |
156 ms |
20900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
280 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
280 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
5 ms |
1108 KB |
Output is correct |
21 |
Correct |
6 ms |
1108 KB |
Output is correct |
22 |
Correct |
7 ms |
980 KB |
Output is correct |
23 |
Correct |
6 ms |
1024 KB |
Output is correct |
24 |
Correct |
8 ms |
1160 KB |
Output is correct |
25 |
Correct |
7 ms |
980 KB |
Output is correct |
26 |
Correct |
5 ms |
980 KB |
Output is correct |
27 |
Correct |
5 ms |
904 KB |
Output is correct |
28 |
Correct |
8 ms |
1108 KB |
Output is correct |
29 |
Correct |
7 ms |
980 KB |
Output is correct |
30 |
Correct |
4 ms |
900 KB |
Output is correct |
31 |
Correct |
3 ms |
980 KB |
Output is correct |
32 |
Correct |
6 ms |
1236 KB |
Output is correct |
33 |
Correct |
6 ms |
1236 KB |
Output is correct |
34 |
Correct |
7 ms |
904 KB |
Output is correct |
35 |
Correct |
8 ms |
1192 KB |
Output is correct |
36 |
Correct |
7 ms |
1288 KB |
Output is correct |
37 |
Correct |
6 ms |
1000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
12252 KB |
Output is correct |
2 |
Correct |
65 ms |
15436 KB |
Output is correct |
3 |
Correct |
128 ms |
15960 KB |
Output is correct |
4 |
Correct |
120 ms |
15960 KB |
Output is correct |
5 |
Correct |
152 ms |
21308 KB |
Output is correct |
6 |
Correct |
123 ms |
15772 KB |
Output is correct |
7 |
Correct |
125 ms |
15368 KB |
Output is correct |
8 |
Correct |
50 ms |
15400 KB |
Output is correct |
9 |
Correct |
54 ms |
15412 KB |
Output is correct |
10 |
Correct |
58 ms |
15268 KB |
Output is correct |
11 |
Correct |
133 ms |
21680 KB |
Output is correct |
12 |
Correct |
150 ms |
21676 KB |
Output is correct |
13 |
Correct |
204 ms |
19860 KB |
Output is correct |
14 |
Correct |
175 ms |
19756 KB |
Output is correct |
15 |
Correct |
108 ms |
15436 KB |
Output is correct |
16 |
Correct |
144 ms |
15452 KB |
Output is correct |
17 |
Correct |
119 ms |
14632 KB |
Output is correct |
18 |
Correct |
156 ms |
20900 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
280 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
5 ms |
1108 KB |
Output is correct |
39 |
Correct |
6 ms |
1108 KB |
Output is correct |
40 |
Correct |
7 ms |
980 KB |
Output is correct |
41 |
Correct |
6 ms |
1024 KB |
Output is correct |
42 |
Correct |
8 ms |
1160 KB |
Output is correct |
43 |
Correct |
7 ms |
980 KB |
Output is correct |
44 |
Correct |
5 ms |
980 KB |
Output is correct |
45 |
Correct |
5 ms |
904 KB |
Output is correct |
46 |
Correct |
8 ms |
1108 KB |
Output is correct |
47 |
Correct |
7 ms |
980 KB |
Output is correct |
48 |
Correct |
4 ms |
900 KB |
Output is correct |
49 |
Correct |
3 ms |
980 KB |
Output is correct |
50 |
Correct |
6 ms |
1236 KB |
Output is correct |
51 |
Correct |
6 ms |
1236 KB |
Output is correct |
52 |
Correct |
7 ms |
904 KB |
Output is correct |
53 |
Correct |
8 ms |
1192 KB |
Output is correct |
54 |
Correct |
7 ms |
1288 KB |
Output is correct |
55 |
Correct |
6 ms |
1000 KB |
Output is correct |
56 |
Correct |
105 ms |
17880 KB |
Output is correct |
57 |
Correct |
121 ms |
16736 KB |
Output is correct |
58 |
Correct |
101 ms |
14608 KB |
Output is correct |
59 |
Correct |
127 ms |
14884 KB |
Output is correct |
60 |
Correct |
132 ms |
15660 KB |
Output is correct |
61 |
Correct |
87 ms |
14508 KB |
Output is correct |
62 |
Correct |
94 ms |
17888 KB |
Output is correct |
63 |
Correct |
98 ms |
15472 KB |
Output is correct |
64 |
Correct |
101 ms |
15464 KB |
Output is correct |
65 |
Correct |
98 ms |
15536 KB |
Output is correct |
66 |
Correct |
112 ms |
15668 KB |
Output is correct |
67 |
Correct |
174 ms |
16348 KB |
Output is correct |
68 |
Correct |
181 ms |
16148 KB |
Output is correct |
69 |
Correct |
139 ms |
16152 KB |
Output is correct |
70 |
Correct |
172 ms |
16484 KB |
Output is correct |
71 |
Correct |
141 ms |
16088 KB |
Output is correct |
72 |
Correct |
132 ms |
16144 KB |
Output is correct |
73 |
Correct |
168 ms |
16416 KB |
Output is correct |
74 |
Correct |
54 ms |
16080 KB |
Output is correct |
75 |
Correct |
55 ms |
16084 KB |
Output is correct |
76 |
Correct |
187 ms |
22016 KB |
Output is correct |
77 |
Correct |
223 ms |
22412 KB |
Output is correct |
78 |
Correct |
206 ms |
20020 KB |
Output is correct |
79 |
Correct |
162 ms |
16172 KB |
Output is correct |
80 |
Correct |
137 ms |
15820 KB |
Output is correct |
81 |
Correct |
94 ms |
16040 KB |
Output is correct |
82 |
Correct |
121 ms |
15296 KB |
Output is correct |
83 |
Correct |
119 ms |
15444 KB |
Output is correct |
84 |
Correct |
155 ms |
15560 KB |
Output is correct |
85 |
Correct |
121 ms |
16168 KB |
Output is correct |
86 |
Correct |
88 ms |
16040 KB |
Output is correct |
87 |
Correct |
106 ms |
16136 KB |
Output is correct |
88 |
Correct |
97 ms |
16096 KB |
Output is correct |
89 |
Correct |
117 ms |
16048 KB |
Output is correct |
90 |
Correct |
216 ms |
22168 KB |
Output is correct |
91 |
Correct |
140 ms |
19240 KB |
Output is correct |
92 |
Correct |
94 ms |
16048 KB |
Output is correct |
93 |
Correct |
148 ms |
16164 KB |
Output is correct |
94 |
Correct |
119 ms |
16476 KB |
Output is correct |
95 |
Correct |
113 ms |
16112 KB |
Output is correct |
96 |
Correct |
161 ms |
22188 KB |
Output is correct |
97 |
Correct |
174 ms |
22316 KB |
Output is correct |
98 |
Correct |
163 ms |
22400 KB |
Output is correct |
99 |
Correct |
178 ms |
22396 KB |
Output is correct |
100 |
Correct |
156 ms |
22192 KB |
Output is correct |
101 |
Correct |
157 ms |
22436 KB |
Output is correct |
102 |
Correct |
155 ms |
22360 KB |
Output is correct |
103 |
Correct |
97 ms |
16980 KB |
Output is correct |