#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#include "happiness.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e18;
const int MAXVAL = 8e6;
ll M;
map<ll, int> MM;
struct Node
{
ll val, sum;
int lc, rc;
Node() : val(0), sum(0), lc(-1), rc(-1) {}
};
Node nodes[MAXVAL];
int ncnt;
int newNode()
{
return ncnt++;
}
void update(int node, ll tl, ll tr, ll p, ll k)
{
if(tl==tr)
{
nodes[node].sum+=k;
nodes[node].val+=k;
return;
}
ll mid=tl+tr>>1;
if(p<=mid)
{
if(nodes[node].lc==-1) nodes[node].lc=newNode();
update(nodes[node].lc, tl, mid, p, k);
}
else
{
if(nodes[node].rc==-1) nodes[node].rc=newNode();
update(nodes[node].rc, mid+1, tr, p, k);
}
nodes[node].sum=0; nodes[node].val=INF;
if(nodes[node].lc!=-1) nodes[node].sum+=nodes[nodes[node].lc].sum;
if(nodes[node].lc!=-1) nodes[node].val=min(nodes[node].val, nodes[nodes[node].lc].val);
else nodes[node].val=min(nodes[node].val, 0ll);
if(nodes[node].rc!=-1) nodes[node].val=min(nodes[node].val, nodes[node].sum+nodes[nodes[node].rc].val);
else nodes[node].val=min(nodes[node].val, nodes[node].sum);
if(nodes[node].rc!=-1) nodes[node].sum+=nodes[nodes[node].rc].sum;
//printf("%lld %lld %lld %lld / %lld %lld\n", tl, tr, p, k, nodes[node].sum, nodes[node].val);
}
int root;
ll query()
{
return nodes[root].val;
}
void update2(ll l, ll r, ll x)
{
if(r<l) return;
if(l<=M) update(root, 1, M, l, x);
if(r+1<=M) update(root, 1, M, r+1, -x);
//printf("%lld %lld %lld => %lld\n", l, r, x, query());
}
void push(ll x)
{
update2(x+1, M, x);
MM[x]++;
if(MM[x]==1)
{
auto it=MM.find(x);
ll val=-x;
if(next(it)!=MM.end()) val+=next(it)->first;
update2(prev(it)->first+1, x, val);
}
}
void pop(ll x)
{
update2(x+1, M, -x);
MM[x]--;
if(MM[x]==0)
{
auto it=MM.find(x);
ll val=x;
if(next(it)!=MM.end()) val-=next(it)->first;
update2(prev(it)->first+1, x, val);
MM.erase(x);
}
}
bool init(int coinsCount, ll maxCoinSize, ll coins[])
{
M=maxCoinSize;
root=newNode();
MM[0];
for(int i=0; i<coinsCount; i++) push(coins[i]);
return query()>=-1;
}
bool is_happy(int event, int coinsCount, ll coins[])
{
if(event == -1) for(int i=0; i<coinsCount; i++) pop(coins[i]);
else for(int i=0; i<coinsCount; i++) push(coins[i]);
return query()>=-1;
}
Compilation message
happiness.cpp: In function 'void update(int, ll, ll, ll, ll)':
happiness.cpp:42:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | ll mid=tl+tr>>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 |
1 |
Correct |
107 ms |
188152 KB |
Output is correct |
2 |
Correct |
121 ms |
188156 KB |
Output is correct |
3 |
Correct |
118 ms |
188152 KB |
Output is correct |
4 |
Correct |
108 ms |
188156 KB |
Output is correct |
5 |
Correct |
115 ms |
188152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
188152 KB |
Output is correct |
2 |
Correct |
121 ms |
188156 KB |
Output is correct |
3 |
Correct |
118 ms |
188152 KB |
Output is correct |
4 |
Correct |
108 ms |
188156 KB |
Output is correct |
5 |
Correct |
115 ms |
188152 KB |
Output is correct |
6 |
Correct |
110 ms |
188280 KB |
Output is correct |
7 |
Correct |
110 ms |
188280 KB |
Output is correct |
8 |
Correct |
150 ms |
189176 KB |
Output is correct |
9 |
Correct |
155 ms |
189048 KB |
Output is correct |
10 |
Correct |
142 ms |
189048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
188152 KB |
Output is correct |
2 |
Correct |
121 ms |
188156 KB |
Output is correct |
3 |
Correct |
118 ms |
188152 KB |
Output is correct |
4 |
Correct |
108 ms |
188156 KB |
Output is correct |
5 |
Correct |
115 ms |
188152 KB |
Output is correct |
6 |
Correct |
1082 ms |
195816 KB |
Output is correct |
7 |
Correct |
1022 ms |
195952 KB |
Output is correct |
8 |
Correct |
1161 ms |
195940 KB |
Output is correct |
9 |
Execution timed out |
2013 ms |
196096 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
188152 KB |
Output is correct |
2 |
Correct |
121 ms |
188156 KB |
Output is correct |
3 |
Correct |
118 ms |
188152 KB |
Output is correct |
4 |
Correct |
108 ms |
188156 KB |
Output is correct |
5 |
Correct |
115 ms |
188152 KB |
Output is correct |
6 |
Correct |
110 ms |
188280 KB |
Output is correct |
7 |
Correct |
110 ms |
188280 KB |
Output is correct |
8 |
Correct |
150 ms |
189176 KB |
Output is correct |
9 |
Correct |
155 ms |
189048 KB |
Output is correct |
10 |
Correct |
142 ms |
189048 KB |
Output is correct |
11 |
Correct |
1082 ms |
195816 KB |
Output is correct |
12 |
Correct |
1022 ms |
195952 KB |
Output is correct |
13 |
Correct |
1161 ms |
195940 KB |
Output is correct |
14 |
Execution timed out |
2013 ms |
196096 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |