93742024-02-21 10:34:29szilMI bróker (50 pont)cpp17Time limit exceeded 47/501.003s6968 KiB
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

#pragma GCC optimize("O3,unroll-loops")

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<int> points;
        for (int i = 1; i <= n; i++) {
            if (a[i] <= l) {
                points.insert(i);
            }
        }
        if (points.empty()) continue;
        int cost = -a[*points.begin()];
        for (int r = 500; r >= l+1; r--) {
            for (int i : pos[r]) {
                auto bef = points.lower_bound(i);
                if (bef != points.begin()) {
                    bef--;
                    if (a[*bef] <= l) {
                        cost += a[i];
                        auto nxt = points.lower_bound(i);
                        if (nxt != points.end()) {
                            cost -= a[*nxt];
                            if (a[*nxt] > l) {
                                points.erase(nxt);
                            }
                        }
                        points.insert(i);
                    }
                }
            }
            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
base47/50
1Accepted0/04ms3944 KiB
2Accepted0/0869ms5352 KiB
3Accepted1/13ms3440 KiB
4Accepted1/13ms3896 KiB
5Accepted2/274ms4648 KiB
6Accepted2/2947ms5876 KiB
7Accepted2/2950ms5836 KiB
8Accepted1/1446ms5456 KiB
9Accepted1/1522ms5468 KiB
10Accepted2/2865ms6092 KiB
11Accepted2/2745ms5880 KiB
12Accepted2/2820ms6032 KiB
13Accepted2/2828ms6080 KiB
14Accepted2/2790ms6164 KiB
15Accepted3/3995ms6548 KiB
16Accepted3/3990ms6544 KiB
17Accepted3/3992ms6548 KiB
18Accepted3/3989ms6608 KiB
19Accepted3/3992ms6540 KiB
20Accepted3/3986ms6532 KiB
21Time limit exceeded0/31.003s6544 KiB
22Accepted3/3989ms6552 KiB
23Accepted3/3984ms6760 KiB
24Accepted3/3990ms6968 KiB