From 67d3d4583f1b87394ec6ebcfd728bdec420f1d77 Mon Sep 17 00:00:00 2001 From: Baoshuo Ren Date: Wed, 1 Dec 2021 19:46:42 +0800 Subject: [PATCH] =?UTF-8?q?P1083=20[NOIP2012=20=E6=8F=90=E9=AB=98=E7=BB=84?= =?UTF-8?q?]=20=E5=80=9F=E6=95=99=E5=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R64048072 --- Luogu/P1083/P1083.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Luogu/P1083/P1083.cpp diff --git a/Luogu/P1083/P1083.cpp b/Luogu/P1083/P1083.cpp new file mode 100644 index 00000000..35cd0a9a --- /dev/null +++ b/Luogu/P1083/P1083.cpp @@ -0,0 +1,93 @@ +#include + +using std::cin; +using std::cout; +using std::endl; + +int n, m, d, s, t, a[1000005]; + +// Segment Tree +void pushup(int); +void pushdown(int); +void build(int, int, int); +void modify(int, int, int, int); +int query(int, int, int); + +int main() { + cin >> n >> m; + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + build(1, 1, n); + for (int i = 1; i <= m; i++) { + cin >> d >> s >> t; + if (query(1, s, t) < d) { + cout << -1 << endl + << i << endl; + exit(0); + } else { + modify(1, s, t, -d); + } + } + cout << 0 << endl; + return 0; +} + +// === Segment Tree === + +struct node { + int l, r, m, d; + + node() + : l(0), r(0), m(std::numeric_limits::max()), d(0) {} + node(int _l, int _r) + : l(_l), r(_r), m(std::numeric_limits::max()), d(0) {} +} tr[1000005 << 1]; + +void pushup(int u) { + tr[u].m = std::min(tr[u << 1].m, tr[u << 1 | 1].m); +} + +void pushdown(int u) { + auto &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1]; + left.m += root.d; + left.d += root.d; + right.m += root.d; + right.d += root.d; + root.d = 0; +} + +void build(int u, int l, int r) { + tr[u] = node(l, r); + if (l == r) { + tr[u].m = a[l]; + return; + } + int mid = l + r >> 1; + build(u << 1, l, mid); + build(u << 1 | 1, mid + 1, r); + pushup(u); +} + +void modify(int u, int l, int r, int k) { + if (l <= tr[u].l && tr[u].r <= r) { + tr[u].m += k; + tr[u].d += k; + return; + } + int mid = tr[u].l + tr[u].r >> 1; + pushdown(u); + if (l <= mid) modify(u << 1, l, r, k); + if (r > mid) modify(u << 1 | 1, l, r, k); + pushup(u); +} + +int query(int u, int l, int r) { + if (l <= tr[u].l && tr[u].r <= r) return tr[u].m; + int mid = tr[u].l + tr[u].r >> 1; + int min = std::numeric_limits::max(); + pushdown(u); + if (l <= mid) min = std::min(min, query(u << 1, l, r)); + if (r > mid) min = std::min(min, query(u << 1 | 1, l, r)); + return min; +}