perf(P8818): 使用预计算的lg2数组优化对数计算

用预计算的lg2数组替代多次log2调用,减少重复计算开销
This commit is contained in:
Zengtudor 2025-09-24 17:36:30 +08:00
parent cebbfa9891
commit 37ca18855a

View File

@ -23,9 +23,9 @@ do{\
printf(#v" :%lld\n",(v));\
}while(0)\
const ll inf = 1e9+7;
const ll inf = 1e9+7,maxn=1e5+5;
ll n,m,q;
std::vector<ll> a,b;
std::vector<ll> a,b,lg2;
struct BT{
std::vector<std::vector<ll>> vmax, vmin, vupmin, vdownmax;
@ -33,7 +33,7 @@ struct BT{
ll vsize;
inline BT(const std::vector<ll> &v){
vsize = ceil(log2(v.size()));
vsize = ceil(lg2[v.size()]);
vmax.resize(v.size(),std::vector<ll>(vsize+1, -inf));
vmin.resize(v.size(),std::vector<ll>(vsize+1,inf));
vupmin.resize(v.size(),std::vector<ll>(vsize+1,inf));
@ -63,14 +63,14 @@ struct BT{
// pv(vdownmax);
}
inline ll getmax(const std::vector<std::vector<ll>> &v,const ll l,const ll r){
const ll lg = log2(r-l+1);
const ll lg = lg2[r-l+1];
return std::max(
v[l][lg],
v[r-(1ull<<lg)+1][lg]
);
}
inline ll getmin(const std::vector<std::vector<ll>> &v,const ll l,const ll r){
const ll lg = log2(r-l+1);
const ll lg = lg2[r-l+1];
return std::min(
v[l][lg],
v[r-(1ull<<lg)+1][lg]
@ -89,7 +89,7 @@ struct BT{
return getmin(vupmin, l, r);
}
inline ll getis0(ll l,ll r){
const ll lg = log2(r-l+1);
const ll lg = lg2[r-l+1];
return vis0[l][lg] || vis0[r-(1ull<<lg)+1][lg];
}
};
@ -97,6 +97,10 @@ struct BT{
int main(){
std::iostream::sync_with_stdio(false);
std::cin.tie(nullptr);
lg2.resize(maxn);
for(ll i=2;i<maxn;i++){
lg2[i]=lg2[i/2]+1;
}
std::cin>>n>>m>>q;
a.resize(n+1);