結果
問題 | No.1194 Replace |
ユーザー | hitonanode |
提出日時 | 2020-08-22 14:43:00 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 315 ms / 2,000 ms |
コード長 | 12,172 bytes |
コンパイル時間 | 2,627 ms |
コンパイル使用メモリ | 225,572 KB |
実行使用メモリ | 67,356 KB |
最終ジャッジ日時 | 2024-10-15 08:56:58 |
合計ジャッジ時間 | 9,179 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 213 ms
43,648 KB |
testcase_01 | AC | 236 ms
46,284 KB |
testcase_02 | AC | 186 ms
38,132 KB |
testcase_03 | AC | 142 ms
33,020 KB |
testcase_04 | AC | 227 ms
46,396 KB |
testcase_05 | AC | 210 ms
43,232 KB |
testcase_06 | AC | 192 ms
40,752 KB |
testcase_07 | AC | 312 ms
67,096 KB |
testcase_08 | AC | 312 ms
67,232 KB |
testcase_09 | AC | 305 ms
67,228 KB |
testcase_10 | AC | 306 ms
67,356 KB |
testcase_11 | AC | 315 ms
67,356 KB |
testcase_12 | AC | 310 ms
67,356 KB |
testcase_13 | AC | 183 ms
26,808 KB |
testcase_14 | AC | 157 ms
22,428 KB |
testcase_15 | AC | 128 ms
24,488 KB |
testcase_16 | AC | 179 ms
26,908 KB |
testcase_17 | AC | 118 ms
22,336 KB |
testcase_18 | AC | 124 ms
19,944 KB |
testcase_19 | AC | 175 ms
27,984 KB |
testcase_20 | AC | 2 ms
5,248 KB |
testcase_21 | AC | 2 ms
5,248 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 55 ms
12,732 KB |
testcase_24 | AC | 18 ms
7,552 KB |
testcase_25 | AC | 10 ms
5,248 KB |
testcase_26 | AC | 67 ms
11,248 KB |
testcase_27 | AC | 13 ms
5,248 KB |
testcase_28 | AC | 31 ms
7,128 KB |
testcase_29 | AC | 5 ms
5,248 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using lint = long long; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template <typename V, typename T> void ndfill(V &x, const T &val) { x = val; } template <typename V, typename T> void ndfill(vector<V> &vec, const T &val) { for (auto &v : vec) ndfill(v, val); } template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; } #endif template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl #else #define dbg(x) #endif // Fibonacci heap // - Complexity: // - empty(), size(), top(), push(), meld(): O(1) // - pop(), decrease(): O(lg N) amortized // - Reference: // - "Introduction to Algorithms, Third Edition", Chapter 19 // - <https://www.cs.princeton.edu/~wayne/teaching/fibonacci-heap.pdf> // - <https://rsk0315.hatenablog.com/entry/2019/10/29/151823> template <typename Tp> struct fibonacci_heap { struct Node { Tp val; int deg; Node *parent, *left, *right, *child; bool mark; Node() = default; Node(Node const &) = default; Node(Node &&) = default; Node &operator=(Node const &) = default; Node &operator=(Node &&) = default; Node(Tp v) : val(v), deg(0), parent(nullptr), left(nullptr), right(nullptr), child(nullptr), mark(false) {} friend std::ostream &operator<<(std::ostream &os, const Node &n) { os << '(' << n.val << ','; if (n.child != nullptr) os << *(n.child) << ','; Node *now = n.right; while (now != &n) { os << now->val << ','; if (now->child != nullptr) os << *now->child << ','; now = now->right; } os << ')'; return os; } }; int sz; std::list<Node*> roots; Node *ptop; inline void _chmin(Node *cand) noexcept { if (ptop == nullptr or cand->val < ptop->val) ptop = cand; } fibonacci_heap() : sz(0), roots({}), ptop(nullptr) {} bool empty() const noexcept { return sz == 0; } int size() const noexcept { return sz; } void _consolidate() { std::array<Node *, 30> arr; arr.fill(nullptr); auto fmerge = [&](auto &&func, Node *ptr) -> void { int d = ptr->deg; if (arr[d] == nullptr) arr[d] = ptr; else { Node *cptr = arr[d]; if (cptr->val < ptr->val) std::swap(ptr, cptr); ptr->deg++; cptr->parent = ptr; if (ptr->child == nullptr) ptr->child = cptr; else { Node *cl = ptr->child, *cr = ptr->child->right; assert(cl->right == cr and cr->left == cl); cptr->left = cl, cptr->right = cr, cl->right = cr->left = cptr; } arr[d] = nullptr; func(func, ptr); } }; for (auto ptr : roots) if (ptr != nullptr) { if (ptr->deg < 0) delete ptr; else fmerge(fmerge, ptr); } roots.clear(), ptop = nullptr; for (auto ptr : arr) if (ptr != nullptr) _add_tree(ptr); } void _add_tree(Node *root) noexcept { root->parent = nullptr; root->left = root->right = root; roots.emplace_back(root); _chmin(root); } Node *push(const Tp &val) noexcept { sz++; Node *ptr = new Node(val); _add_tree(ptr); return ptr; } void meld(fibonacci_heap &&hp) { sz += hp.sz; roots.splice(roots.end(), hp.roots); if (hp.ptop != nullptr) _chmin(hp.ptop); } void pop() { assert(sz > 0); sz--; Node* ch1 = ptop->child; if (ch1 != nullptr) { Node *now = ch1; while (true) { Node *nxt = now->right; _add_tree(now); now = nxt; if (now == ch1) break; } } ptop->deg = -1; _consolidate(); } void clear() { auto deldfs = [&](auto &&f, Node *now) -> void { while (now != nullptr) { if (now->child != nullptr) f(f, now->child); Node *nxt = now->right; delete now; now = nxt; } }; for (auto root : roots) { deldfs(deldfs, root); } sz = 0; roots.clear(); ptop = nullptr; } void _cut(Node *x) noexcept { Node *y = x->parent; assert(y != nullptr and y->deg > 0); Node *xr = x->right, *xl = x->left; if (x == xr) { y->child = nullptr; } else { y->child = xr; xl->right = xr, xr->left = xl; } y->deg--; _add_tree(x); x->mark = false; } void _cascading_cut(Node *now) noexcept { assert(now != nullptr); Node *par = now->parent; if (par == nullptr) return; if (!now->mark) now->mark = true; else { _cut(now); _cascading_cut(par); } } void erase(Node *r) { if (r->parent != nullptr) { Node *rpar = r->parent; _cut(r); _cascading_cut(rpar); } ptop = r; pop(); } bool decrease(Node *r, const Tp new_val) { assert(r != nullptr); if (!(new_val < r->val)) return false; r->val = new_val; if (r->parent != nullptr and new_val < r->parent->val) { Node *rpar = r->parent; _cut(r); _cascading_cut(rpar); } _chmin(r); return true; } Tp top() const { assert(ptop != nullptr); return ptop->val; } friend std::ostream &operator<<(std::ostream &os, const fibonacci_heap &hp) { os << "[(fibonacci_heap: sz=" << hp.sz << ", top=" << hp.ptop->val << ", #tree = " << hp.roots.size() << ")"; for (auto x : hp.roots) { os << *x << ", "; } os << ']'; return os; } }; #include <utility> #include <vector> template <typename Tp> struct heap { using P = std::pair<Tp, int>; fibonacci_heap<P> _heap; std::vector<typename fibonacci_heap<P>::Node *> vp; std::vector<Tp> result; void initialize(int N, Tp initval) { _heap.clear(); vp.resize(N); result.assign(N, initval); for (int i = 0; i < N; i++) { vp[i] = _heap.push(std::make_pair(initval, i)); } } heap(int N, Tp initval) { initialize(N, initval); } bool chmin(int i, Tp val) { if (val < result[i]) { result[i] = val; if (vp[i] == nullptr) { vp[i] = _heap.push(std::make_pair(result[i], i)); } else { _heap.decrease(vp[i], std::make_pair(result[i], i)); } return true; } return false; } Tp operator[](int i) const { return result.at(i); } P top() { return _heap.top(); } P pop() { P ret = _heap.top(); _heap.pop(); return ret; } int size() { return _heap.size(); } bool empty() { return _heap.empty(); } }; int main() { int N, M; cin >> N >> M; vector<int> B(M), C(M); REP(i, M) cin >> B[i] >> C[i]; vector<int> Z = B; Z.insert(Z.end(), ALL(C)); Z = srtunq(Z); int V = Z.size(); vector<vector<pint>> to(V); heap<int> hp(V, 0); REP(i, M) { int s = lower_bound(ALL(Z), B[i]) - Z.begin(); int t = lower_bound(ALL(Z), C[i]) - Z.begin(); to[t].emplace_back(s, B[i] - C[i]); hp.chmin(s, B[i] - C[i]); } vector<int> visited(V); vector<int> result(V); queue<int> q; IREP(i, V) if (!visited[i]) { q.emplace(i); while (q.size()) { int now = q.front(); visited[now] = true; q.pop(); for (auto [j, adv] : to[now]) if (!visited[j]) { result[j] = result[now] + adv; q.emplace(j); } } } dbg(to); int last_idx = -1; // while (hp.size()) // { // auto [val, i] = hp.top(); // if (i == last_idx) // { // hp.pop(); // continue; // } // last_idx = i; // for (auto [j, adv] : to[i]) // { // hp.chmin(j, val + adv); // } // } // dbg(hp.result); cout << 1LL * (1 + N) * N / 2 - accumulate(ALL(result), 0LL) << '\n'; }