93722024-02-21 10:24:24szilMI bróker (50 pont)cpp17Time limit exceeded 20/501.027s8272 KiB
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

const int MAXN = 10'001;

int a[MAXN], ans[501][501];
vector<int> pos[501];

void solve() {
    int n, m; cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        pos[a[i]].emplace_back(i);
    }
    for (int l = 1; l <= 500; l++) {
        set<pair<int, bool>> points;
        for (int i = 1; i <= n; i++) {
            if (a[i] <= l) {
                points.insert({i, true});
            }
        }
        if (points.empty()) continue;
        int idx = points.begin()->first;
        int cost = a[idx];
        for (int r = 500; r >= l+1; r--) {
            for (int i : pos[r]) {
                auto bef = points.lower_bound({i, false});
                if (bef != points.begin()) {
                    bef--;
                    if (bef->second) {
                        cost -= a[i];
                        auto nxt = points.lower_bound({i, false});
                        if (nxt != points.end()) {
                            cost += a[nxt->first];
                            if (!nxt->second) {
                                points.erase(nxt);
                            }
                        }
                        points.insert({i, false});
                    }
                }
            }
            ans[l][r] = cost;
        }
    }
    while (m--) {
        int a, b; cin >> a >> b;
        cout << -ans[a][b] << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base20/50
1Accepted0/04ms3796 KiB
2Accepted0/0898ms5772 KiB
3Accepted1/13ms4000 KiB
4Accepted1/14ms4716 KiB
5Accepted2/276ms5456 KiB
6Accepted2/2977ms6436 KiB
7Accepted2/2975ms6712 KiB
8Accepted1/1398ms6052 KiB
9Accepted1/1497ms6068 KiB
10Accepted2/2862ms6912 KiB
11Accepted2/2734ms6728 KiB
12Accepted2/2810ms7232 KiB
13Accepted2/2819ms7112 KiB
14Accepted2/2779ms6968 KiB
15Time limit exceeded0/31.027s7180 KiB
16Time limit exceeded0/31.019s7364 KiB
17Time limit exceeded0/31.026s7356 KiB
18Time limit exceeded0/31.014s7584 KiB
19Time limit exceeded0/31.021s7636 KiB
20Time limit exceeded0/31.018s7824 KiB
21Time limit exceeded0/31.024s7828 KiB
22Time limit exceeded0/31.019s7824 KiB
23Time limit exceeded0/31.014s8056 KiB
24Time limit exceeded0/31.019s8272 KiB