- 문제 링크 : 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
'PS' 카테고리의 다른 글
BOJ 20006 : 랭킹전 대기열 (0) | 2024.12.16 |
---|---|
BOJ 2942 : 퍼거슨과 사과 (1) | 2024.12.15 |
BOJ 24511 : queuestack (0) | 2024.12.14 |
BOJ 31216 : 슈퍼 소수 (1) | 2024.12.12 |
BOJ 16956 : 늑대와 양 (0) | 2024.12.11 |