The key to this question is to find 10 ^ 5 ~~ Up to 450 x. in this case, 1 + 2 + 3 + 4 + .... + n <= 100000 .... n <450... this problem is much simpler .... first, find the 450 numbers .. then, use h [100000] [450] to record the sum of the numbers that meet the requirements in each position... to construct h] [], you must perform 10 ^ 5*450 = 4.5*10 ^ 7 operations ~~ Acceptable
Then the query is very fast, because the number of these 450 numbers is already 1 ~ The number of I (1 <= I <= n ~ You just need to check the number of these 450 numbers in this range ~~
Program:
[Cpp]
# Include <iostream>
# Include <stdio. h>
# Include <algorithm>
# Include <string. h>
# Include <math. h>
# Include <map>
# Include <queue>
# Include <stack>
# Define ll long
# Define OOS 1000000000
# Define pi acos (-1)
Using namespace std;
Struct node
{
Int x, num;
} A [100005];
Int n, t, m, k, I, j, s [100005], B [450], h [100005] [450];
Map <int, int> mymap;
Int main ()
{
While (~ Scanf ("% d", & n, & t ))
{
Mymap. clear ();
M = 0;
For (I = 1; I <= n; I ++) scanf ("% d", & s [I]);
For (I = 1; I <= n; I ++)
{
If (mymap. find (s [I]) = mymap. end ())
{
Mymap [s [I] = ++ m;
A [m]. x = s [I];
A [m]. num = 0;
}
A [mymap [s [I]. num ++;
}
K = 0;
For (I = 1; I <= m; I ++)
If (a [I]. x <= a [I]. num)
B [++ k] = a [I]. x;
For (I = 1; I <= k; I ++)
{
H [0] [I] = 0;
For (j = 1; j <= n; j ++)
If (s [j] = B [I]) h [j] [I] = h [J-1] [I] + 1;
Else h [j] [I] = h [J-1] [I];
}
Int l, r, ans;
While (t --)
{
Ans = 0;
Scanf ("% d", & l, & r );
For (I = 1; I <= k; I ++)
If (h [r] [I]-h L-1] [I] = B [I]) ans ++;
Printf ("% d \ n", ans );
}
}
Return 0;
}