#include <iostream>
#include <algorithm>
#include <vector>
struct Reindex
{
Reindex(int d, int e, int p) : device(d), elem(e), pos(p) {}
int device, elem, pos;
bool operator <(const Reindex &other) const
{
return (pos < other.pos);
}
};
const int MAXN = 1e5;
const long long INFTY = 1e18;
const int LEAVES = 1 << 19;
const int TSIZE = LEAVES << 1;
int left[MAXN], right[MAXN];
int drop[MAXN];
int cost[MAXN];
std::vector<Reindex> compress;
long long tree[TSIZE];
long long getLeft[MAXN];
long long getRight[MAXN];
void reset()
{
std::fill_n(tree, TSIZE, INFTY);
}
void upd(int node, long long val)
{
tree[node] = std::min(tree[node], val);
if (node > 1)
upd(node / 2, val);
}
long long req(int leftNode, int rightNode)
{
long long ans = INFTY;
if (leftNode % 2 == 1)
ans = std::min(ans, tree[leftNode++]);
if (rightNode % 2 == 1)
ans = std::min(ans, tree[--rightNode]);
if (leftNode < rightNode)
ans = std::min(ans, req(leftNode / 2, rightNode / 2));
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
int lines, columns;
std::cin >> lines >> columns;
for (int iDevice = 0; iDevice < lines; iDevice++)
{
std::cin >> left[iDevice] >> right[iDevice] >> drop[iDevice] >> cost[iDevice];
compress.emplace_back(iDevice, 0, left[iDevice] - 1);
compress.emplace_back(iDevice, 1, right[iDevice] - 1);
compress.emplace_back(iDevice, 2, drop[iDevice] - 1);
}
compress.emplace_back(-1, -1, 0);
compress.emplace_back(-1, -1, columns - 1);
std::sort(compress.begin(), compress.end());
int trueCol = 0;
for (int iElem = 0; iElem < (int)compress.size(); trueCol++)
for (int jElem = iElem; iElem < (int)compress.size() && compress[iElem].pos == compress[jElem].pos; iElem++)
{
if (compress[iElem].elem == 0)
left[compress[iElem].device] = trueCol;
else if (compress[iElem].elem == 1)
right[compress[iElem].device] = trueCol + 1;
else if (compress[iElem].elem == 2)
drop[compress[iElem].device] = trueCol;
}
reset();
upd(LEAVES, 0);
for (int iDev = 0; iDev < lines; iDev++)
{
getLeft[iDev] = req(LEAVES + left[iDev], LEAVES + right[iDev]);
upd(LEAVES + drop[iDev], getLeft[iDev] + cost[iDev]);
}
reset();
upd(LEAVES + trueCol - 1, 0);
for (int iDev = 0; iDev < lines; iDev++)
{
getRight[iDev] = req(LEAVES + left[iDev], LEAVES + right[iDev]);
upd(LEAVES + drop[iDev], getRight[iDev] + cost[iDev]);
}
long long minCost = INFTY;
for (int iDev = 0; iDev < lines; iDev++)
minCost = std::min(minCost, getLeft[iDev] + getRight[iDev] + cost[iDev]);
if (minCost == INFTY)
std::cout << "-1\n";
else
std::cout << minCost << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8568 KB |
Output is correct |
2 |
Correct |
9 ms |
8568 KB |
Output is correct |
3 |
Correct |
9 ms |
8568 KB |
Output is correct |
4 |
Correct |
9 ms |
8568 KB |
Output is correct |
5 |
Correct |
9 ms |
8568 KB |
Output is correct |
6 |
Correct |
9 ms |
8568 KB |
Output is correct |
7 |
Correct |
10 ms |
8568 KB |
Output is correct |
8 |
Correct |
9 ms |
8568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8568 KB |
Output is correct |
2 |
Correct |
9 ms |
8568 KB |
Output is correct |
3 |
Correct |
9 ms |
8568 KB |
Output is correct |
4 |
Correct |
9 ms |
8568 KB |
Output is correct |
5 |
Correct |
9 ms |
8568 KB |
Output is correct |
6 |
Correct |
9 ms |
8568 KB |
Output is correct |
7 |
Correct |
10 ms |
8568 KB |
Output is correct |
8 |
Correct |
9 ms |
8568 KB |
Output is correct |
9 |
Correct |
9 ms |
8568 KB |
Output is correct |
10 |
Correct |
10 ms |
8568 KB |
Output is correct |
11 |
Correct |
10 ms |
8568 KB |
Output is correct |
12 |
Correct |
10 ms |
8572 KB |
Output is correct |
13 |
Correct |
10 ms |
8540 KB |
Output is correct |
14 |
Correct |
10 ms |
8568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8568 KB |
Output is correct |
2 |
Correct |
9 ms |
8568 KB |
Output is correct |
3 |
Correct |
9 ms |
8568 KB |
Output is correct |
4 |
Correct |
9 ms |
8568 KB |
Output is correct |
5 |
Correct |
9 ms |
8568 KB |
Output is correct |
6 |
Correct |
9 ms |
8568 KB |
Output is correct |
7 |
Correct |
10 ms |
8568 KB |
Output is correct |
8 |
Correct |
9 ms |
8568 KB |
Output is correct |
9 |
Correct |
9 ms |
8568 KB |
Output is correct |
10 |
Correct |
10 ms |
8568 KB |
Output is correct |
11 |
Correct |
10 ms |
8568 KB |
Output is correct |
12 |
Correct |
10 ms |
8572 KB |
Output is correct |
13 |
Correct |
10 ms |
8540 KB |
Output is correct |
14 |
Correct |
10 ms |
8568 KB |
Output is correct |
15 |
Correct |
10 ms |
8568 KB |
Output is correct |
16 |
Correct |
10 ms |
8696 KB |
Output is correct |
17 |
Correct |
11 ms |
8756 KB |
Output is correct |
18 |
Correct |
11 ms |
8696 KB |
Output is correct |
19 |
Correct |
11 ms |
8668 KB |
Output is correct |
20 |
Correct |
10 ms |
8696 KB |
Output is correct |
21 |
Correct |
10 ms |
8696 KB |
Output is correct |
22 |
Correct |
10 ms |
8824 KB |
Output is correct |
23 |
Correct |
10 ms |
8696 KB |
Output is correct |
24 |
Correct |
10 ms |
8716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8568 KB |
Output is correct |
2 |
Correct |
9 ms |
8568 KB |
Output is correct |
3 |
Correct |
9 ms |
8568 KB |
Output is correct |
4 |
Correct |
9 ms |
8568 KB |
Output is correct |
5 |
Correct |
9 ms |
8568 KB |
Output is correct |
6 |
Correct |
9 ms |
8568 KB |
Output is correct |
7 |
Correct |
10 ms |
8568 KB |
Output is correct |
8 |
Correct |
9 ms |
8568 KB |
Output is correct |
9 |
Correct |
9 ms |
8568 KB |
Output is correct |
10 |
Correct |
10 ms |
8568 KB |
Output is correct |
11 |
Correct |
10 ms |
8568 KB |
Output is correct |
12 |
Correct |
10 ms |
8572 KB |
Output is correct |
13 |
Correct |
10 ms |
8540 KB |
Output is correct |
14 |
Correct |
10 ms |
8568 KB |
Output is correct |
15 |
Correct |
10 ms |
8568 KB |
Output is correct |
16 |
Correct |
10 ms |
8696 KB |
Output is correct |
17 |
Correct |
11 ms |
8756 KB |
Output is correct |
18 |
Correct |
11 ms |
8696 KB |
Output is correct |
19 |
Correct |
11 ms |
8668 KB |
Output is correct |
20 |
Correct |
10 ms |
8696 KB |
Output is correct |
21 |
Correct |
10 ms |
8696 KB |
Output is correct |
22 |
Correct |
10 ms |
8824 KB |
Output is correct |
23 |
Correct |
10 ms |
8696 KB |
Output is correct |
24 |
Correct |
10 ms |
8716 KB |
Output is correct |
25 |
Correct |
25 ms |
9716 KB |
Output is correct |
26 |
Correct |
47 ms |
11240 KB |
Output is correct |
27 |
Correct |
117 ms |
15844 KB |
Output is correct |
28 |
Correct |
134 ms |
19160 KB |
Output is correct |
29 |
Correct |
92 ms |
13924 KB |
Output is correct |
30 |
Correct |
150 ms |
19288 KB |
Output is correct |
31 |
Correct |
168 ms |
19288 KB |
Output is correct |
32 |
Correct |
171 ms |
19240 KB |
Output is correct |
33 |
Correct |
29 ms |
10736 KB |
Output is correct |
34 |
Correct |
77 ms |
14036 KB |
Output is correct |
35 |
Correct |
112 ms |
19256 KB |
Output is correct |
36 |
Correct |
164 ms |
19132 KB |
Output is correct |
37 |
Correct |
133 ms |
19160 KB |
Output is correct |
38 |
Correct |
143 ms |
19220 KB |
Output is correct |