From 4e09ef8f7fdd2efbcf333a254aaf80525e71d13a Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 27 Sep 2022 09:09:44 +0800 Subject: [PATCH] 833B - The Bakery https://codeforces.com/problemset/submission/833/173637014 --- Codeforces/833/B/B.cpp | 138 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 Codeforces/833/B/B.cpp diff --git a/Codeforces/833/B/B.cpp b/Codeforces/833/B/B.cpp new file mode 100644 index 00000000..b6642742 --- /dev/null +++ b/Codeforces/833/B/B.cpp @@ -0,0 +1,138 @@ +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 35005, + K = 55; + +template +class SegmentTree { + private: + struct node { + int l, r; + T m, d; + node *lchild, *rchild; + + node(int _l = 0, int _r = 0) + : l(_l), + r(_r), + m(0), + d(0), + lchild(nullptr), + rchild(nullptr) {} + + ~node() { + delete lchild; + delete rchild; + } + + void pushup() { + m = 0; + if (lchild) m = std::max(m, lchild->m); + if (rchild) m = std::max(m, rchild->m); + } + + void pushdown() { + if (!d) return; + + lchild->d += d; + rchild->d += d; + + lchild->m += d; + rchild->m += d; + + d = 0; + } + }; + + void build(node *&u, int l, int r) { + u = new node(l, r); + + if (l == r) return; + + int mid = l + r >> 1; + build(u->lchild, l, mid); + build(u->rchild, mid + 1, r); + } + + void modify(node *u, int l, int r, T v) { + if (l <= u->l && u->r <= r) { + u->d += v; + u->m += v; + + return; + } + + u->pushdown(); + + int mid = u->l + u->r >> 1; + if (l <= mid) modify(u->lchild, l, r, v); + if (r > mid) modify(u->rchild, l, r, v); + + u->pushup(); + } + + T query(node *u, int l, int r) { + if (l <= u->l && u->r <= r) return u->m; + + u->pushdown(); + + int mid = u->l + u->r >> 1; + T res = 0; + if (l <= mid) res = std::max(res, query(u->lchild, l, r)); + if (r > mid) res = std::max(res, query(u->rchild, l, r)); + + return res; + } + + node *root; + + public: + SegmentTree(int n) + : root(nullptr) { + build(root, 1, n); + } + + void modify(int l, int r, T v) { + modify(root, l, r, v); + } + + T query(int l, int r) { + return query(root, l, r); + } +}; + +int n, k, a[N], pre[N], pos[N], f[K][N]; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> k; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + pre[i] = pos[a[i]]; + pos[a[i]] = i; + } + + for (int i = 1; i <= k; i++) { + SegmentTree tr(n); + + for (int j = 1; j <= n; j++) { + tr.modify(j, j, f[i - 1][j - 1]); + } + + for (int j = 1; j <= n; j++) { + tr.modify(pre[j] + 1, j, 1); + f[i][j] = tr.query(i, j); + } + } + + cout << f[k][n] << endl; + + return 0; +}