Submission #1016282

# Submission time Handle Problem Language Result Execution time Memory
1016282 2024-07-07T16:34:19 Z socpite Salesman (IOI09_salesman) C++14
100 / 100
544 ms 65536 KB
#include<bits/stdc++.h>
using namespace std;

const int maxn = 5e5 + 5;
const long long INF = 1e18;

vector<pair<int, int>> ff[maxn];

int U, D, S, n;

long long st[4*maxn][2];
long long mx[maxn];

void upd(long long val, int pos, int l = 1, int r = maxn - 1, int id = 1){
    if(l == r){
        st[id][0] = val - D*pos;
        st[id][1] = val + U*pos;
    }
    else {
        int mid = (l+r)>>1;
        if(pos <= mid)upd(val, pos, l, mid, id<<1);
        else upd(val, pos, mid+1, r, id<<1|1);
        st[id][0] = max(st[id<<1][0], st[id<<1|1][0]);
        st[id][1] = max(st[id<<1][1], st[id<<1|1][1]);
    }
}

long long gt(int ql, int qr, int ty, int l = 1, int r = maxn - 1, int id = 1){
    if(l > qr || ql > r)return -INF;
    if(ql <= l && r <= qr){
        return st[id][ty];
    }
    int mid = (l+r)>>1;
    return max(gt(ql, qr, ty, l, mid, id<<1), gt(ql, qr, ty, mid+1, r, id<<1|1));
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> D >> U >> S;
    for(int i = 0; i < 4*maxn; i++)st[i][0] = st[i][1] = -INF;
    for(int i = 0; i < maxn; i++)mx[i] = -INF;

    mx[S] = 0;
    upd(0, S);

    for(int i = 0; i < n; i++){
        int t, l, m;
        cin >> t >> l >> m;
        ff[t].push_back({l, m});
    }
    for(int t = 0; t < maxn; t++){
        auto vec = ff[t];
        int sz = vec.size();
        sort(vec.begin(), vec.end());
        long long best_back = -INF, best = -INF, sum = 0;
        for(int i = 0; i < sz; i++){
            int prv;
            if(i)prv = vec[i-1].first+1;
            else prv = 1;
            best = max(best, gt(prv, vec[i].first, 1) - sum);
            best_back = max(best_back, -sum + (U+D)*vec[i].first);

            sum += vec[i].second;
            mx[vec[i].first] = max(mx[vec[i].first], best - U*vec[i].first + sum);

            if(i+1 == sz)break;
            int nxt = vec[i+1].first - 1;
            best = max(best, best_back + gt(vec[i].first, nxt, 0));
        }

        best_back = -INF, best = -INF, sum = 0;
        for(int i = sz-1; i >= 0; i--){
            int prv;
            if(i + 1 < sz)prv = vec[i+1].first-1;
            else prv = maxn-1;
            best = max(best, gt(vec[i].first, prv, 0) - sum);
            best_back = max(best_back, -sum - (U+D)*vec[i].first);

            sum += vec[i].second;
            mx[vec[i].first] = max(mx[vec[i].first], best + D*vec[i].first + sum);

            if(!i)break;
            int nxt = vec[i-1].first + 1;
            best = max(best, best_back + gt(nxt, vec[i].first, 1));
        }
        for(auto v: vec){
            upd(mx[v.first], v.first);
        }
    }
    long long ans = -INF;
    for(int i = 0; i < S; i++)ans = max(ans, -U*(S-i) + mx[i]);
    for(int i = S; i < maxn-1; i++)ans = max(ans, -D*(i - S) + mx[i]);
    cout << ans;
}

// 23*8
// 0 is -D, 1 is +U
# Verdict Execution time Memory Grader output
1 Correct 7 ms 47192 KB Output is correct
2 Correct 8 ms 47196 KB Output is correct
3 Correct 8 ms 47196 KB Output is correct
4 Correct 9 ms 47488 KB Output is correct
5 Correct 12 ms 47504 KB Output is correct
6 Correct 34 ms 47964 KB Output is correct
7 Correct 57 ms 48860 KB Output is correct
8 Correct 100 ms 50536 KB Output is correct
9 Correct 133 ms 51968 KB Output is correct
10 Correct 256 ms 56656 KB Output is correct
11 Correct 279 ms 56728 KB Output is correct
12 Correct 403 ms 60016 KB Output is correct
13 Correct 344 ms 60688 KB Output is correct
14 Correct 442 ms 65536 KB Output is correct
15 Correct 477 ms 65536 KB Output is correct
16 Correct 443 ms 65536 KB Output is correct
17 Correct 10 ms 47192 KB Output is correct
18 Correct 10 ms 47196 KB Output is correct
19 Correct 10 ms 47192 KB Output is correct
20 Correct 10 ms 47464 KB Output is correct
21 Correct 10 ms 47452 KB Output is correct
22 Correct 13 ms 47500 KB Output is correct
23 Correct 12 ms 47448 KB Output is correct
24 Correct 12 ms 47452 KB Output is correct
25 Correct 124 ms 48492 KB Output is correct
26 Correct 235 ms 49744 KB Output is correct
27 Correct 377 ms 51456 KB Output is correct
28 Correct 430 ms 51520 KB Output is correct
29 Correct 544 ms 52304 KB Output is correct
30 Correct 513 ms 53216 KB Output is correct