86692024-01-25 03:52:21SleepyOverlordBináris fa magassága (50 pont)cpp17Elfogadva 50/5030ms6056 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 MX(x, y) x = max(x, y);
#define MN(x, y) x = min(x, y);

#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...);
}

void in(int &n, VI &a) //array of ints
{
	cin >> n;
	a.CL, a.RS(n + 1);
	FOR(i, 1, n) cin >> a[i];
}

void in(int &n, VS &a) //array of strings
{
	cin >> n;
	a.CL, a.RS(n + 1);
	FOR(i, 1, n) cin >> a[i];
}

void in(int &n, VPII &a) //array of pairs
{
	cin >> n;
	a.CL, a.RS(n + 1);
	FOR(i, 1, n) cin >> a[i].X >> a[i].Y;
}

void in(int &n, int &m, VVI &g) //unweighted graph
{
	cin >> n >> m;
	g.CL, g.RS(n + 1);
	FOR(i, 1, n)
	{
		int x, y;
		cin >> x >> y;
		g[x].PB(y);
		g[y].PB(x);
	}
}

void in(int &n, VVI &g) //unweighted tree
{
	cin >> n;
	g.CL, g.RS(n + 1);
	FOR(i, 1, n - 1)
	{
		int x, y;
		cin >> x >> y;
		g[x].PB(y);
		g[y].PB(x);
	}
}

void in(int &n, int &m, vector <VPII> &g) //weighted graph
{
	cin >> n >> m;
	g.CL, g.RS(n + 1);
	FOR(i, 1, n)
	{
		int x, y, z;
		cin >> x >> y >> z;
		g[x].PB({y, z});
		g[y].PB({x, z});
	}
}

void in(int &n, vector <VPII> &g) //weighted tree
{
	cin >> n;
	g.CL, g.RS(n + 1);
	FOR(i, 1, n - 1)
	{
		int x, y, z;
		cin >> x >> y >> z;
		g[x].PB({y, z});
		g[y].PB({x, z});
	}
}

int n, nn, q, leaves;
VI a, tree, lazy;

void propagate(int node)
{
	lazy[node * 2] += lazy[node];
	lazy[node * 2 + 1] += lazy[node];
	lazy[node] = 0;
}

void update(int node, int left, int right, int l, int r, int delta)
{
	if (l <= left and right <= r) 
	{
		lazy[node] += delta;
		return;
	}
	propagate(node);
	int mid = (left + right) / 2;
	if (l <= mid) update(node * 2, left, mid, l, r, delta);
	if (mid + 1 <= r) update(node * 2 + 1, mid + 1, right, l, r, delta);
	tree[node] = max(tree[node * 2] + lazy[node * 2], 
					tree[node * 2 + 1] + lazy[node * 2 + 1]);
}

int main()
{
	#ifdef AT_HOME
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	cin >> nn >> q;
	n = (1 << nn) - 1;
	leaves = n - n / 2;
	a.RS(n + 1, 1);
	tree.RS(2 * n), lazy.RS(2 * n);
	update(1, 1, leaves, 1, leaves, nn - 1);

	FOR(i, 1, q)
	{
		int node, cost;
		cin >> node >> cost;
		int delta = cost - a[node];
		a[node] = cost;
		int l, r;
		l = r = node;
		while (l <= n / 2) l *= 2, r = r * 2 + 1;
		l -= n / 2, r -= n / 2;
		update(1, 1, leaves, l, r, delta);
		pr(tree[1] + lazy[1]);
		while(node) 
			node /= 2;
	}

	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1892 KiB
2Elfogadva0/026ms4624 KiB
3Elfogadva2/23ms2200 KiB
4Elfogadva2/23ms2204 KiB
5Elfogadva2/23ms2340 KiB
6Elfogadva2/23ms2424 KiB
7Elfogadva3/33ms2616 KiB
8Elfogadva3/33ms2592 KiB
9Elfogadva3/33ms2724 KiB
10Elfogadva3/33ms3216 KiB
11Elfogadva2/230ms5716 KiB
12Elfogadva2/229ms5628 KiB
13Elfogadva2/229ms5500 KiB
14Elfogadva2/229ms5568 KiB
15Elfogadva2/229ms5636 KiB
16Elfogadva2/225ms5500 KiB
17Elfogadva2/225ms5508 KiB
18Elfogadva2/225ms5508 KiB
19Elfogadva2/225ms5544 KiB
20Elfogadva3/326ms5712 KiB
21Elfogadva3/325ms5716 KiB
22Elfogadva3/326ms5940 KiB
23Elfogadva3/325ms6056 KiB