From c5c309b935e33ecdfecffec50a93b72b0cbcd6e5 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sun, 31 Aug 2025 16:17:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0P5200=E9=A2=98?= =?UTF-8?q?=E8=A7=A3=E5=AE=9E=E7=8E=B0=EF=BC=8C=E4=BD=BF=E7=94=A8=E6=A0=91?= =?UTF-8?q?=E7=8A=B6=E6=95=B0=E7=BB=84=E8=AE=A1=E7=AE=97=E9=80=86=E5=BA=8F?= =?UTF-8?q?=E5=AF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现了一个使用树状数组计算逆序对数量的算法,用于解决P5200题目。通过维护树状数组来高效计算区间和,从而确定每个元素的逆序对数量。 --- src/8/31/P5200.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/8/31/P5200.cpp diff --git a/src/8/31/P5200.cpp b/src/8/31/P5200.cpp new file mode 100644 index 0000000..e0c9b13 --- /dev/null +++ b/src/8/31/P5200.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +using ll = int64_t; + +ll n; +std::vector v,t,ans; + +#define pv(v)do{std::cout<<#v<<" :"<<(v)<<"\n";}while(0) + +static inline constexpr ll lowbit(ll n){ + return n&(-n); +} + +static inline void set(ll idx,ll addval){ + while(idx<=n){ + t[idx]+=addval; + idx+=lowbit(idx); + } +} + +static inline ll getsfx(ll idx){ + ll res=0; + while(idx){ + res+=t[idx]; + idx-=lowbit(idx); + } + return res; +} + +static inline ll getrange(ll l,ll r){ + return getsfx(r)-getsfx(l-1); +} + +int main(){ + std::iostream::sync_with_stdio(false); + std::cin.tie(nullptr); + + std::cin>>n; + v.resize(n+1); + t.resize(n+1); + ans.reserve(n+1); + for(ll i=1;i<=n;++i){ + std::cin>>v[i]; + } + ll idx=n; + for(;idx>=2 && v[idx-1]<=v[idx];--idx); + // pv(idx); + for(ll i=idx;i<=n;i++){ + set(v[i], 1); + } + ll sum=0; + for(ll i=1;i