94992024-02-22 12:19:28zeytonxMI bróker (50 pont)cpp17Time limit exceeded 6/501.088s116456 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define endl "\n"
#define pll pair<ll,ll>
#define vll vector<ll>
#define mll map<ll,ll>
#define fs first
#define sc second

const ll MOD = 1e9+7;

void solve()
{
	ll n, q;
	cin >> n >> q;
	vector<ll> p(n);
	
	map<ll, vll> lw, bg;

	for(ll i = 0; i < n; i++)
	{
		cin >> p[i];
		for(ll j = p[i]; j <= 500; j++)
			lw[j].push_back(i);
		for(ll j = 0; j <= p[i]; j++)
			bg[j].push_back(i);
	}

	vector<pll> qr(q);
	for(auto &i : qr)
		cin >> i.fs >> i.sc;
	
	for(auto i : qr)
	{
		ll ans = 0;
		ll i1 = 0, i2 = 0;

		while(true)
		{
			ans -= p[lw[i.fs][i1]];
			i1++;
			while(i2 < bg[i.sc].size() && lw[i.fs][i1-1] >= bg[i.sc][i2])
				i2++;
			if(i2 == bg[i.sc].size())
				break;
			ans += p[bg[i.sc][i2]];
			i2++;
			while(i1 < lw[i.fs].size() && lw[i.fs][i1] <= bg[i.sc][i2-1])
				i1++;
			if(i1 == lw[i.fs].size())
				break;
		}
		
		cout << ans << "\n";
	}
	cout << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll t = 1;
    //cin >> t;
    while(t--)
        solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base6/50
1Accepted0/03ms2060 KiB
2Time limit exceeded0/01.08s49700 KiB
3Runtime error0/13ms2528 KiB
4Runtime error0/13ms2796 KiB
5Accepted2/239ms14164 KiB
6Accepted2/2358ms108068 KiB
7Accepted2/2356ms108556 KiB
8Runtime error0/1300ms113852 KiB
9Runtime error0/1331ms114716 KiB
10Time limit exceeded0/21.057s59132 KiB
11Runtime error0/2372ms116328 KiB
12Runtime error0/2386ms116132 KiB
13Runtime error0/2400ms116456 KiB
14Runtime error0/2379ms115952 KiB
15Time limit exceeded0/31.069s57376 KiB
16Time limit exceeded0/31.049s57696 KiB
17Time limit exceeded0/31.07s58412 KiB
18Time limit exceeded0/31.05s57872 KiB
19Time limit exceeded0/31.062s58180 KiB
20Time limit exceeded0/31.065s58564 KiB
21Time limit exceeded0/31.06s59104 KiB
22Time limit exceeded0/31.078s59152 KiB
23Time limit exceeded0/31.067s58384 KiB
24Time limit exceeded0/31.088s58348 KiB