给定一个数列 \(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. vue中组件传值的几种方式

    一.父组件给子组件传值方式(步骤) 1.VC1(子组件)定义props[a,b,c] 注意:props中的每个值都可以加各种修饰,如数据类型,是否可为空,默认值... 2.VC2(父组件)引用子组件 ...

  2. WPF 文本逐字一个个出现的动画效果

    一.效果图: 二.前台代码: <Grid> <TextBlock Foreground="Transparent" x:Name="text" ...

  3. QSS学习

    padding:内边距 margin:外边距用法

  4. Python的入门学习Day 14~15——form”夜曲编程“

    Day 14 time: 2021.8.11. ​ 原文再续,输接上一回.在数据结构之上,我们又开启了新一章"循环"的学习.而今天,我们首先学习了"for"循环 ...

  5. docker-compose任务编排

    一.docker-compose介绍 使用一个Dockerfile模板文件可以定义一个单独的应用容器,如果需要定义多个容器就需要服务编排.下面介绍Docker官方产品,Docker Compose. ...

  6. vue 简单原理

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

  7. vue3 门户网站搭建1-路由

    从 0 到 1搭建门户网站,记录一下. 因为需求不大,所以比较简单,门户和后台管理直接一个项目出来,路由配置则想的是: 1.门户,用  /portal 标识 2.后台管理,用 /admin 标识 3. ...

  8. 集群activemq重启报错java.lang.OutOfMemoryError: GC overhead limit exceeded

    最近安全部门同事说我们环境有个弱密码漏洞,activemq后台的密码不够复杂,需要改为复杂密码. 我登录了他们扫出来的url,输入admin admin,果然进来了.不得不说上一位已离职同事的安全意识 ...

  9. VSCode+EIDE开发CH32V系列RISC-V MCU

    VSCode+EIDE开发CH32V系列RISC-V MCU 1. VS Code Visual Studion Code (VS Code),是一款由微软开发且跨平台的免费源代码编辑器.该软件支持语 ...

  10. C# List间的交集并集差集

    一.简单类型List的交集并集差集 1.先定义两个简单类型的List List<int> listA = new List<int>() { 1, 2, 3, 4, 5, 6, ...