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 erase_child() {
if(d.sum == 0) {
delete left;
delete right;
}
}
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);
erase_child();
}
}
int query() {
return d.max_d <= 1;
}
void update(int i, int x) {
update(i, x, 0, 1LL << 40);
}
} root;
bool init(int32_t n, int m, int* a) {
for(int i = 0; i < n; i++) {
root.update(a[i], 1);
}
return root.query();
}
bool is_happy(int32_t id, int32_t n, int* a) {
for(int i = 0; i < n; i++) {
root.update(a[i], id);
}
return root.query();
}
Compilation message (stderr)
happiness.cpp: In member function 'void Node::update(long long int, long long int, long long int, long long int)':
happiness.cpp:58:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | int m = l + r >> 1;
| ~~^~~
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |