Source Code
#include <bits/stdc++.h> 
#define int long long
#define f first
#define s second
#define pb push_back
#define endl "\n"
#define SORT(v) sort(begin(v), end(v))
using namespace std;
int32_t main(){ ios::sync_with_stdio(false);cin.tie(0);
    int n,q;
    cin >> n >> q;
    vector<int> v(n);
    int add = n;
    vector<int> a(2*1e5,1);
    int mx = 0;
    for(int i = 0;i<n;i++){
        cin >> v[i];
        mx = max(mx,v[i]);
    }
    SORT(v);
    while(q--){
        int l,r;cin >> l >> r;
        r--;
        a[r] = 0;
    }
    int all = 0;
    for(int i = 0;i<mx;i++){
        int l = 0;
        int r = n-1;
        int ans = n+1;
        while(l<=r){
            int m = l + (r - l)/ 2;
            if(v[m] >= (i + 1)){
                ans = min(ans,m);
                r = m - 1;
            }
            else l = m + 1;
        }
        if(ans == n+1){
            break;
        }
        if(a[i]){
            all += n - ans;
        }
    }
    cout << all;
}
Copy
Dr. Evil barakat
GNU G++17
103 ms
4.1 MB
Accepted