-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
78 lines (75 loc) · 1.47 KB
/
C.cpp
File metadata and controls
78 lines (75 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<bits/stdc++.h>
#define vi vector<int>
#define mp map<int,int>
#define ump unordered_map <int,int>
#define ll long long int
#define pb push_back
#define mod 1000000007
#define int long long
#define loop(i,x,n) for(int i=x;i<n;i++)
#define rloop(i,n) for(int i=n-1;i>=0;i--)
using namespace std;
/*************************************************************************************************/
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,q,x,y,an=0;
cin>>n>>q;
int a[n];
loop(i,0,n)
{
cin>>a[i];
}
sort(a,a+n);
pair <int,int> p[q];
vi v(n,0);
loop(i,0,q)
{
cin>>x>>y;
p[i].first=x;
p[i].second=y;
if(y==n)
{
v[x-1]++;
}
else
{
v[x-1]++;
v[y]--;
}
}
loop(i,1,n)
{
v[i]+=v[i-1];
}
multimap <int,int> m;
loop(i,0,n)
{
m.insert({v[i],i});
}
vi ans(n);
int j=0;
for(auto it=m.begin();it!=m.end();it++)
{
ans[it->second]=a[j];
j++;
}
loop(i,1,n)
ans[i]+=ans[i-1];
loop(i,0,q)
{
x=p[i].first;
y=p[i].second;
if(x==1)
{
an+=ans[y-1];
}
else
{
an+=ans[y-1]-ans[x-2];
}
}
cout<<an;
return 0;
}