Submission #53340353


Source Code Expand

// #pragma GCC optimize("Ofast,O3,unroll-loops")
// #pragma GCC target("avx,avx2")

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
#define bit(x, i) (((x) >> (i)) & 1)
//#define endl '\n'

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 2e5 + 50;

template <typename T, T neutral_element, class F = function<T(const T&, const T&)>>
struct SegmentTree{
    int n = 0;
    vector<T> tree;
    F func;

    SegmentTree(){};
    SegmentTree(int _n, const F& f){
        n = _n;
        func = f;
        tree.assign(n * 4 + 5, neutral_element);
    }

    inline void build(int v, int tl, int tr, const vector<T> &a){
        if (tl == tr){
            tree[v] = a[tl];
            return;
        }
        int tm = (tl + tr) >> 1;
        build(v << 1, tl, tm, a);
        build(v << 1 | 1, tm + 1, tr, a);
        tree[v] = func(tree[v << 1], tree[v << 1 | 1]);
    }

    inline void init(const vector<T> &a, const F& f){
        n = (int)a.size();
        func = f;
        tree.assign(n * 4 + 5, neutral_element);
        build(1, 0, n - 1, a);
    }

    inline void update(int v, int tl, int tr, int pos, T val){
        if (tl == tr){
            tree[v] = max(tree[v], val);
            return;
        }
        int tm = (tl + tr) >> 1;
        if (pos <= tm){
            update(v << 1, tl, tm, pos, val);
        } else {
            update(v << 1 | 1, tm + 1, tr, pos, val);
        }
        tree[v] = func(tree[v << 1], tree[v << 1 | 1]);
    }

    inline void update(int pos, T val){
        update(1, 0, n - 1, pos, val);
    }

