티스토리 뷰
13547번: 수열과 쿼리 5
길이가 N인 수열 A1, A2, ..., AN이 주어진다. 이때, 다음 쿼리를 수행하는 프로그램을 작성하시오. i j: Ai, Ai+1, ..., Aj에 존재하는 서로 다른 수의 개수를 출력한다.
www.acmicpc.net
백준 소스코드 [C++] 13547 수열과 쿼리 5
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
typedef long long ll;
using namespace std;
int n, m,sqrtn;
int arr[100001];
int table[1000001];
struct query {
int idx, i, j;
};
bool compare(query& x,query& y) {
if (x.i / sqrtn != y.i / sqrtn) return x.i / sqrtn < y.i / sqrtn;
return x.j < y.j;
}
vector<query> q;
vector<int>res;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
sqrtn = (int)sqrt(n);
for (int i = 0;i < n;i++)cin >> arr[i];
cin >> m;
q.resize(m);
res.resize(m);
int x, y;
for (int a = 0;a < m;a++) {
cin >> x >> y;
q[a] = {a,x-1,y-1};
}
sort(q.begin(), q.end(),compare);
int ans = 0;
int s = q[0].i, e = q[0].j;
for (int a = s;a <= e;a++) {
if (table[arr[a]] == 0) ans++;
table[arr[a]]++;
}
res[q[0].idx] = ans;
for (int a = 1;a < m;a++) {
while (q[a].i < s) {
if (table[arr[--s]] == 0) ans++;
table[arr[s]]++;
}
while (q[a].j > e) {
if (table[arr[++e]] == 0) ans++;
table[arr[e]]++;
}
while (q[a].i > s) {
table[arr[s]]--;
if (table[arr[s++]] == 0) ans--;
}
while (q[a].j < e) {
table[arr[e]]--;
if (table[arr[e--]] == 0) ans--;
}
res[q[a].idx] = ans;
}
for (int i = 0;i < m;i++) cout << res[i] << '\n';
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 8462 배열의 힘 (0) | 2020.12.24 |
---|---|
백준 소스코드 [C++] 13548 수열과 쿼리 6 (0) | 2020.12.23 |
백준 소스코드 [C++] 1958 LCS 3 (0) | 2020.12.20 |
백준 소스코드 [C++] 11660 구간 합 구하기 5 (0) | 2020.12.20 |
백준 소스코드 [C++] 6603 로또 (0) | 2020.12.19 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- WWDC19
- CPU와 Memory
- 최대 매칭
- CompositionalLayout
- MeTal
- 벨만포드 시간복잡도
- 최단경로 알고리즘
- 포드 풀커슨 알고리즘
- 컴퓨터 추상화
- 벨만포드 알고리즘
- IOS
- 부스트캠프 6기
- State Restoration
- test coverage
- 다익스트라 시간복잡도
- rxswift
- WWDC21
- 코딩대회
- 최단경로 문제
- WWDC16
- 최단경로문제
- Testable
- 네트워크 플로우
- 에드몬드 카프 알고리즘
- WWDC17
- 강한 순환 참조
- observeOn
- mach-o
- 네트워크 유량
- HIG
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함