基础讲解:

  • https://blog.csdn.net/weixin_72060925/article/details/127835303

前缀和:

  • 模版题:https://www.luogu.com.cn/problem/P8218
  • 二维前缀和:https://www.luogu.com.cn/problem/P2004
  • 前缀和应用:https://www.luogu.com.cn/problem/T430521
    • 重点是将题目转换为前缀和。
    • 子串满足“0和1两种字符的个数相等”,将0变为-1,相当于区间和为0; 
    • 区间和为0 相当于s[r+1]=s[l]
    • 采用下标法统计不同前缀和结果数量,然后计算组合数相加
  • 前缀和应用二:https://www.luogu.com.cn/problem/T430522
    • 方法一:计算所有k的前缀和,要点:使用vector,效率nlogn
    • 其他解法,核心是要让循环里面的计算尽量少:j初始化放在外面,while替换if  
#include<bits/stdc++.h>
using namespace std;
int a[100005];
long long q,l,r,k,n,result;
int main()
{
    cin>>n>>q;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<q;i++)
    {
        cin>>l>>r>>k;
        int j=l;
        if(j%k!=0) j+=k-j%k;
        while(j<=r)
        {
            result+=a[j];
            j+=k;
        }
        cout<<result<<endl;
        result=0;
    }
    return 0;
}
  • 前缀和+数学:https://www.luogu.com.cn/problem/T430523(n太大没法计算差分,需要利用数学直接计算对前缀和的影响)
  • 前缀和+后缀和:https://www.luogu.com.cn/problem/T432687(可以用数学解决,也可以用双向前缀和解决)
  • 前缀和+欧拉筛:https://www.luogu.com.cn/problem/T432688(需要用较快的读取数据方式)

差分:

  • 二维差分:https://www.luogu.com.cn/problem/P3397
  • 二阶差分:https://www.luogu.com.cn/problem/P4231
  • 差分应用:https://www.luogu.com.cn/problem/T432689
    • 难点首先要想到从左到右更新,其次找到正确右端点,最后是代码不能出bug  
posted on 2024-03-18 15:17  风生水起  阅读(1)  评论(0编辑  收藏  举报