777E - Hanoi Factory - CodeForces Solution


brute force data structures dp greedy sortings *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double
#define el "\n"
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<ll, null_type,less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
const ll N = 2e5 + 5, INF = 1e18;
const ld pi = acos(-1);
const int mod = 1e9 + 7, LOG = 20;
const ld eps = 1e-4;
int dx[] = {0, -1, 0, 1, -1, 1, -1, 1};
int dy[] = { -1, 0, 1, 0, 1, -1, -1, 1};
ll n, m, k, x, y;
struct Node {
    ll val;
} zero;
Node seg[N * 4];

Node merge(Node a, Node b) {
    Node ans;
    ans.val = max(a.val , b.val);
    return ans;
}


Node solve(int idx, int l, int r, int st, int en) {
    if (en < l || st > r) {
        return zero;
    }
    if (st >= l && en <= r) {
        return seg[idx];
    }
    int mid = (st + en) / 2;
    return merge(solve(idx * 2, l, r, st, mid), solve(idx * 2 + 1, l, r, mid + 1, en));
}

void update(int idx, int l, int r, int st, int en, ll val) {
    if (st > r || en < l) {
        return;
    }
    if (st >= l && en <= r) {
        seg[idx].val = val;
        return;
    }
    int mid = (st + en) / 2;
    update(idx * 2, l, r, st, mid, val);
    update(idx * 2 + 1, l, r, mid + 1, en, val);
    seg[idx] = merge(seg[idx * 2], seg[idx * 2 + 1]);
}
struct item
{
    int a, b, h;
    item() {

    }
    item(int a, int b, int h) : a(a), b(b), h(h) {

    }

    const bool operator<(const item &other) const {
        return make_pair(b, a) < make_pair(other.b, other.a);
    }
};
item a[N];
void dowork() {
    set<int>st;
    zero.val = 0;
    map<int, int>id;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x >> y >> m;
        a[i] = item(x, y, m);
        st.insert(x);
        st.insert(y);
    }
    for (auto j : st) {
        id[j] = id.size() + 1;
    }
    sort(a + 1, a + n + 1);
    for (int i = n; i > 0; i--) {
        //cout << a[i].a << " " << a[i].b << " " << a[i].h << el;
    }
    for (int i = 1; i <= n; i++) {
        a[i].a = id[a[i].a];
        a[i].b = id[a[i].b];
    }
    ll ans, tmp;
    for (int i = n; i > 0; i--) {
        ans = a[i].h;
        if (a[i].b - 1 >= 1) {
            ans += solve(1, 1, a[i].b - 1, 1, 2 * n).val;
        }
        tmp = solve(1, a[i].a, a[i].a, 1, 2 * n).val;
        if (ans > tmp) {
            update(1, a[i].a, a[i].a, 1, 2 * n, ans);
        }
    }
    cout << solve(1, 1, 2 * n, 1, 2 * n).val << el;;

}

signed main() {
    fast
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; i++) {
        dowork();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game