Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- struct vertex
- {
- int in, out, cost;
- };
- using heap = priority_queue<ll>;
- void combine (heap *to_ptr, heap *what_ptr) {
- heap &to = *to_ptr;
- heap &what = *what_ptr;
- if (to.size() < what.size())
- swap(to, what);
- vector<ll> to_push;
- while (!what.empty())
- {
- ll x = what.top();
- ll y = to.top();
- what.pop(); to.pop();
- to_push.push_back(x + y);
- }
- for (ll x : to_push)
- to.push(x);
- delete what_ptr;
- }
- bool in (const vertex &inner, const vertex &outer)
- {
- return outer.in <= inner.in && inner.out <= outer.out;
- }
- heap* dfs (int v, int &ptr, const vector<vertex> &vs)
- {
- heap* ans = new heap();
- while (ptr < int(vs.size()) && in(vs[ptr], vs[v]))
- {
- ptr++;
- auto son = dfs(ptr - 1, ptr, vs);
- combine(ans, son);
- }
- ans->push(vs[v].cost);
- return ans;
- }
- void solve(istream &cin = std::cin, ostream &cout = std::cout) {
- auto solve_test = [&]
- {
- int n;
- if (!(cin >> n))
- return false;
- const int bnd = int(1e6) + 1;
- vector<vertex> vs(n);
- for (auto &p : vs)
- cin >> p.in >> p.out >> p.cost;
- vs.push_back(vertex{0, bnd, 0});
- sort(vs.begin(), vs.end(), [&] (const vertex &l, const vertex &r)
- {
- return (l.in < r.in) || (l.in == r.in && l.out > r.out);
- });
- int ptr = 1;
- auto *ans = dfs(0, ptr, vs);
- vector<ll> ans_vector;
- while (!ans->empty())
- ans_vector.push_back(ans->top()), ans->pop();
- assert(!ans_vector.empty());
- while (int(ans_vector.size()) > n)
- ans_vector.pop_back();
- for (int i = 1; i < int(ans_vector.size()); i++)
- ans_vector[i] += ans_vector[i - 1];
- while (int(ans_vector.size()) < n)
- ans_vector.push_back(ans_vector.back());
- for (ll it : ans_vector)
- cout << it << ' ';
- cout << endl;
- };
- while (solve_test())
- {
- break;
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout << fixed;
- #ifdef LOCAL
- auto st = clock();
- ifstream fin("../input.txt");
- solve(fin);
- cout << "clock: " << setprecision(4) << (clock() - st) / (double) CLOCKS_PER_SEC << endl;
- #else
- solve();
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement