给定一个数列 \(a= { a_1,a_2,...,a_n }\) 以及 \(q\) 次查询。

其中第 \(i\) 次查询如同:\(l_i, r_i\),意指求 \(\sum_{j=l_i}^{r_i} {a_j}\)。

但是查询前可以对数列任意排序,使得查询结果的和最大,问最大的和是多少。

Input

第 \(1\) 行 \(n,q\);

第 \(2\) 行 \(a_1, a_2, ..., a_n\);

接下来 \(q\) 行,每行 \(2\) 个整数 \(l_i,r_i\);

数据范围:\(1≤n,q,a_i≤2·10^5,1≤l_i≤r_i≤n\)。

Output

最大的和

Sample Input

3 3
5 3 2
1 2
2 3
1 3

Sample Output

25

分析

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+10,INF=0x3f3f3f3f;
int n,m,a[N],b[N];
LL res=0; //#define local
int main() {
#ifdef local
freopen("data.in", "r", stdin);
// freopen("data.out", "w", stdout);
#endif int n,q; cin>>n>>q;
for(int i=1; i<=n; i++) cin>>a[i];
int l,r;
while(q--) {
cin>>l>>r; b[l]++, b[r+1]--;
}
for(int i=1; i<=n; i++) b[i]+=b[i-1]; sort(a+1, a+1+n); sort(b+1, b+1+n);
for(int i=1; i<=n; i++) res += 1ll*a[i]*b[i];
cout<<res;
return 0;
}

Little Girl and Maximum Sum CodeForces - 276C - 差分的更多相关文章

  1. CF 276C Little Girl and Maximum Sum【贪心+差分】

    C. Little Girl and Maximum Sum time limit per test2 seconds memory limit per test256 megabytes input ...

  2. CodeForces 1060 B Maximum Sum of Digits

    Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...

  3. POJ2479 Maximum sum[DP|最大子段和]

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 Des ...

  4. ural 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  5. UVa 108 - Maximum Sum(最大连续子序列)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  6. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

  7. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  8. ural 1146. Maximum Sum(动态规划)

    1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...

  9. UVa 10827 - Maximum sum on a torus

    题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...

  10. POJ 2479 Maximum sum 解题报告

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40596   Accepted: 12663 Des ...

随机推荐

  1. javaweb常用的配置文件

    1.applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  2. WDA学习(26):Phase Indicator使用

    1.19 UI Element:Phase Indicator使用 本实例测试创建Phase Indicator; 运行结果: 1.创建Component,View: V_PHASE_IND; 2.创 ...

  3. js 防抖

    // 防抖 作用:防止重复触发事件 var count = 1; var container = document.getElementById('container'); function getU ...

  4. Jenkins Pipeline(一) - 创建一个新的pipeline

    jenkins pipeline: 使用Goovy语法,以代码形式更自由地编写jenkins job. 代码文本被称为Jenkinsfile IDE: vscode 推荐安装vscode插件: jen ...

  5. 【layui】下拉控件dropdown 简单的使用

    官方网站地址: http://test.microanswer.cn/page/dropdown.html 1.代码 layui.use(['index', 'table', 'form', 'dro ...

  6. windows导出当前目录结构

    cd 进入目录 tree /f>>tree.txt

  7. 17.SQLite数据库存储

    Android系统内置一个SQLite数据库,SQLite是一款轻量级的关系型数据库,它的运算速度非常快,占用资源很少,通常只需要几百K的内存就足够了. SQLite不仅支持标准的SQL语法,还遵循了 ...

  8. js实现数字每三位加逗号

    需求: 一个数字,比如 1234,23456.23 实现每三位加逗号 改成如下形式: 1234 => 1,234 23456.23 => 23,456.23 方法一 function fo ...

  9. pillow 创建图片并添加一些自定义信息

    from PIL import Image vm = Image.new('RGBA', (dshape[1], dshape[0])) vm = Image.fromarray(np.array(s ...

  10. Linux FTP服务器配置文件vsftpd.conf 配置

    配置文件/etc/vsftpd/vsftpd.conf local_enable=YES            write_enable=YESlocal_umask=022dirmessage_en ...