本文使用 WBLT 实现,通常来说无旋 treap 也能适用,但不保证 splay 也适用。
平衡树合并对于不存在分裂的情况,有 的时间复杂度。
证明:考虑朴素实现的有交合并,有 的时间复杂度,这是由 finger search 的性质决定的。于是容易取极限得到 的时间复杂度。
对于存在分裂的情况下,有 的时间复杂度。
注意到 split 成功的次数是 ,不成功次数是 ,也即复杂度只与 和 的值域段数有关。
考虑定义势能 ,记 为 和 的值域段数,那么对于每次合并,有 。
证明:假设有:
------x---x----
--------y---y—
那么 。 因为有 ,故 。
容易发现这个势能函数可以在很多情况下保持相等:如整体 reverse,整体加,等等。
附 P11622 代码:
#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define uint unsigned
#define ldb long double
#define uLL unsigned long long
using namespace std;
#ifndef FASTIO
#define FASTIO
namespace FastIO
{
// Don't cheat
} using FastIO::read, FastIO::read_cstr, FastIO::write, FastIO::write_cstr, FastIO::println, FastIO::println_cstr;
#endif
template < class T, int N, int M > class darry
{
inline constexpr static int calc(int x) { return ( __lg(x) + 2 ) >> 1; }
T a[M]; int b[N], c[1 << calc(N)], n, m, o;
struct E { int u; T v; } e[M], f[M];
class V { T *b, *e; public: explicit inline V(T *b, T *e) : b(b), e(e) {} inline T *begin()const { return b; } inline T *end()const { return e; } inline auto size()const { return e - b; } };
public:
inline void init(int n_) { n = n_, m = 0, o = calc(n), fill(b + 1, b + n + 1, 0); }
template < class ...U > inline void emplace(int u, U&& ...v) { b[u]++, e[++m] = E{u, T(forward<U>(v)...)}; }
inline void build() { fill(c, c + ( 1 << o ), 0); for(int i=1;i<=m;++i) c[e[i].u & ( ( 1 << o ) - 1 )]++; for(int i=1;i<=( 1 << o ) - 1;++i) c[i] += c[i - 1]; for(int i= m;i>=1;--i) f[c[e[i].u & ( ( 1 << o ) - 1 )]--] = move(e[i]); fill(c, c + ( 1 << o ), 0); for(int i=1;i<=m;++i) c[f[i].u >> o]++; for(int i=1;i<=( 1 << o ) - 1;++i) c[i] += c[i - 1]; for(int i= m;i>=1;--i) a[c[f[i].u >> o]--] = move(f[i].v); *b = 1; for(int i=1;i<=n;++i) b[i] += b[i - 1]; }
inline V operator[](int u) { return V(a + b[u - 1], a + b[u]); }
};
const int N=2e5+5,M=N*2;
const double alp=1-1/sqrtl(2),dlp=sqrtl(2)-1;
int n,q,rt,top,cnt;
LL V,a[N],ans[N];
int st[N];
darry<int,N,N>Ql,Qr;
struct node{
int ch[2],sz,f,k;LL b,l,r;
}T[M];
inline void tag(int p,int k,LL b){
if(T[p].sz>1)T[p].k*=k,T[p].b=T[p].b*k+b;
T[p].l=T[p].l*k+b,T[p].r=T[p].r*k+b;
if(k<0)swap(T[p].l,T[p].r);
}
inline int pushup(int x,int y){
int p=top?st[top--]:++cnt;
T[T[p].ch[0]=x].f=T[T[p].ch[1]=y].f=p,T[p].k=1,T[p].b=0,T[p].f=0;
T[p].sz=T[x].sz+T[y].sz,T[p].l=T[x].l,T[p].r=T[y].r;
return p;
}
inline void pushdown(int p){
if(T[p].sz>1&&(T[p].k!=1||T[p].b!=0)){
if(T[p].k<0)swap(T[p].ch[0],T[p].ch[1]);
tag(T[p].ch[0],T[p].k,T[p].b),tag(T[p].ch[1],T[p].k,T[p].b);
T[p].k=1,T[p].b=0;
}
}
inline int merge(int x,int y){
if(!x||!y)return T[x+y].f=0,x+y;
if(T[x].sz>=dlp*T[y].sz&&T[y].sz>=dlp*T[x].sz)return pushup(x,y);
if(T[x].sz>=T[y].sz){
pushdown(x);
const int ls=T[x].ch[0],rs=T[x].ch[1];
st[++top]=x;
if(T[ls].sz>=alp*(T[x].sz+T[y].sz))return merge(ls,merge(rs,y));
pushdown(rs);
const int lrs=T[rs].ch[0],rrs=T[rs].ch[1];
st[++top]=rs;
return merge(merge(ls,lrs),merge(rrs,y));
}
else{
pushdown(y);
const int ls=T[y].ch[0],rs=T[y].ch[1];
st[++top]=y;
if(T[rs].sz>=alp*(T[x].sz+T[y].sz))return merge(merge(x,ls),rs);
pushdown(ls);
const int lls=T[ls].ch[0],rls=T[ls].ch[1];
st[++top]=ls;
return merge(merge(x,lls),merge(rls,rs));
}
}
inline int build(int* A,int l,int r){
if(l==r)return A[l];
const int mid=(l+r)>>1;
return merge(build(A,l,mid),build(A,mid+1,r));
}
inline void split(int p,LL k,int&x,int&y){
T[p].f=0;
if(!p||k<T[p].l)return x=0,y=p,void();
if(T[p].r<=k)return x=p,y=0,void();
pushdown(p);
const int ls=T[p].ch[0],rs=T[p].ch[1];
st[++top]=p;
if(k<T[rs].l)split(ls,k,x,y),y=merge(y,rs);
else split(rs,k,x,y),x=merge(ls,x);
}
inline int Merge(int x,int y){
if(!x||!y)return x+y;
if(T[x].r<=T[y].l)return merge(x,y);
if(T[y].r<=T[x].l)return merge(y,x);
pushdown(x),pushdown(y);
if(T[x].sz<T[y].sz)swap(x,y);
const int ls=T[x].ch[0],rs=T[x].ch[1];
int p,q;split(y,T[ls].r,p,q);
st[++top]=x;
return merge(Merge(ls,p),Merge(rs,q));
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
read(n,q),cnt=q,Ql.init(n),Qr.init(n);
for(int i=1;i<=n;++i)read(a[i]),V=max(V,a[i]);
for(int i=1;i<=q;++i){
LL x;int l,r;read(x,l,r);
Ql.emplace(l,i),Qr.emplace(r,i);
T[i].l=T[i].r=x,T[i].sz=1;
}
Ql.build(),Qr.build();
for(int i=1;i<=n;++i){
sort(Ql[i].begin(),Ql[i].end(),[&](int x,int y){return T[x].l<T[y].l;});
if(Ql[i].size())rt=Merge(rt,build(Ql[i].begin(),0,Ql[i].size()-1));
int p,q;split(rt,(a[i]-(a[i]<0))/2,p,q);
if(p)tag(p,-1,a[i]);
rt=Merge(p,q),split(rt,V/2,rt,q);
for(int u:Qr[i]){
ans[u]=T[u].l;
for(int j=u;j=T[j].f;)ans[u]=T[j].k*ans[u]+T[j].b;
}
}
for(int i=1;i<=q;++i)println(ans[i]);
return 0;
}
/*
*/