# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1086889 | serifefedartar | Happiness (Balkan15_HAPPINESS) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "happiness.h"
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
#define f first
#define s second
#define LOGN 21
const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 100;
#define int long long
struct Node {
int l, r;
int sum;
Node *ln, *rn;
Node() : l(-1), r(-1), sum(0), ln(NULL), rn(NULL) { }
Node(int _l, int _r) : l(_l), r(_r), sum(0), ln(NULL), rn(NULL) { }
int query(int a, int b) {
if (r < a || l > b)
return 0;
if (l <= a && b <= r)
return sum;
int res = 0;
if (ln)
res += ln->sum;
if (rn)
res += rn->sum;
return res;
}
void update(int plc, int val) {
sum += val;
if (l != r) {
int mid = (l + r) / 2;
if (plc > mid) {
if (!rn)
rn = new Node(mid + 1, r);
rn->update(plc, val);
} else {
if (!ln)
ln = new Node(l, mid);
ln->update(plc, val);
}
}
}
};
Node *root;
bool solve() {
int now = 1;
while (now < root->sum) {
int t = root->query(1, now);
if (t < now)
return false;
now = t + 1;
}
return true;
}
bool init(int coinsCount, int maxCoinSize, int coins[]) {
root = new Node(1, maxCoinSize);
root->update(1, 1);
for (int i = 0; i < coinsCount; i++)
root->update(coins[i], coins[i]);
return solve();
}
bool is_happy(int event, int coinsCount, int coins[]) {
for (int i = 0; i < coinsCount; i++)
root->update(coins[i], event * coins[i]);
return solve();
}