246772026-02-13 13:57:42ubormaciMI bróker (50 pont)cpp17Időlimit túllépés 9/501.09s14140 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
// #pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*

notes:

int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string

vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));

{if statement} ? {truth value} : {false value}

#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

std::lcm(ll a, ll b), std::gcd(int a, int b)

cout << fixed << setprecision(n);

set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);

*/

typedef int32_t ll;
typedef int16_t ls;

void solve() {

	ll n, q;
	cin >> n >> q;

	vector<ls> v(n+1, 0);

	vector<vector<ls>> up(501);
	vector<vector<ls>> down(501);
	for(ll i = 1; i <= n; i++) {
		ls h;
		cin >> h;
		up[h].push_back(i);
		down[h].push_back(i);
		v[i] = h;
	}

	for(ll i = 499; i >= 1; i--) {
		for(const auto & x : up[i+1]) {
			up[i].push_back(x);
		}
		sort(up[i].begin(), up[i].end());
	}
	for(ll i = 2; i <= 500; i++) {
		for(const auto & x : down[i-1]) {
			down[i].push_back(x);
		}
		sort(down[i].begin(), down[i].end());
	}

	//cerr << "\nup, down handled";

	for(ll qi = 0; qi < q; qi++) {

		ls mxbuy, mnsell;
		cin >> mxbuy >> mnsell;

		//cerr << "\nqi=" << qi << "; mxbuy=" << mxbuy << "; mnsell=" << mnsell;

		ll ans = 0;

		ll curr = 0;
		bool h = false;

		while(curr <= n) {

			//cerr << "\ncurr=" << curr;

			if(curr == 0) {
				
				//cerr << "\nwe're at the very beginning, how joyful";
				if(down[mxbuy].empty()) {
					//cerr << "\nwe can't EVER buy it? a pity";
					break;
				}

				//cerr << "\nwe can buy it at the following moments: " << down[mxbuy];

				curr = down[mxbuy][0];
				h = true;
				ans -= v[curr];

				//cerr << "\nwe can buy it at " << curr;

				continue;
			}

			if(h) {

				//cerr << "\nand now I have it";

				// has it, wants to sell
				// searches for first moment where prices is mnsell or more

				if(up[mnsell].empty()) {
					//cerr << "\nand I can't ever sell it, great";
					break;
				}

				if(curr > up[mnsell].back())
				{
					//cerr << "\nor I can't see it anymore";
					// can't sell any more	
					curr = n+1;
					break;
				}else{

					//cerr << "\nall the moments when I'll be able to sell it: " << up[mnsell];
					//cerr << "\ncurr, again=" << curr;
					
					ll nxt = upper_bound(up[mnsell].begin(), up[mnsell].end(), curr) - up[mnsell].begin();
					//cerr << "\nnxt=" << nxt;

					curr = up[mnsell][nxt];

					//curr = *up[mnsell].upper_bound(curr);
					h = false;
					ans += v[curr];
					//cerr << "\nI've sold it at time " << curr;
				}
			}else{
				// doesn't have, wants to buy

				// searches for first moment where prices is mxbuy or less
				if(curr > down[mxbuy].back()) {
					//cerr << "\nwe cannot ever buy another";
					// no moment where he can buy another, it's joever
					curr = n+1;
					break;
				}else{

					//cerr << "\nwe can buy it at the following moments: " << down[mxbuy];
					//cerr << "\ncurr, again=" << curr;

					ll nxt = upper_bound(down[mxbuy].begin(), down[mxbuy].end(), curr) - down[mxbuy].begin();
					//cerr << "\nnxt=" << nxt;

					curr = down[mxbuy][nxt];

					//curr = *down[mxbuy].upper_bound(curr);
					h = true;
					ans -= v[curr];

					//cerr << "\nwe've bought another at " << curr;
				}
			}
		}

		cout << ans << "\n";
	}
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);

	//vector<ll> v = {1, 3};
	//cerr << upper_bound(v.begin(), v.end(), 2) - v.begin();

	solve();

	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base9/50
1Elfogadva0/01ms492 KiB
2Időlimit túllépés0/01.09s11568 KiB
3Elfogadva1/11ms316 KiB
4Elfogadva1/11ms316 KiB
5Elfogadva2/214ms1768 KiB
6Elfogadva2/2389ms12832 KiB
7Elfogadva2/2391ms12896 KiB
8Elfogadva1/1310ms13620 KiB
9Időlimit túllépés0/11.078s13364 KiB
10Időlimit túllépés0/21.08s14140 KiB
11Időlimit túllépés0/21.08s13696 KiB
12Időlimit túllépés0/21.083s14028 KiB
13Időlimit túllépés0/21.088s14088 KiB
14Időlimit túllépés0/21.088s13800 KiB
15Időlimit túllépés0/31.088s12852 KiB
16Időlimit túllépés0/31.085s12856 KiB
17Időlimit túllépés0/31.082s12852 KiB
18Időlimit túllépés0/31.083s12760 KiB
19Időlimit túllépés0/31.083s12916 KiB
20Időlimit túllépés0/31.088s13108 KiB
21Időlimit túllépés0/31.083s12828 KiB
22Időlimit túllépés0/31.083s13032 KiB
23Időlimit túllépés0/31.083s12668 KiB
24Időlimit túllépés0/31.085s12852 KiB