Home BOJ. Digital Video Disk (9345)
Post
Cancel

BOJ. Digital Video Disk (9345)

[Link] https://www.acmicpc.net/problem/9345


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static int max = 0, maxIdx = 0;
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String[] line;
		MM[] seg;
		int[] orig;

		int t = toi(br.readLine());
		for(int iter = 0; iter < t; iter++) {
			line = getLine();
			int n = toi(line[0]), k = toi(line[1]);
			orig = new int[n];
			seg = new MM[1 << (int)Math.ceil(Math.log(n)/Math.log(2)) + 1];
			for(int i = 0; i < n; i++) orig[i] = i;
			for(int i = 0; i < seg.length; i++) seg[i] = new MM();
			init(seg, orig, 0, n-1, 1);
			for(int i = 0; i < k; i++) {
				line = getLine();
				int q = toi(line[0]), a = toi(line[1]), b = toi(line[2]);
				if(q == 0) {
					int valA = orig[a];
					update(seg, orig, 0, n-1, 1, a, orig[b]);
					update(seg, orig, 0, n-1, 1, b, orig[a]);
					orig[a] = orig[b];
					orig[b] = valA;
				} else {
					MM mm = get(seg, orig, 0, n-1, 1, a, b);
					if(mm.min == a && mm.max == b) sb.append("YES");
					else sb.append("NO");
					sb.append("\n");
				}
			}
		}
		print(sb);
	}

	static MM init(MM[] seg, int[] orig, int l, int r, int idx) {
		if(l == r) {
			seg[idx].max = seg[idx].min = orig[l];
			return seg[idx];
		}
		int mid = (l + r) / 2;
		MM leftM = init(seg, orig, l, mid, 2 * idx), rightM = init(seg, orig, mid + 1, r, 2 * idx + 1);
		seg[idx].max = Math.max(leftM.max, rightM.max);
		seg[idx].min = Math.min(leftM.min, rightM.min);
		return seg[idx];
	}

	static MM update(MM[] seg, int[] orig, int l, int r, int idx, int targetIdx, int val) {
		if(targetIdx < l || r < targetIdx) return seg[idx];
		if(l == r) {
			seg[idx].max = seg[idx].min = val;
			return seg[idx];
		}
		int mid = (l + r) / 2;
		MM leftM = update(seg, orig, l, mid, 2 * idx, targetIdx, val), rightM = update(seg, orig, mid + 1, r, 2 * idx + 1, targetIdx, val);
		seg[idx].max = Math.max(leftM.max, rightM.max);
		seg[idx].min = Math.min(leftM.min, rightM.min);
		return seg[idx];
	}

	static MM get(MM[] seg, int[] orig, int l, int r, int idx, int start, int end) {
		if(end < l || r < start) return new MM(Integer.MIN_VALUE, Integer.MAX_VALUE);
		if(start <= l && r <= end) return seg[idx];
		int mid = (l + r) / 2;
		MM leftM = get(seg, orig, l, mid, 2 * idx, start, end), rightM = get(seg, orig, mid + 1, r, 2 * idx + 1, start, end);
		int max = Math.max(leftM.max, rightM.max);
		int min = Math.min(leftM.min, rightM.min);
		return new MM(max, min);
	}

	static class MM {
		int max;
		int min;
		MM() {}
		MM(int max, int min) { this.max = max; this.min = min; }
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.