Skip to content

Commit 236f26b

Browse files
committed
modify code
1 parent 0bd1694 commit 236f26b

File tree

4 files changed

+67
-69
lines changed

4 files changed

+67
-69
lines changed

src/class166/Code01_SegmentTreeDivideConquer1.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
// 线段树分治模版题,java版
44
// 测试链接 : https://loj.ac/p/121
5-
// 提交以下的code,提交时请把类名改成"Main"
6-
// 测试平台看似支持java语言,其实无法通过,内存过大报警,导致验证失败
7-
// 想通过用C++实现,就是Code01_SegmentTreeDivideConquer2文件
8-
// 逻辑完全一样,C++实现可以通过全部测试用例
5+
// 提交以下的code,提交时类名改成"Main",多提交几次,可以通过所有测试用例
96

107
import java.io.IOException;
118
import java.io.InputStream;
@@ -18,7 +15,10 @@ public class Code01_SegmentTreeDivideConquer1 {
1815
public static int MAXT = 5000001;
1916
public static int n, m;
2017

21-
public static int[][] event = new int[MAXM][3];
18+
public static int[] op = new int[MAXM];
19+
public static int[] u = new int[MAXM];
20+
public static int[] v = new int[MAXM];
21+
2222
public static int[][] last = new int[MAXN][MAXN];
2323

2424
public static int[] father = new int[MAXN];
@@ -96,11 +96,8 @@ public static void dfs(int l, int r, int i) {
9696
}
9797
}
9898
if (l == r) {
99-
int op = event[l][0];
100-
int x = event[l][1];
101-
int y = event[l][2];
102-
if (op == 2) {
103-
ans[l] = find(x) == find(y);
99+
if (op[l] == 2) {
100+
ans[l] = find(u[l]) == find(v[l]);
104101
}
105102
} else {
106103
int mid = (l + r) / 2;
@@ -117,13 +114,13 @@ public static void prepare() {
117114
father[i] = i;
118115
siz[i] = 1;
119116
}
120-
for (int i = 1, op, x, y; i <= m; i++) {
121-
op = event[i][0];
122-
x = event[i][1];
123-
y = event[i][2];
124-
if (op == 0) {
117+
for (int i = 1, t, x, y; i <= m; i++) {
118+
t = op[i];
119+
x = u[i];
120+
y = v[i];
121+
if (t == 0) {
125122
last[x][y] = i;
126-
} else if (op == 1) {
123+
} else if (t == 1) {
127124
add(last[x][y], i - 1, x, y, 1, m, 1);
128125
last[x][y] = 0;
129126
}
@@ -141,18 +138,18 @@ public static void main(String[] args) {
141138
FastIO io = new FastIO(System.in, System.out);
142139
n = io.nextInt();
143140
m = io.nextInt();
144-
for (int i = 1, op, x, y; i <= m; i++) {
145-
op = io.nextInt();
141+
for (int i = 1, t, x, y; i <= m; i++) {
142+
t = io.nextInt();
146143
x = io.nextInt();
147144
y = io.nextInt();
148-
event[i][0] = op;
149-
event[i][1] = Math.min(x, y);
150-
event[i][2] = Math.max(x, y);
145+
op[i] = t;
146+
u[i] = Math.min(x, y);
147+
v[i] = Math.max(x, y);
151148
}
152149
prepare();
153150
dfs(1, m, 1);
154151
for (int i = 1; i <= m; i++) {
155-
if (event[i][0] == 2) {
152+
if (op[i] == 2) {
156153
if (ans[i]) {
157154
io.write("Y\n");
158155
} else {

src/class166/Code01_SegmentTreeDivideConquer2.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
//const int MAXT = 5000001;
1515
//int n, m;
1616
//
17-
//int event[MAXM][3];
17+
//int op[MAXM];
18+
//int u[MAXM];
19+
//int v[MAXM];
20+
//
1821
//int last[MAXN][MAXN];
1922
//
2023
//int father[MAXN];
@@ -92,11 +95,8 @@
9295
// }
9396
// }
9497
// if (l == r) {
95-
// int op = event[l][0];
96-
// int x = event[l][1];
97-
// int y = event[l][2];
98-
// if (op == 2) {
99-
// ans[l] = find(x) == find(y);
98+
// if (op[l] == 2) {
99+
// ans[l] = find(u[l]) == find(v[l]);
100100
// }
101101
// } else {
102102
// int mid = (l + r) >> 1;
@@ -113,13 +113,13 @@
113113
// father[i] = i;
114114
// siz[i] = 1;
115115
// }
116-
// for (int i = 1, op, x, y; i <= m; i++) {
117-
// op = event[i][0];
118-
// x = event[i][1];
119-
// y = event[i][2];
120-
// if (op == 0) {
116+
// for (int i = 1, t, x, y; i <= m; i++) {
117+
// t = op[i];
118+
// x = u[i];
119+
// y = v[i];
120+
// if (t == 0) {
121121
// last[x][y] = i;
122-
// } else if (op == 1) {
122+
// } else if (t == 1) {
123123
// add(last[x][y], i - 1, x, y, 1, m, 1);
124124
// last[x][y] = 0;
125125
// }
@@ -137,16 +137,16 @@
137137
// ios::sync_with_stdio(false);
138138
// cin.tie(nullptr);
139139
// cin >> n >> m;
140-
// for (int i = 1, op, x, y; i <= m; i++) {
141-
// cin >> op >> x >> y;
142-
// event[i][0] = op;
143-
// event[i][1] = min(x, y);
144-
// event[i][2] = max(x, y);
140+
// for (int i = 1, t, x, y; i <= m; i++) {
141+
// cin >> t >> x >> y;
142+
// op[i] = t;
143+
// u[i] = min(x, y);
144+
// v[i] = max(x, y);
145145
// }
146146
// prepare();
147147
// dfs(1, m, 1);
148148
// for (int i = 1; i <= m; i++) {
149-
// if (event[i][0] == 2) {
149+
// if (op[i] == 2) {
150150
// if (ans[i]) {
151151
// cout << "Y" << "\n";
152152
// } else {

src/class166/Code05_GreatIntegration1.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public class Code05_GreatIntegration1 {
1616
public static int MAXT = 3000001;
1717
public static int n, q;
1818

19-
public static int[][] event = new int[MAXN][3];
19+
public static int[] op = new int[MAXN];
20+
public static int[] u = new int[MAXN];
21+
public static int[] v = new int[MAXN];
22+
2023
public static int[][] sorted = new int[MAXN][3];
2124

2225
public static int[] father = new int[MAXN];
@@ -92,8 +95,8 @@ public static void dfs(int l, int r, int i) {
9295
}
9396
}
9497
if (l == r) {
95-
if (event[l][0] == 2) {
96-
ans[l] = (long) siz[find(event[l][1])] * siz[find(event[l][2])];
98+
if (op[l] == 2) {
99+
ans[l] = (long) siz[find(u[l])] * siz[find(v[l])];
97100
}
98101
} else {
99102
int mid = (l + r) >> 1;
@@ -111,21 +114,19 @@ public static void prepare() {
111114
siz[i] = 1;
112115
}
113116
for (int i = 1; i <= q; i++) {
114-
sorted[i][0] = i;
115-
sorted[i][1] = event[i][1];
116-
sorted[i][2] = event[i][2];
117+
sorted[i][0] = u[i];
118+
sorted[i][1] = v[i];
119+
sorted[i][2] = i;
117120
}
118-
Arrays.sort(sorted, 1, q + 1, (a, b) -> a[1] != b[1] ? a[1] - b[1] : a[2] != b[2] ? a[2] - b[2] : a[0] - b[0]);
121+
Arrays.sort(sorted, 1, q + 1, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] != b[1] ? a[1] - b[1] : a[2] - b[2]);
119122
for (int l = 1, r = 1; l <= q; l = ++r) {
120-
int t = sorted[l][0];
121-
int x = sorted[l][1];
122-
int y = sorted[l][2];
123-
while (r + 1 <= q && sorted[r + 1][1] == x && sorted[r + 1][2] == y) {
123+
int x = sorted[l][0], y = sorted[l][1], t = sorted[l][2];
124+
while (r + 1 <= q && sorted[r + 1][0] == x && sorted[r + 1][1] == y) {
124125
r++;
125126
}
126127
for (int i = l + 1; i <= r; i++) {
127-
add(t, sorted[i][0] - 1, x, y, 1, q, 1);
128-
t = sorted[i][0] + 1;
128+
add(t, sorted[i][2] - 1, x, y, 1, q, 1);
129+
t = sorted[i][2] + 1;
129130
}
130131
if (t <= q) {
131132
add(t, q, x, y, 1, q, 1);
@@ -138,20 +139,20 @@ public static void main(String[] args) throws IOException {
138139
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
139140
n = in.nextInt();
140141
q = in.nextInt();
141-
char op;
142-
int u, v;
142+
char t;
143+
int x, y;
143144
for (int i = 1; i <= q; i++) {
144-
op = in.nextChar();
145-
u = in.nextInt();
146-
v = in.nextInt();
147-
event[i][0] = op == 'A' ? 1 : 2;
148-
event[i][1] = Math.min(u, v);
149-
event[i][2] = Math.max(u, v);
145+
t = in.nextChar();
146+
x = in.nextInt();
147+
y = in.nextInt();
148+
op[i] = t == 'A' ? 1 : 2;
149+
u[i] = Math.min(x, y);
150+
v[i] = Math.max(x, y);
150151
}
151152
prepare();
152153
dfs(1, q, 1);
153154
for (int i = 1; i <= q; i++) {
154-
if (event[i][0] == 2) {
155+
if (op[i] == 2) {
155156
out.println(ans[i]);
156157
}
157158
}

src/class166/Code06_ConnectedGraph1.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public class Code06_ConnectedGraph1 {
1717
public static int MAXT = 10000001;
1818
public static int n, m, k;
1919

20-
public static int[] x = new int[MAXM];
21-
public static int[] y = new int[MAXM];
20+
public static int[] u = new int[MAXM];
21+
public static int[] v = new int[MAXM];
2222

2323
public static int[][] event = new int[MAXE][2];
2424
public static int ecnt = 0;
@@ -137,17 +137,17 @@ public static void prepare() {
137137
t = 1;
138138
for (int i = l; i <= r; i++) {
139139
if (t <= event[i][1] - 1) {
140-
add(t, event[i][1] - 1, x[eid], y[eid], 1, k, 1);
140+
add(t, event[i][1] - 1, u[eid], v[eid], 1, k, 1);
141141
}
142142
t = event[i][1] + 1;
143143
}
144144
if (t <= k) {
145-
add(t, k, x[eid], y[eid], 1, k, 1);
145+
add(t, k, u[eid], v[eid], 1, k, 1);
146146
}
147147
}
148148
for (int i = 1; i <= m; i++) {
149149
if (!visit[i]) {
150-
add(1, k, x[i], y[i], 1, k, 1);
150+
add(1, k, u[i], v[i], 1, k, 1);
151151
}
152152
}
153153
}
@@ -157,8 +157,8 @@ public static void main(String[] args) {
157157
n = io.nextInt();
158158
m = io.nextInt();
159159
for (int i = 1; i <= m; i++) {
160-
x[i] = io.nextInt();
161-
y[i] = io.nextInt();
160+
u[i] = io.nextInt();
161+
v[i] = io.nextInt();
162162
}
163163
k = io.nextInt();
164164
for (int i = 1, s; i <= k; i++) {

0 commit comments

Comments
 (0)