Skip to content

Commit ecb50ee

Browse files
committed
modify code
1 parent c2a6d3c commit ecb50ee

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

src/class183/Code07_Maschera1.java

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class Code07_Maschera1 {
1313

1414
public static int MAXN = 100001;
15-
public static int n, l, r;
15+
public static int n, minEdge, maxEdge;
1616

1717
public static int[] head = new int[MAXN];
1818
public static int[] nxt = new int[MAXN << 1];
@@ -88,7 +88,7 @@ public static int lowbit(int i) {
8888
}
8989

9090
public static void add(int i, int v) {
91-
while (i <= r) {
91+
while (i <= maxEdge) {
9292
tree[i] += v;
9393
i += lowbit(i);
9494
}
@@ -158,7 +158,7 @@ public static void getCentroid2(int cur, int fa) {
158158

159159
// 收集信息递归版,java会爆栈,C++可以通过
160160
public static void dfs1(int u, int fa, int maxv, int edge) {
161-
if (edge > r) {
161+
if (edge > maxEdge) {
162162
return;
163163
}
164164
curMaxv[++cntc] = maxv;
@@ -178,7 +178,7 @@ public static void dfs2(int cur, int fa, int pmaxv, int pedge) {
178178
while (stacksize > 0) {
179179
pop();
180180
if (e == -1) {
181-
if (edge > r) {
181+
if (edge > maxEdge) {
182182
continue;
183183
}
184184
curMaxv[++cntc] = maxv;
@@ -205,49 +205,37 @@ public static void calc(int u) {
205205
cntc = 0;
206206
// dfs1(v, u, weight[e], 1);
207207
dfs2(v, u, weight[e], 1);
208-
if (cntc > 0) {
209-
sort(curMaxv, curEdge, 1, cntc);
210-
for (int i = 1; i <= cntc; i++) {
211-
int left = l - curEdge[i] - 1;
212-
int right = r - curEdge[i];
213-
if (right >= 0) {
214-
left = Math.max(left, 0);
215-
right = Math.min(right, r);
216-
ans -= 1L * curMaxv[i] * (sum(right) - sum(left));
217-
}
218-
add(curEdge[i], 1);
219-
}
220-
for (int i = 1; i <= cntc; i++) {
221-
add(curEdge[i], -1);
222-
}
223-
for (int i = 1; i <= cntc; i++) {
224-
allMaxv[++cnta] = curMaxv[i];
225-
allEdge[cnta] = curEdge[i];
226-
}
208+
sort(curMaxv, curEdge, 1, cntc);
209+
for (int i = 1; i <= cntc; i++) {
210+
int l = minEdge - curEdge[i] - 1;
211+
int r = maxEdge - curEdge[i];
212+
ans -= 1L * curMaxv[i] * (sum(r) - sum(l));
213+
add(curEdge[i], 1);
227214
}
228-
}
229-
}
230-
if (cnta > 0) {
231-
sort(allMaxv, allEdge, 1, cnta);
232-
for (int i = 1; i <= cnta; i++) {
233-
int left = l - allEdge[i] - 1;
234-
int right = r - allEdge[i];
235-
if (right >= 0) {
236-
left = Math.max(left, 0);
237-
right = Math.min(right, r);
238-
ans += 1L * allMaxv[i] * (sum(right) - sum(left));
215+
for (int i = 1; i <= cntc; i++) {
216+
add(curEdge[i], -1);
239217
}
240-
add(allEdge[i], 1);
241-
}
242-
for (int i = 1; i <= cnta; i++) {
243-
add(allEdge[i], -1);
244-
}
245-
for (int i = 1; i <= cnta; i++) {
246-
if (allEdge[i] >= l) {
247-
ans += allMaxv[i];
218+
for (int i = 1; i <= cntc; i++) {
219+
allMaxv[++cnta] = curMaxv[i];
220+
allEdge[cnta] = curEdge[i];
248221
}
249222
}
250223
}
224+
sort(allMaxv, allEdge, 1, cnta);
225+
for (int i = 1; i <= cnta; i++) {
226+
int l = minEdge - allEdge[i] - 1;
227+
int r = maxEdge - allEdge[i];
228+
ans += 1L * allMaxv[i] * (sum(r) - sum(l));
229+
add(allEdge[i], 1);
230+
}
231+
for (int i = 1; i <= cnta; i++) {
232+
add(allEdge[i], -1);
233+
}
234+
for (int i = 1; i <= cnta; i++) {
235+
if (allEdge[i] >= minEdge) {
236+
ans += allMaxv[i];
237+
}
238+
}
251239
}
252240

253241
public static void solve(int u) {
@@ -269,8 +257,8 @@ public static void main(String[] args) throws Exception {
269257
FastReader in = new FastReader(System.in);
270258
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
271259
n = in.nextInt();
272-
l = in.nextInt();
273-
r = in.nextInt();
260+
minEdge = in.nextInt();
261+
maxEdge = in.nextInt();
274262
for (int i = 1, u, v, w; i < n; i++) {
275263
u = in.nextInt();
276264
v = in.nextInt();

0 commit comments

Comments
 (0)