93692024-02-21 10:11:21szilMI bróker (50 pont)cpp17Time limit exceeded 4/501.1s6644 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>> indices, points;
        for (int i = 1; i <= n; i++) {
            if (a[i] <= l) {
                indices.insert({i, false});
                points.insert({i, true});
            }
        }
        if (indices.empty()) continue;
        int idx = indices.begin()->first;
        indices.erase(indices.begin());
        int cost = a[idx];
        indices.insert({idx, true});
        for (int r = 500; r >= l+1; r--) {
            for (int i : pos[r]) {
                auto it = indices.lower_bound({i, false});
                if (it != indices.end() && !it->second) {
                    idx = it->first;
                    indices.erase(it);
                    indices.insert({idx, true});
                    cost += a[idx];
                }

                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() && !nxt->second) {
                            cost += a[nxt->first];
                            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
base4/50
1Accepted0/04ms3848 KiB
2Time limit exceeded0/01.1s2776 KiB
3Accepted1/13ms3528 KiB
4Accepted1/14ms4244 KiB
5Accepted2/2136ms5000 KiB
6Time limit exceeded0/21.075s3296 KiB
7Time limit exceeded0/21.057s3452 KiB
8Time limit exceeded0/11.019s6436 KiB
9Time limit exceeded0/11.062s6644 KiB
10Time limit exceeded0/21.059s4012 KiB
11Time limit exceeded0/21.062s4368 KiB
12Time limit exceeded0/21.067s4196 KiB
13Time limit exceeded0/21.075s4124 KiB
14Time limit exceeded0/21.075s4188 KiB
15Time limit exceeded0/31.082s4120 KiB
16Time limit exceeded0/31.07s4184 KiB
17Time limit exceeded0/31.07s4256 KiB
18Time limit exceeded0/31.062s4380 KiB
19Time limit exceeded0/31.075s4436 KiB
20Time limit exceeded0/31.07s4748 KiB
21Time limit exceeded0/31.082s4660 KiB
22Time limit exceeded0/31.05s4856 KiB
23Time limit exceeded0/31.067s4760 KiB
24Time limit exceeded0/31.067s4688 KiB