49672023-04-07 23:03:17SleepyOverlordMI bróker (50 pont)cpp17Accepted 50/50953ms7160 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

#define MAX 500

int n, q, profit;
VI a, buy, sell;
VVI sol, pos;
SI unpaired, paired;

void addNewUnpaired(int sellPos)
{
	auto it = upper_bound(ALL(buy), sellPos);
	if (it != buy.EN && !paired.CNT(*it) && !unpaired.CNT(*it))
	{
		unpaired.INS(*it);
		profit -= a[*it];
	}
}

void addNewPair(int buyPos, int sellPos)
{
	profit += a[sellPos];
	sell[buyPos] = sellPos;
	unpaired.ER(buyPos);
	paired.INS(buyPos);
}

bool pairWithUnpaired(int sellPos)
{
	auto buyPos = unpaired.lower_bound(sellPos);
	if (buyPos != unpaired.BG)
	{
		--buyPos;
		int aux = *buyPos;
		addNewPair(aux, sellPos);
		addNewUnpaired(sellPos);
	}
	return false;
}

void pairWithPaired(int sellPos)
{
	auto buyPos = paired.lower_bound(sellPos);
	if (buyPos == paired.BG) return;
	
	--buyPos;
	int aux = *buyPos;
	int oldSellPos = sell[aux];
	if (sellPos > oldSellPos) return;

	profit += a[sellPos] - a[oldSellPos];
	addNewUnpaired(sellPos);
	sell[aux] = sellPos;
						
	buyPos = unpaired.lower_bound(oldSellPos);
	if (buyPos == unpaired.BG) return;

	--buyPos;
	aux = *buyPos;
	if (sell[aux] == -1)
	{
		addNewPair(aux, oldSellPos);
		addNewUnpaired(oldSellPos);
	}
}

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;
		profit = -a[buy[0]];
		unpaired = {buy[0]}, paired = {};
		fill(ALL(sell), -1);
		FORD(j, MAX, i + 1)
		{
			for(int sellPos : pos[j])
			{
				if (!pairWithUnpaired(sellPos)) pairWithPaired(sellPos);
			}
			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/0829ms4996 KiB
3Accepted1/14ms4140 KiB
4Accepted1/13ms4384 KiB
5Accepted2/264ms4884 KiB
6Accepted2/2906ms5756 KiB
7Accepted2/2916ms6076 KiB
8Accepted1/145ms6116 KiB
9Accepted1/1246ms6336 KiB
10Accepted2/2662ms6164 KiB
11Accepted2/2527ms6556 KiB
12Accepted2/2587ms6528 KiB
13Accepted2/2595ms6768 KiB
14Accepted2/2575ms6756 KiB
15Accepted3/3945ms6916 KiB
16Accepted3/3936ms6892 KiB
17Accepted3/3953ms6856 KiB
18Accepted3/3934ms7128 KiB
19Accepted3/3949ms7128 KiB
20Accepted3/3935ms7100 KiB
21Accepted3/3934ms7092 KiB
22Accepted3/3935ms7104 KiB
23Accepted3/3938ms7096 KiB
24Accepted3/3949ms7160 KiB