HDU1556 Color the ball & 牛客 contest 135-I 区间 [差分标记]
一、差分标记介绍
差分标记用来解决针对区间(修改-查询)的问题,复杂度比线段树要更低。推荐这个博客。
例如,给数组中处于某个区间的数进行加减操作,然后查询某个位置上数的变化值。
二、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 区间 [差分标记]的更多相关文章
- HDU1556 Color the ball(差分数组)题解
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 树状数组求区间和模板 区间可修改 参考题目:牛客小白月赛 I 区间
从前有个东西叫树状数组,它可以轻易实现一些简单的序列操作,比如单点修改,区间求和;区间修改,单点求值等. 但是我们经常需要更高级的操作,比如区间修改区间查询.这时候树状数组就不起作用了,只能选择写一个 ...
- 牛客练习赛14 B 区间的连续段 (倍增)
链接:https://ac.nowcoder.com/acm/contest/82/B来源:牛客网 区间的连续段 时间限制:C/C++ 7秒,其他语言14秒 空间限制:C/C++ 262144K,其他 ...
- hdu1556 Color the ball
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- hdu1556 Color the ball 简单线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 简单的线段树的应用 直接贴代码了: 代码: #include<iostream> # ...
- hdu1556 Color the ball 线段树区间染色问题
都是老套路了,如果n=5,要把区间[1,4]染色,可以递归去染区间[1,3]和区间[4,4],如果区间相等就自加,不相等继续递归寻找对应区间. 打印结果时,把所有到达叶节点包含i的区间值相加,就是最后 ...
- HDU1556:Color the ball(简单的线段树区域更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定 ...
- HDU1556 Color the ball [线段树模板]
题意:区间修改序列值,最后输出. //hdu1166 #include<iostream> #include<cstdio> #include<cstring> # ...
- 牛客网 223C 区区区间间间(单调栈)
题目链接:区区区间间间 题意:给出长度为n的数字序列ai,定义区间(l,r)的价值为, 请你计算出. 题解:单调栈求ai左边和右边第一个比它小的位置,需要减去ai的个数为$(R_i-i+1)*(i-L ...
随机推荐
- hdu1525 博弈
/* n%m==0 n状态 n>=2*m 先手决定谁来面对当前的状态,并且可以知道状态,所以先手必胜. */ #include<stdio.h> int min(int x,int ...
- python HTTP请求过程
- nodeJs学习-01 http模块
http模块基础: const http = require("http"); //引入http系统模块 var server = http.createServer(functi ...
- Xcode编译报错:< Apple Mach-O Linker Warning > clang: error: no such file or directory: 'xxxx'
Xcode编译报错概述: clang: error: no such file or directory: 'CoreGraphics' 一般原因是链接库内容导入丢失,这种的排查下target - B ...
- C++大体概况 标签: c++总结 2015-01-31 20:41 792人阅读 评论(15) 收藏
今年又一次报名了二级的C++考试,现在再来把C++总结一下,也不能算是总结,大体提炼了一下需要注意的地方,考试之前打算把这些东西好好看一看,今年一定要过啊! 前两天才知道,unix是用C语言编写的,这 ...
- LeetCode74 Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- Element-ui学习笔记1
1.col,row布局注意事项 el-row el-col gutter就是css,span的时候宽度是按boder-box来计算. 将 type 属性赋值为 'flex',可以启用 flex 布局, ...
- 5款实用的jQuery验证码插件
效果图 文档结构 HTML <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- 微信小程序弹框wx.showModal如何修改样式
由于官方API提供的显示模态弹窗,只能简单地显示文字内容,不能对对话框内容进行自定义,欠缺灵活性,所以自己从模态弹窗的原理角度来实现了自定义的模态对话框. wx.showModal修改样式后的效果,如 ...
- 5分钟了解为什么学习Go
1.什么是Go语言? Google开源 编译型语言 21世纪的C语言(主流编程语言都是单线程环境下发布的) 2.Go语言的特点? 简单易学习(类似python学习难度,自带格式化) 开发效率高 执行性 ...