前缀和

sum[i]表示前i个数的和
每次读入a[i]的时候
sum[i] = sum[i - 1] + a[i]; 查询l ~ r区间的和: sum[r] - sum[l - 1]

差分

即前缀和的修改操作
我们定义pre[i]表示前i个数需要改变的值
则对一个区间l ~ r + k的操作是
pre[r] + k; pre[l - 1] - k;
则多算的1 ~ l - 1部分就抵消,等价与l ~ r + k; 本题中同一关系可能给出多次,需用map或hash判重
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<pair<int,int>,bool> vis;
int a[],b[];
int n,p,h,m;
int main()
{
scanf("%d%d%d%d",&n,&p,&h,&m);//本题中给出的p(最高牛的位置)没有用处
for(int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);// x < y
if(vis[make_pair(x,y)]) continue;//map判重
a[x+]--,a[y]++;//差分 影响从a[x+1]开始 持续到a[y-1]
vis[make_pair(x,y)]=true;
}
for(int i=;i<=n;i++)
{
b[i]=b[i-]+a[i];
printf("%d\n",h+b[i]);
}
return ;
}
 

[USACO07JAN]区间统计Tallest Cow的更多相关文章

  1. bzoj1635 / P2879 [USACO07JAN]区间统计Tallest Cow

    P2879 [USACO07JAN]区间统计Tallest Cow 差分 对于每个限制$(l,r)$,我们建立一个差分数组$a[i]$ 使$a[l+1]--,a[r]++$,表示$(l,r)$区间内的 ...

  2. 洛谷P2879 [USACO07JAN]区间统计Tallest Cow

    To 洛谷.2879 区间统计 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. ...

  3. [Luogu2879][USACO07JAN]区间统计Tallest Cow

    题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a p ...

  4. 洛谷 P2879 [USACO07JAN]区间统计Tallest Cow

    传送门 题目大意: n头牛,其中最高身高为h,给出r对关系(x,y) 表示x能看到y,当且仅当y>=x并且x和y中间的牛都比 他们矮的时候,求每头牛的最高身高. 题解:贪心+差分 将每头牛一开始 ...

  5. 题解 P2879 【[USACO07JAN]区间统计Tallest Cow】

    题目链接: https://www.luogu.org/problemnew/show/P2879 思路: 先不管最大高度,我们读入一对x,y.说明,x+1~y-1之间牛的身高都小于x,y. 然后不妨 ...

  6. [Luogu] 区间统计Tallest Cow

    https://www.luogu.org/problemnew/show/P2879 差分 | 线段树 #include <iostream> #include <cstdio&g ...

  7. Tallest Cow POJ - 3263 (区间点修改)

    FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positi ...

  8. 【BZOJ】1635: [Usaco2007 Jan]Tallest Cow 最高的牛(差分序列)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1635 差分序列是个好东西啊....很多地方都用了啊,,, 线性的进行区间操作orz 有题可知 h[a ...

  9. POJ 3263 Tallest Cow 题解

    题目 FJ's \(N (1 ≤ N ≤ 10,000)\) cows conveniently indexed 1..N are standing in a line. Each cow has a ...

随机推荐

  1. 一站式WPF--依赖属性(DependencyProperty)一

    Windows Presentation Foundation (WPF) 提供了一组服务,这些服务可用于扩展公共语言运行时 (CLR) 属性的功能,这些服务通常统称为 WPF 属性系统.由 WPF ...

  2. golang在import自己的包报错问题

    原因:使用git clone项目后,项目根路径是小写英文名称,比如cmdbapi,但是项目里面的import导入自己的相关包时,红色报错 解决:把项目名称改写成import导入包的名称,即cmdbAp ...

  3. Html5之localStorage和sessionStorage缓存

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  4. font-family:黑体;导致css定义全部不起作用

    css文件里font-family: "黑体";这句会导致后面的css定义全部不起作用了. 只要把font-family: "黑体"; 改成 font-fami ...

  5. DateTimeFormatter

    //解析日期 String dateStr= "2018年12月18日"; DateTimeFormatter formatter = DateTimeFormatter.ofPa ...

  6. 学习JDK1.8集合源码之--LinkedHashSet

    1. LinkedHashSet简介 LinkedHashSet继承自HashSet,故拥有HashSet的全部API,LinkedHashSet内部实现简单,核心参数和方法都继承自HashSet,只 ...

  7. Jmeter压测报错:Non HTTP response code: java.net.ConnectExceptionexception的解决办法

    前一段时间进行jmeter压测时,一直报错,查看了下日志才发现报了一堆Non HTTP response code: java.net.ConnectExceptionexception,直接jmet ...

  8. python ndarray相关操作:拼接

  9. spark-ML基础

    一.ML组件 ML的标准API使用管道(pipeline)这样的方式,可以将多个算法或者数据处理过程整合到一个管道或者一个流程里运行,其中包含下面几个部分: 1. dataFrame:用于ML的dat ...

  10. ios开发ARC,IBOutlets之strong与weak

    今天在写程序的时候,用IBOutlets连了一个自定义的控件,出现了问题,后面访问的时候,控件里有些subviews没有初始化好,取到的时候为nil, 程序里用了ARC, IBOutlets一连接上, ...