|
| 1 | +package class166; |
| 2 | + |
| 3 | +// 线段树分治模版题,java版 |
| 4 | +// 测试链接 : https://www.luogu.com.cn/problem/P5787 |
| 5 | +// 提交以下的code,提交时请把类名改成"Main",可以通过所有测试用例 |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.io.OutputStreamWriter; |
| 11 | +import java.io.PrintWriter; |
| 12 | +import java.io.StreamTokenizer; |
| 13 | + |
| 14 | +public class Code01_SegmentTreeDivideAndConquer1 { |
| 15 | + |
| 16 | + public static int MAXN = 100001; |
| 17 | + public static int MAXM = 200001; |
| 18 | + public static int MAXT = 3000001; |
| 19 | + public static int n, m, k; |
| 20 | + public static int[][] edge = new int[MAXM][2]; |
| 21 | + |
| 22 | + public static int[] father = new int[MAXN << 1]; |
| 23 | + public static int[] siz = new int[MAXN << 1]; |
| 24 | + public static int[][] rollback = new int[MAXN << 1][2]; |
| 25 | + public static int opsize; |
| 26 | + |
| 27 | + public static int[] head = new int[MAXT]; |
| 28 | + public static int[] next = new int[MAXT]; |
| 29 | + public static int[] to = new int[MAXT]; |
| 30 | + public static int cnt = 0; |
| 31 | + |
| 32 | + public static boolean[] ans = new boolean[MAXN]; |
| 33 | + |
| 34 | + public static void addQuery(int u, int v) { |
| 35 | + next[++cnt] = head[u]; |
| 36 | + to[cnt] = v; |
| 37 | + head[u] = cnt; |
| 38 | + } |
| 39 | + |
| 40 | + public static int find(int i) { |
| 41 | + while (i != father[i]) { |
| 42 | + i = father[i]; |
| 43 | + } |
| 44 | + return i; |
| 45 | + } |
| 46 | + |
| 47 | + public static void union(int x, int y) { |
| 48 | + int fx = find(x); |
| 49 | + int fy = find(y); |
| 50 | + if (siz[fx] < siz[fy]) { |
| 51 | + int tmp = fx; |
| 52 | + fx = fy; |
| 53 | + fy = tmp; |
| 54 | + } |
| 55 | + father[fy] = fx; |
| 56 | + siz[fx] += siz[fy]; |
| 57 | + rollback[++opsize][0] = fx; |
| 58 | + rollback[opsize][1] = fy; |
| 59 | + } |
| 60 | + |
| 61 | + public static void undo() { |
| 62 | + int fx = rollback[opsize][0]; |
| 63 | + int fy = rollback[opsize--][1]; |
| 64 | + father[fy] = fy; |
| 65 | + siz[fx] -= siz[fy]; |
| 66 | + } |
| 67 | + |
| 68 | + public static void add(int jobl, int jobr, int jobv, int l, int r, int i) { |
| 69 | + if (l > jobr || r < jobl) { |
| 70 | + return; |
| 71 | + } |
| 72 | + if (jobl <= l && r <= jobr) { |
| 73 | + addQuery(i, jobv); |
| 74 | + } else { |
| 75 | + int mid = (l + r) / 2; |
| 76 | + add(jobl, jobr, jobv, l, mid, i << 1); |
| 77 | + add(jobl, jobr, jobv, mid + 1, r, i << 1 | 1); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void dfs(int l, int r, int i) { |
| 82 | + boolean check = true; |
| 83 | + int x, y, fx, fy, unionCnt = 0; |
| 84 | + for (int e = head[i]; e > 0; e = next[e]) { |
| 85 | + x = edge[to[e]][0]; |
| 86 | + y = edge[to[e]][1]; |
| 87 | + fx = find(x); |
| 88 | + fy = find(y); |
| 89 | + if (fx == fy) { |
| 90 | + check = false; |
| 91 | + break; |
| 92 | + } else { |
| 93 | + union(x, y + n); |
| 94 | + union(y, x + n); |
| 95 | + unionCnt += 2; |
| 96 | + } |
| 97 | + } |
| 98 | + if (check) { |
| 99 | + if (l == r) { |
| 100 | + ans[l] = true; |
| 101 | + } else { |
| 102 | + int mid = (l + r) / 2; |
| 103 | + dfs(l, mid, i << 1); |
| 104 | + dfs(mid + 1, r, i << 1 | 1); |
| 105 | + } |
| 106 | + } else { |
| 107 | + for (int k = l; k <= r; k++) { |
| 108 | + ans[k] = false; |
| 109 | + } |
| 110 | + } |
| 111 | + for (int k = 1; k <= unionCnt; k++) { |
| 112 | + undo(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public static void main(String[] args) throws IOException { |
| 117 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 118 | + StreamTokenizer in = new StreamTokenizer(br); |
| 119 | + PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); |
| 120 | + in.nextToken(); |
| 121 | + n = (int) in.nval; |
| 122 | + in.nextToken(); |
| 123 | + m = (int) in.nval; |
| 124 | + in.nextToken(); |
| 125 | + k = (int) in.nval; |
| 126 | + for (int i = 1, l, r; i <= m; i++) { |
| 127 | + in.nextToken(); |
| 128 | + edge[i][0] = (int) in.nval; |
| 129 | + in.nextToken(); |
| 130 | + edge[i][1] = (int) in.nval; |
| 131 | + in.nextToken(); |
| 132 | + l = (int) in.nval + 1; |
| 133 | + in.nextToken(); |
| 134 | + r = (int) in.nval; |
| 135 | + add(l, r, i, 1, k, 1); |
| 136 | + } |
| 137 | + for (int i = 1; i <= n * 2; i++) { |
| 138 | + father[i] = i; |
| 139 | + siz[i] = 1; |
| 140 | + } |
| 141 | + dfs(1, k, 1); |
| 142 | + for (int i = 1; i <= k; i++) { |
| 143 | + if (ans[i]) { |
| 144 | + out.println("Yes"); |
| 145 | + } else { |
| 146 | + out.println("No"); |
| 147 | + } |
| 148 | + } |
| 149 | + out.flush(); |
| 150 | + out.close(); |
| 151 | + br.close(); |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments