一、差分标记介绍

差分标记用来解决针对区间(修改-查询)的问题,复杂度比线段树要更低。推荐这个博客

例如,给数组中处于某个区间的数进行加减操作,然后查询某个位置上数的变化值。

二、HDU1556 Color the ball 

2.1题意

N个气球依次编号为1,2,3....N.每次给定2个整数a b(a <= b),从气球a开始到气球b依次给每个气球涂一次颜色。求查询每个气球被涂的次数。

2.2分析

这里所有初始值为0,最后答案就是经过差分标记得到的变化值。

2.3代码

 # include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
const int maxn = 1e5+;
int sum[maxn];
int N;
void Init()
{
memset(sum,,sizeof(sum));
}
void Solve()
{
int l,r;
for(int i=;i<N;i++)
{
scanf("%d%d",&l,&r);
sum[l]++;
sum[r+]--;
}
int res = ;
for(int i=;i<N;i++)
{
res += sum[i];
printf("%d ",res);
}
res += sum[N];
printf("%d\n",res);
}
int main()
{
while(scanf("%d",&N)!=EOF)
{
if(N==) break;
Init();
Solve();
}
return ;
}

三、牛客contest 135-I 区间(题面)

3.1题意

有一个n个元素的数组a,对a[L]-a[R]进行M次操作:将a[L]-a[R]内的元素都加上P,询问a[l]-a[r]内的元素之和

3.2分析

经过M次操作的元素值等于初始值加上变化值,所以用差分标记得到变化值最后输出时再与原始值相加即可。注意会爆int以及不要开多个大数组。

3.3代码

 # include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
const int maxn = 1e6+;
int n,M;
int a[maxn],sum[maxn];
void Init()
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(sum,,sizeof(sum));
}
void Solve()
{
int q,L,R,P,l,r;
for(int i=;i<M;i++)
{
scanf("%d%d%d%d",&q,&L,&R,&P);
if(q==)
{
sum[L] -= P;
sum[R+] += P;
}
else
{
sum[L] += P;
sum[R+] -= P;
}
}
scanf("%d%d",&l,&r);
long long ans = ;
long long res = ;
for(int i=;i<=r;i++)
{
res += sum[i];
if(i>=l) ans += res + a[i];
}
printf("%lld\n",ans);
}
int main()
{
while(scanf("%d%d",&n,&M)!=EOF)
{
Init();
Solve();
}
return ;
}

HDU1556 Color the ball & 牛客 contest 135-I 区间 [差分标记]的更多相关文章

  1. HDU1556 Color the ball(差分数组)题解

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. 树状数组求区间和模板 区间可修改 参考题目:牛客小白月赛 I 区间

    从前有个东西叫树状数组,它可以轻易实现一些简单的序列操作,比如单点修改,区间求和;区间修改,单点求值等. 但是我们经常需要更高级的操作,比如区间修改区间查询.这时候树状数组就不起作用了,只能选择写一个 ...

  3. 牛客练习赛14 B 区间的连续段 (倍增)

    链接:https://ac.nowcoder.com/acm/contest/82/B来源:牛客网 区间的连续段 时间限制:C/C++ 7秒,其他语言14秒 空间限制:C/C++ 262144K,其他 ...

  4. hdu1556 Color the ball

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  5. hdu1556 Color the ball 简单线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 简单的线段树的应用 直接贴代码了: 代码: #include<iostream> # ...

  6. hdu1556 Color the ball 线段树区间染色问题

    都是老套路了,如果n=5,要把区间[1,4]染色,可以递归去染区间[1,3]和区间[4,4],如果区间相等就自加,不相等继续递归寻找对应区间. 打印结果时,把所有到达叶节点包含i的区间值相加,就是最后 ...

  7. HDU1556:Color the ball(简单的线段树区域更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定 ...

  8. HDU1556 Color the ball [线段树模板]

    题意:区间修改序列值,最后输出. //hdu1166 #include<iostream> #include<cstdio> #include<cstring> # ...

  9. 牛客网 223C 区区区间间间(单调栈)

    题目链接:区区区间间间 题意:给出长度为n的数字序列ai,定义区间(l,r)的价值为, 请你计算出. 题解:单调栈求ai左边和右边第一个比它小的位置,需要减去ai的个数为$(R_i-i+1)*(i-L ...

随机推荐

  1. ActiveX控件的消息处理函数

    首先切换到类视图 然后鼠标单击选中类(如果你要给ClockCtrl类添加事件,你就选中ClockCtrl类) PS:顺便多说一句,如果不用这种方法,而是手动添加,即使你的代码跟MFC添加的一模一样,那 ...

  2. js数组增删

    1.shift() 2.pop() 3.push() 4.unshift() 5.splice(start,num,string...)

  3. 【NS2】ns2 otcl与c++关联(转载)

    最近几天,对ns2进行研究,ns2为什么要使用两种语言,因为C++执行速度快,因此对于一些不需要经常改变的东西:例如包的发送.而对于需要经常进行修改的就不能够使用C++,而使用OTcl脚本语言.所有O ...

  4. javascript —— 禁止通过 Enter 键提交表单

    $('btn').on('keydown', function () { return false; })

  5. JavaWeb登录、注销、退出、记住用户名和密码

    应该是保存在Cookie里,session是放在服务器的内存里的.在用户关闭了网页窗口后,session就清空了.而Cookie是保存在用户的IE临时文件夹中的,再次登录时,读取其中的值传给服务器. ...

  6. tf.cast用法

    tf.cast:用于改变某个张量的数据类型 例如: import tensorflow as tf;import numpy as np; A = tf.convert_to_tensor(np.ar ...

  7. H3C 分页显示

  8. js递归遍历树结构(tree)

    如图: 代码: let datas = [] //是一个树结构的数据 setName(datas){ //遍历树 获取id数组 for(var i in datas){ this.expandedKe ...

  9. 2012-4-2 通过MdiParent设置窗体最前

    SentenceForm form = new SentenceForm(); form.MdiParent = this; form.Show(); //form.MdiParent = this; ...

  10. angular select框 option空行

    1.使用option <select class="form-control" ng-model="searchType"> <option ...