Submission #526934

# Submission time Handle Problem Language Result Execution time Memory
526934 2022-02-16T16:48:50 Z peuch Happiness (Balkan15_HAPPINESS) C++17
0 / 100
0 ms 204 KB
#include "happiness.h"
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

long long m, sum;
vector<long long> seg, lzy;
vector<int> e, d;

unordered_map<long long, int> freq;

bool init(int coinsCount, long long maxCoinSize, long long coins[]);
bool is_happy(int event, int coinsCount, long long coins[]);
int create();
void update(int pos, long long ini, long long fim, long long p, long long q, long long val);

bool init(int coinsCount, long long maxCoinSize, long long coins[]) {
	m = maxCoinSize;
	create();
	create();
	return is_happy(1, coinsCount, coins);
}

bool is_happy(int event, int coinsCount, long long coins[]) {
	for(int i = 0; i < coinsCount; i++){
		freq[coins[i]] += event;
		if(event == 1 && freq[coins[i]] == 1) update(1, 1, m, coins[i], coins[i], 1 - coins[i]);
		if(event == -1 && freq[coins[i]] == 0) update(1, 1, m, coins[i], coins[i], coins[i] - 1);
		sum += event * coins[i];
		update(1, 1, m, coins[i] + 1, m, event * coins[i]);
	}
	return seg[1] + lzy[1] >= 0;
}

int create(){
	seg.push_back(0);
	lzy.push_back(0);
	e.push_back(0);
	d.push_back(0);
	return seg.size() - 1;
}

void update(int pos, long long ini, long long fim, long long p, long long q, long long val){
	if(ini > q || fim < p) return;
	if(ini >= p && fim <= q){
		seg[pos] += val;
		lzy[pos] += val;
		return;
	}
	long long mid = (ini + fim) >> 1;
	if(p <= mid){
		if(e[pos] == 0){
			int aux = create();
			e[pos] = aux;
		}
		update(e[pos], ini, mid, p, q, val);
	}
	if(q > mid){
		if(d[pos] == 0){
			int aux = create();
			d[pos] = aux;
		}
		update(d[pos], mid + 1, fim, p, q, val);
	}
	if(e[pos] == 0) seg[pos] = min(seg[d[pos]], -mid) + lzy[pos];
	else if(d[pos] == 0) seg[pos] = min(seg[e[pos]], -fim) + lzy[pos];
	else seg[pos] = min(seg[e[pos]], seg[d[pos]]) + lzy[pos];
}

Compilation message

grader.cpp: In function 'int main()':
grader.cpp:16:12: warning: unused variable 'max_code' [-Wunused-variable]
   16 |  long long max_code;
      |            ^~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -