code:
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const int maxn = 1e5 + 5;
priority_queue<int, vector<int>, greater<int>> a[maxn];
int c[maxn];
multiset<int> s;
int main(){
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
scanf("%d", c + i);
}
c[n + 1] = INF;
while(a[n + 1].size()){
a[n + 1].pop();
}
for(int i = 1, s, e; i <= m; i++){
scanf("%d%d", &s, &e);
a[s].push(e + 1);
}
int ans = 0;
for(int i = 1; i <= n + 1; i++){
while(s.size() && *s.begin() == i) s.erase(s.begin()), ans++;
if(i == n + 1) break;
if(s.size() > c[i]){
multiset<int> :: iterator it = s.end();
it--;
while(s.size() > c[i]) {
multiset<int> :: iterator temp = it;
s.erase(temp);
it--;
}
}
while(true){
if(!a[i].size()) break;
int cur = a[i].top();
a[i].pop();
if(s.size() < c[i]){
s.insert(cur);
continue;
}
multiset<int> :: iterator it = s.end();
it--;
if(*it > cur){
s.erase(it);
s.insert(cur);
}
else{
break;
}
}
}
printf("%d\n", ans);
return 0;
}
在我们校内OJ上用C++11(NOI)才能过,luoguRE,求调。