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 "biscuits.h"
#include <cstdio>
using ll = long long;
ll x;
std::vector<ll> a;
bool test(ll v) // O(K)
{
ll n=0;
for(int i=0;v;v>>=1, n>>=1, ++i)
{
if(i<(int)a.size()) n+=a[i];
if(v&1)
if((n-=x)<0)
return 0;
}
return 1;
}
ll get(int b) // O(K^2) -- max, with b'th bit turned on
{
ll v=1ll<<b;
for(int i=b-1;i>=0;--i)
if(test(v|1ll<<i))
v|=1ll<<i;
return v;
}
const int MX = 2e5; // idk, just smth large
ll stv[MX];
int l[MX], r[MX], X;
int nn(int p=0)
{
++X;
stv[X]=stv[p];
l[X]=l[p];
r[X]=r[p];
return X;
}
void up(int n) // do NOT call on leaves
{
stv[n]=stv[l[n]]+stv[r[n]];
}
int pull(int n, ll nl, ll nr, ll ql, ll qr)
{
if(ql <= nl && nr <= qr) return n;
if(nr-nl>1)
{
n = nn(n);
ll m=nl+(nr-nl)/2;
if(ql < m) l[n] = pull(l[n], nl, m, ql, qr); else l[n]=0;
if(m < qr) r[n] = pull(r[n], m, nr, ql, qr); else r[n]=0;
up(n);
}
return n;
}
long long count_tastiness(long long _x, std::vector<long long> _a)
{
x=_x;
a=_a;
X=0;
int root=nn();
stv[root]=1;
for(int i=0;;++i)
{
int nroot = nn();
l[nroot] = root;
r[nroot] = 0;
if(test(1ll<<i))
{
ll bound = get(i);
bound ^= 1ll<<i;
r[nroot] = pull(root, 0, 1ll<<i, 0, bound+1);
//printf("%d: %lld [%lld]\n", i, bound+1, stv[r[nroot]]);
}
else if(i>=(int)a.size()) break;
root = nroot;
up(root);
//printf("%d: %lld\n", i, stv[root]);
}
return stv[root];
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |