49652023-04-07 22:39:57SleepyOverlordMI bróker (50 pont)cpp17Time limit exceeded 20/501.077s7348 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...);
}

#define MAX 500

int n, q;
VI a, buy, sell;
VVI sol, pos;

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;
	a.RS(n + 1);
	pos.RS(MAX + 1);
	FOR(i, 1, n) 
	{
		cin >> a[i];
		pos[a[i]].PB(i);
	}

	sol.RS(MAX + 1, VI(MAX + 1));
	sell.RS(n + 1);
	FOR(i, 1, MAX - 1)
	{
		VI res(buy.SZ + pos[i].SZ);
		merge(ALL(buy), ALL(pos[i]), res.BG);
		buy = res;
		if (buy.empty()) continue;
		int profit = -a[buy[0]];
		SI unpaired = {buy[0]}, paired;
		fill(ALL(sell), -1);
		FORD(j, MAX, i + 1)
		{
			for(int sellPos : pos[j])
			{
				auto buyPos = unpaired.lower_bound(sellPos);
				if (buyPos != unpaired.BG)
				{
					--buyPos;
					int aux = *buyPos;
					profit += a[sellPos];
					sell[*buyPos] = sellPos;
					unpaired.ER(buyPos);
					paired.INS(aux);
					auto it = upper_bound(ALL(buy), sellPos);
					if (it == buy.EN) continue;
					profit -= a[*it];
					unpaired.INS(*it);
				}
				else
				{
					auto buyPos = paired.lower_bound(sellPos);
					if (buyPos == paired.BG) continue;
					--buyPos;
					int aux = *buyPos;
					int oldSellPos = sell[aux];
					if (oldSellPos > sellPos)
					{
						profit += a[sellPos] - a[oldSellPos];
						auto it = upper_bound(ALL(buy), sellPos);
						if (it != buy.EN && !paired.CNT(*it) && !unpaired.CNT(*it))
						{
							unpaired.INS(*it);
							profit -= a[*it];
						}
						sell[aux] = sellPos;
						
						buyPos = unpaired.lower_bound(oldSellPos);
						if (buyPos == unpaired.BG) continue;
						--buyPos;
						aux = *buyPos;
						if (sell[aux] == -1)
						{
							profit += a[oldSellPos];
							sell[*buyPos] = oldSellPos;
							unpaired.ER(buyPos);
							paired.INS(aux);
							auto it = upper_bound(ALL(buy), oldSellPos);
							if (it != buy.EN && !paired.CNT(*it) && !unpaired.CNT(*it))
							{
								profit -= a[*it];
								unpaired.INS(*it);
							}
						}
					}
				}
			}
			sol[i][j] = profit;
		}
	}

	FOR(i, 1, q)
	{
		int x, y;
		cin >> x >> y;
		cout << sol[x][y] << endl;
	}

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base20/50
1Accepted0/04ms3756 KiB
2Time limit exceeded0/01.034s4800 KiB
3Accepted1/13ms4056 KiB
4Accepted1/14ms4268 KiB
5Accepted2/264ms4920 KiB
6Accepted2/2890ms5460 KiB
7Accepted2/2897ms5592 KiB
8Accepted1/1168ms5676 KiB
9Accepted1/1319ms5720 KiB
10Accepted2/2558ms5712 KiB
11Accepted2/2635ms5804 KiB
12Accepted2/2504ms5916 KiB
13Accepted2/2513ms5964 KiB
14Accepted2/2712ms6280 KiB
15Time limit exceeded0/31.069s4240 KiB
16Time limit exceeded0/31.077s4588 KiB
17Time limit exceeded0/31.034s4356 KiB
18Time limit exceeded0/31.077s4676 KiB
19Time limit exceeded0/31.067s7072 KiB
20Time limit exceeded0/31.047s4920 KiB
21Time limit exceeded0/31.07s5128 KiB
22Time limit exceeded0/31.047s7348 KiB
23Time limit exceeded0/31.065s5384 KiB
24Time limit exceeded0/31.054s5412 KiB