Pagini recente » Cod sursa (job #918054) | Cod sursa (job #2262343) | Cod sursa (job #797007) | Cod sursa (job #656576) | Cod sursa (job #818814)
Cod sursa(job #818814)
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
const int N = 100005, Nr = 450, inf = 0x3f3f3f3f;
int val[N], a[N], n, nrP, nrQ;
vector<int> app[N];
struct Query{
int nrAp[Nr], x, y;
Query(){
x = y = 0;
}
Query(int _x, int _y){
x = _x;
y = _y;
}
void upt(int poz, int val){
nrAp[poz] += val;
}
int ans(){
int rez = 0;
for (int i = 1 ; i <= nrP ; i++)
if (nrAp[i] == val[i])
++rez;
return rez;
}
} query[N];
struct Per{
int Q, x, y;
void operator=(const Query& X){
Q = (&X) - query;
x = X.x;
y = X.y;
}
} ord[N];
inline bool dupaX(const Per& X, const Per& Y){
return X.x < Y.x;
}
inline bool dupaY(const Per& X, const Per& Y){
return X.y > Y.y;
}
void get_data(){
int x;
cin >> n >> nrQ;
for (int i = 1 ; i <= n ; i++)
app[i].push_back(-inf);
for (int i = 1 ; i <= n ; i++){
cin >> x;
if (x && x <= n)
app[x].push_back(i);
}
for (int i = 1 ; i <= n ; i++)
app[i].push_back(inf);
for (int i = 1 ; i <= nrQ ; i++){
cin >> query[i].x >> query[i].y;
ord[i] = query[i];
}
}
void normalise(){
for (int i = 1 ; i <= n ; i++)
if (i + 2 <= app[i].size()){
val[++nrP] = i;
if (nrP != i)
app[nrP].swap(app[i]);
}
}
void compute(){
normalise();
sort(ord + 1, ord + nrQ + 1, dupaX);
for (int x = 1 ; x <= nrP ; x++)
for (int i = 0 , j = 1 ; i < app[x].size() ; i++)
for ( ; j <= nrQ && ord[j].x <= app[x][i] ; j++)
query[ ord[j].Q ].upt(x, -i + 1);
sort(ord + 1, ord + nrQ + 1, dupaY);
for (int x = 1 ; x <= nrP ; x++)
for (int i = app[x].size() - 1 , j = 1 ; i >= 0 ; i--)
for ( ; j <= nrQ && ord[j].y >= app[x][i] ; j++)
query[ ord[j].Q ].upt(x, i);
}
void print(){
for (int i = 1 ; i <= nrQ ; i++)
cout << query[i].ans() << "\n";
}
int main(){
#ifndef ONLINE_JUDGE
freopen("cf.in", "r", stdin);
freopen("cf.out", "w", stdout);
#endif
get_data();
compute();
print();
return 0;
}