Interval Maximum (C++) kkmd66

error prone

If the timeout is exceeded, first consider replacing cin and cout with scanf and printf;
secondly, consider reducing the number of loops, here we choose to sort in advance;

Description:

There is a sequence of length n (the subscript starts from 1), and there are q queries, each querying the minimum value between the interval [l, r].

Input:

There are multiple sets of data.

For each set of data, the first row has an n, 1<=n<=100000, the second row has n integers, representing a sequence, the third row is a number q, 1<=q<=100000,

Next q lines, each line has two numbers l, r, 1<=l<=r<=n.

Output:

For each query, output the minimum value on one line.

Sample Input:

5
3 1 2 4 0
2
1 2
2 5

Sample Output:

1
0

#include <iostream>
#include <algorithm>
#include "vector"

using namespace std;

class arr {
    
    
public:
    int m_data;
    int m_id;
};

/**
 * kkmd66
 * @param a1
 * @param a2
 * @return
 */

bool mCompare(const arr &a1, const arr &a2) {
    
    
    return a1.m_data < a2.m_data;
}

/**
 * kkmd66
 * @return
 */

int main() {
    
    

    int n;
    while (scanf("%d", &n) != EOF) {
    
    

        //放入
        vector<arr> vector(n);
        for (int i = 0; i < n; ++i) {
    
    
            scanf("%d", &vector[i].m_data);
            vector[i].m_id = i + 1;
        }

        //排序
        sort(vector.begin(), vector.end(), mCompare);

        int q;
        scanf("%d",&q);
        for (int i = 0; i < q; ++i) {
    
    
            int a, b;
            scanf("%d %d",&a,&b);
            for (int j = 0; j < vector.size(); ++j) {
    
    
                //满足条件弹出
                if (vector[j].m_id >= a && vector[j].m_id <= b) {
    
    
                    printf("%d\n",vector[j].m_data);
                    break;
                }
            }
        }
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324224725&siteId=291194637