#include <bits/stdc++.h>
#define int long long
using namespace std;
struct Segtree
{
vector<int> seg;
int nbFeuilles;
const int UNDEF = 1e18;
Segtree(int nbElem)
{
nbFeuilles = 1;
while (nbFeuilles < nbElem) ++nbFeuilles;
seg.assign(2 * nbFeuilles, UNDEF);
}
void update(int pos, int val)
{
pos += nbFeuilles;
seg[pos] = min(seg[pos], val);
while (pos > 1)
{
pos /= 2;
seg[pos] = min(seg[2*pos], seg[2*pos+1]);
}
}
int query(int deb, int fin)
{
int ans = UNDEF;
deb += nbFeuilles, fin += nbFeuilles;
while (deb < fin)
{
if (deb % 2)
ans = min(ans, seg[deb]);
if (fin%2==0)
ans = min(ans, seg[fin]);
deb = (deb + 1) / 2;
fin = (fin - 1) / 2;
}
if (deb == fin)
ans = min(ans, seg[deb]);
return ans;
}
};
signed main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int nbLig, nbCol;
cin >> nbLig >> nbCol;
vector<int> valeurs;
valeurs.push_back(1); valeurs.push_back(nbCol);
vector<tuple<int, int ,int, int>> dispositifs(nbLig);
for (auto &[deb, fin, trou, cout] : dispositifs)
{
cin >> deb >> fin >> trou >> cout;
valeurs.push_back(deb);
valeurs.push_back(fin);
valeurs.push_back(trou);
}
sort(valeurs.begin(), valeurs.end());
valeurs.resize(unique(valeurs.begin(), valeurs.end()) - valeurs.begin());
auto calcInd = [&](int val)
{
return lower_bound(valeurs.begin(), valeurs.end(), val) - valeurs.begin();
};
Segtree segLeft(valeurs.size());
Segtree segRight(valeurs.size());
segLeft.update(calcInd(1), 0);
segRight.update(calcInd(nbCol), 0);
int sol(1e18);
for (auto [deb, fin, trou, cout] : dispositifs)
{
deb = calcInd(deb);
fin = calcInd(fin);
trou = calcInd(trou);
int queryLeft = segLeft.query(deb, fin);
int queryRight = segRight.query(deb, fin);
sol = min(sol, cout + queryLeft + queryRight);
segLeft.update(trou, queryLeft + cout);
segRight.update(trou, queryRight + cout);
}
if (sol == 1e18) sol = -1;
cout << sol << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
2 ms |
748 KB |
Output is correct |
25 |
Correct |
14 ms |
1932 KB |
Output is correct |
26 |
Correct |
41 ms |
4604 KB |
Output is correct |
27 |
Correct |
136 ms |
10104 KB |
Output is correct |
28 |
Correct |
130 ms |
11236 KB |
Output is correct |
29 |
Correct |
89 ms |
8036 KB |
Output is correct |
30 |
Correct |
154 ms |
11236 KB |
Output is correct |
31 |
Correct |
225 ms |
15068 KB |
Output is correct |
32 |
Correct |
183 ms |
13532 KB |
Output is correct |
33 |
Correct |
25 ms |
4096 KB |
Output is correct |
34 |
Correct |
94 ms |
9960 KB |
Output is correct |
35 |
Correct |
127 ms |
19344 KB |
Output is correct |
36 |
Correct |
240 ms |
19292 KB |
Output is correct |
37 |
Correct |
194 ms |
19292 KB |
Output is correct |
38 |
Correct |
186 ms |
19292 KB |
Output is correct |