PS

BOJ 26168 : 배열 전체 탐색하기

lickelon 2024. 12. 14. 23:56
  • 문제 링크 : boj.kr/26168
  • 난이도 : S4
  • 태그 : 정렬, 이분탐색

코드

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

#define INF 0x7FFFFFFF

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n, m;
    cin >> n >> m;
    vector<ll> arr(n);
    for(auto &e : arr) cin >> e;
    sort(all(arr));

    while(m--) {
        ll q;
        cin >> q;
        if(q == 1) {
            ll i;
            cin >> i;
            cout << arr.end() - lower_bound(all(arr), i) << "\n";
        }
        if(q == 2) {
            ll i;
            cin >> i;
            cout << arr.end() - upper_bound(all(arr), i) << "\n";
        }
        if(q == 3) {
            ll i, j;
            cin >> i >> j;
            cout << upper_bound(all(arr), j) - lower_bound(all(arr), i) << "\n";
        }
    }

    return 0;
}

풀이

정렬하고, upper_bound, lower_bound를 적절히 사용한다.

728x90