Submission #409901

#TimeUsernameProblemLanguageResultExecution timeMemory
409901kaagePainting Walls (APIO20_paint)C++17
0 / 100
1 ms204 KiB
#line 1 "paint.h" #include <vector> int minimumInstructions( int N, int M, int K, std::vector<int> C, std::vector<int> A, std::vector<std::vector<int>> B); #line 2 "/Users/kaage/Desktop/ProgrammingWorkspace/library/other/template.hpp" #define _CRT_SECURE_NO_WARNINGS #ifndef __clang__ #ifdef ONLINE_JUDGE #pragma GCC target("avx512f") #elif defined EVAL #else #pragma GCC target("avx2") #endif #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif #include <string.h> #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #line 41 "/Users/kaage/Desktop/ProgrammingWorkspace/library/other/template.hpp" #define rep(i, n) for (int i = 0; i < int(n); i++) #define REP(i, n) for (int i = 1; i <= int(n); i++) #define all(V) V.begin(), V.end() using i128 = __int128_t; using u128 = __uint128_t; using uint = unsigned int; using lint = long long; using ulint = unsigned long long; using IP = std::pair<int, int>; using LP = std::pair<lint, lint>; constexpr int INF = INT_MAX / 2; constexpr lint LINF = LLONG_MAX / 2; constexpr double eps = DBL_EPSILON; constexpr double PI = 3.141592653589793238462643383279; template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; int popcount(uint x) { #if __cplusplus >= 202002L return std::popcount(x); #else #ifndef __clang__ return __builtin_popcount(x); #endif #endif x = (x & 0x55555555) + (x >> 1 & 0x55555555); x = (x & 0x33333333) + (x >> 2 & 0x33333333); x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f); x = (x & 0x00ff00ff) + (x >> 8 & 0x00ff00ff); return (x & 0x0000ffff) + (x >> 16 & 0x0000ffff); } template <class F> inline constexpr decltype(auto) lambda_fix(F&& f) { return [f = std::forward<F>(f)](auto&&... args) { return f(f, std::forward<decltype(args)>(args)...); }; } template <class T> constexpr std::vector<T> make_vec(size_t n) { return std::vector<T>(n); } template <class T, class... Args> constexpr auto make_vec(size_t n, Args&&... args) { return std::vector<decltype(make_vec<T>(args...))>( n, make_vec<T>(std::forward<Args>(args)...)); } template <class T, class U> std::istream& operator>>(std::istream& ist, std::pair<T, U>& x) { return ist >> x.first >> x.second; } template <class T, class U> std::ostream& operator<<(std::ostream& ost, const std::pair<T, U>& x) { return ost << x.first << " " << x.second; } template <class T, class U> constexpr inline bool chmax(T& lhs, const U& rhs) noexcept { if (lhs < rhs) { lhs = rhs; return true; } return false; } template <class T, class U> constexpr inline bool chmin(T& lhs, const U& rhs) noexcept { if (lhs > rhs) { lhs = rhs; return true; } return false; } constexpr inline lint gcd(lint a, lint b) noexcept { while (b) { lint c = a; a = b; b = c % b; } return a; } inline lint lcm(lint a, lint b) noexcept { return a / gcd(a, b) * b; } constexpr bool isprime(lint n) noexcept { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } template <class T> constexpr T mypow(T a, lint b) noexcept { T res(1); while (true) { if (b & 1) res *= a; b >>= 1; if (!b) break; a *= a; } return res; } constexpr lint modpow(lint a, lint b, lint m) noexcept { a %= m; lint res(1); while (b) { if (b & 1) { res *= a; res %= m; } a *= a; a %= m; b >>= 1; } return res; } template <class T> constexpr void printArray(const std::vector<T>& vec, char split = ' ') { rep(i, vec.size()) { std::cout << vec[i]; std::cout << (i == (int)vec.size() - 1 ? '\n' : split); } } template <class InputIter> constexpr void printArray(InputIter l, InputIter r, char split = ' ') { auto rprev = std::prev(r); for (auto i = l; i != r; i++) { std::cout << *i; std::cout << (i == rprev ? '\n' : split); } } LP extGcd(lint a, lint b) noexcept { if (b == 0) return {1, 0}; LP s = extGcd(b, a % b); std::swap(s.first, s.second); s.second -= a / b * s.first; return s; } LP ChineseRem(const lint& b1, const lint& m1, const lint& b2, const lint& m2) noexcept { auto p = extGcd(m1, m2); lint g = gcd(m1, m2), l = m1 / g * m2; lint tmp = (b2 - b1) / g * p.first % (m2 / g); lint r = (b1 + m1 * tmp + l) % l; return {r, l}; } int LCS(const std::string& a, const std::string& b) { auto dp = make_vec<int>(a.size() + 1, b.size() + 1); rep(i, a.size()) { rep(j, b.size()) { chmax(dp[i + 1][j], dp[i][j]); chmax(dp[i][j + 1], dp[i][j]); if (a[i] == b[j]) chmax(dp[i + 1][j + 1], dp[i][j] + 1); } chmax(dp[i + 1][b.size()], dp[i][b.size()]); } rep(j, b.size()) chmax(dp[a.size()][j + 1], dp[a.size()][j]); return dp[a.size()][b.size()]; } template <class T, std::enable_if_t<std::is_convertible<int, T>::value, std::nullptr_t> = nullptr> void compress(std::vector<T>& vec) { auto tmp = vec; std::sort(all(tmp)); tmp.erase(std::unique(all(tmp)), tmp.end()); for (T& i : vec) i = std::lower_bound(all(tmp), i) - tmp.begin(); } template <class T> void compress(T* l, T* r) { std::vector<T> tmp(l, r); std::sort(all(tmp)); tmp.erase(std::unique(all(tmp)), tmp.end()); for (auto i = l; i < r; i++) { *i = std::lower_bound(all(tmp), *i) - tmp.begin(); } } template <class InputIter> void compress(InputIter l, InputIter r) { std::vector<typename InputIter::value_type> tmp(l, r); std::sort(all(tmp)); tmp.erase(std::unique(all(tmp)), tmp.end()); for (auto i = l; i < r; i++) { *i = std::lower_bound(all(tmp), *i) - tmp.begin(); } } #line 3 "/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/SegTree.hpp" template <class T, T (*nodef)(const T&, const T&)> class SegTree { protected: unsigned int n = 1, rank = 0; std::vector<T> node; T ident; public: SegTree(unsigned int m, T e_) : ident(e_) { while (n < m) { n *= 2; rank++; } node.resize(2 * n, ident); } SegTree(unsigned int m, T init, T e_) : ident(e_) { while (n < m) { n *= 2; rank++; } node.resize(2 * n, ident); for (unsigned int i = n; i < 2 * n; i++) node[i] = init; for (unsigned int i = n - 1; i > 0; i--) node[i] = nodef(node[i << 1], node[i << 1 | 1]); } template <class U> SegTree(const std::vector<U>& initvec, T e_) : ident(e_) { unsigned int m = initvec.size(); while (n < m) { n *= 2; rank++; } node.resize(2 * n, ident); for (unsigned int i = n; i < 2 * n; i++) { if (i - n < m) node[i] = initvec[i - n]; } for (unsigned int i = n - 1; i > 0; i--) node[i] = nodef(node[i << 1], node[i << 1 | 1]); } void update(int i, T x) { i += n; node[i] = x; while (i != 1) { i >>= 1; node[i] = nodef(node[2 * i], node[2 * i + 1]); } } T query(int l, int r) const { l += n; r += n; T ls = ident, rs = ident; while (l < r) { if (l & 1) ls = nodef(ls, node[l++]); if (r & 1) rs = nodef(node[--r], rs); l >>= 1; r >>= 1; } return nodef(ls, rs); } const T& operator[](const int& x) const { return node[n + x]; } T queryForAll() const { return node[1]; } private: template <class F> int max_right(int st, F& check, T& acc, int k, int l, int r) const { if (l + 1 == r) { acc = nodef(acc, node[k]); return check(acc) ? -1 : k - n; } int m = (l + r) >> 1; if (m <= st) return max_right(st, check, acc, (k << 1) | 1, m, r); if (st <= l && check(nodef(acc, node[k]))) { acc = nodef(acc, node[k]); return -1; } int vl = max_right(st, check, acc, k << 1, l, m); if (vl != -1) return vl; return max_right(st, check, acc, (k << 1) | 1, m, r); } public: template <class F> int max_right(int st, F check) const { T acc = ident; return max_right(st, check, acc, 1, 0, n); } template <bool (*check)(const T&)> int max_right(int st) const { T acc = ident; return max_right(st, check, acc, 1, 0, n); } }; namespace { lint RSQ_nodef(const lint& lhs, const lint& rhs) { return lhs + rhs; } lint RMiQ_nodef(const lint& lhs, const lint& rhs) { return std::min(lhs, rhs); } lint RMaQ_nodef(const lint& lhs, const lint& rhs) { return std::max(lhs, rhs); } } // namespace class RSQ : public SegTree<lint, RSQ_nodef> { using Base = SegTree<lint, RSQ_nodef>; public: template <class... Args> RSQ(Args&&... args) : Base(std::forward<Args>(args)..., 0) {} }; class RMiQ : public SegTree<lint, RMiQ_nodef> { using Base = SegTree<lint, RMiQ_nodef>; public: template <class... Args> RMiQ(Args&&... args) : Base(std::forward<Args>(args)..., LINF) {} }; class RMaQ : public SegTree<lint, RMaQ_nodef> { using Base = SegTree<lint, RMaQ_nodef>; public: template <class... Args> RMaQ(Args&&... args) : Base(std::forward<Args>(args)..., -LINF) {} }; /** * @title Segment Tree */ #line 4 "/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/IntervalSegTree.hpp" template <class T, class U, T (*nodef)(const T&, const T&), void (*lazyf)(U&, const U&), void (*updf)(T&, const U&, const unsigned int&)> class IntervalSegTree : public SegTree<T, nodef> { using Base = SegTree<T, nodef>; using Base::ident; using Base::n; using Base::node; using Base::rank; std::vector<U> lazy; std::vector<bool> lazyflag; std::vector<int> width; void eval(int k) { for (int i = rank; i > 0; i--) { int nk = k >> i; if (lazyflag[nk]) { updf(node[2 * nk], lazy[nk], width[2 * nk]); updf(node[2 * nk + 1], lazy[nk], width[2 * nk + 1]); if (lazyflag[2 * nk]) lazyf(lazy[2 * nk], lazy[nk]); else lazy[2 * nk] = lazy[nk]; if (lazyflag[2 * nk + 1]) lazyf(lazy[2 * nk + 1], lazy[nk]); else lazy[2 * nk + 1] = lazy[nk]; lazyflag[2 * nk] = lazyflag[2 * nk + 1] = true; lazyflag[nk] = false; } } } public: IntervalSegTree(unsigned int m, T e_) : Base(m, T(), e_) { lazy.resize(2 * n); lazyflag.resize(2 * n); width.resize(2 * n); width[1] = n; for (unsigned int i = 2; i < 2 * n; i++) { width[i] = width[i >> 1] >> 1; } } IntervalSegTree(unsigned int m, T init, T e_) : Base(m, init, e_) { lazy.resize(2 * n); lazyflag.resize(2 * n); width.resize(2 * n); width[1] = n; for (unsigned int i = 2; i < 2 * n; i++) { width[i] = width[i >> 1] >> 1; } } IntervalSegTree(const std::vector<T>& initvec, T e_) : Base(initvec, e_) { lazy.resize(2 * n); lazyflag.resize(2 * n); width.resize(2 * n); width[1] = n; for (unsigned int i = 2; i < 2 * n; i++) { width[i] = width[i >> 1] >> 1; } } void update(int i, U x) { i += n; eval(i); updf(node[i], x, width[i]); if (lazyflag[i]) lazyf(lazy[i], x); else { lazyflag[i] = true; lazy[i] = x; } while (i /= 2) node[i] = nodef(node[2 * i], node[2 * i + 1]); } void update(int l, int r, U x) { l += n, r += n; int nl = l, nr = r; while (!(nl & 1)) nl >>= 1; while (!(nr & 1)) nr >>= 1; nr--; eval(nl), eval(nr); while (l < r) { if (l & 1) { updf(node[l], x, width[l]); if (lazyflag[l]) lazyf(lazy[l], x); else { lazyflag[l] = true; lazy[l] = x; } l++; } if (r & 1) { r--; updf(node[r], x, width[r]); if (lazyflag[r]) lazyf(lazy[r], x); else { lazyflag[r] = true; lazy[r] = x; } } l >>= 1, r >>= 1; } while (nl >>= 1) node[nl] = nodef(node[2 * nl], node[2 * nl + 1]); while (nr >>= 1) node[nr] = nodef(node[2 * nr], node[2 * nr + 1]); } T query(int l, int r) { l += n, r += n, eval(l); eval(r - 1); T ls = ident, rs = ident; while (l < r) { if (l & 1) ls = nodef(ls, node[l++]); if (r & 1) rs = nodef(node[--r], rs); l >>= 1, r >>= 1; } return nodef(ls, rs); } T operator[](const int& x) { eval(n + x); return node[n + x]; } private: template <class F> int max_right(int st, F& check, T& acc, int k, int l, int r) { eval(k); if (l + 1 == r) { acc = nodef(acc, node[k]); return check(acc) ? -1 : k - n; } int m = (l + r) >> 1; if (m <= st) return max_right(st, check, acc, (k << 1) | 1, m, r); if (st <= l && check(nodef(acc, node[k]))) { acc = nodef(acc, node[k]); return -1; } int vl = max_right(st, check, acc, k << 1, l, m); if (vl != -1) return vl; return max_right(st, check, acc, (k << 1) | 1, m, r); } template <class F> int min_left(int st, F& check, T& acc, int k, int l, int r) { eval(k); if (l + 1 == r) { acc = nodef(node[k], acc); return check(acc) ? -1 : k - n + 1; } int m = (l + r) >> 1; if (st <= m) return min_left(st, check, acc, k << 1, l, m); if (r <= st && check(nodef(node[k], acc))) { acc = nodef(node[k], acc); return -1; } int vr = min_left(st, check, acc, (k << 1) | 1, m, r); if (vr != -1) return vr; return min_left(st, check, acc, k << 1, l, m); } public: template <class F> int max_right(int st, F check) { T acc = ident; return max_right(st, check, acc, 1, 0, n); } template <bool (*check)(const T&)> int max_right(int st) { T acc = ident; return max_right(st, check, acc, 1, 0, n); } template <class F> int min_left(int st, F check) { T acc = ident; return min_left(st, check, acc, 1, 0, n); } template <bool (*check)(const T&)> int min_left(int st) { T acc = ident; return min_left(st, check, acc, 1, 0, n); } }; namespace { lint RAQRSQ_nodef(const lint& a, const lint& b) { return a + b; } void RAQRSQ_lazyf(lint& a, const lint& b) { a += b; } void RAQRSQ_updf(lint& a, const lint& b, const unsigned int& width) { a += width * b; } lint RAQRMiQ_nodef(const lint& a, const lint& b) { return std::min(a, b); } void RAQRMiQ_lazyf(lint& a, const lint& b) { a += b; } void RAQRMiQ_updf(lint& a, const lint& b, const unsigned int& width) { a += b; } lint RAQRMaQ_nodef(const lint& a, const lint& b) { return std::max(a, b); } void RAQRMaQ_lazyf(lint& a, const lint& b) { a += b; } void RAQRMaQ_updf(lint& a, const lint& b, const unsigned int& width) { a += b; } lint RUQRSQ_nodef(const lint& a, const lint& b) { return a + b; } void RUQRSQ_lazyf(lint& a, const lint& b) { a = b; } void RUQRSQ_updf(lint& a, const lint& b, const unsigned int& width) { a = width * b; } lint RUQRMiQ_nodef(const lint& a, const lint& b) { return std::min(a, b); } void RUQRMiQ_lazyf(lint& a, const lint& b) { a = b; } void RUQRMiQ_updf(lint& a, const lint& b, const unsigned int& width) { a = b; } lint RUQRMaQ_nodef(const lint& a, const lint& b) { return std::max(a, b); } void RUQRMaQ_lazyf(lint& a, const lint& b) { a = b; } void RUQRMaQ_updf(lint& a, const lint& b, const unsigned int& width) { a = b; } } // namespace class RAQRSQ : public IntervalSegTree<lint, lint, RAQRSQ_nodef, RAQRSQ_lazyf, RAQRSQ_updf> { using Base = IntervalSegTree<lint, lint, RAQRSQ_nodef, RAQRSQ_lazyf, RAQRSQ_updf>; public: template <class... Args> RAQRSQ(Args&&... args) : Base(std::forward<Args>(args)..., 0) {} }; class RAQRMiQ : public IntervalSegTree<lint, lint, RAQRMiQ_nodef, RAQRMiQ_lazyf, RAQRMiQ_updf> { using Base = IntervalSegTree<lint, lint, RAQRMiQ_nodef, RAQRMiQ_lazyf, RAQRMiQ_updf>; public: template <class... Args> RAQRMiQ(Args&&... args) : Base(std::forward<Args>(args)..., LINF) {} }; class RAQRMaQ : public IntervalSegTree<lint, lint, RAQRMaQ_nodef, RAQRMaQ_lazyf, RAQRMaQ_updf> { using Base = IntervalSegTree<lint, lint, RAQRMaQ_nodef, RAQRMaQ_lazyf, RAQRMaQ_updf>; public: template <class... Args> RAQRMaQ(Args&&... args) : Base(std::forward<Args>(args)..., -LINF) {} }; class RUQRSQ : public IntervalSegTree<lint, lint, RUQRSQ_nodef, RUQRSQ_lazyf, RUQRSQ_updf> { using Base = IntervalSegTree<lint, lint, RUQRSQ_nodef, RUQRSQ_lazyf, RUQRSQ_updf>; public: template <class... Args> RUQRSQ(Args&&... args) : Base(std::forward<Args>(args)..., 0) {} }; class RUQRMiQ : public IntervalSegTree<lint, lint, RUQRMiQ_nodef, RUQRMiQ_lazyf, RUQRMiQ_updf> { using Base = IntervalSegTree<lint, lint, RUQRMiQ_nodef, RUQRMiQ_lazyf, RUQRMiQ_updf>; public: template <class... Args> RUQRMiQ(Args&&... args) : Base(std::forward<Args>(args)..., LINF) {} }; class RUQRMaQ : public IntervalSegTree<lint, lint, RUQRMaQ_nodef, RUQRMaQ_lazyf, RUQRMaQ_updf> { using Base = IntervalSegTree<lint, lint, RUQRMaQ_nodef, RUQRMaQ_lazyf, RUQRMaQ_updf>; public: template <class... Args> RUQRMaQ(Args&&... args) : Base(std::forward<Args>(args)..., -LINF) {} }; /** * @title Interval Segment Tree */ #line 4 "paint.cpp" int nodef(const int& a, const int& b) { return std::min(a, b); } void lazyf(int& a, const int& b) { chmin(a, b); } void updf(int& a, const int& b, const uint& c) { chmin(a, b); } using Seg = IntervalSegTree<int, int, nodef, lazyf, updf>; int minimumInstructions(int N, int M, int K, std::vector<int> C, std::vector<int> A, std::vector<std::vector<int>> B) { std::vector<std::set<int>> st(N); std::vector<bool> trans(N); rep(i, M) rep(j, A[i]) st[B[i][j]].insert(i); rep(i, N - M + 1) { for (int j : st[C[i]]) { int now = j; bool ok = true; for (int k = i; k < i + M; k++) { if (st[C[k]].find(now) == st[C[k]].end()) { ok = false; break; } now++; if (now >= M) now -= M; } if (ok) { trans[i] = true; break; } } } Seg dp(N + 1, INF, INF); dp.update(0, 1, 0); rep(i, N - M + 1) { int x = dp[i]; if (x == INF) continue; if (trans[i]) dp.update(i + 1, i + M + 1, x + 1); } rep(i, N + 1) std::cout << dp[i] << " "; std::cout << std::endl; if (dp[N] == INF) return -1; return dp[N]; }

