From b2c80a8161907f627cb8d78119292190348f3718 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sat, 11 Oct 2025 12:10:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0P6006.cpp=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E4=B8=89=E5=85=83=E7=BB=84=E8=AE=A1=E6=95=B0=E7=AE=97?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现一个动态规划算法来计算满足条件的三元组数量,用于解决特定范围的查询问题。算法通过预处理和动态规划表优化查询效率,并处理了数组边界条件和内存使用问题。 --- src/10/10/P6006.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/10/10/P6006.cpp diff --git a/src/10/10/P6006.cpp b/src/10/10/P6006.cpp new file mode 100644 index 0000000..6295990 --- /dev/null +++ b/src/10/10/P6006.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +using ll = int64_t; + +const ll maxn = 5e3+5; +ll n,q, dp[maxn][maxn], a[maxn]; + +struct T{ + const static ll of = 2e6+3; + ll v[4*ll(1e6+5)]; // 注意限制的大小要*2 + inline ll&operator[](const ll n){ + return v[n+of]; + } +}tot; + +std::vector used; + +int main(){ + std::iostream::sync_with_stdio(false); + std::cin.tie(nullptr); + + std::cin>>n>>q; + for(ll i=1;i<=n;i++){ + std::cin>>a[i]; + } + + for(ll i=n-2;i>=1;i--){ // 注意循环顺序!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + for(ll j:used)tot[j]=0; // 注意清空耗时 + used.clear(); + tot[a[i+1]]++; + used.emplace_back(a[i+1]); + for(ll j=i+2;j<=n;j++){ + dp[i][j] = tot[-(a[i]+a[j])]; + // printf("before dp[%lld][%lld]=%lld\n",i,j,dp[i][j]); + dp[i][j] += dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1]; + tot[a[j]]++; + used.emplace_back(a[j]); + // printf("after dp[%lld][%lld]=%lld\n",i,j,dp[i][j]); + } + } + + while(q--){ + ll a,b; + std::cin>>a>>b; + std::cout<