#include #include #include

using namespace std;

int main() { int n, q; cin >> n >> q;

vector<int> ages(n);
for (int i = 0; i < n; i++) {
    cin >> ages[i];
}

for (int i = 0; i < q; i++) {
    int x;
    cin >> x;

    auto lower = lower_bound(ages.begin(), ages.end(), x);
    auto upper = upper_bound(ages.begin(), ages.end(), x);

    if (lower == upper) {
        cout << "-1 -1" << endl;
    } else {
        cout << lower - ages.begin() + 1 << " " << upper - ages.begin() << endl;
    }
}

return 0;
描述有n位同学按照年龄从小到大排好队。王老师想要查询年龄为x的同学在队伍中首次出现的位置和最后一次出现的位置;如果队伍中不存在年龄为x的同学请输出-1。由于人数太多一个一个数太慢啦请你编程求解。请注意:本题中王老师查询年龄x出现的起止位置并不是查询了1次而是查询了q次。比如:假设有6位同学的年龄为:1 2 2 2 3 3王老师查询了4个年龄分别是2 1 3 8那么:年龄为2的同学首次和最后一次出现

原文地址: https://www.cveoy.top/t/topic/iOAo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录