93752024-02-21 10:39:07szilMI bróker (50 pont)cpp17Accepted 50/50995ms7148 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]) {
                if (*points.begin() < i) {
                    auto bef = points.lower_bound(i);
                    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
base50/50
1Accepted0/04ms3812 KiB
2Accepted0/0865ms5196 KiB
3Accepted1/13ms3416 KiB
4Accepted1/13ms4004 KiB
5Accepted2/272ms4800 KiB
6Accepted2/2948ms5780 KiB
7Accepted2/2947ms5844 KiB
8Accepted1/1441ms5320 KiB
9Accepted1/1514ms5416 KiB
10Accepted2/2856ms6364 KiB
11Accepted2/2731ms6284 KiB
12Accepted2/2810ms6656 KiB
13Accepted2/2815ms6696 KiB
14Accepted2/2777ms6632 KiB
15Accepted3/3995ms6828 KiB
16Accepted3/3990ms6812 KiB
17Accepted3/3991ms7092 KiB
18Accepted3/3986ms7032 KiB
19Accepted3/3990ms7028 KiB
20Accepted3/3989ms7024 KiB
21Accepted3/3992ms7140 KiB
22Accepted3/3986ms7140 KiB
23Accepted3/3987ms7140 KiB
24Accepted3/3987ms7148 KiB