C. Little Girl and Maximum Sum

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

The little girl loves the problems on array queries very much.

One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). You need to find for each query the sum of elements of the array with indexes from li to ri, inclusive.

The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum.

Input

The first line contains two space-separated integers n (1 ≤ n ≤ 2·105) and q (1 ≤ q ≤ 2·105) — the number of elements in the array and the number of queries, correspondingly.

The next line contains n space-separated integers ai (1 ≤ ai ≤ 2·105) — the array elements.

Each of the following q lines contains two space-separated integers li and ri (1 ≤ li ≤ ri ≤ n) — the i-th query.

Output

In a single line print a single integer — the maximum sum of query replies after the array elements are reordered.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

inputCopy

3 3

5 3 2

1 2

2 3

1 3

outputCopy

25

inputCopy

5 3

5 2 4 1 3

1 5

2 3

2 3

outputCopy

33

【题意】:有若干对区间和的查询,问如何重组数组使查询结果的和最大。

【分析】:统计每个数出现的次数,则总和最大的策略为:依次让最大的数放在出现查询区间最多的位置上。

重点在于如何快速的统计每个区间总共被查询的次数。可使用线段树的区间更新,也可使用巧妙一点的方法——差分标记,通过一个数组记录下每次查询的 L、 R,然后 cnt[L]++,cnt[R+1]--,然后统计cnt数组前缀和,因为是cnt[R+1]--,所以从头开始遍历计算前缀和不会多统计那些没有查询的区间的次数。

【代码】:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
typedef long long ll;
ll p[maxn];
ll x[maxn];
ll n,q;
ll a[maxn];
int main()
{
while(~scanf("%lld%lld",&n,&q))
{
memset(p,0,sizeof(p));
for(ll i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
while(q--)
{
ll l,r;
scanf("%lld%lld",&l,&r);
p[l]++;
p[r+1]--;
}
for(ll i=1; i<=n; i++)
{
p[i] += p[i-1];
}
sort(p+1,p+n+1);
sort(a+1,a+n+1);
ll sum = 0;
for(ll i=n;i>=1;i--)
{
sum += (ll)(a[i]*p[i]);
}
cout<<sum<<endl;
}
}

CF 276C Little Girl and Maximum Sum【贪心+差分】的更多相关文章

  1. [CF 276C]Little Girl and Maximum Sum[差分数列]

    题意: 给出n项的数列A[ ], q个询问, 询问 [ l, r ] 之间项的和. 求A的全排列中该和的最大值. 思路: 记录所有询问, 利用差分数列qd[ ], 标记第 i 项被询问的次数( 每次区 ...

  2. cf#513 B. Maximum Sum of Digits

    B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

  3. 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 ...

  4. 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值

    [抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

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

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

  6. ural 1146. Maximum Sum

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

  7. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

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

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

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

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

随机推荐

  1. 【bzoj2705】[SDOI2012]Longge的问题 欧拉函数

    题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). 输入 一个整数,为N. 输出 ...

  2. bootstrap-table 回显选中行,行样式

    { filed:'status', checkbox:true, formatter:function(value,row,index){ if (row.status == 1) //根据行里字段判 ...

  3. HDU 6191 Query on A Tree(可持久化Trie+DFS序)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  4. Conjugate 解题报告

    Conjugate 问题描述 在不存在的 \(\text{noip day3}\) 中,小 \(\text{w}\) 见到了一堆堆的谜题. 比如这题为什么会叫共轭? 他并不知道答案. 有 \(n\) ...

  5. BZOJ day2

    十六题...(好难啊) 1051105910881191119214321876195119682242243824562463276128184720

  6. 【BZOJ 3505】 [Cqoi2014]数三角形 容斥原理+排列组合+GCD

    我们先把所有三角形用排列组合算出来,再把一行一列上的三点共线减去,然后我们只观察向右上的三点共线,向左上的乘二即可,我们发现我们如果枚举所有的两边点再乘中间点的个数(GCD),那么我们发现所有的两边点 ...

  7. [Python]安装完pip、pygame后,仍然import pygame报错

    按照<python编程从入门到实践>上的教程下载了pygame的whl文件进行安装, 在cmd窗口里import pygame提示无错误,在IDEL里程序也能正常运行, 但是pycharm ...

  8. iOS12、iOS11、iOS10、iOS9常见适配

    作者:花丶满楼 链接:https://juejin.im/post/5c49a7d0518825254e4d46fc 一.iOS12(Xcode10) 1.1.升级Xcode10后项目报错 不允许多个 ...

  9. DecimalFormat中格式化问题

    一:前言 每天自己斗会看到新的东西,每天自己都会学到东西,但是觉得自己老是想一口吃一个胖子.每天看到一个知识点都把其收藏了,但是自己也没有时间去看,不知道自己到底想感谢什么.真是自己无语,本来说是把自 ...

  10. jsp中的路径问题

    在学jsp的时候我每次都遇到路径的问题,每次都不能够说100%的把这搞定,这让我很烦恼,今天下午花了点时间来把路径问题整理了下. 一:首先我们在加载项目(我的项目名称是FinalExam)是的路径是h ...