티스토리 뷰
14413번: Poklon
The output must consist of Q lines, each line containing the answer to a query, respectively.
www.acmicpc.net
백준 소스코드 [C++] 14413 Poklon
#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>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;
int n, m,sqrtn;
int cnt[500001];
int two=0;
void add(int a) {
if (cnt[a] == 2) two--;
cnt[a]++;
if (cnt[a] == 2) two++;
}
void sub(int a) {
if (cnt[a] == 2) two--;
cnt[a]--;
if (cnt[a] == 2) two++;
}
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;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n>>m;
sqrtn = (int)sqrt(n);
vector<int> arr(n);
vector<int> brr(n);
for (int i = 0;i < n;i++)cin >> arr[i];
brr = arr;
sort(all(brr));
make_unique(brr);
for (int i = 0;i < n;i++)arr[i]=lower_bound(all(brr),arr[i])-brr.begin();
int x, y;
vector<query> q(m);
vector<int>res(m);
for (int a = 0;a < m;a++) {
cin >> x >> y;
q[a] = { a,x - 1,y - 1 };
}
sort(q.begin(), q.end(), compare);
int s = q[0].i, e = q[0].j;
for (int a = s;a <= e;a++) add(arr[a]);
res[q[0].idx] = two;
for (int a = 1;a < m;a++) {
while (q[a].i < s)add(arr[--s]);
while (q[a].j > e)add(arr[++e]);
while (q[a].i > s)sub(arr[s++]);
while (q[a].j < e)sub(arr[e--]);
res[q[a].idx] = two;
}
for (int i = 0;i < m;i++) cout << res[i] << '\n';
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 11725 트리의 부모 찾기 (0) | 2021.01.04 |
---|---|
백준 소스코드 [C++] 16978 수열과 쿼리 22 (0) | 2021.01.02 |
백준 소스코드 [C++] 14897 서로 다른 수와 쿼리 1 (0) | 2020.12.25 |
백준 소스코드 [C++] 6515 Frequent values (0) | 2020.12.24 |
백준 소스코드 [C++] 12986 화려한 마을 2 (2) | 2020.12.24 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- MeTal
- IOS
- 네트워크 플로우
- CPU와 Memory
- State Restoration
- 포드 풀커슨 알고리즘
- 부스트캠프 6기
- rxswift
- 강한 순환 참조
- WWDC19
- 네트워크 유량
- 컴퓨터 추상화
- 최단경로 알고리즘
- WWDC17
- 최대 매칭
- observeOn
- mach-o
- CompositionalLayout
- 에드몬드 카프 알고리즘
- WWDC16
- 벨만포드 알고리즘
- 다익스트라 시간복잡도
- 최단경로 문제
- HIG
- Testable
- test coverage
- 코딩대회
- 벨만포드 시간복잡도
- 최단경로문제
- WWDC21
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함