This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// moreflags=grader.cpp
// ......
#include "biscuits.h"
#include<array>
#include<cstdint>
#include<vector>
#include<cmath>
#include<iostream>
#if not LOCAL
#define NDEBUG
#endif
#include<cassert>
struct Node{ // represents a non-strictly decreasing list of numbers
int64_t count;
int64_t first; // also the maximum
int64_t delta;
std::array<int, 2> children; // index in nodes vector, or {{-1, -1}} if count==1
};
std::vector<Node> nodes;
int deltaed(int node, int64_t delta){
if(node<0 or delta==0) return node;
nodes.push_back(nodes[node]);
nodes.back().first+=delta;
nodes.back().delta+=delta;
return int(nodes.size()-1);
}
int append(int first, int sec, int64_t delta){
assert(first>=0); // precondition
if(sec==-1) return deltaed(first, delta);
assert(sec>=0);
nodes.push_back({
nodes[first].count+nodes[sec].count,
nodes[first].first+delta,
delta,
{{first, sec}}
});
return int(nodes.size()-1);
}
int prefixLargerEqual(int node, int64_t minimum){
// return -1 if result is empty
assert((nodes[node].count==1)==(nodes[node].children==std::array<int, 2>{{-1, -1}}));
if(nodes[node].first<minimum) return -1;
if(nodes[node].count==1){
// single node
assert(nodes[node].first==nodes[node].delta);
return node;
}
auto const [a, b]=nodes[node].children;
auto const t=minimum-nodes[node].delta;
auto const b1=prefixLargerEqual(b, t);
if(b1==b) return node;
if(b1<0) return deltaed(prefixLargerEqual(a, t), nodes[node].delta);
return append(a, b1, nodes[node].delta);
}
long long count_tastiness(long long x, std::vector<long long> a) {
nodes.clear();
nodes.push_back({1, a[0], a[0], {{-1, -1}}});
int all=0; // node reference
// initial list: {a[0]} (note that the first element will always be a[0])
int64_t s=0;
for(int i=0;; ++i){
assert(nodes[all].first==a[0]);
if(a[0]-x*std::round(std::pow(2., i))>=s){
all=append(all, deltaed(prefixLargerEqual(all, s+(x<<i)), -(x<<i)), 0);
}
if(i+1<(int)a.size())
s-=a[i+1]<<(i+1);
if(i==(int)a.size()+62){
return nodes[all].count;
}
}
}
# | 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... |