    inline T get(int v, int tl, int tr, int l, int r){
        if (l <= tl && tr <= r) return tree[v];
        if (tl > r || tr < l) return neutral_element;
        int tm = (tl + tr) >> 1;
        return func(get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
    }

    inline T get(int l, int r){
        return get(1, 0, n - 1, l, r);
    }

};

/*
vector<int> a = {1, 2, 5, 3, 4};
SegmentTree<int, INT_MIN> st(a, [&](int i, int j){ return max(i, j); });

SegmentTree<int, 0> st(100, [&](int i, int j){ return i + j; });
*/

void solve() {
    int n, m; long long C;
    cin >> n >> C >> m;

    const long long INF = 4e18;

    SegmentTree<long long, -INF> st1(n, [&](long long x, long long y){return max(x, y);});
    SegmentTree<long long, -INF> st2(n, [&](long long x, long long y){return max(x, y);});

    st1.update(0, 0);
    st2.update(0, 0);

    long long ans = 0;

    while (m--) {
        int t; long long p;
        cin >> t >> p;
        --t;

        long long val1 = p - C * t + st1.get(0, t);
        long long val2 = p + C * t + st2.get(t, n - 1);
        long long val = max(val1, val2);

        st1.update(t, val + C * t);
        st2.update(t, val - C * t);

        ans = max(ans, val);
    }

    cout << ans << endl;

}

int main(){
    clock_t startTime = clock();
    ios_base::sync_with_stdio(false);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int test_cases = 1;
    // cin >> test_cases;

    for (int test = 1; test <= test_cases; test++){
        // cout << (solve() ? "YES" : "NO") << endl;
        solve();
    }

#ifdef LOCAL
    cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif

    return 0;
}

Submission Info

Submission Time
Task G - Merchant Takahashi
User dxz05
Language C++ 20 (gcc 12.2)
Score 550
Code Size 3725 Byte
Status AC
Exec Time 145 ms
Memory 16044 KiB

Compile Error

Main.cpp: In function ‘int main()’:
Main.cpp:125:13: warning: unused variable ‘startTime’ [-Wunused-variable]
  125 |     clock_t startTime = clock();
      |             ^~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 4
AC × 69
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 01_random_64.txt, 01_random_65.txt, 01_random_66.txt, 01_random_67.txt, 01_random_68.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3480 KiB
00_sample_01.txt AC 1 ms 3512 KiB
00_sample_02.txt AC 1 ms 3592 KiB
00_sample_03.txt AC 1 ms 3572 KiB
01_random_04.txt AC 132 ms 15952 KiB
01_random_05.txt AC 132 ms 15548 KiB
01_random_06.txt AC 136 ms 15960 KiB
01_random_07.txt AC 141 ms 15844 KiB
01_random_08.txt AC 142 ms 15828 KiB
01_random_09.txt AC 145 ms 15552 KiB
01_random_10.txt AC 131 ms 15840 KiB
01_random_11.txt AC 132 ms 15880 KiB
01_random_12.txt AC 136 ms 15528 KiB
01_random_13.txt AC 143 ms 15828 KiB
01_random_14.txt AC 143 ms 15532 KiB
01_random_15.txt AC 142 ms 15840 KiB
01_random_16.txt AC 132 ms 15588 KiB
01_random_17.txt AC 133 ms 16040 KiB
01_random_18.txt AC 136 ms 15924 KiB
01_random_19.txt AC 143 ms 15544 KiB
01_random_20.txt AC 143 ms 15876 KiB
01_random_21.txt AC 142 ms 15868 KiB
01_random_22.txt AC 131 ms 15572 KiB
01_random_23.txt AC 132 ms 15548 KiB
01_random_24.txt AC 137 ms 15880 KiB
01_random_25.txt AC 142 ms 15836 KiB
01_random_26.txt AC 141 ms 15532 KiB
01_random_27.txt AC 141 ms 15548 KiB
01_random_28.txt AC 131 ms 15912 KiB
01_random_29.txt AC 133 ms 15880 KiB
01_random_30.txt AC 135 ms 15556 KiB
01_random_31.txt AC 145 ms 15920 KiB
01_random_32.txt AC 142 ms 15536 KiB
01_random_33.txt AC 141 ms 15916 KiB
01_random_34.txt AC 132 ms 15540 KiB
01_random_35.txt AC 132 ms 15592 KiB
01_random_36.txt AC 137 ms 15572 KiB
01_random_37.txt AC 142 ms 15576 KiB
01_random_38.txt AC 143 ms 15572 KiB
01_random_39.txt AC 142 ms 15872 KiB
01_random_40.txt AC 134 ms 16044 KiB
01_random_41.txt AC 132 ms 15924 KiB
01_random_42.txt AC 135 ms 15840 KiB
01_random_43.txt AC 142 ms 15912 KiB
01_random_44.txt AC 141 ms 15912 KiB
01_random_45.txt AC 142 ms 15532 KiB
01_random_46.txt AC 132 ms 15840 KiB
01_random_47.txt AC 133 ms 15532 KiB
01_random_48.txt AC 136 ms 15532 KiB
01_random_49.txt AC 142 ms 15952 KiB
01_random_50.txt AC 143 ms 15824 KiB
01_random_51.txt AC 141 ms 15912 KiB
01_random_52.txt AC 21 ms 8028 KiB
01_random_53.txt AC 74 ms 11044 KiB
01_random_54.txt AC 126 ms 9752 KiB
01_random_55.txt AC 17 ms 3676 KiB
01_random_56.txt AC 61 ms 11172 KiB
01_random_57.txt AC 17 ms 8196 KiB
01_random_58.txt AC 46 ms 13736 KiB
01_random_59.txt AC 67 ms 9840 KiB
01_random_60.txt AC 137 ms 15296 KiB
01_random_61.txt AC 64 ms 15836 KiB
01_random_62.txt AC 40 ms 5816 KiB
01_random_63.txt AC 60 ms 15820 KiB
01_random_64.txt AC 122 ms 13012 KiB
01_random_65.txt AC 16 ms 12420 KiB
01_random_66.txt AC 88 ms 4304 KiB
01_random_67.txt AC 23 ms 3508 KiB
01_random_68.txt AC 23 ms 3572 KiB