mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-10-15 12:32:19 +00:00
Compare commits
2 Commits
daba2089c4
...
de497e2c80
Author | SHA1 | Date | |
---|---|---|---|
de497e2c80 | |||
b2c80a8161 |
@ -1,4 +1,11 @@
|
||||
# 算法笔记
|
||||
|
||||
## P6476 题解易错点
|
||||
|
||||
1. **循环顺序**:dp注意必须从遍历顺序(i从n-2递减),正向遍历会导致状态转移错误
|
||||
2. **清空操作优化**:每次使用后需要清空tot数组,但直接memset会超时,改用标记数组记录修改位置
|
||||
3. **下标偏移**:结构体T使用of常量处理负数下标,数组大小需要*2
|
||||
4. **状态转移公式**:注意容斥原理的应用,dp[i][j] = 当前三元组数 + 子区间累计值 - 重复部分
|
||||
## 线性动态规划优化为$O(n\log{n})$方法
|
||||
>如果是递增序列就lower_bound
|
||||
|
||||
|
58
src/10/10/P6006.cpp
Normal file
58
src/10/10/P6006.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
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<ll> 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<<dp[a][b]<<"\n";
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
dp[i][j] = i~j的
|
||||
dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1]
|
||||
|
||||
|
||||
*/
|
Loading…
Reference in New Issue
Block a user