From 3c62a2e879dc9779487463084d1ff6b5c28a521e Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 29 Dec 2022 21:10:21 +0800 Subject: [PATCH] =?UTF-8?q?P3332=20[ZJOI2013]K=E5=A4=A7=E6=95=B0=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/98238416 --- Luogu/P3332/P3332.cpp | 199 ++++++++++++++++++++++++++++++++++ Luogu/P3332/data/P3332_1.in | 3 + Luogu/P3332/data/P3332_1.out | 3 + Luogu/P3332/data/P3332_12.in | 3 + Luogu/P3332/data/P3332_12.out | 3 + 5 files changed, 211 insertions(+) create mode 100644 Luogu/P3332/P3332.cpp create mode 100644 Luogu/P3332/data/P3332_1.in create mode 100644 Luogu/P3332/data/P3332_1.out create mode 100644 Luogu/P3332/data/P3332_12.in create mode 100644 Luogu/P3332/data/P3332_12.out diff --git a/Luogu/P3332/P3332.cpp b/Luogu/P3332/P3332.cpp new file mode 100644 index 00000000..def0eab8 --- /dev/null +++ b/Luogu/P3332/P3332.cpp @@ -0,0 +1,199 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +template +class SegmentTree { + private: + struct node { + int l, r; + T sum, tag; + node *lchild, *rchild; + + node(const int &_l = 0, const int &_r = 0) + : l(_l), r(_r), sum(0), tag(0), lchild(nullptr), rchild(nullptr) {} + + ~node() { + if (lchild) delete lchild; + if (rchild) delete rchild; + } + + void pushup() { + sum = 0; + + if (lchild != nullptr) sum += lchild->sum; + if (rchild != nullptr) sum += rchild->sum; + } + + void pushdown() { + int mid = l + r >> 1; + + if (lchild == nullptr) lchild = new node(l, mid); + if (rchild == nullptr) rchild = new node(mid + 1, r); + + lchild->sum += static_cast(lchild->r - lchild->l + 1) * tag; + lchild->tag += tag; + + rchild->sum += static_cast(rchild->r - rchild->l + 1) * tag; + rchild->tag += tag; + + tag = 0; + } + }; + + const int n; + node *root; + + void modify(node *&cur, int l, int r, int ql, int qr, T val) { + if (cur == nullptr) cur = new node(l, r); + + if (ql <= l && r <= qr) { + cur->sum += static_cast(r - l + 1) * val; + cur->tag += val; + + return; + } + + int mid = l + r >> 1; + + cur->pushdown(); + + if (ql <= mid) modify(cur->lchild, l, mid, ql, qr, val); + if (qr > mid) modify(cur->rchild, mid + 1, r, ql, qr, val); + + cur->pushup(); + } + + T query(node *cur, int l, int r, int ql, int qr) { + if (cur == nullptr) return T(); + if (ql <= l && r <= qr) return cur->sum; + + int mid = l + r >> 1; + T res = 0; + + cur->pushdown(); + + if (ql <= mid) res += query(cur->lchild, l, mid, ql, qr); + if (qr > mid) res += query(cur->rchild, mid + 1, r, ql, qr); + + return res; + } + + public: + SegmentTree(const int &_n) + : n(_n), root(nullptr) {} + + ~SegmentTree() { + if (root) delete root; + } + + void modify(int ql, int qr, T val) { + modify(root, 1, n, ql, qr, val); + } + + T query(int ql, int qr) { + return query(root, 1, n, ql, qr); + } +}; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, cnt = 0; + std::vector> qs; + + cin >> n >> m; + + for (int i = 1; i <= m; i++) { + int op, l, r; + long long k; + + cin >> op >> l >> r >> k; + + if (op == 1) { + qs.emplace_back(op, l, r, k, -1); + } else { // op == 2 + qs.emplace_back(op, l, r, k, cnt++); + } + } + + std::vector ans(cnt); + SegmentTree tr(n); + + std::function> &, int, int)> solve = [&](const std::vector> &qs, int l, int r) -> void { + if (qs.empty()) return; + + if (l == r) { + for (auto o : qs) { + int op, id; + + std::tie(op, std::ignore, std::ignore, std::ignore, id) = o; + + if (op == 2) ans[id] = l; + } + + return; + } + + int mid = l + r >> 1; + std::vector> ql, qr; + std::vector cur(qs.size()); + + for (int i = 0; i < qs.size(); i++) { + int op, l, r; + long long k; + + std::tie(op, l, r, k, std::ignore) = qs[i]; + + if (op == 1) { + if (k > mid) tr.modify(l, r, 1); + } else { // op == 2 + cur[i] = tr.query(l, r); + } + } + + for (int i = 0; i < qs.size(); i++) { + int op, l, r; + long long k; + + std::tie(op, l, r, k, std::ignore) = qs[i]; + + if (op == 1 && k > mid) { + tr.modify(l, r, -1); + } + } + + for (int i = 0; i < qs.size(); i++) { + int op, l, r, id; + long long k; + + std::tie(op, l, r, k, id) = qs[i]; + + if (op == 1) { + if (k <= mid) ql.emplace_back(qs[i]); + else qr.emplace_back(qs[i]); + } else { // op == 2 + if (cur[i] < k) { + ql.emplace_back(op, l, r, k - cur[i], id); + } else { + qr.emplace_back(qs[i]); + } + } + } + + solve(ql, l, mid); + solve(qr, mid + 1, r); + }; + + solve(qs, 0, n); + + for (int x : ans) cout << x << endl; + + return 0; +} diff --git a/Luogu/P3332/data/P3332_1.in b/Luogu/P3332/data/P3332_1.in new file mode 100644 index 00000000..a4621146 --- /dev/null +++ b/Luogu/P3332/data/P3332_1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:68a3e345f2ce35dd63731e9c7ce41c6e725dffd692c95879357e73d86e036814 +size 14449 diff --git a/Luogu/P3332/data/P3332_1.out b/Luogu/P3332/data/P3332_1.out new file mode 100644 index 00000000..fac768b5 --- /dev/null +++ b/Luogu/P3332/data/P3332_1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1b6f4b464a2e653fa2af92714d5f2c1155b007ca783b77e23e4e07ef478fad31 +size 2876 diff --git a/Luogu/P3332/data/P3332_12.in b/Luogu/P3332/data/P3332_12.in new file mode 100644 index 00000000..f7d59316 --- /dev/null +++ b/Luogu/P3332/data/P3332_12.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e31aa067a3767251270b41440edf3b844d1fc954a904620915147dbc7cf56ec3 +size 850019 diff --git a/Luogu/P3332/data/P3332_12.out b/Luogu/P3332/data/P3332_12.out new file mode 100644 index 00000000..dcaeab15 --- /dev/null +++ b/Luogu/P3332/data/P3332_12.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:92b53a210b121a3df0fd8a15a17fdaabf83ec635a26d3f2d331ebf27b762d06d +size 7