|
| 1 | +#include <iostream> |
| 2 | +#include <cstring> |
| 3 | +#include <queue> |
| 4 | +#include <algorithm> |
| 5 | +#include <cstdio> |
| 6 | +using namespace std; |
| 7 | + |
| 8 | + |
| 9 | +const int MAXN = 200001; |
| 10 | + |
| 11 | +int trees[MAXN*8]; |
| 12 | +int mark[MAXN*8]; |
| 13 | +int data[MAXN]; |
| 14 | + |
| 15 | +int N, Q; |
| 16 | + |
| 17 | +void pushUp(int idx){ |
| 18 | + trees[idx] = trees[idx<<1] + trees[idx<<1|1]; |
| 19 | +} |
| 20 | + |
| 21 | +void pushDown(int idx){ |
| 22 | + if(mark[idx]){ |
| 23 | + //cout<<"now at "<<idx<<" and i'm pushing down"<<endl; |
| 24 | + mark[idx<<1] = mark[idx<<1|1] = mark[idx]; |
| 25 | + trees[idx<<1] += trees[idx]; |
| 26 | + trees[idx<<1|1] += trees[idx]; |
| 27 | + |
| 28 | + trees[idx] = 0; |
| 29 | + |
| 30 | + mark[idx] = 0; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +void insert(int L, int R, int l, int r, int idx){ |
| 35 | + //cout<<"try to insert "<<L<<" "<<R<<" "<<l<<" "<<r<<endl; |
| 36 | + if(L<=l && R>=r){ |
| 37 | + //cout<<"insert!"<<endl; |
| 38 | + trees[idx] += 1; |
| 39 | + //ncout<<" idx = "<<idx<<" cur = "<<trees[idx]<<endl; |
| 40 | + mark[idx] = 1; |
| 41 | + return ; |
| 42 | + } |
| 43 | + pushDown(idx); |
| 44 | + int m = (l+r)>>1; |
| 45 | + if(L<=m){ |
| 46 | + insert(L, R, l, m, idx<<1); |
| 47 | + } |
| 48 | + if(R>m){ |
| 49 | + insert(L, R, m+1, r, idx<<1|1); |
| 50 | + } |
| 51 | + //pushUp(idx); |
| 52 | +} |
| 53 | + |
| 54 | +int query(int L, int R, int l, int r, int idx){ |
| 55 | + if(L<=l && R>=r){ |
| 56 | + return trees[idx]; |
| 57 | + } |
| 58 | + pushDown(idx); |
| 59 | + int m = (l+r)>>1; |
| 60 | + if(L<=m){ |
| 61 | + return query(L, R, l, m, idx<<1); |
| 62 | + } |
| 63 | + if(R>m){ |
| 64 | + return query(L, R, m+1, r, idx<<1|1); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +void build(int l, int r, int idx){ |
| 70 | + if(l==r){ |
| 71 | + trees[idx] = 0; |
| 72 | + return ; |
| 73 | + } |
| 74 | + int m = (l+r)>>1; |
| 75 | + build(l, m, 2*idx); |
| 76 | + build(m+1, r, 2*idx + 1); |
| 77 | + pushUp(idx); |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +int main(){ |
| 84 | + memset(trees,0,sizeof(trees)); |
| 85 | + memset(mark, 0, sizeof(mark)); |
| 86 | + scanf("%d %d", &N,&Q); |
| 87 | + |
| 88 | + |
| 89 | + //build(1, N, 1); |
| 90 | + for(int i=0; i<N; i++){ |
| 91 | + scanf("%d",&data[i]); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + int left, right; |
| 96 | + for(int i=0; i<Q; i++){ |
| 97 | + //cout<<"inserting -> "<<i<<" tree[1] = "<<trees[1]<<endl; |
| 98 | + scanf("%d %d", &left, &right); |
| 99 | + insert(left, right, 1, N, 1); |
| 100 | + } |
| 101 | + long long int qts[MAXN]; |
| 102 | + //cout<<"query 1 1 "<<query(1,1,1,N,1)<<endl; |
| 103 | + for(int i=0; i<N; i++){ |
| 104 | + qts[i] = query(i+1, i+1, 1, N, 1); |
| 105 | + //cout<<"querying "<<i+1<<" res = "<<qts[i]<<endl; |
| 106 | + } |
| 107 | + sort(qts, qts+N, std::greater<int>()); |
| 108 | + sort(data, data+N , std::greater<int>()); |
| 109 | + long long int ans = 0; |
| 110 | + for(int i=0; i<N; i++){ |
| 111 | + ans += (qts[i])*data[i]; |
| 112 | + //cout<<"qts["<<i<<"] = "<<qts[i]<<" and data["<<i<<"] = "<<data[i]<<endl; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + cout<<ans<<endl; |
| 117 | + |
| 118 | + return 0; |
| 119 | +} |
| 120 | + |
0 commit comments