#include "happiness.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200005;
const long long inf = (long long) 1.01e12;
int root, ls[MAX * 20], rs[MAX * 20], tsz;
long long sum[MAX * 20];
long long tot[MAX];
long long f[MAX];
void modify(int& v, long long l, long long r, long long i, int x) {
if (!v) {
v = ++tsz;
}
sum[v] += x * i;
if (l == r) {
return;
}
long long mid = l + r >> 1;
if (i <= mid) {
modify(ls[v], l, mid, i, x);
} else {
modify(rs[v], mid + 1, r, i, x);
}
}
long long query(int v, long long l, long long r, long long ql, long long qr) {
if (ql <= l && r <= qr) {
return sum[v];
}
if (l > qr || r < ql || l > r) {
return 0LL;
}
long long mid = l + r >> 1;
return query(ls[v], l, mid, ql, qr) + query(rs[v], mid + 1, r, ql, qr);
}
long long walk(long long s) {
long long l = s, r = inf, pos = inf;
long long target = query(root, 1, inf, s, inf);
while (l <= r) {
long long mid = l + r >> 1;
if (query(root, 1, inf, mid, inf) == target) {
pos = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return pos;
}
bool Check() {
long long total_sum = 0;
for (int i = 1; i <= 500; i++) {
total_sum += f[i];
}
if (total_sum == 0) {
return true;
}
if (f[1] == 0) {
return false;
}
long long sum = f[1];
for (int i = 2; i <= 500; i++) {
if (f[i] > 0 && sum + 1 < i) {
return false;
}
sum += f[i];
}
return true;
/*long long p = 1;
while (true) {
if (query(root, 1, inf, p + 1, inf) == 0) {
break;
}
long long s = query(root, 1, inf, 1, p);
long long nxt = walk(s + 2);
if (nxt == inf) {
break;
}
if (query(root, 1, inf, 1, nxt - 1) + 1 < nxt) {
return false;
}
p = nxt;
}
return true;*/
}
bool init(int coinsCount, long long maxCoinSize, long long coins[]) {
for (int i = 0; i < coinsCount; i++) {
f[coins[i]] += coins[i];
modify(root, 1, inf, coins[i], +1);
}
return Check();
}
bool is_happy(int event, int coinsCount, long long coins[]) {
if (event == -1) {
for (int i = 0; i < coinsCount; i++) {
f[coins[i]] -= coins[i];
assert(query(root, 1, inf, coins[i], coins[i]) > 0);
modify(root, 1, inf, coins[i], -1);
}
} else {
for (int i = 0; i < coinsCount; i++) {
f[coins[i]] += coins[i];
modify(root, 1, inf, coins[i], +1);
}
}
return Check();
}
Compilation message
happiness.cpp: In function 'void modify(int&, long long int, long long int, long long int, int)':
happiness.cpp:22:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | long long mid = l + r >> 1;
| ~~^~~
happiness.cpp: In function 'long long int query(int, long long int, long long int, long long int, long long int)':
happiness.cpp:37:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | long long mid = l + r >> 1;
| ~~^~~
happiness.cpp: In function 'long long int walk(long long int)':
happiness.cpp:45:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | long long mid = 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;
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
348 ms |
18528 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |