[BOJ] 13537 수열과 쿼리 1

수열과 쿼리 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>

#define MAX_N 100001
#define ll long long
#define all(a) begin(a), end(a)
using namespace std;

ll n, m;
ll A[MAX_N];
vector<ll> seg[MAX_N * 4];
void init(ll u, ll s, ll e) {
seg[u] = vector<ll>(A + s, A + e + 1);
sort(all(seg[u]));
if(s != e) {
ll m = (s + e) >> 1;
init(u * 2, s, m);
init(u * 2 + 1, m + 1, e);
}
}
ll query(ll u, ll s, ll e, ll l, ll r, ll v) {
if(r < s or e < l) return 0ll;
if(l <= s and e <= r) {
return seg[u].size() - (upper_bound(begin(seg[u]), end(seg[u]), v) - begin(seg[u]));
}
ll m = (s + e) >> 1;
return query(u * 2, s, m, l, r, v) + query(u * 2 + 1, m + 1, e, l, r, v);
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
for(ll i = 1; i <= n; i++) cin>>A[i];
init(1, 1, n);
cin>>m;
for(ll i = 0, s, e, v; i < m; i++) {
cin>>s>>e>>v;
cout<<query(1, 1, n, s, e, v)<<'\n';
}
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/21/PS/BOJ/13537/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.