CodeForces Round #136 (220B)-Little Elephant and Array

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.