Compilation message (stderr)

/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/SegTree.hpp:107:7: warning: 'RSQ' has a base 'SegTree<long long int, {anonymous}::RSQ_nodef>' whose type uses the anonymous namespace [-Wsubobject-linkage]
/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/SegTree.hpp:114:7: warning: 'RMiQ' has a base 'SegTree<long long int, {anonymous}::RMiQ_nodef>' whose type uses the anonymous namespace [-Wsubobject-linkage]
/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/SegTree.hpp:121:7: warning: 'RMaQ' has a base 'SegTree<long long int, {anonymous}::RMaQ_nodef>' whose type uses the anonymous namespace [-Wsubobject-linkage]
/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/IntervalSegTree.hpp:218:7: warning: 'RAQRSQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RAQRSQ_nodef, {anonymous}::RAQRSQ_lazyf, {anonymous}::RAQRSQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage]
/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/IntervalSegTree.hpp:227:7: warning: 'RAQRMiQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RAQRMiQ_nodef, {anonymous}::RAQRMiQ_lazyf, {anonymous}::RAQRMiQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage]
/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/IntervalSegTree.hpp:236:7: warning: 'RAQRMaQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RAQRMaQ_nodef, {anonymous}::RAQRMaQ_lazyf, {anonymous}::RAQRMaQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage]
/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/IntervalSegTree.hpp:245:7: warning: 'RUQRSQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RUQRSQ_nodef, {anonymous}::RUQRSQ_lazyf, {anonymous}::RUQRSQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage]
/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/IntervalSegTree.hpp:254:7: warning: 'RUQRMiQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RUQRMiQ_nodef, {anonymous}::RUQRMiQ_lazyf, {anonymous}::RUQRMiQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage]
/Users/kaage/Desktop/ProgrammingWorkspace/library/data-structure/IntervalSegTree.hpp:263:7: warning: 'RUQRMaQ' has a base 'IntervalSegTree<long long int, long long int, {anonymous}::RUQRMaQ_nodef, {anonymous}::RUQRMaQ_lazyf, {anonymous}::RUQRMaQ_updf>' whose type uses the anonymous namespace [-Wsubobject-linkage]
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...