From bc3456167a0a7e79c47b8e537a9c63a8123f6f2e Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 13 Jun 2022 11:18:55 +0800 Subject: [PATCH] =?UTF-8?q?#118.=20=E3=80=90UR=20#8=E3=80=91=E8=B5=B4?= =?UTF-8?q?=E4=BA=AC=E8=B5=B6=E8=80=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://uoj.ac/submission/558145 --- UniversalOJ/118/118.cpp | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 UniversalOJ/118/118.cpp diff --git a/UniversalOJ/118/118.cpp b/UniversalOJ/118/118.cpp new file mode 100644 index 00000000..2ace33f6 --- /dev/null +++ b/UniversalOJ/118/118.cpp @@ -0,0 +1,57 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 100005; + +int n, m, q, d_a[N], d_b[N]; +bool a[N], b[N]; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + for (int i = 1; i <= m; i++) { + cin >> b[i]; + } + + if (a[1] != a[n]) d_a[0] = 1; + for (int i = 1; i <= n; i++) { + d_a[i] = d_a[i - 1] + (a[i] != a[i + 1]); + } + + if (b[1] != b[m]) d_b[0] = 1; + for (int i = 1; i <= m; i++) { + d_b[i] = d_b[i - 1] + (b[i] != b[i + 1]); + } + + cin >> q; + + while (q--) { + std::pair s, e; + + cin >> s.first >> s.second >> e.first >> e.second; + + cout << std::min({ + std::abs(d_a[s.first - 1] - d_a[e.first - 1]), + d_a[n - 1] - d_a[std::max(s.first, e.first) - 1] + d_a[std::min(s.first, e.first) - 1], + }) + + std::min({ + std::abs(d_b[s.second - 1] - d_b[e.second - 1]), + d_b[m - 1] - d_b[std::max(s.second, e.second) - 1] + d_b[std::min(s.second, e.second) - 1], + }) + << endl; + } + + return 0; +}