Skip to content

Commit 1a93be0

Browse files
committed
refactored names
1 parent 39c44b9 commit 1a93be0

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/main/java/main/java/codechef/DISTNUM3.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
public class DISTNUM3 {
99
private static final int MAX = 100000;
1010
static List<Integer>[] adj;
11-
static int clock = 0, eulerTour[], start[], end[], V;
11+
static int clock = 0, start[], end[], V;
1212
/* LCA <NlogN , logN> dependency : level , log , V , DP = new int[log(V) + 1][V + 1];, parent (for the first level of DP) */
1313
static int DP[][];
1414
static int level[];
1515
static int parent[];
16-
static int freq[];
1716
static int distinctCount;
1817
static boolean marked[];
1918
static int val[];
@@ -54,38 +53,40 @@ static int LCA(int u, int v) {
5453
return u;
5554
}
5655

57-
static void dfs(int u, int par, int lev) {
56+
static void dfs(int u, int par, int lev, final int[] eulerTour) {
5857
eulerTour[clock] = u;
5958
start[u] = clock++;
6059
parent[u] = par;
6160
level[u] = lev;
6261
for (final int v : adj[u]) {
6362
if (v != par) {
64-
dfs(v, u, lev + 1);
63+
dfs(v, u, lev + 1, eulerTour);
6564
}
6665
}
6766
eulerTour[clock] = u;
6867
end[u] = clock++;
6968
}
7069

71-
static void visit(int idx) {
70+
static void visit(final int idx, final int[] frequency) {
7271
if (marked[idx]) {
73-
freq[val[idx]]--;
74-
if (freq[val[idx]] == 0)
72+
frequency[val[idx]]--;
73+
if (frequency[val[idx]] == 0) {
7574
distinctCount--;
75+
}
7676
} else {
77-
freq[val[idx]]++;
78-
if (freq[val[idx]] == 1)
77+
frequency[val[idx]]++;
78+
if (frequency[val[idx]] == 1) {
7979
distinctCount++;
80+
}
8081
}
8182
marked[idx] = !marked[idx];
8283
}
8384

84-
static void update(int idx, int newVal) {
85+
static void update(final int idx, final int newVal, final int[] frequency) {
8586
if (marked[idx]) {
86-
visit(idx);
87+
visit(idx, frequency);
8788
val[idx] = newVal;
88-
visit(idx);
89+
visit(idx, frequency);
8990
} else {
9091
val[idx] = newVal;
9192
}
@@ -95,10 +96,6 @@ public static void main(String[] args) throws IOException {
9596
final InputReader in = new InputReader(System.in);
9697
V = in.readInt();
9798
final int Q = in.readInt();
98-
final Query queries[] = new Query[MAX];
99-
final Update updates[] = new Update[MAX];
100-
final Map<Integer, Integer> map = new HashMap<>(); // Used to compress the keys
101-
10299
adj = new ArrayList[V + 1];
103100
for (int i = 1; i <= V; i++) {
104101
adj[i] = new ArrayList<>();
@@ -107,6 +104,7 @@ public static void main(String[] args) throws IOException {
107104
for (int i = 1; i <= V; i++) {
108105
val[i] = in.readInt();
109106
}
107+
final Map<Integer, Integer> map = new HashMap<>();
110108
for (int i = 1; i <= V; i++) {
111109
if (!map.containsKey(val[i])) {
112110
map.put(val[i], map.size());
@@ -124,15 +122,17 @@ public static void main(String[] args) throws IOException {
124122
}
125123
start = new int[V + 1];
126124
end = new int[V + 1];
127-
eulerTour = new int[2 * (V + 1)];
125+
final int[] eulerTour = new int[2 * (V + 1)];
128126
level = new int[V + 1];
129127
marked = new boolean[V + 1];
130128
DP = new int[log(V) + 1][V + 1];
131129
parent = new int[V + 1];
132130
final int block[] = new int[2 * (V + 1)];
133-
dfs(1, 0, 0);
131+
dfs(1, 0, 0, eulerTour);
134132
binaryLift();
135133
int numberOfQueries = 0, numberOfUpdates = 0;
134+
final Query queries[] = new Query[MAX];
135+
final Update updates[] = new Update[MAX];
136136
for (int i = 0; i < Q; i++) {
137137
if (in.readInt() == 1) { // Query
138138
final int u = in.readInt();
@@ -163,7 +163,6 @@ public static void main(String[] args) throws IOException {
163163
currVal[idx] = newVal;
164164
}
165165
}
166-
freq = new int[map.size()];
167166
final int BLOCK_SIZE = (int) (Math.pow(2 * V, 2.0 / 3.0) + 1);
168167
for (int i = 0; i < block.length; i++) {
169168
block[i] = i / BLOCK_SIZE;
@@ -180,43 +179,44 @@ public static void main(String[] args) throws IOException {
180179
final int ans[] = new int[numberOfQueries];
181180
int moLeft = -1, moRight = -1;
182181
int currentUpdateCount = 0;
182+
final int[] freq = new int[map.size()];
183183
for (int i = 0; i < numberOfQueries; i++) {
184184
final Query query = queries[i];
185185
while (currentUpdateCount < query.updatesTillNow) {
186186
final Update update = updates[currentUpdateCount];
187-
update(update.idx, update.newVal);
187+
update(update.idx, update.newVal, freq);
188188
currentUpdateCount++;
189189
}
190190
while (currentUpdateCount > query.updatesTillNow) {
191191
currentUpdateCount--;
192192
final Update update = updates[currentUpdateCount];
193-
update(update.idx, update.prevVal);
193+
update(update.idx, update.prevVal, freq);
194194
}
195195
while (moLeft < query.L - 1) {
196196
moLeft++;
197-
visit(eulerTour[moLeft]);
197+
visit(eulerTour[moLeft], freq);
198198
}
199199
while (moLeft >= query.L) {
200-
visit(eulerTour[moLeft]);
200+
visit(eulerTour[moLeft], freq);
201201
moLeft--;
202202
}
203203
while (moRight < query.R) {
204204
moRight++;
205-
visit(eulerTour[moRight]);
205+
visit(eulerTour[moRight], freq);
206206
}
207207
while (moRight > query.R) {
208-
visit(eulerTour[moRight]);
208+
visit(eulerTour[moRight], freq);
209209
moRight--;
210210
}
211211
if (query.LCA != -1) {
212-
visit(query.LCA);
212+
visit(query.LCA, freq);
213213
}
214214
ans[query.id] = distinctCount;
215215
if (query.LCA != -1) {
216-
visit(query.LCA);
216+
visit(query.LCA, freq);
217217
}
218218
}
219-
final StringBuilder stringBuilder=new StringBuilder();
219+
final StringBuilder stringBuilder = new StringBuilder();
220220
for (final int a : ans) {
221221
stringBuilder.append(a).append('\n');
222222
}

0 commit comments

Comments
 (0)