A problem of winter vacation homework of Fujian Engineering College

Source: Internet
Author: User

Problem Description

Gives a sequence of positive integers from an increment a0,a1......an-1
Corresponding results for different query outputs

Input

Only one set of data
The first line is an integer n, which represents the length of the sequence (subscript starts from 0 to n-1)
The next n integers, representing the number of the corresponding position in the sequence
And then the next line is an integer m representing the number of queries
Next M line
Each of the next lines is one of these 5 types
0 x represents the largest subscript for querying the number x, if there is no X output-1
1 x represents the smallest subscript for querying the number x, if there is no X output-1
2 x y represents the number of queries that are greater than or equal to x and less than or equal to Y, and that x<=y
3 x represents the size of the number of queries larger than x and the lowest subscript, if there is no digital output greater than X-1
4 x represents the size of the number that the query is smaller than x and the highest subscript, if there is no digital output less than X-1
N,m<10^5
0<=ai,x,y<10^9

Output

Output corresponding result, one row per result

Sampleinput
10 1 1 1 3 4 5 5 5 5 850 51 52 1 53 44 8
Sampleoutput
85955

The problem is the application of the dichotomy method. Then you can choose if.
Below is a basic template of two points

According to the question request, query the number x to appear the largest subscript, then Find_upper_bound (q)-1 is the last position that X appears

The query represents the smallest subscript that appears in the query number x, then Find_upper_bound (q-1) is the first position where x appears

To query for numbers greater than or equal to x and less than or equal to Y, and to guarantee x<=y, you need the last position where y appears minus the first position where x appears, and then +1.

To query the size of a number larger than X and the lowest subscript, then Find_upper_bound (q) is the first position greater than X, which requires a number, so A[find_upper_bound (q)]

Represents the size of the number that the query is smaller than X and the largest subscript, then Find_upper_bound (q-1)-1 is the first position less than X, and the question is also required to be a number.

When Find_upper_bound (q) ==find_upper_bound (q-1), X does not exist.

The above is the problem.

A problem of winter vacation homework of Fujian Engineering College

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.