49662023-04-07 22:48:29SleepyOverlordMI bróker (50 pont)cpp17Accepted 50/50919ms7008 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()
{
	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] << "\n";
	}

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/04ms3740 KiB
2Accepted0/0805ms4860 KiB
3Accepted1/13ms4384 KiB
4Accepted1/14ms4600 KiB
5Accepted2/264ms5128 KiB
6Accepted2/2871ms5720 KiB
7Accepted2/2879ms5804 KiB
8Accepted1/143ms5684 KiB
9Accepted1/1172ms5928 KiB
10Accepted2/2432ms6128 KiB
11Accepted2/2354ms6160 KiB
12Accepted2/2388ms6104 KiB
13Accepted2/2391ms6328 KiB
14Accepted2/2377ms6504 KiB
15Accepted3/3915ms6492 KiB
16Accepted3/3912ms6720 KiB
17Accepted3/3919ms6920 KiB
18Accepted3/3902ms6948 KiB
19Accepted3/3916ms6952 KiB
20Accepted3/3904ms6932 KiB
21Accepted3/3907ms6940 KiB
22Accepted3/3908ms6928 KiB
23Accepted3/3904ms7008 KiB
24Accepted3/3916ms6928 KiB