제출 #1365945

#제출 시각아이디문제언어결과실행 시간메모리
1365945gelastropodTeleporter 2 (JOI26_teleporter)C++20
100 / 100
2277 ms19984 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

vector<pair<pair<int, int>, int>> vals;
vector<vector<pair<int, int>>> mem;

int N, M, K;

struct node {
    int s, e, m, v, v1, lazy;
    node* l, * r;

    node(int _s, int _e) : s(_s), e(_e), m((_s + _e) / 2), v(1e15), v1(0), lazy(0) {
        if (s != e)
            l = new node(s, m),
            r = new node(m + 1, e);
    }

    void prop() {
        if (s == e || lazy == 0) return;
        l->v += lazy;
        r->v += lazy;
        l->lazy += lazy;
        r->lazy += lazy;
        lazy = 0;
    }

    void reset() {
        v = 1e15;
        v1 = 0;
        lazy = 0;
        if (s == e) return;
        l->reset();
        r->reset();
    }

    void upd(int a, int b, int x) {
        if (b < s || a > e) return;
        if (a <= s && b >= e) {
            v += x;
            lazy += x;
            return;
        }
        prop();
        l->upd(a, b, x);
        r->upd(a, b, x);
        v = min(l->v, r->v);
        if (l->v < r->v) v1 = l->v1;
        else if (l->v > r->v) v1 = r->v1;
        else v1 = min(l->v1, r->v1);
    }

    void upds(int n, int x, int o) {
        if (s == e) {
            if (x < v) {
                v = x;
                v1 = o;
            }
            return;
        }
        prop();
        if (n <= m) l->upds(n, x, o);
        else r->upds(n, x, o);
        v = min(l->v, r->v);
        if (l->v < r->v) v1 = l->v1;
        else if (l->v > r->v) v1 = r->v1;
        else v1 = min(l->v1, r->v1);
    }

    pair<int, int> qry(int a, int b) {
        if (b < s || a > e) return { 1e15, 0 };
        if (a <= s && b >= e) return { v, v1 };
        prop();
        auto resl = l->qry(a, b), resr = r->qry(a, b);
        if (resl.first < resr.first) return resl;
        if (resl.first > resr.first) return resr;
        return { resl.first, min(resl.second, resr.second) };
    }
} *root;

pair<int, int> relax(int lambda) {
    root->reset();
    root->upds(0, 0, 0);
    vector<pair<int, int>> mem(M, { 0, 0 });
    for (int i = 0; i < M; i++) {
        mem[i] = root->qry(0, vals[i].first.second);
        root->upd(0, vals[i].first.second, vals[i].second);
        mem[i].first += lambda;
        mem[i].first = max(mem[i].first, (int)-1e15);
        mem[i].second++;
        root->upds(vals[i].first.first, mem[i].first, mem[i].second);
    }
    return root->qry(0, N - 1);
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int a, b, c; cin >> N >> M >> K;
    for (int i = 0; i < M; i++) {
        cin >> a >> b >> c; a--, b--;
        vals.push_back({ {b, a}, c });
    }
    sort(vals.begin(), vals.end());
    root = new node(0, N - 1);
    int low = 0, high = 1e15, ans = -1;
    while (low <= high) {
        int x = (low + high) / 2;
        if (relax(x).second <= K) high = x - 1, ans = x;
        else low = x + 1;
    }
    auto res = relax(ans);
    cout << res.first - ans * K << '\n';
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…