83082024-01-14 15:21:54ananászBináris fa magassága (50 pont)cpp17Time limit exceeded 20/50578ms5296 KiB

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

struct pont {
	int h;
	int k;
	int v;
};
int main() {
	int N;
	int M;
	cin >> N >> M;
	int x = int(pow(2, N)) - 1;
	vector<pont> hossz(x);  //maximum 65535
	int szint = 1;
	int id = 1;
	int y;
	while (szint < N)
	{
		int db = int(pow(2, szint));
		y = int(pow(2, N - 1 - szint));
		for (int i = 0; i < db; i++)
		{
			hossz[id].h = 1;
			hossz[id].k = i * y;
			hossz[id].v = (i + 1) * y - 1;
			id = id + 1;
		}
		szint = szint + 1;
	}
	x = int(pow(2, N - 1));
	vector<int> also(x); //maximum hossz 32768
	for (int i = 0; i < x; i++)
	{
		also[i] = N - 1;
	}
	vector<int> magassag(M);
	int max = N - 1;
	int maxid;
	int el;
	int hossza;
	int valtozas;
	for (int i = 0; i < M; i++)
	{
		cin >> el >> hossza;
		el = el - 1;
		valtozas = hossza - hossz[el].h;
		hossz[el].h = hossza;
		if (valtozas > 0)
		{
			for (int j = hossz[el].k; j <= hossz[el].v; j++)
			{
				if (max < also[j] + valtozas)
				{
					max = also[j] + valtozas;
				}
				also[j] = also[j] + valtozas;
			}
		}
		else if (valtozas < 0)
		{
			for (int j = hossz[el].k; j <= hossz[el].v; j++)
			{
				also[j] = also[j] + valtozas;
			}
			maxid = 0;
			for (int j = 1; j < x; j++)
			{
				if (also[maxid] < also[j])
				{
					maxid = j;
				}
			}
			max = also[maxid];
		}
		magassag[i]=max;
	}
	for (int i = 0; i < M; i++)
	{
		cout << magassag[i] << endl;
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base20/50
1Accepted0/03ms1828 KiB
2Time limit exceeded0/0546ms2408 KiB
3Accepted2/24ms2344 KiB
4Accepted2/24ms2408 KiB
5Accepted2/24ms2488 KiB
6Accepted2/24ms2616 KiB
7Accepted3/34ms2852 KiB
8Accepted3/34ms3124 KiB
9Accepted3/34ms3148 KiB
10Accepted3/34ms3148 KiB
11Time limit exceeded0/2568ms5296 KiB
12Time limit exceeded0/2561ms3988 KiB
13Time limit exceeded0/2561ms3900 KiB
14Time limit exceeded0/2550ms4060 KiB
15Time limit exceeded0/2556ms3624 KiB
16Time limit exceeded0/2565ms3924 KiB
17Time limit exceeded0/2554ms4152 KiB
18Time limit exceeded0/2574ms4448 KiB
19Time limit exceeded0/2559ms4500 KiB
20Time limit exceeded0/3570ms4788 KiB
21Time limit exceeded0/3578ms4792 KiB
22Time limit exceeded0/3565ms4868 KiB
23Time limit exceeded0/3578ms4992 KiB