codeforces740B
Alyona and flowers
Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative.
Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied by the number of chosen subarrays the flower is in.
For example, consider the case when the mother has 5 flowers, and their moods are equal to 1, - 2, 1, 3, - 4. Suppose the mother suggested subarrays (1, - 2), (3, - 4), (1, 3), (1, - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then:
- the first flower adds 1·1 = 1 to the girl's happiness, because he is in one of chosen subarrays,
- the second flower adds ( - 2)·1 = - 2, because he is in one of chosen subarrays,
- the third flower adds 1·2 = 2, because he is in two of chosen subarrays,
- the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays,
- the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays.
Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!
Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of flowers and the number of subarrays suggested by the mother.
The second line contains the flowers moods — n integers a1, a2, ..., an ( - 100 ≤ ai ≤ 100).
The next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers li and ri (1 ≤ li ≤ ri ≤ n) denoting the subarray a[li], a[li + 1], ..., a[ri].
Each subarray can encounter more than once.
Output
Print single integer — the maximum possible value added to the Alyona's happiness.
Examples
5 4
1 -2 1 3 -4
1 2
4 5
3 4
1 4
7
4 3
1 2 3 4
1 3
2 4
1 1
16
2 2
-1 -2
1 1
1 2
0
Note
The first example is the situation described in the statements.
In the second example Alyona should choose all subarrays.
The third example has answer 0 because Alyona can choose none of the subarrays.
sol:这题意TMD居然看成了一道数据结构题(智减inf)
搞出前缀和,显然如果是正数就算入答案,否则不算
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,a[N],Qzh[N];
int S[N];
int main()
{
int i,j,ans=;
R(n); R(m);
for(i=;i<=n;i++) R(a[i]);
for(i=;i<=n;i++) Qzh[i]=Qzh[i-]+a[i];
for(i=;i<=m;i++)
{
int l=read(),r=read();
if(Qzh[r]-Qzh[l-]>) ans+=(Qzh[r]-Qzh[l-]);
}
Wl(ans);
return ;
}
/*
input
5 4
1 -2 1 3 -4
1 2
4 5
3 4
1 4
output
7 input
4 3
1 2 3 4
1 3
2 4
1 1
output
16 input
2 2
-1 -2
1 1
1 2
output
0
*/
codeforces740B的更多相关文章
- CodeForces-740B Alyona and flowers
题目要求选择一些花的集合,如果暴力去枚举每种选择方法明显是不行的.换种方式考虑:每一个集合都能为最后的答案做出要么正的.要么负的.要么0贡献,先把所有集合能做出的贡献预处理,然后从m个集合里面选择贡献 ...
随机推荐
- Django:安装和启动
最近在学习利用python语言进行web站点开发,使用的框架是Django.这篇博客主要介绍Django的安装和简单使用. 一.Django介绍 Django是一个开源的Web应用框架,由Python ...
- LeetCode11:Container With Most Water
public int MaxArea(int[] height) { ; ; ; while(start<end) { max=Math.Max(max,Math.Min(height[star ...
- Spring_AOP 记录系统关键操作日志用法
问题: 系统需要记录用户的关键操作日志,以便后期的系统维护,方便的查看问题,及时排除 分析: (1)保存字段:作为一个日志记录功能,首先数据库新建一张表保存用户的操作关键字段, 用户名,ip,操作描述 ...
- ASP.NET MVC必须知道的那些事!
MVC的由来: 在MVC模式之前,View界面的呈现.用户交互操作的捕捉与相应.业务流程的执行以及数据的存储等都是在一起的,这种设计模式叫自治视图. 这重设计模式主要存在三大弊端: 重用性:业务逻辑与 ...
- 官宣 ! 52abp_Pro版本低调上线
上线了 官宣!官宣!官宣! 重要的事情说三次. 是的,你没有看错 https://www.52abp.com/ 的官方企业模板:52abp-pro已经正式上线. 至此52abp.com的官方网站就算告 ...
- MemSQL与MySQL不兼容问题总结
1.数据行Update更新数据行时,如果数据行没有变化,MySQL返回受影响的数据行数为1,但MemSQL返回的数据行数为0. 2.MemSQL不支持唯一约束 3.MemSQL不支持外键约束
- python之socket模块详解--小白博客
主要是创建一个服务端,在创建服务端的时候,主要步骤如下:创建socket对象socket——>绑定IP地址和端口bind——>监听listen——>得到请求accept——>接 ...
- 牛客---java练习
一. 1. abstract可以修饰方法和类,不能修饰属性.抽象方法没有方法体,即没有大括号{}.抽象类中的成员属性都是public static final类型的:成员方法都是public abst ...
- javascript与php与python的函数写法区别与联系
1.javascript函数写法种类: (一).第一种 function test(param){ return 111; } (二).第二种 var test = function(param){ ...
- JSP页面的基本元素
JSP页面元素构成:静态内容.指令.表达式.小脚本.声明.注释. JSP指令包括: page指令:通常位于jsp页面的顶端,同一个页面可以有多个page指令. include指令:将一个外部文件嵌入到 ...