Tue, 12 Feb 2019 17:12:11 GMT

master
大蒟蒻 7 years ago
parent b9f0d92e34
commit 42e3fdc2f1

@ -101,3 +101,9 @@ multiset的count好像格外的慢呢。或许是通过`distance(lower_bound,upp
### U - One-Dimensional Battle Ships
维护一下每一段最多能放下的个数之和。
### V - King's Path
看起来像个什么离散化后最短路一样的东西。实际上map套map就可以了。
//(如果强行说正解是树套树会不会显得高级一点?)

@ -2,7 +2,30 @@
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const pair<int, int> dirs[] = {{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
int main()
{
auto pack = [](int64_t hi, int64_t lo) { return hi << 32 | lo; };
unordered_map<int, unordered_map<int, bool>> m;
unordered_set<int64_t> s;
int x0, y0, x1, y1, n, r, a, b;
scanf("%d%d%d%d%d", &x0, &y0, &x1, &y1, &n);
while (n--)
{
scanf("%d%d%d", &r, &a, &b);
auto &row = m[r];
for (int i = a; i <= b; i++) row[i] = true;
}
queue<tuple<int, int, int>> q;
int ans = -1;
for (q.push({x0, y0, 0}); !q.empty() && ans == -1; q.pop())
{
auto [x, y, l] = q.front();
if (x == x1 && y == y1) ans = l;
for (auto [dx, dy] : dirs)
if (m[x + dx][y + dy] && s.count(pack(x + dx, y + dy)) == 0)
q.push({x + dx, y + dy, l + 1}), s.insert(pack(x + dx, y + dy));
}
printf("%d", ans);
return 0;
}

Loading…
Cancel
Save