답안 #514848

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
514848 2022-01-18T14:53:50 Z Be_dos 금 캐기 (IZhO14_divide) C++17
100 / 100
74 ms 10620 KB
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <unordered_set>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <random>
#include <cfloat>
#include <chrono>
#include <bitset>
#include <complex>
#include <immintrin.h>
#include <cassert>

struct Point {
    int32_t x;
    int32_t gold, energy;
};

std::mt19937 rng;
struct Node {
    Node* left = nullptr, *right = nullptr;
    int32_t priority;
    int64_t key;
    int32_t value;
    int32_t min_value;

    Node(int64_t key_, int32_t value_) {
        key = key_;
        value = value_;
        min_value = value;
        priority = rng() % 1'000'00;
    }
};

void recalc(Node* node) {
    node->min_value = node->value;
    if(node->left != nullptr)
        node->min_value = std::min(node->min_value, node->left->min_value);
    if(node->right != nullptr)
        node->min_value = std::min(node->min_value, node->right->min_value);
}

std::pair<Node*, Node*> split(Node* node, int64_t x) {
    if(node == nullptr)
        return {nullptr, nullptr};
    if(node->key < x) {
        std::pair<Node*, Node*> res = split(node->right, x);
        node->right = res.first;
        recalc(node);
        return {node, res.second};
    } else {
        std::pair<Node*, Node*> res = split(node->left, x);
        node->left = res.second;
        recalc(node);
        return {res.first, node};
    }
}

Node* merge(Node* node1, Node* node2) {
    if(node1 == nullptr)
        return node2;
    if(node2 == nullptr)
        return node1;
    if(node1->priority < node2->priority) {
        node1->right = merge(node1->right, node2);
        recalc(node1);
        return node1;
    } else {
        node2->left = merge(node1, node2->left);
        recalc(node2);
        return node2;
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int32_t n;
    std::cin >> n;

    Point* points = new Point[n];
    for(int32_t i = 0; i < n; i++)
        std::cin >> points[i].x >> points[i].gold >> points[i].energy;

    int64_t* pref_sum = new int64_t[n + 1];
    pref_sum[0] = 0;
    for(int32_t i = 1; i <= n; i++)
        pref_sum[i] = pref_sum[i - 1] + points[i - 1].energy;

    int64_t* gold_pref_sum = new int64_t[n + 1];
    gold_pref_sum[0] = 0;
    for(int32_t i = 1; i <= n; i++)
        gold_pref_sum[i] = gold_pref_sum[i - 1] + points[i - 1].gold;

    int64_t ans = 0;
    Node* root = nullptr;
    for(int32_t i = 0; i < n; i++) {
        std::pair<Node*, Node*> parts = split(root, points[i].x - pref_sum[i]);
        root = merge(merge(parts.first, new Node(points[i].x - pref_sum[i], i)), parts.second);

        int64_t val = points[i].x - pref_sum[i + 1];
        parts = split(root, val);
        ans = std::max(ans, gold_pref_sum[i + 1] - gold_pref_sum[parts.second->min_value]);
        root = merge(parts.first, parts.second);
    }
    std::cout << ans;
    return 0;
}







# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 3 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 3 ms 716 KB Output is correct
24 Correct 3 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 3 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 3 ms 716 KB Output is correct
24 Correct 3 ms 716 KB Output is correct
25 Correct 2 ms 716 KB Output is correct
26 Correct 5 ms 1100 KB Output is correct
27 Correct 6 ms 1228 KB Output is correct
28 Correct 28 ms 4956 KB Output is correct
29 Correct 30 ms 5244 KB Output is correct
30 Correct 64 ms 10620 KB Output is correct
31 Correct 52 ms 9408 KB Output is correct
32 Correct 55 ms 9464 KB Output is correct
33 Correct 54 ms 9268 KB Output is correct
34 Correct 65 ms 9264 KB Output is correct
35 Correct 59 ms 9848 KB Output is correct
36 Correct 74 ms 10044 KB Output is correct