CodeForces - 276C Little Girl and Maximum Sum

2455 단어 단순 알고리즘
C. Little Girl and Maximum Sum
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 %I64dspecifier.
Examples
input
3 3
5 3 2
1 2
2 3
1 3

output
25

input
5 3
5 2 4 1 3
1 5
2 3
2 3

output
33

제목: n개수, m개 지령을 입력하고 각 지령은 l, r로 구간을 표시하며 n개수로 구성된 수열에서 각 지령구간 lr간의 원소를 합쳐서 마지막 모든 구간의 합을 최대로 하고 출력의 최대치를 나타낸다.
사고방식: 큰 숫자는 가능한 한 많이 나타나고 소수는 가능한 한 적게 나타난다.아래 첨자마다 나타나는 횟수를 계산하고 정렬하면 됩니다.
아래 첨자가 나타나는 횟수를 계산하는 방법은 다음과 같습니다.
창설 수조는 아래 표시가 나타나는 횟수를 저장하고 수조는 0으로 초기화하며 명령을 입력할 때마다 왼쪽 경계를 +1, 오른쪽 경계의 뒷자리를 -1로 하고 수조에 대해 접두사와 합을 구하면 된다.(이 원리를 나는 이해하지 못했다)
#include
#include
#define maxn 200010
using namespace std;
long long a[maxn],b[maxn];
int main()
{
    long long n,q,i,l,r,ans;

    while(scanf("%lld%lld",&n,&q)!=EOF)
    {
        for(i=0;i

좋은 웹페이지 즐겨찾기