Skip to content

Commit 48f35f4

Browse files
authored
Merge pull request #1023 from AlgorithmWithGod/khj20006
[20251002] BOJ / P2 / 수열과 쿼리 5 / 권혁준
2 parents e74febd + 5513fe4 commit 48f35f4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int N, Q, k, a[100001]{}, b[100000]{}, c[1000001]{};
6+
vector<tuple<int, int, int>> q;
7+
8+
int main(){
9+
cin.tie(0)->sync_with_stdio(0);
10+
11+
cin>>N;
12+
k = sqrt(N);
13+
for(int i=0;i<N;i++) cin>>a[i];
14+
cin>>Q;
15+
q.resize(Q);
16+
int t = 0;
17+
for(auto &[l,r,_]:q) {
18+
cin>>l>>r;
19+
_ = t++;
20+
l--, r--;
21+
}
22+
sort(q.begin(), q.end(), [](auto x, auto y) -> bool {
23+
auto [al, ar, ax] = x;
24+
auto [bl, br, bx] = y;
25+
if(al/k == bl/k) return ar < br;
26+
return al/k < bl/k;
27+
});
28+
29+
int l = 0, r = -1, s = 0;
30+
for(auto [pl,pr,x]:q) {
31+
while(r < pr) if(!c[a[++r]]++) s++;
32+
while(pr < r) if(!--c[a[r--]]) s--;
33+
while(l < pl) if(!--c[a[l++]]) s--;
34+
while(pl < l) if(!c[a[--l]]++) s++;
35+
b[x] = s;
36+
}
37+
for(int i=0;i<Q;i++) cout<<b[i]<<'\n';
38+
39+
}
40+
```

0 commit comments

Comments
 (0)