From df2ee95279b356d1be9219d48023340dd61b4454 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 13 Jul 2024 22:18:52 +0800 Subject: [PATCH] =?UTF-8?q?P1496=20=E7=81=AB=E7=83=A7=E8=B5=A4=E5=A3=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/165811402 --- Luogu/P1496/P1496.cpp | 54 ++++++++++++++++++++++++++++++++++++ Luogu/P1496/data/P1496_8.in | 3 ++ Luogu/P1496/data/P1496_8.out | 3 ++ 3 files changed, 60 insertions(+) create mode 100644 Luogu/P1496/P1496.cpp create mode 100644 Luogu/P1496/data/P1496_8.in create mode 100644 Luogu/P1496/data/P1496_8.out diff --git a/Luogu/P1496/P1496.cpp b/Luogu/P1496/P1496.cpp new file mode 100644 index 00000000..e0a7a76b --- /dev/null +++ b/Luogu/P1496/P1496.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2e4 + 5; + +int n, ans, + l[N], r[N], // 原位置 + f[N << 1]; // 染色情况(离散化以后),注意开两倍空间(左右端点) +std::vector nums; // 统计出现的数字,离散化用 + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> l[i] >> r[i]; + + // 统计一下哪些数字出现过 + nums.push_back(l[i]); + nums.push_back(r[i]); + } + + // 先排序才能用 std::unique 去重,两个函数都在 里面定义 + std::sort(nums.begin(), nums.end()); + nums.erase(std::unique(nums.begin(), nums.end()), nums.end()); // 去重完移除后面多余的数字 + // 可以讲一下 std::unique 跑完以后数组的特性:前面不重复且单增,后面是多余的数 + + for (int i = 1; i <= n; i++) { + // 求出离散化以后的位置 + int nl = std::lower_bound(nums.begin(), nums.end(), l[i]) - nums.begin(), + nr = std::lower_bound(nums.begin(), nums.end(), r[i]) - nums.begin(); + + // 这里用差分也可以,不过数据范围比较小,这么写也能过 + for (int j = nl; j < nr; j++) { + f[j] = 1; + } + } + + for (int i = 0; i < nums.size() - 1; i++) { + // 如果被染色就统计一下长度 + if (f[i]) ans += nums[i + 1] - nums[i]; + } + + cout << ans << endl; + + return 0; +} diff --git a/Luogu/P1496/data/P1496_8.in b/Luogu/P1496/data/P1496_8.in new file mode 100644 index 00000000..72de9db0 --- /dev/null +++ b/Luogu/P1496/data/P1496_8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bac7b9d50ebad0cc319d7718824671385033eb1250de93533de11bd18647c41e +size 321665 diff --git a/Luogu/P1496/data/P1496_8.out b/Luogu/P1496/data/P1496_8.out new file mode 100644 index 00000000..25b93046 --- /dev/null +++ b/Luogu/P1496/data/P1496_8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9aa210ad9b9c22fdfb8d6a9e232ec885b333874e122aaebdeeaf1d4c2a2f8ecd +size 12