#include "happiness.h"
#include <cstdint>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;
int64_t tree_size = 1e18; // M*(N+K*Q)
int64_t tree_block = 8e5; // N+K*Q
struct Node{
Node *left = nullptr, *right = nullptr;
int64_t node_l, node_r;
int64_t mn_diff = 0; //S - i
int64_t lazy = 0;
Node(int64_t node_l, int64_t node_r) : node_l(node_l), node_r(node_r){}
void add_lazy(int64_t _lazy){
lazy += _lazy;
mn_diff += _lazy;
}
void check_children(){
if (left == nullptr){
int64_t mid = (node_l + node_r)/2;
left = new Node(node_l, mid);
right = new Node(mid+1, node_r);
}
}
void push(){
check_children();
left->add_lazy(lazy);
right->add_lazy(lazy);
lazy = 0;
}
void add(int64_t l, int64_t r, int64_t x){
if (l > node_r || r < node_l)
return;
if (l <= node_l && node_r <= r)
return add_lazy(x);
push();
left->add(l, r, x);
right->add(l, r, x);
mn_diff = min(left->mn_diff, right->mn_diff);
}
};
unordered_map<int64_t, int64_t> cur_ptr;
Node root = Node(0, tree_size);
int64_t inf = tree_size*2;
int64_t M;
int64_t find_to_insert(int64_t x){
return (tree_block * x) + (cur_ptr[x]++);
}
int64_t find_to_delete(int64_t x){
return (tree_block * x) + (--cur_ptr[x]);
}
bool is_happy(int event, int coinsCount, long long coins[]) {
int sign = event;
function<int64_t(int64_t)> find_place = find_to_insert;
if (event == -1)
find_place = find_to_delete;
for (int i = 0; i < coinsCount; i++)
{
int64_t place = find_place(coins[i]);
root.add(place, place, (-inf-coins[i]) * sign );
root.add(place+1, tree_size, sign * coins[i]);
}
return root.mn_diff >= 0;
}
bool init(int coinsCount, long long maxCoinSize, long long coins[]) {
M = maxCoinSize;
root.add(0, tree_size, 1);
root.add(0, tree_size, inf);
return is_happy(1, coinsCount, coins);
}
Compilation message
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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
612 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
856 KB |
Output is correct |
5 |
Correct |
1 ms |
692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
612 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
856 KB |
Output is correct |
5 |
Correct |
1 ms |
692 KB |
Output is correct |
6 |
Correct |
8 ms |
7004 KB |
Output is correct |
7 |
Correct |
7 ms |
7772 KB |
Output is correct |
8 |
Correct |
69 ms |
61712 KB |
Output is correct |
9 |
Correct |
69 ms |
62548 KB |
Output is correct |
10 |
Correct |
63 ms |
59988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
612 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
856 KB |
Output is correct |
5 |
Correct |
1 ms |
692 KB |
Output is correct |
6 |
Correct |
1313 ms |
522532 KB |
Output is correct |
7 |
Correct |
1226 ms |
514392 KB |
Output is correct |
8 |
Correct |
1251 ms |
523324 KB |
Output is correct |
9 |
Runtime error |
1329 ms |
524288 KB |
Execution killed with signal 9 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
612 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
856 KB |
Output is correct |
5 |
Correct |
1 ms |
692 KB |
Output is correct |
6 |
Correct |
8 ms |
7004 KB |
Output is correct |
7 |
Correct |
7 ms |
7772 KB |
Output is correct |
8 |
Correct |
69 ms |
61712 KB |
Output is correct |
9 |
Correct |
69 ms |
62548 KB |
Output is correct |
10 |
Correct |
63 ms |
59988 KB |
Output is correct |
11 |
Correct |
1313 ms |
522532 KB |
Output is correct |
12 |
Correct |
1226 ms |
514392 KB |
Output is correct |
13 |
Correct |
1251 ms |
523324 KB |
Output is correct |
14 |
Runtime error |
1329 ms |
524288 KB |
Execution killed with signal 9 |
15 |
Halted |
0 ms |
0 KB |
- |