49462023-04-07 18:18:00SleepyOverlordMI bróker (50 pont)cpp17Time limit exceeded 6/501.077s4868 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;
	}

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

	FOR(i, 1, q)
	{
		int x, y;
		cin >> x >> y;
		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 << sol[x - mn][y - mn] << "\n";
	}

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base6/50
1Accepted0/03ms1996 KiB
2Time limit exceeded0/01.077s2504 KiB
3Accepted1/13ms2548 KiB
4Accepted1/13ms3300 KiB
5Accepted2/2216ms4612 KiB
6Time limit exceeded0/21.054s3104 KiB
7Time limit exceeded0/21.059s3128 KiB
8Accepted1/141ms3204 KiB
9Accepted1/1870ms3436 KiB
10Time limit exceeded0/21.057s3596 KiB
11Time limit exceeded0/21.059s3520 KiB
12Time limit exceeded0/21.062s3776 KiB
13Time limit exceeded0/21.036s3924 KiB
14Time limit exceeded0/21.044s4016 KiB
15Time limit exceeded0/31.052s4504 KiB
16Time limit exceeded0/31.052s4648 KiB
17Time limit exceeded0/31.041s4696 KiB
18Time limit exceeded0/31.062s4856 KiB
19Time limit exceeded0/31.057s4812 KiB
20Time limit exceeded0/31.065s4816 KiB
21Time limit exceeded0/31.036s4868 KiB
22Time limit exceeded0/31.065s4816 KiB
23Time limit exceeded0/31.057s4796 KiB
24Time limit exceeded0/31.065s4792 KiB