# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
789056 | lamduybao03 | Happiness (Balkan15_HAPPINESS) | C++14 | 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>
#define int long long
using namespace std;
struct Type {
int sum;
int max_d;
Type() {
sum = 0;
max_d = 0;
}
void update(int id, int k) {
if(id == 1) {
max_d = k;
sum += k;
}
else {
sum -= k;
if(sum == 0) {
max_d = 0;
}
}
}
};
struct Node {
Node* left;
Node* right;
Type d;
Type merge(Type a, Type b) {
Type c;
c.sum = a.sum + b.sum;
c.max_d = max(a.max_d, b.max_d - a.sum);
return c;
}
Node() {
left = right = nullptr;
}
Node* new_node(Node* p) {
if(p == nullptr)
return new Node();
else
return p;
}
void add_child() {
left = new_node(left);
right = new_node(right);
}
void update(int i, int x, int l, int r) {
if(l == i && i == r)
d.update(x, i);
else if(l <= i && i <= r) {
int m = l + r >> 1;
add_child();
left->update(i, x, l, m);
right->update(i, x, m+1, r);
d = merge(left->d, right->d);
}
}
int query() {
return d.max_d == 1;
}
void update(int i, int x) {
update(i, x, 0, 1LL << 40);
}
} root;
bool init(int n, int m, int* a) {
for(int i = 0; i < n; i++) {
root.update(a[i], 1);
}
return root.query();
}
bool is_happy(int id, int n, int* a) {
for(int i = 0; i < n; i++) {
root.update(a[i], id);
}
return root.query();
}