91452024-02-15 21:52:43UVinceMI bróker (50 pont)cpp17Time limit exceeded 0/501.082s4352 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<int> v(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i];
    }
    vector<int> mi(n, -1), ma(n, -1);
    stack<pair<int, int>> minst;
    for (int i = n - 1; i >= 0; i--)
    {
        if (!minst.empty())
            mi[i] = minst.top().second;
        while (!minst.empty() && minst.top().first >= v[i])
        {
            minst.pop();
        }
        minst.push({v[i], i});
    }
    stack<pair<int, int>> maxt;
    for (int i = n - 1; i >= 0; i--)
    {
        if (!maxt.empty())
            ma[i] = maxt.top().second;
        while (!maxt.empty() && maxt.top().first <= v[i])
        {
            maxt.pop();
        }
        maxt.push({v[i], i});
    }
    for (int i = 0; i < q; i++)
    {
        int a, b;
        cin >> a >> b;
        int win = 0;
        int cur = 0;
        bool own = false;
        while (cur != -1 && cur < n)
        {
            if (!own)
            {
                while (cur != -1 && v[cur] > a)
                    cur = mi[cur];
                if (v[cur] <= a)
                {
                    win -= v[cur];
                    own = true;
                    cur++;
                }
            }
            else
            {
                while (cur != -1 && v[cur] < b)
                    cur = ma[cur];
                if (v[cur] >= b)
                {
                    win += v[cur];
                    own = false;
                    cur++;
                }
            }
        }
        cout << win << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
base0/50
1Time limit exceeded0/01.05s1804 KiB
2Time limit exceeded0/01.077s2224 KiB
3Time limit exceeded0/11.07s2208 KiB
4Time limit exceeded0/11.041s2304 KiB
5Time limit exceeded0/21.06s2700 KiB
6Time limit exceeded0/21.065s2812 KiB
7Time limit exceeded0/21.049s3016 KiB
8Time limit exceeded0/11.065s3224 KiB
9Time limit exceeded0/11.054s3296 KiB
10Time limit exceeded0/21.054s2468 KiB
11Time limit exceeded0/21.078s3420 KiB
12Time limit exceeded0/21.029s2680 KiB
13Time limit exceeded0/21.062s3744 KiB
14Time limit exceeded0/21.075s3868 KiB
15Time limit exceeded0/31.059s3112 KiB
16Time limit exceeded0/31.075s4064 KiB
17Time limit exceeded0/31.067s4188 KiB
18Time limit exceeded0/31.062s4244 KiB
19Time limit exceeded0/31.062s3320 KiB
20Time limit exceeded0/31.074s4208 KiB
21Time limit exceeded0/31.065s4352 KiB
22Time limit exceeded0/31.082s4336 KiB
23Time limit exceeded0/31.072s3384 KiB
24Time limit exceeded0/31.075s4236 KiB