We need to process dynamic updates to the first row (array A of length n) and queries that ask for the value at cell (x, y) in the generated infinite table. The table is defined as:
Row 1: given array a1[1..n] (initial and can be updated)
For i >= 2, a_i[p] = number of times a_{i-1}[p] occurs in the prefix a_{i-1}[1..p] (i.e., frequency of value a_{i-1}[p] up to position p in the previous row).
So essentially we have a process where each row is a transformation T applied to the previous row. T maps an array b[1..n] to array c[1..n] where c[p] = count of b[p] in b[1..p].
We need to answer queries (x, y) efficiently: value at row x (1-indexed) column y.
Constraints: n up to 100k, m up to 100k. x up to 1e5 (maybe larger? It says 1 ≤ x ≤ 10^5 in problem statement. So row index limited to 100k, which is manageable but still large. We need to support updates to row 1.
Observations:
The transformation T is deterministic. Let's denote for a given array b, the resulting c = T(b). This transformation is similar to the "frequency table" of the array.
Let's analyze properties:
Given b, define for each value v, a running count. For each position p, c[p] = current count of b[p] so far.
This is similar to mapping each occurrence of a value to its occurrence index: e.g., for a value v, the positions where b[p]=v have c[p] = 1, 2, 3, ... in order of appearance.
Now, consider applying T repeatedly. There might be some eventual periodicity or fixed points.
We need to answer for any row up to 1e5. Could we simulate the process row by row? That would be O(n * x) per query, too slow. Need to precompute or use data structures.
We note that the transformation T is "local" in some sense? Let's compute T^2, T^3, etc.
Define b1 = original row. b2 = T(b1). b3 = T(b2). etc.
We can try to understand what b_i looks like. Possibly it stabilizes after a few steps? Since values are integers up to 1e9, but after transformation, b_i values are positive integers, but they can be at most p (since count ≤ p). So after first transformation, values become between 1 and n. After second, they become counts of numbers that are at most n, so again bounded by n. So all rows from row2 onward have values ≤ n. But row1 can be arbitrary up to 1e9.
After row2, the values are small. But still n can be 100k, so values up to 100k.
Now consider the transformation's effect: b2[p] = frequency of b1[p] up to p. b3[p] = frequency of b2[p] up to p in b2. Hard to directly compute.
Maybe there's a connection to the concept of "run-length encoding" or "self-describing". Another thought: The transformation T essentially records the position of each occurrence of a value as its occurrence count. So if we know for each value v, its occurrences in row i, then row i+1 at those positions becomes the occurrence index (1-indexed) among occurrences of v.
Now applying again: row i+2 at those positions becomes the occurrence index of the occurrence index? This seems like we are looking at the sequence of "order statistics" of the positions.
Maybe we can find a representation in terms of permutation? Not exactly.
Alternate approach: Because n and x are up to 100k, we could precompute all rows up to some limit? But n * max x = 1e10, too large.
But note: The transformation T may cause the rows to eventually become constant? Let's test small examples.
Consider any array b. b2[p] = count of b[p] in prefix. That is always at least 1. For a given position p, as we iterate, the values may change.
Suppose b is an array of numbers. T applied repeatedly might converge to an array where each element equals 1? Let's test simple examples.
Example: b = [1,2,3,...] all distinct. Then b2[p] = 1 for all p (since each value appears once). b3[p] = count of 1 in prefix of b2: b2 is all 1s, so at each position, count of 1 so far = p. So b3 = [1,2,3,...,n]. b4 = T(b3) =? b3[p] = p, but b3 values are distinct except maybe if duplicates? Actually p values are all distinct. So b4[p] = count of p in prefix? p appears only at position p, so count=1. So b4 = all 1s again. So we get alternation between all-1 and [1,2,...,n] from row2? Wait, row1: [1,2,3,...,n], row2: [1,1,1,...,1]? Actually for row1 distinct, row2 all 1s (since each value appears first time). Then row3: count of 1 in prefix: each step increments count, so row3 = [1,2,3,...,n]. So row4: back to all 1s. So period 2 after row1. So for distinct row1, rows alternate from row2 onward: odd rows (3,5,7,...) are [1,2,...,n]; even rows (2,4,6,...) are all 1s. So values depend on parity.
Now consider row1: [1,2,2,2,3,1] (example from problem). Compute manually:
Row1: 1,2,2,2,3,1
Row2: count of each value at prefix:
p1: value1 appears first ->1
p2: value2 appears first ->1
p3: value2 appears second ->2
p4: value2 appears third ->3
p5: value3 appears first ->1
p6: value1 appears second ->2
So row2 = [1,1,2,3,1,2]
Row3: compute counts of row2 values:
Row2: 1,1,2,3,1,2
p1: value1 count=1 ->1
p2: value1 appears again ->2 (since two 1s)
p3: value2 first ->1
p4: value3 first ->1
p5: value1 third ->3
p6: value2 second ->2
So row3 = [1,2,1,1,3,2]
Row4:
p1: value1 ->1
p2: value2 ->1? Wait row3[2]=2, so count of 2 so far? At p2, row3 prefix: [1,2], value 2 appears first time ->1? Actually count of 2 so far =1 -> row4[2]=1
p3: row3[3]=1, count of 1 so far: up to p3, we have [1,2,1], so count of 1 =2 -> row4[3]=2
p4: row3[4]=1, count of 1 now becomes 3 -> row4[4]=3
p5: row3[5]=3, count of 3 first time ->1 -> row4[5]=1
p6: row3[6]=2, count of 2 so far: at p6, we have seen 2 at p2 and maybe elsewhere? only at p2 and p6, so count=2 -> row4[6]=2
So row4 = [1,1,2,3,1,2] which is same as row2? Row2 was [1,1,2,3,1,2] indeed. So row4 = row2. So row3 =? row5 = row3, row6 = row2, etc. So period 2 starting from row2? Actually row2 = row4, row3 = row5? Check: row2=[1,1,2,3,1,2]; row3=[1,2,1,1,3,2]; row4=[1,1,2,3,1,2]=row2; row5 should be row3. So yes, period 2 after row1.
So maybe in general, from row2 onward, the transformation T has period at most 2? Let's test other arrays.
Take row1: all same, say [1,1,1,1]. Then row2: [1,2,3,4] (counts of 1). Row3: counts of [1,2,3,4] each? Row3[1]=1 (value1 count=1). Row3[2]= value2 first occurrence? 2 appears first time ->1? Actually count of 2 so far =1 ->1. Row3[3]= value3 first ->1. Row3[4]= value4 first ->1? But need to check: row3[4] = count of 4 in prefix of row2 up to position4: row2[1..4]=[1,2,3,4], 4 appears only at position4 ->1. So row3 = [1,1,1,1] = row1? Actually row1 was [1,1,1,1]. So row3 = row1. So period 2 again (row1 -> row2 -> row1). So from row1, it's period 2? Not exactly: row1 and row3 are same. So row1, row3, row5... same; row2, row4, row6... same. So after the first transformation, we get two alternating states: T(row1) = row2, T(row2)=row1. So row1 is a fixed point of T^2? Actually if T(T(row1)) = row1, then row1 is a 2-cycle (unless row1 is fixed by T). So row1 is a fixed point? Could there be arrays that are fixed by T? Let's find b such that T(b) = b. That means for all p, b[p] = number of times b[p] occurs in b[1..p]. For each position, b[p] is its own cumulative frequency. This is reminiscent of the "self-descriptive" property. For example, b = [1,2,1,3]? Check: p1: b1=1, count1=1 ok. p2: b2=2, count of 2 in first2? only at p2 ->1, but b2=2, not 1. So not fixed. Could there be any fixed? Possibly the array of all 1's? b=[1,1,1,...]. T(b) would be [1,2,3,...] not equal. So not fixed. Maybe only certain arrays are fixed? Let's try b = [1,2,2,?] hmm. Condition: b[p] = count of b[p] up to p. So b[p] must be exactly the number of times value v = b[p] appears in prefix. This means that the value at position p cannot exceed p. Also the counts are exactly the values. This is like each value v appears exactly v times? Not exactly. For each distinct value v, if it appears at positions, then at its last occurrence, the count equals total occurrences. So if b is such that for each distinct value v, all occurrences are contiguous? Not sure.
But from experiments, many arrays lead to a 2-cycle after first step. Let's test another: row1 = [2,2,2]? But row1 values are positive integers, but could be > n? For n=3, row1 = [2,2,2]. Row2: counts of 2 -> [1,2,3]. Row3: count of each: p1:1->1, p2:2->1? count of 2 up to p2: at p2, row2[2]=2, first time? Actually row2[1]=1, row2[2]=2, so 2 appears first time at p2, count=1 -> row3[2]=1, p3: row2[3]=3, first time count=1 -> row3[3]=1? Also p3: row2[3]=3, count of 3 =1 => row3[3]=1. So row3 = [1,1,1]? Let's compute properly: row2 = [1,2,3]; then row3: p1: count of 1 so far =1 ->1; p2: row2[2]=2, count of 2 up to p2 =1 (first occurrence) ->1; p3: row2[3]=3, count of 3 =1 ->1. So row3 = [1,1,1]. Then row4: T(row3) = counts of 1: [1,2,3] = row2. So row4 = row2, row5 = row3, etc. So cycle length 2 after row2? Actually row2->row3->row2? Row2 = [1,2,3]; row3 = [1,1,1]; row4 = [1,2,3] = row2; so period 2 for row2 and row3? Row2 and row3 are distinct, and T(row2)=row3, T(row3)=row2. So it's a 2-cycle.
What about row1 = [1,1,2,2]? Compute: row1 = [1,1,2,2]; row2: [1,2,1,2] (since counts: p1:1->1, p2:1->2, p3:2->1, p4:2->2). row3: counts of row2: row2[1]=1 ->1; row2[2]=2 -> count of 2 so far? At p2, row2 values [1,2], 2 appears first time ->1; row2[3]=1 -> count of 1 up to p3: we have 1 at p1 and p3 ->2; row2[4]=2 -> count of 2 up to p4: we have 2 at p2 and p4 ->2. So row3 = [1,1,2,2] which is row1. So row1 = row3, row2 = row4, period 2. So row1 is part of a 2-cycle? Actually T(row1)=row2, T(row2)=row1. So row1 and row2 form a 2-cycle. In this example, row1 is not fixed but T^2(row1)=row1.
Now what about row1 = [1,2,1,2]? Compute: row2: counts: p1:1->1, p2:2->1, p3:1->2 (since second 1), p4:2->2 -> row2=[1,1,2,2]. row3: T(row2)=? row2=[1,1,2,2] -> row3: p1:1->1, p2:1->2, p3:2->1, p4:2->2 => row3=[1,2,1,2] = row1. So again 2-cycle.
So from these examples, it seems that for any array, T(T(b)) = b? Does T^2 always equal identity? Let's test with earlier example: row1 distinct [1,2,3,...,n]. row2 = all 1s. T(row2) = [1,2,3,...,n] = row1. So T^2(row1)=row1. With row1 = [1,2,2,2,3,1] we had row2 = [1,1,2,3,1,2] and row3 = [1,2,1,1,3,2] and row4 = row2. So row1 != row3, but T^2(row1) = row3, not row1. Wait compute: T(row1) = row2. T(row2) = row3. So T^2(row1) = row3, which is not equal to row1. But we saw row4 = row2, row5 = row3, so T^2(row2)=row2? Actually T(row2)=row3, T(row3)=row4, and row4=row2, so T^2(row2)=row2? Let's check: T^2(row2) = T(T(row2)) = T(row3) = row4 = row2. So row2 is a fixed point of T^2? Actually row2 is not fixed by T (since T(row2)=row3 != row2), but T^2(row2)=row2, so row2 is in a 2-cycle? But we have three rows: row2, row3, row4=row2. So row2 and row3 are in a 2-cycle? Wait, if row4=row2, then T(row3)=row2, and T(row2)=row3. So yes, row2 and row3 form a 2-cycle. So row1 is not part of the cycle; row1 maps to row2, then cycle.
Thus the transformation T may have an interesting structure: Starting from any initial array b (row1), after one application we get into a set of arrays that are part of 2-cycles (or maybe fixed points?) under T. In other words, T^2(b') = b' for b' = T(b). So for row2 onward, we have T^2 = identity? Actually we observed that row2 and row3 are swapped: T(row2)=row3, T(row3)=row2, so T^2(row2)=row2. But is this always true? Let's test another arbitrary array: b = [2,3,2,3,1] maybe.
Compute b = [2,3,2,3,1] (n=5).
b2 = T(b):
p1:2 ->1
p2:3 ->1
p3:2 ->2 (since second 2)
p4:3 ->2 (second 3)
p5:1 ->1
So b2 = [1,1,2,2,1]
b3 = T(b2):
b2: [1,1,2,2,1]
p1:1->1
p2:1->2
p3:2->1 (first 2)
p4:2->2 (second 2)
p5:1->3 (third 1)
So b3 = [1,2,1,2,3]
b4 = T(b3):
b3: [1,2,1,2,3]
p1:1->1
p2:2->1 (first 2)
p3:1->2 (second 1)
p4:2->2 (second 2)
p5:3->1
So b4 = [1,1,2,2,1] = b2.
Thus b2 and b3 form a 2-cycle. So again, after the first step, we have a 2-cycle.
What about b1 that is already in such a set? e.g., b1 = [1,1,2,2,1] which was b2 above. Then b2 = T(b1) = [1,2,1,2,3] (b3), b3 = T(b2) = b1. So b1 is part of the 2-cycle? Actually b1 and b2 are swapped? Wait, if b1 = [1,1,2,2,1], then b2 = T(b1) = [1,2,1,2,3] (as computed), and T(b2) = b1. So indeed b1 and b2 form a 2-cycle. So any array that is in the image of T (i.e., reachable from some array) might be part of a 2-cycle? Possibly T^2 = identity on its image. Let's test with b = [1,2,3] (distinct). T(b) = [1,1,1]. T^2(b) = [1,2,3] = b. So b is in the image? Actually b is not in the image of T? T([1,1,1]) = [1,2,3], so [1,2,3] is in the image of T. And we have T^2([1,2,3]) = T([1,1,1]) = [1,2,3], so T^2= identity on [1,2,3] as well? But T([1,2,3]) = [1,1,1] and T([1,1,1]) = [1,2,3], so they form a 2-cycle. So indeed, for arrays that are in the image of T (which might be all arrays? Not sure), T^2 is identity. Actually we saw that for row1 distinct, row1 itself is not fixed by T^2? Wait row1 distinct gave T(row1)= all 1s, T^2(row1)= row1. So row1 is in the image of T? Because row1 = T(all 1s). So row1 is also in the image of T. So indeed row1 distinct is in the image. So maybe all arrays that can appear (starting from arbitrary row1) are in the image of T? The first row can be arbitrary, but after one step we get row2 which is definitely in the image of T (since it's output of T). And then row2 is in the image, and we observed that for any array b in the image, T^2(b) = b. Let's test if this property always holds: For any array b, does T(T(b)) always equal b? Not true for arbitrary b as we saw: b = [1,2,2,2,3,1] gave T(b)=[1,1,2,3,1,2] and T(T(b)) = [1,2,1,1,3,2] which is not equal to b. So T^2(b) ≠ b for that b. But maybe if b is in the image of T (i.e., there exists some a such that T(a)=b), then T^2(b)=b. Is that always true? Let's test b = [1,2,1,1,3,2] from earlier. This b is row3 which is output of T(row2). Is it in the image of T? We need to find a such that T(a) = b. Does such a exist? Possibly row2 = [1,1,2,3,1,2] gave T(row2)=b. So b is in the image (since row2 exists). Now compute T(b): we got row4 = [1,1,2,3,1,2] which is row2. So T(b) = row2, and T(row2)=b, so indeed T^2(b)=b. So property holds for that b.
Now we need to confirm: Is it always true that for any array c that is the result of T applied to some array (i.e., c is in the image of T), we have T(T(c)) = c? In other words, T is an involution on its image. Let's try to prove.
Consider an array c = T(b). For each position p, c[p] = count of b[p] in b[1..p].
Now compute d = T(c). For each p, d[p] = count of c[p] in c[1..p].
We want to show that d = b. That would prove T^2 = identity on the image. But is that always true? Let's try to reason.
Given b, define for each value v, the positions where b[p]=v. Let those positions be p1, p2, ..., pk in increasing order. Then c at those positions will be 1,2,...,k respectively (the occurrence index). So for value v, the positions are the same, but the assigned numbers are the rank of occurrence.
Now compute d = T(c). For each position p, d[p] = count of c[p] in c[1..p]. Now c[p] is the occurrence index of value b[p] (i.e., the rank). For a given value v, the c-values at its positions are 1,2,3,... in order. So consider the positions where b = v. At the first occurrence (p1), c = 1. At the second, c = 2, etc.
Now what is d at position p_i? d[p_i] = count of value (i) in c[1..p_i]. Since c contains numbers which are occurrence indices for various values. But note: The value i appears in c only at positions where the occurrence index for some value equals i. Might appear multiple times across different original values? Possibly yes, e.g., for two different values v and w, both could have occurrence index i at some positions. So the count of i in c up to p_i includes not only the i-th occurrence of v but also maybe i-th occurrence of other values that occur before p_i. Could that cause d to not equal b? Let's test with a counterexample if possible.
We need to see whether T(T(b)) always returns b. Our earlier example with b = [1,2,2,2,3,1] gave T(b) = [1,1,2,3,1,2] and T(T(b)) = [1,2,1,1,3,2] which is not equal to b. So b is not in the image of T? But b itself might not be in the image. So we only claim for c = T(b). In that case, T(T(b)) =? In the example, T(T(b)) = [1,2,1,1,3,2] which is not b, but is that equal to some other? So for c = T(b), T(c) is not necessarily b; in this case T(c) = row4 which is not b but is some array. But we observed that if we start from b, then after two steps we get something else. However, if we take c = T(b), then T(T(c)) = c? Let's test with b = [1,2,2,2,3,1], c = T(b) = [1,1,2,3,1,2]. Then T(c) = [1,2,1,1,3,2] = d. Then T(d) =? We computed earlier row4 = [1,1,2,3,1,2] = c. So indeed T(d) = c, so T(T(c)) = c. So c is in the image, and T^2(c)=c. That matches the property.
Thus the property: For any array a, T(a) is in the set where T^2 = identity. In other words, the image of T consists exactly of arrays that are fixed points of T^2 (i.e., T^2(b)=b). Moreover, T is an involution on its image (since T(T(b)) = b for b in the image). Actually if b is in the image, then there exists a such that T(a)=b, then T(b)=a? Not necessarily because T might not be invertible: Could there be multiple a such that T(a)=b? Possibly, but if b is in the image, T(b) is some array, and T(T(b)) = b. So T is indeed an involution on its image: applying T twice gives identity. So T restricted to its image is a bijection (its own inverse).
Now, how does this help answer queries? Given row1 (maybe arbitrary), we can compute row2 = T(row1). Then for i >= 2, we have a cycle of length 2: row_{2k} = row2, row_{2k+1} = row3 where row3 = T(row2). Actually careful: row2 = T(row1). row3 = T(row2). Then row4 = T(row3) = T(T(row2)) = row2 (since row2 is in the image? Is row2 always in the image? row2 = T(row1) so row2 is in the image of T, yes. So T^2(row2) = row2, meaning row4 = row2. So indeed row4 = row2, row5 = T(row4) = T(row2) = row3, etc. So from row2 onward, rows alternate between two arrays: even rows = row2, odd rows (starting from 3) = row3. But wait: row2 is even index 2, row3 is odd index 3, row4 is even (4) = row2, row5 odd = row3, etc. So for any x >= 2, we have:
if x is even: a[x] = a[2]
if x is odd: a[x] = a[3]
But is that always true? Let's verify with examples: For row1 distinct, row2 = all 1s, row3 = [1,2,...,n]. Then row4 = all 1s = row2, row5 = [1,2,...,n] = row3. Yes.
For row1 = [1,2,2,2,3,1], row2 = [1,1,2,3,1,2], row3 = [1,2,1,1,3,2], row4 = row2, row5 = row3. So pattern holds.
For row1 = [1,1,2,2], row2 = [1,2,1,2], row3 = [1,1,2,2] = row1. But row4 = T(row3) = T(row1) = row2, row5 = T(row4) = row3 = row1, etc. Here row1 appears again at row3, which is odd >=3. But according to the pattern above, for x>=2, even rows = row2, odd rows = row3. In this case row3 = row1. So row3 is not necessarily a new array; it could equal row1, but that's fine. The pattern still holds: row3 = T(row2). So we can compute row2 and row3 from row1. Then for any x >= 2, we can answer based on parity.
But what about x=1? Queries can ask row 1 as well (x can be 1). So we need to handle that.
Thus the problem reduces to: maintain an array A of length n (the first row). Support point updates to A (change A[p] to v). After each update, we need to be able to answer queries for any (x,y):
- If x == 1: answer A[y] (current first row).
- If x >= 2: if x even: answer row2[y]; if x odd: answer row3[y] (where row2 = T(A), row3 = T(row2) = T^2(A)). But careful: For x = 2, it's even -> row2; x = 3 -> odd -> row3; and so on.
Now we need to support updates to A and quickly compute the transformed arrays row2 and row3 after each update. Since queries up to 100k and n up to 100k, we need to recompute row2 and row3 efficiently upon updates. But row2 = T(A) depends on the entire array A. Updating a single element in A could change row2 at many positions? Because T(A)[p] depends on the frequency of A[p] in prefix up to p. Changing A[p] from old value to v will affect the frequencies for that value and possibly for the new value. It could affect row2 at position p (since directly uses A[p]) and also affect row2 at later positions where the same value (old or new) appears? Actually row2[q] for q > p depends on count of A[q] in prefix up to q. Changing A[p] changes the prefix counts for the old value (decrement count after p) and for new value (increment). This can affect row2 at positions q > p where A[q] equals the old value or new value, because the cumulative count changes. For example, if we change a value at position p, the counts of that value for positions after p shift by 1. So row2 at those positions may change. Similarly, row3 depends on row2, so changes propagate.
Thus naive recomputation after each update would be O(n), leading to O(n*m) worst-case, too slow.
We need a data structure to maintain the array A and support point updates, and quickly answer queries for any (x,y) using precomputed row2 and row3 that are maintained dynamically.
But note that row2 and row3 are defined by a transformation T that is essentially a "counting" transformation. It might be possible to compute the value at a specific (x,y) on the fly without fully maintaining row2 and row3, using some range query structures.
Alternatively, we can note that row2 and row3 have a specific structure given A. Perhaps we can express a[x][y] directly in terms of A and some combinatorial properties.
Another thought: Since for x>=2, the rows alternate between two arrays that are functions of A. So we only need to maintain two arrays B = T(A) and C = T(B). And answer queries for even/odd x accordingly. So the core challenge is to maintain B and C under point updates to A.
But perhaps we can avoid maintaining the full B and C; we can answer a query for a specific y by computing B[y] or C[y] on demand, using data structures that support queries of the form: given y, what is the frequency of value v = A[y] in prefix up to y? That's B[y]. So B[y] = count of A[y] in A[1..y]. So if we can query for any position y, the number of occurrences of value A[y] in the prefix [1..y], we can compute B[y] on the fly. Similarly, C[y] = count of B[y] in B[1..y]. So to compute C[y] we would need to query on array B: number of times the value B[y] appears in B[1..y]. This requires ability to query prefix frequencies on B. But B itself changes when A changes. However, perhaps we can compute C[y] directly from A without constructing B? Possibly using some combinatorial mapping.
Let's explore the transformation deeper.
Given A, define B = T(A): B[p] = f_A(p, A[p]) where f_A(p, val) = number of times val appears in A[1..p].
Define C = T(B): C[p] = number of times B[p] appears in B[1..p].
We need to be able to compute B[y] and C[y] quickly after updates.
If we can answer B[y] via a data structure that maintains frequencies of values in A with point updates (like a Fenwick tree per value, but values up to 1e9, so need coordinate compression with dynamic updates). Since updates change a value at a position, we need to update frequency counts for old and new values. To query B[y] = count of A[y] in prefix up to y, we can use a BIT for each distinct value? But values can be many and change over time. However total distinct values over all time is at most n + m (since each update introduces a new value possibly). That's up to 200k, manageable. But we need to support prefix count queries for arbitrary values efficiently. That is a classic problem: dynamic array with point updates, query: for a given position y, get the number of times the value at that position occurs up to that position. This can be done with a data structure that maintains, for each value, a sorted set of positions where it occurs, and we can query the rank of y in that set (i.e., number of occurrences <= y). Since we need to support updates (change value at p from old to new), we can remove p from old value's set and insert into new value's set. Then B[y] can be computed as (index of y in the set for value = A[y]) i.e., the rank. That's O(log n) per operation if we use balanced BST (like std::set) and we can get order of a given element (e.g., using a fenwick tree on a sorted list per value? But we need to compute rank in a dynamic set. For each value, we can maintain a Fenwick tree over positions? But positions are fixed 1..n. For each value, we could maintain a BIT over the whole array? That would be too heavy because there are many values.
Alternative: Use a single Fenwick tree over the array but treat each value separately? Not possible.
Instead, we can use a data structure like a binary indexed tree (or segment tree) with persistent? Hmm.
Better: Use an order statistic tree for each value. In C++, we can use a balanced BST (e.g., tree data structure) that supports insertion, deletion, and order of key (like GNU pbds). But we need to do it in competitive programming; we can implement a Fenwick tree over the whole array, but to query count of a specific value in prefix, we need to isolate that value. That's essentially a 2D problem: we have points (position, value). Queries: given value v and position y, count points with value = v and position <= y. Updates: change value at position p from old to new, i.e., remove point (p, old) and add (p, new). This is a classic dynamic 2D point counting problem (with one dimension being value, other position). We can solve it using a Fenwick tree of maps? Or using a BIT over positions for each value but with compression of values.
Given constraints n,m up to 1e5, we can possibly use a sqrt decomposition: block positions, maintain frequency per block per value, but values many.
Alternatively, use a Mo's algorithm? Not for dynamic updates.
Another idea: Because B[y] is simply the occurrence index of A[y] among positions with that value, we can compute it as: if we maintain for each value a vector of positions (sorted) and we can binary search to find the rank. Updates require removing and inserting positions. If we use sorted list (like an array) with deletions/insertions, that would be O(n) per update if we shift. But we can use balanced BST (like TreeSet in Java) or in C++ we can use std::set and to compute rank we can use order statistics tree (e.g., using GNU's __gnu_pbds::tree). Many competitions allow this. However, we also need to compute C[y] which depends on B. Could we compute C[y] directly from A? Maybe we can derive a formula: C[p] = ? Possibly it's the number of times the value A[p] occurs in A[1..p] but also considering something else.
Let's try to characterize B and C in terms of A.
Given A, define for each position p, let val = A[p]. Let r = rank of p among positions with value val (i.e., number of occurrences of val in A[1..p]). Then B[p] = r.
Now for C[p], we need the rank of p among positions where B equals B[p]? Actually C[p] = number of times value B[p] occurs in B[1..p]. That is, the count of positions q ≤ p such that B[q] = B[p]. So if we define for each possible rank value k (1..maybe up to something), we have positions where B[p] = k. C[p] is the occurrence index of p among positions where B[p] = k (with k = B[p]).
Thus C[p] depends on the distribution of ranks of A.
Is there a direct combinatorial interpretation? Perhaps C[p] equals the number of distinct values seen up to position p? Or something else? Let's test with examples.
Example A = [1,2,2,2,3,1].
Positions and A:
1:1
2:2
3:2
4:2
5:3
6:1
Compute ranks r (B):
1: r1 = 1 (first 1)
2: r2 = 1 (first 2)
3: r3 = 2 (second 2)
4: r4 = 3 (third 2)
5: r5 = 1 (first 3)
6: r6 = 2 (second 1)
So B = [1,1,2,3,1,2].
Now C = number of times B[p] occurs in prefix:
p1: B1=1 -> count of 1 in B[1..1]=1 ->1
p2: B2=1 -> count of 1 in B[1..2]=2 ->2
p3: B3=2 -> count of 2 in B[1..3]=1 ->1
p4: B4=3 -> count of 3 in B[1..4]=1 ->1
p5: B5=1 -> count of 1 in B[1..5]=3 ->3
p6: B6=2 -> count of 2 in B[1..6]=2 ->2
So C = [1,2,1,1,3,2].
Now, can we express C[p] directly from A? For p=6: A6=1, it's the second occurrence of 1. Among positions where A=1, we have positions 1 and 6. At p=6, the rank is 2. Now C[6]=2. That equals the rank of that occurrence among occurrences of 1? Actually rank of this occurrence of 1 is 2, and C[6]=2. For p=5: A5=3, first occurrence of 3, rank=1, C[5]=3. That is not equal to rank. So not direct.
Maybe C[p] equals the rank of the value A[p] among distinct values? Not.
Another approach: Since we only need to answer queries for B[y] and C[y] on demand, maybe we can maintain data structures to compute these values in O(log n) per query, without needing to update the whole B and C after each change. Because each query asks for a single cell (x,y). For row2 (x even >=2) we need B[y]; for row3 (x odd >=3) we need C[y]. So we could compute B[y] on the fly by querying the frequency of A[y] up to y. That is a classic problem as described. Similarly, for C[y], we need frequency of B[y] in B[1..y], but B is derived from A. Could we compute C[y] also using some structures derived from A? Perhaps we can precompute for each position the rank r = B[y], and then we need to know how many positions ≤ y have B value equal to r. That is essentially: count of positions q ≤ y such that rank of A[q] (i.e., B[q]) = r. This is a range counting query on the array of ranks B. But B changes with updates to A. However, we could maintain a data structure that supports updates to A and allows us to query: given y, compute r = rank of A[y] up to y, and then count how many positions q ≤ y have rank equal to r.
That's a more complex query: it's like for each position we have a value (the rank) that depends on the entire prefix of A. Updating A at position p changes ranks for all subsequent positions where the same value (old and new) occurs. So B changes for many positions! So we cannot maintain B explicitly without heavy updates.
But maybe we can answer C[y] without maintaining B globally by using some combinatorial insight that relates C directly to A. Let's try to derive C in terms of A.
We have: B[p] = rank of A[p] in prefix (occurrence index). So B is a sequence of numbers between 1 and maybe up to the maximum frequency of any value in A. The value B[p] essentially tells which occurrence of A[p] it is.
Now C[p] = rank of B[p] in prefix of B. That is, for the occurrence index k, how many times has that index k appeared in B up to position p? Since B contains the occurrence indices for each value. So C[p] counts how many values have reached at least the k-th occurrence? Hmm.
Alternatively, consider mapping each position p to a pair (value, occurrence_index). Then B[p] = occurrence_index. C[p] = number of pairs (value', occurrence_index') with position ≤ p and occurrence_index' = occurrence_index.
So C[p] is the count of positions q ≤ p such that the occurrence rank of A[q] equals the occurrence rank of A[p]. In other words, for the rank k, C[p] = total number of positions up to p where the value appears for the k-th time (i.e., it's the k-th occurrence of some value). So C[p] is the number of distinct values that have at least k occurrences up to position p? Not exactly: if we consider each value v, it contributes to rank k at the position of its k-th occurrence (if it exists). So C[p] is essentially the number of values v for which the k-th occurrence (where k = occurrence rank of A[p]) occurs at or before p. But that count includes only values that have at least k occurrences by position p. But careful: For a given rank k, a value v contributes exactly one position (its k-th occurrence) if it has at least k occurrences. So C[p] = number of values v such that the k-th occurrence of v occurs at position ≤ p. However, note that if a value has multiple occurrences, each occurrence has its own rank. So at position p, with rank k, C[p] counts all values that have their k-th occurrence at or before p. So C[p] can be interpreted as: let k = (number of times A[p] has occurred up to p). Then C[p] = number of distinct values that have occurred at least k times up to position p.
Let's test with example: A = [1,2,2,2,3,1]
- For p=1: A[1]=1, k=1. Values with at least 1 occurrence up to pos1: {1}. Count=1. C[1]=1, matches.
- p=2: A[2]=2, k=1. Values with at least 1 occurrence up to pos2: {1,2} ->2. C[2]=2, matches.
- p=3: A[3]=2, k=2 (second 2). Values with at least 2 occurrences up to pos3: Which values have appeared at least twice? Value 2 has appeared twice (positions 2 and 3). Value 1 has appeared only once. So count=1. C[3]=1, matches.
- p=4: A[4]=2, k=3. Values with at least 3 occurrences up to pos4: Only value 2 has three occurrences. So count=1. C[4]=1, matches.
- p=5: A[5]=3, k=1. Values with at least 1 occurrence up to pos5: {1,2,3} ->3. C[5]=3, matches.
- p=6: A[6]=1, k=2. Values with at least 2 occurrences up to pos6: Value 1 has 2 occurrences, value 2 has 3, value 3 has 1. So values with ≥2: {1,2} ->2. C[6]=2, matches.
Yes! So C[p] = number of distinct values that appear at least k times in the prefix up to p, where k = number of times A[p] appears up to p (i.e., its occurrence count). Let's prove:
Define for each value v, let its occurrences be at positions p_{v,1} < p_{v,2} < ... (the times it appears). Then B[p_{v,i}] = i. Now for a given position p = p_{v,i}, we have B[p] = i. C[p] = number of positions q ≤ p with B[q] = i. Which positions q have B[q] = i? Those are exactly the i-th occurrence of some value u: q = p_{u,i} (provided that the i-th occurrence of u exists and occurs ≤ p). So C[p] = |{ u : p_{u,i} ≤ p }|. That is the number of distinct values u for which the i-th occurrence has happened by position p. That's exactly the count of values that have at least i occurrences in the prefix [1..p].
Thus we have a nice interpretation:
Let f(v, p) = number of occurrences of value v in A[1..p].
Then B[p] = f(A[p], p).
C[p] = number of values v such that f(v, p) ≥ f(A[p], p).
Now we need to answer queries:
- For x=1: A[y].
- For x even (≥2): answer = B[y] = f(A[y], y).
- For x odd (≥3): answer = C[y] = count_{v} ( f(v, y) ≥ f(A[y], y) ).
Note that x up to 1e5, but we only care about parity for x>=2. However note: x can be 1, and also maybe x could be very large but we only use parity. So we just need to compute these two quantities for any y.
Thus the problem reduces to maintaining an array A of length n, with point updates (change a position to a new value), and being able to answer two types of queries for a given y:
- f(A[y], y) = number of times the value at position y appears in the prefix up to y.
- The number of distinct values v such that f(v, y) ≥ f(A[y], y).
We must process up to 1e5 operations (mix of updates and these queries). This seems doable with appropriate data structures.
Now we need to support:
- Update(p, v): set A[p] = v (from old value old). Need to update data structures to reflect change.
- Query1(y): return freq of A[y] up to y.
- Query2(y): return count of values with frequency >= freq_of_A[y] up to y.
We need to support prefix queries up to arbitrary y (not necessarily the same as p). Also note that after update at position p, frequencies for positions > p are affected for the old and new values. Specifically, for any position q > p:
- If A[q] == old (before change), its f(old, q) will decrease by 1 because the old occurrence at p is removed. But careful: f(old, q) counts occurrences of old up to q. After removal, the count decreases by 1 for q ≥ p. Similarly, for A[q] == new, f(new, q) increases by 1 for q ≥ p.
- Moreover, the value of B at those positions (which is f(A[q], q)) will change accordingly.
- Also, the predicate for Query2 for those positions may change because the frequency thresholds change.
But we are not asked to process range queries for arbitrary y; we only need to answer queries for specific y at query time. So we can compute answers on the fly using data structures that can give us f(v, y) for any v and y, and also the number of values with frequency >= k up to y.
We need to maintain a data structure that supports:
- Point updates: change the value at a position.
- For a given index y, we need to know:
a) The frequency of the value at position y in the prefix [1..y].
b) The number of distinct values v such that their frequency in [1..y] is at least that frequency.
This is reminiscent of a data structure for maintaining an array with updates and answering queries about prefix frequencies. It's like we have an array, and we need to support updates and queries on the multiset of values in a prefix, but the prefix length y varies per query. Classic approach: use a Fenwick tree (BIT) over positions, but frequencies depend on values.
We could treat each value separately: maintain for each value a BIT over positions to count occurrences? But there can be many distinct values. However total updates are 1e5, so total distinct values is at most n + m (200k). We could compress values and assign IDs. For each value ID, we could maintain a Fenwick tree (BIT) of size n to support prefix count queries: how many occurrences of value v up to position y. But there could be up to 200k such BITs, each of size n, leading to O(n * distinct) memory, too large.
We need a more efficient method.
Alternative: Use a Binary Indexed Tree of frequency arrays? We could treat the problem as offline? But queries are online interleaved with updates.
Observation: Queries are prefix queries up to y. For a fixed y, we need to know the frequency of a specific value A[y] in [1..y]. That is essentially: among all positions where value = A[y], how many are ≤ y. If we maintain for each value a sorted set of positions (e.g., a balanced BST), we can answer by binary searching: the rank = number of elements ≤ y. That is O(log size) per query, using e.g., a Fenwick tree per value? But we can use a treap or a binary search tree with order statistics. In C++, we can use std::set but we can't directly get rank (number of elements ≤ y) efficiently (can use distance which is O(N)). However we can use OrderedMultiset from policy-based data structures (pbds) which supports order_of_key. That would give O(log n