Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- LCA
- Union-Find
- 2-sat
- MST
- Bridge
- backtracking
- BinarySearch
- IndexedTree
- DFS
- Math
- Dijkstra
- DP
- LIS
- Floyd
- Flow
- TwoPointers
- Sweeping
- topologicalsort
- Implementation
- ShortestPath
- KMP
- Tree
- ArticulationPoint
- greedy
- SlidingWindow
- BFS
- Bellman-Ford
- mergesorttree
- FenwickTree
- scc
Archives
- Today
- Total
정리충의 정리노트
[백준] 9342: 디지털 비디오 디스크(DVDs) 본문
0. 문제 주소
https://www.acmicpc.net/problem/9345
1. 풀이
"선반 A번부터 선반 B번까지 A번 ~ B번 dvd가 순서에 상관없이 모두 꽂혀있는 경우"
<=>
"선반 A번부터 선반 B번까지 가장 작은 dvd 번호가 A, 가장 큰 dvd 번호가 B인 경우"
2. 풀이 코드
* 유의할 점
#include <bits/stdc++.h>
const int INF = 1e9+7;
const int NMAX = 100005;
const int MAX = 1 << (int)(ceil(log2(NMAX))+1);
using namespace std;
int N, K, leaf_size;
int maxtree[MAX];
int mintree[MAX];
int dvd[NMAX];
void init(){
for (int i=leaf_size;i<leaf_size*2;i++){
maxtree[i] = i-leaf_size;
mintree[i] = i-leaf_size;
}
for (int i=leaf_size-1;i>=1;i--){
maxtree[i] = max(maxtree[i*2], maxtree[i*2+1]);
mintree[i] = min(mintree[i*2], mintree[i*2+1]);
}
}
void update(int pos, int diff){
int maxpos = pos + leaf_size;
maxtree[maxpos] = diff;
while(maxpos > 1){
maxpos /= 2;
maxtree[maxpos] = max(maxtree[maxpos*2], maxtree[maxpos*2+1]);
}
int minpos = pos + leaf_size;
mintree[minpos] = diff;
while(minpos > 1){
minpos /= 2;
mintree[minpos] = min(mintree[minpos*2], mintree[minpos*2+1]);
}
}
int getmin(int node, int l, int r, int s, int e){
if (r < s || e < l) return INF;
if (s <= l && r <= e) return mintree[node];
int mid = (l+r) / 2;
return min(getmin(node*2, l, mid, s, e), getmin(node*2+1, mid+1, r, s, e));
}
int getmax(int node, int l, int r, int s, int e){
if (r < s || e < l) return -1;
if (s <= l && r <= e) return maxtree[node];
int mid = (l+r) / 2;
return max(getmax(node*2, l, mid, s, e), getmax(node*2+1, mid+1, r, s, e));
}
void solve(){
scanf("%d %d", &N, &K);
leaf_size = 1 << (int)ceil(log2(N));
memset(mintree, 0x3f, sizeof(mintree));
memset(maxtree, -1, sizeof(maxtree));
for (int i=0;i<N;i++){
dvd[i] = i;
}
init();
for (int i=0;i<K;i++){
int op, a, b;
scanf("%d %d %d", &op, &a, &b);
// a--; b--;
if (op == 0){
swap(dvd[a], dvd[b]);
update(a, dvd[a]);
update(b, dvd[b]);
}
else if (op == 1){
int cmin = getmin(1, 0, leaf_size-1, a, b);
int cmax = getmax(1, 0, leaf_size-1, a, b);
if (cmin == a && cmax == b) printf("YES\n");
else printf("NO\n");
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
scanf("%d", &t);
while(t--) solve();
return 0;
}
'PS > IndexedTree' 카테고리의 다른 글
[백준] 7469: K번째 수 (0) | 2020.08.11 |
---|---|
[백준] 3392: 화성지도 (0) | 2020.07.18 |
[백준] 10167: 금광 (0) | 2020.04.22 |
[백준] 2243: 사탕상자 (0) | 2020.02.20 |
[백준] 2517: 달리기 (0) | 2020.02.18 |
Comments