49502023-04-07 19:09:21SleepyOverlordMI bróker (50 pont)cpp17Time limit exceeded 20/501.1s4412 KiB
#include <vector>
#include <string> 
#include <set> 
#include <map> 
#include <unordered_set>
#include <unordered_map>
#include <queue> 
#include <bitset> 
#include <stack>
#include <list>

#include <numeric> 
#include <algorithm> 
#include <random>
#include <chrono>

#include <cstdio>
#include <fstream>
#include <iostream> 
#include <sstream> 
#include <iomanip>
#include <climits>

#include <cctype>
#include <cmath> 
#include <ctime>
#include <cassert>

using namespace std;

#define ULL unsigned long long
#define LL long long
#define PII pair <int, int>
#define VB vector <bool>
#define VI vector <int>
#define VLL vector <LL>
#define VD vector <double>
#define VS vector <string>
#define VPII vector <pair <int, int> >
#define VVI vector < VI >
#define VVB vector < VB >
#define SI set < int >
#define USI unordered_set <int>
#define MII map <int, int>
#define UMII unordered_map <int, int>
#define MS multiset
#define US unordered_set
#define UM unordered_map
#define UMS unordered_multiset
#define UMM unordered_multimap

#define FORN(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define SZ size()
#define BG begin() 
#define EN end() 
#define CL clear()
#define X first
#define Y second
#define RS resize
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(), x.end()
#define INS insert
#define ER erase
#define CNT count

template <class T> ostream& operator <<(ostream & os, const vector<T> &vec)
{
	for (int i = 0; i < vec.size() - 1; ++i) os << vec[i] << ' ';
	return os << vec[vec.size() - 1];
}

template <class T1, class T2> ostream& operator <<(ostream & os, const pair<T1, T2> &p)
{
	return os << p.X << " " << p.Y;
}

template <typename T>
void pr(T var1)
{
	cout << var1 << '\n';
}
template <typename T, typename... Types>
void pr(T var1, Types... var2)
{
	cout << var1;
	pr(var2...);
}

int n, q, mn, mx;
VI a, prefMin;
VVI sol;

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	cin >> n >> q;
	mn = 500, mx = 0;
	a.RS(n + 1);
	prefMin.RS(n + 1);
	prefMin[0] = 500;
	FOR(i, 1, n)
	{
		cin >> a[i];
		mn = min(mn, a[i]);
		mx = max(mx, a[i]);
		prefMin[i] = mn;
	}

	bool onlyTwo = true;
	FOR(i, 1, n)
		if (a[i] != mn && a[i] != mx) onlyTwo = false;

	int profit = 0;
	bool has = false;
	FOR(k, 1, n)
		if (!has && a[k] <= mn) 
		{
			profit -= a[k];
			has = true;
		}
		else
			if (has && a[k] >= mx)
			{
				profit += a[k];
				has = false;
			}

	FOR(i, 1, q)
	{
		int x, y;
		cin >> x >> y;
		if (onlyTwo)
		{
			if (x < mn) cout << "0\n";
			else
				if (y > mx)
				{
					int l = 0, r = n;
					while (r - l > 1)
					{
						int m = (l + r) / 2;
						if (prefMin[m] > x) l = m;
						else r = m;
					}
					cout << -prefMin[r] << "\n";
				}
				else cout << profit << "\n";
		}
		else
		{
			bool has = false;
			int p = 0;
			FOR(k, 1, n)
				if (!has && a[k] <= x) 
				{
					p -= a[k];
					has = true;
				}
				else
					if (has && a[k] >= y)
					{
						p += a[k];
						has = false;
					}
			cout << p << "\n";
		}
	}

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base20/50
1Accepted0/03ms1768 KiB
2Time limit exceeded0/01.1s1432 KiB
3Accepted1/13ms2192 KiB
4Accepted1/13ms2488 KiB
5Accepted2/23ms2488 KiB
6Accepted2/24ms2712 KiB
7Accepted2/26ms2872 KiB
8Accepted1/141ms3228 KiB
9Accepted1/139ms3368 KiB
10Accepted2/241ms3420 KiB
11Accepted2/241ms3540 KiB
12Accepted2/243ms3752 KiB
13Accepted2/243ms3964 KiB
14Accepted2/241ms4192 KiB
15Time limit exceeded0/31.072s3560 KiB
16Time limit exceeded0/31.065s4412 KiB
17Time limit exceeded0/31.049s3684 KiB
18Time limit exceeded0/31.032s3552 KiB
19Time limit exceeded0/31.062s3460 KiB
20Time limit exceeded0/31.082s3560 KiB
21Time limit exceeded0/31.065s3628 KiB
22Time limit exceeded0/31.067s3612 KiB
23Time limit exceeded0/31.044s3648 KiB
24Time limit exceeded0/31.027s3744 KiB