差分入门

闲来无事,差分一下

差分

根据我的理解,写一点差分的东西

差分是什么?


首先一个数组
1 2 3 4 5 6 7
那么差分之后
1 1 1 1 1 1 1

懂了吗哈哈哈哈
就是 c h a f e n [ i ] 存的是 n u m [ i ] n u m [ i 1 ]
哈哈哈数组名字是不是很傻x
多说无益,先来例题

例题

Haybale Stacking
xoj传送门
题目描述:一些干草堆,要给m次区间 [ l i , r i ] 的干草堆添加一堆干草,最后求出中间高度的干草堆 的高度。

题解:
[ l , r ] 的区间添加干草,说明 l 干草堆比 l 1 干草堆的高度高了1,同理 r r + 1 的干草堆高度减少了1(假设高度递增)

然后在用前缀和求一下每堆干草堆的高度,排序出解

#include <bits/stdc++.h> 
using namespace std;
int a[1000010],n,m;
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1; i<=m; i++)
      {
        int x,y;
        scanf("%d%d",&x,&y);
        a[x]++,a[y+1]--;
      }
    for (int i=1; i<=n; i++)
      a[i]+=a[i-1];
    sort(a+1,a+n+1);
    cout<<a[n/2+1];
}

差分入门而已。。。
明天再写qwq

猜你喜欢

转载自blog.csdn.net/beautiful_CXW/article/details/81987967
今日推荐