Since chandu_don is busy in talking to his girlfriend,he wants you to solve a problem for him requiring range queries.The problem is as follows:-
Given an array of N integers we have to answer Q queries on the array.
Each Query is of the format X Y val where we have to output number of integers between X to Y index in the array having value as val.
Note:-All values are zero indexed
Input Format:-
First Line will contain a integer N denoting number of elements of the array.Next line will contains N spaced integers denoting the elements of array.
Next line will follow Q i.e. number of queries on the array.Next Q lines will have Q queries.
Each query have three space integers of the form X Y val.
Output Format:-
Output for each query consist of a single integer denoting number of integers between a[X] to a[Y] having value equal to val.
Constraints:-
1 ≤ N ≤ 10^4
1 ≤ Q ≤ 10^6
1 ≤ a[i],val ≤ 10^2
0 ≤ X,Y<N
X ≤ YSAMPLE INPUT
5
2 3 1 4 2
3
0 4 2
1 4 2
0 4 7
SAMPLE OUTPUT
2
1
0
Explanation
For each
1:
we have the query 0 4 2 i.e. we need to find count of number 2 between a[0] to a[4] inclusive.
Now we have 2 array values a[0] and a[4] which have value equal to 2.
so output is 2
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We have to solve a problem with constraints: N up to 10^4, Q up to 10^6, and values of array and val up to 10^2 (so small range: values from 1 to 100 maybe inclusive, perhaps up to 100 or 102? It says 1 ≤ a[i],val ≤ 10^2, so maximum value is 100). Each query asks count of value val in subarray [X, Y] inclusive. Output count.
Given large Q (10^6) but small N (10^4) and small value range (100), we can precompute prefix sums for each value. Since values are small, we can create a 2D array prefix[value][index] or maybe a matrix of size (max_val+1) x (N+1) storing count of that value up to each index.
Compute prefix[val][i] = number of occurrences of val in array up to index i (0-indexed). Then query count = prefix[val][Y] - (X>0 ? prefix[val][X-1] : 0). That's O(1) per query.
Memory: max_val = 100, N = 10^4, so prefix 101 x (10001) ≈ 1.01 million integers, which is fine (if using int, ~4 MB). Q up to 10^6, O(1) per query, total time ~1e6, feasible.
Implementation details: Input reading might be large due to Q up to 1e6, use fast I/O (like sys.stdin.buffer in Python). Also note that val is between 1 and 100, but could be out-of-range? Not, but we can handle if val > 100 then answer 0.
We need to output each answer on separate line.
Alternatively, we could use dictionary of lists, but prefix sum approach is simplest.
Edge Cases: X and Y inclusive. Also X ≤ Y.
We'll implement as:
Read N
Read array of N ints.
Determine max_val = 100 (or we can compute max from array but given constraint it's up to 100, but it's safe to fix 101 for all possible val).
Initialize prefix = [ [0](N+1) for _ in range(max_val+1) ] maybe prefix[val][i+1] for convenience (i from 0 to N-1 => prefix[val][i+1] = count up to i inclusive). Then for i from 0 to N-1:
val_i = arr[i]
for v in range(max_val+1)? That would be O(Nmax_val) = 10^4101 = ~1e6, which is fine. But we can do more efficiently: increment only that val's row. But building prefix by copying previous counts for all values each step could be O(Nmax_val) which is acceptable (1e6 operations). However, Python loops nested might be okay, but we can do smarter: for each i, we can copy previous counts from i to i+1 for all values, but that requires iterating over all values each time. But 1e6 operations is fine.
Simpler: we can have prefix[val] as a list of length N+1 where prefix[val][i] = count of val in arr[0..i-1] (i positions). Then we can compute by:
for val in range(1, max_val+1):
cnt = 0
prefix[val][0] = 0
for i in range(N):
if arr[i] == val: cnt += 1
prefix[val][i+1] = cnt
That's O(N * max_val) = 1e6, also fine.
Alternatively, we can create a list of lists of size (max_val+1) x (N+1) and fill using a single pass:
prefix = [ [0]*(N+1) for _ in range(max_val+1) ]
for i in range(N):
a_val = arr[i]
# For each val, copy previous count
for v in range(max_val+1):
prefix[v][i+1] = prefix[v][i]
# Then increment current val
prefix[a_val][i+1] += 1
But this inner loop over all v each i is O(N*max_val) = 1e6, but Python nested loops might be a bit slower but still okay given constraints. However, we can optimize: we can just update only the row for a_val: increment count, and for other rows copy previous count? That would need copying all rows each step, which is similar.
Better to use the first approach: for each val, build its prefix array in one pass. That's O(max_val * N). That's also 1e6, but the loops are separate, but each pass is N iterations. In Python, that's 101 * 10000 = 1,010,000 iterations, which is trivial. The inner loop may be fast. So we can do:
max_val = 100
prefix = [ [0]*(N+1) for _ in range(max_val+1) ]
for v in range(1, max_val+1):
cnt = 0
for i in range(N):
if arr[i] == v:
cnt += 1
prefix[v][i+1] = cnt
But note that we need to answer queries for any val, including maybe val not in 1..100? According to constraints, val is also between 1 and 100. But we can handle by checking if val > max_val: print 0.
This approach uses about 1 million integer assignments plus comparisons. That's fine.
We can also use dictionary: for each value we store list of indices or prefix sum. But since val range is small, array is fine.
Now Q up to 1e6, reading and output: in Python, using sys.stdin.buffer.read() to read all data, parse ints fast. We can use map(int, data.split()) but that would create a large list of ints (N+1+3Q maybe up to 3e6+). That's okay? 3e6 ints list, memory ~24 MB? Actually Python int overhead is large (~28 bytes each) => 84 MB for 3e6 ints maybe too much. Better to read line by line with fast I/O? But reading 1e6 lines individually using sys.stdin.readline might be slower but possibly okay in Python if optimized. But we need to be careful about time limit.
Constraints: Q up to 1e6. In many programming challenges, Python can handle 1e6 input lines with sys.stdin.buffer.readline, but might be borderline. However, we can parse all input quickly using sys.stdin.buffer.read() and using a generator to yield ints, without storing all at once.
We can implement a fast integer reader that yields ints from bytes. For example:
import sys
data = sys.stdin.buffer.read().split()
That splits into list of bytes and then map to int? That will create a list of bytes objects length maybe N+2+3Q (~3e6). Each bytes object overhead? Actually .split() returns list of bytes, each representing a token. That's also memory heavy but may be acceptable if memory limit is generous. However, N=1e4, Q=1e6, tokens count: first line: 1 token (N), second line: N tokens, third line: 1 token (Q), then Q lines each 3 tokens => total tokens = 1 + N + 1 + 3Q ≈ 3,000,000 + 10,000 + 2 ≈ 3,010,002. Storing 3 million small bytes objects could be memory heavy (each bytes object overhead maybe ~33 bytes plus the data). 3 million * 33 ≈ 99 MB plus data, so maybe > 100 MB. That might exceed typical memory limit (maybe 256 MB? but could be okay, but safer to avoid storing all tokens).
We can read line by line and process queries on the fly, outputting answers as we go. Q is 1e6, reading line by line is fine if we use sys.stdin.buffer.readline. However, printing each answer with standard print might be slow. Instead accumulate answers in a list of strings and join at the end. That will store up to 1e6 strings, memory maybe 10-20 MB (each answer as small string like "0\n", ~2-3 bytes each => ~3 MB?). Actually string objects overhead is large, but if we store as bytes? Better: use sys.stdout.write for each answer, but that might be slower due to many write calls but can be mitigated by using a buffer. In Python, writing 1e6 lines via sys.stdout.write is okay if you join them. Usually I use a list of strings and then ''.join(list) and print that (or sys.stdout.write). That will store the whole output string in memory: 1e6 lines, average maybe 2 digits + newline = 3 characters, so 3 MB, plus list overhead: list of 1e6 references (8 MB) and strings (each string overhead ~49 bytes? Actually small strings are interned? Not necessarily, but 1e6 strings each overhead ~49 bytes -> 49 MB! That's too much. So better to write incrementally using sys.stdout.write per answer. But 1e6 writes to stdout might be okay if buffer is used; Python's sys.stdout.write is buffered by default (line-buffered? Actually it's buffered in text mode). But calling it 1e6 times might be okay within time (maybe ~0.2s per 1e6? Actually not sure; typical I/O overhead can be ~50-100 ns per write? Actually in Python, each call has overhead. But 1e6 calls might be ~0.2-0.5 seconds, combined with computation, likely okay if total time limit is e.g., 2-3 seconds. However, we can use a io.StringIO buffer to collect output and then flush once, but that still builds a huge string. The string size is about 3 million characters, that's fine (3 MB). But building it via concatenation is efficient if we use a list and join. The memory for the list of 1e6 strings is significant: each list entry is a pointer (8 bytes) to a string object. That's 8 MB. The strings themselves: if we create a new string for each number plus newline, each string object overhead is about 49 bytes (for Python's object header). So 49 MB for 1e6 strings! That's a lot. However, we could accumulate as a bytearray or use write to sys.stdout.buffer directly. Alternatively, we can use sys.stdout.write for each answer; Python's write is buffered, so it's not writing to OS each time; it will buffer and flush when full. So it's okay. I think using sys.stdout.write for each answer is fine.
But to be safe, we can use a list of strings for answers and then join, as many Python solutions do for 1e6 outputs. Actually, overhead of string objects might be high but still within memory? 1e6 string objects with small content: each string object in CPython has overhead of 49 bytes (maybe more). 49 MB for strings, plus 8 MB for list = 57 MB. Plus other data (prefix arrays ~ 4 MB, array ~ 80KB). Total maybe ~61 MB. Could be okay if memory limit is e.g., 256 MB. But if memory limit is lower, might be close. Safer to use incremental write. I'll do sys.stdout.write for each answer.
Implementation plan in Python:
import sys
def main():
data = sys.stdin.buffer
# read first line: N
line = data.readline()
while line.strip() == b'': # skip empty? not needed
line = data.readline()
N = int(line)
# read array line
arr_line = data.readline()
arr = list(map(int, arr_line.split()))
# It may be that array is spread across lines? Problem says next line contains N spaced integers. So one line.
# read Q
Q_line = data.readline()
Q = int(Q_line)
max_val = 100
# Build prefix counts for each value 1..100
# Initialize prefix as list of lists: prefix[val] is list length N+1 (index from 0 to N)
# We'll use 0-index for values; index 0 unused
prefix = [ [0]*(N+1) for _ in range(max_val+1) ]
# For each val v from 1 to 100, fill prefix[v]
# We can do one pass for each v:
for v in range(1, max_val+1):
cnt = 0
pref_v = prefix[v]
# for i in range(N):
# if arr[i] == v:
# cnt += 1
# pref_v[i+1] = cnt
# That's typical.
# But Python loops may be faster if we use local variables and avoid attribute lookups.
for i, x in enumerate(arr):
if x == v:
cnt += 1
pref_v[i+1] = cnt
# Now process queries
out_lines = [] # maybe not
# Instead write directly
out = sys.stdout.write
for _ in range(Q):
line = data.readline()
# Could be that line is empty? assume not.
X_str, Y_str, val_str = line.split()
X = int(X_str)
Y = int(Y_str)
val = int(val_str)
if val > max_val:
# answer 0
out('0\n')
continue
# inclusive range count = prefix[val][Y+1] - prefix[val][X]
# because prefix[val][i] is count up to i-1 (i positions)
# Alternatively, we defined prefix[val][i] = count in arr[0..i-1]. So for index i (0-based), prefix[val][i] = count before i.
# Then count in [X,Y] = prefix[val][Y+1] - prefix[val][X].
# Yes.
cnt = prefix[val][Y+1] - prefix[val][X]
out(str(cnt) + '\n')
if name == "main":
main()
But building prefix by iterating over each val and scanning array each time leads to 100 scans of array of size 10k = 1M iterations, each iteration does a comparison and assignment. That's fine. However, the above method does a nested loop: for v in range(1,101): for i in range(N): if arr[i]==v: cnt+=1; pref_v[i+1]=cnt. That's 1M iterations of inner loop (100*10k). But each inner iteration also does a check arr[i]==v which is constant time. So 1M operations. This should be fast (<0.1s). However, we also have building prefix array: each of the 100 rows has N+1 entries (10001) so total 1,000,000+ assignments, also fine.
Alternatively, we could build prefix with a single pass:
prefix = [ [0](N+1) for _ in range(max_val+1) ]
for i, x in enumerate(arr):
# copy previous counts for all v? Actually we can just increment for x and for others copy? But copying all each step is also O(Nmax_val). But we can do this:
for v in range(1, max_val+1):
prefix[v][i+1] = prefix[v][i]
prefix[x][i+1] += 1
That's also O(N*max_val). But the previous method (100 passes) might be more cache friendly because each pass processes a row sequentially. In Python, row-wise iteration is often faster because it uses list of ints, and we iterate over arr each time, which is a list of ints. But the difference may be minor.
But we must consider that N=10k, max_val=100, so 1M operations, trivial.
Now main potential issue: reading Q=1e6 lines. Using data.readline() inside loop 1e6 times. That's fine but might be a bit slower. To speed up, we could read all queries at once using .read() and parse tokens. But as argued, that could cause memory high but maybe still okay. However, reading line by line might be simpler and also memory efficient. 1e6 lines reading in Python: each line small (like "0 4 2\n"). Using sys.stdin.buffer.readline in a loop can handle ~10-20 MB of input quickly. Usually reading 1e6 lines of about 10 characters each (10 MB) is fine. So I'll use line by line.
But we need to be careful about input format: The array line may have more than N numbers? It says "Next line will contains N spaced integers", so one line. But sometimes input may have multiple lines for array if they wrap? Not expected.
Implementation details: We'll read N, then arr, then Q, then Q queries.
Now I think the solution is straightforward.
But note: The constraints say "1 ≤ a[i],val ≤ 10^2". That means values are up to 100, inclusive. So max_val=100.
Edge Cases: val could be 0? According to constraints, val min 1. But we can handle if val=0, answer 0.
Also X and Y are zero-indexed and X ≤ Y.
Time Complexity: O(max_val*N + Q) which is about 1e6 + 1e6 = 2e6, which is fine.
We'll implement.
We might also consider using array module or numpy? Not needed.
We can also compress: Since values are small, we can store for each value a list of indices and use binary search to count within [X,Y]. That would be O(log n) per query. But Q=1e6 and N=10k, logN ~14, so 14e6 operations, still fine. But prefix approach is simpler and O(1) per query.
But building prefix using 100 passes might be okay.
Query 0 4 7 => 7 > 100? Actually 7 ≤100, but prefix[7] is all zeros, so count=0.
Output matches.
Edge Cases: If val=101? Not possible by constraints, but we check.
Now we need to ensure that prefix indices are correct: arr indices 0..N-1, we define prefix[v][i] = count of v in arr[0..i-1] (i positions). So for i from 0 to N, prefix[v][0] = 0; after processing arr[0], prefix[v][1] = count in arr[0]; after arr[1], prefix[v][2] = count in arr[0..1]; etc.
Thus count in [X,Y] inclusive = prefix[v][Y+1] - prefix[v][X].
Now we implement.
Potential improvement: Instead of building all 101 rows, we could build only rows for values that appear? But val can be any up to 100, so need all.
Alternative: Use list of 101 lists, each of length N+1. Good.
Now code.
We need to import sys.
Potential memory for prefix: 101 * (N+1) integers. Python ints are objects, so memory per int is ~28 bytes (maybe 28). 101 * 10001 = 1,010,101 ints. Times 28 ≈ 28.28 MB. That's okay. But maybe more due to list overhead: each row list has overhead, but total still < 40 MB. Plus arr and other structures, total < 50 MB. Good.
But we can reduce memory by using array('I') or using list of bytearrays? Since counts max N=10k, fits in 2 bytes? Actually 10k fits in 16-bit, but Python ints are bigger. But memory still acceptable.
If we want to be extra safe, we could use a 2D list of ints but still okay.
But building the prefix rows using for v in range(1,101): for i in range(N): ... will produce many int assignments; but Python int objects will be created; each assignment sets the element to an int (maybe small ints are cached? Actually Python caches small ints from -5 to 256, so 0,1,2,... are cached objects, so each assignment just stores reference to existing int object, memory for ints not duplicated, but the list holds references. So memory for the integers themselves is not allocated per element; the int objects for 0,1,2,... up to N are already cached? Actually, numbers up to 256 are cached, but counts can go up to N=10000, which is beyond 256, so numbers >256 will be created as new int objects? But when we assign counts like 10000, that int is not cached, so each occurrence of 10000 in the list is a reference to a separate int object? Actually ints are immutable; Python may reuse small ints via interning only for -5..256. For larger ints, each time we create the int 10000, it will be a new object? Actually, Python might reuse the same object if it is the same value? When we do cnt += 1, cnt is an int variable. When we assign pref_v[i+1] = cnt, we are storing the reference to the current int object. If later we update cnt, we create a new int object, but earlier stored references point to old ints. So many distinct int values will be stored (0,1,2,...,10000). Each such int object is allocated. So each row will have a sequence of counts from 0 to possibly N. But many of those ints are repeated across rows? Each row will have its own sequence of counts. For each row, counts increase from 0 to total count of that value (max maybe N). But many counts will be repeated across positions? Actually each position stores the count up to that index. For a given value v, the count sequence is non-decreasing. So many different integer values appear. For a row, we might have up to N+1 distinct ints (0 to some number). However, across rows, many ints may be reused (e.g., 0,1,2,...), but Python ints are objects; if we assign the same integer value from a cached source, they will refer to the same object if it's a small int (<=256). For values >256, each time we create that integer literal, Python may create a new object, but if we assign from the same variable referencing an int object, it will store reference to that same object. Let's think: When we compute cnt = 0, then for each i: if condition, cnt += 1. cnt is a variable referencing an int object. At each step, we assign pref_v[i+1] = cnt. That means that element of the list holds reference to the same int object that cnt refers to at that moment. Then cnt may change later; cnt += 1 creates a new int object (or may reuse from small int cache). The previous reference is stored. So for each step, we store a reference to the current int object. Those int objects are different if their values differ. So for each row, we will have many int objects, each representing the count at that index. That could be up to (N+1) distinct ints per row, but the actual values are monotonic and may not be all distinct; but typically, if the value appears many times, the count increments at those indices, so there could be up to (number of occurrences+1) distinct count values. For a value that appears many times, there could be up to N+1 distinct values? Actually if the value appears at almost every index, then the count increments each time, so we get 0,1,2,...,N distinct values (N+1 values). So each row could have up to N+1 distinct int objects, each unique. So total number of distinct int objects across all rows could be up to (max_val+1)*(N+1) = 1 million. That's a lot of int objects, each about 28 bytes => 28 MB for the ints, plus list overhead (8 bytes per reference) => 8 MB, plus the lists themselves. So total maybe 36-40 MB. That's acceptable.
But we can reduce memory by using array('H') (unsigned short) because N <= 10000, so counts fit in 16 bits. That would use 2 bytes per element, total ~2 MB. But that's extra complexity; not needed.
Given constraints, 40 MB is okay.
But we also need to store arr (list of ints) ~10k ints => 280KB.
Queries processing: reading lines, etc.
So overall memory usage < 50 MB, which is fine for typical online judge memory limit (256 MB or 512 MB). So okay.
But note: In Python, list of lists of ints: each inner list is a list object, each element is a PyObject pointer (8 bytes). The int objects themselves stored elsewhere. So total memory might be roughly: for each row (101 rows) list overhead ~56 bytes? Actually list object has overhead and an array of pointers (8 bytes per element). So 101 * (overhead of list ~56 + 8*(N+1)) ≈ 101 * (56 + 8*10001) ≈ 101 * (56 + 80008) ≈ 101 * 80064 ≈ 8,086,464 bytes ≈ 7.7 MB for pointers. Plus the int objects: For each int stored, there is an int object (~28 bytes) but note many ints may be shared? For small ints (0-256) they are shared, but for larger ints, each stored count will be a separate int object? Actually when we assign pref_v[i+1] = cnt, we store a reference to an int object. If later we assign another element to same integer value (like 5), it could reference the same int object if it's from the same source? But if we derived that int from arithmetic on previous ints, Python may create new int objects each time, but may also reuse small ints? For example, when we do cnt += 1, if cnt was 4 (which is cached), then cnt+1 = 5 (also cached), new object for 5 is the cached one. For numbers >256, new ints are created each time, but Python may also intern some? Not typical. So for each distinct count value >256, each time we create it (through cnt += 1) we create a new int object. However, note that for a given row, the count increments at most number_of_occurrences times; each time we assign the new count value to the prefix array at that index. The count variable holds that int. The next step we may assign the same count to multiple consecutive indices if no occurrence. So multiple positions might reference the same int object (if we assign the same int object to multiple list entries). For example, if cnt remains 5 for several indices, we assign pref_v[i+1] = cnt, and cnt is still 5, so each assignment stores reference to the same int object for 5. So number of distinct int objects per row is at most number_of_occurrences + 1 (unique count values). For a value that appears many times (e.g., 5000 times), the count goes from 0 to 5000, yielding 5001 distinct integer objects (0..5000). For values that appear rarely, there are few distinct counts. So overall total number of int objects across all rows is sum over v of (occurrences(v) + 1). Since total occurrences = N, sum (occurrences(v) + 1) = N + (max_val+1) maybe? Actually sum over v of (occurrences(v) + 1) = sum(occurrences(v)) + number_of_values = N + (max_val+1) ≈ N+101. So that's only about 10k+101, not 1 million! That's important: Because each row stores references to int objects; many positions share the same int object if the count hasn't changed. In the prefix array building we do: for each i, we set pref_v[i+1] = cnt. If cnt hasn't changed since last index, then we assign the same int object to the next position. So the number of unique int objects per row equals the number of times cnt changes + 1 (starting 0). The cnt changes exactly as many times as the value appears. So each row v will have at most (freq(v) + 1) distinct int objects. So total distinct int objects across rows is sum_{v} (freq(v)+1) = N + (max_val+1). That's about 10k+101 ~ 10k. So the memory for int objects is small! The list of pointers still holds N+1 pointers per row, but those pointers point to a much smaller set of int objects. So memory for int objects is not large. The bulk memory is the pointer arrays: 101 rows * (N+1) * 8 bytes = ~8 MB, plus overhead. So overall memory is fine.
Thus our approach is efficient.
Now implement code.
Potential pitfalls: Input reading may have extra spaces, empty lines. But we can strip each line.
We can also optimize prefix building by using a single pass and updating all rows? But anyway.
Let's write final code.
I'll use sys.stdin.buffer for fast input.
Testing with sample.
Will include condition: if val > 100 or val < 1: output 0, but val from constraints is >=1, so ok.
Now I'll produce final answer.
But the problem description: "Since chandu_don is busy in talking to his girlfriend,he wants you to solve a problem for him requiring range queries." Just story.
We are to output each query result.
Now let's ensure we handle X and Y inclusive correctly. We defined prefix[v][i] as count before index i. So count between X and Y inclusive = prefix[v][Y+1] - prefix[v][X].
We must be careful: Y+1 <= N. Since Y < N, Y+1 <= N, so fine.
Edge: X=0, then prefix[v][0] = 0.
Now code.
Potential micro-optimizations: Use local variables for prefix and arr to avoid global lookups. But not needed.
Implementation details: While building prefix, we might have to create rows for 0..100. We'll use range(1, max_val+1) to skip 0 if we want. But we'll keep index 0 unused.
Alternatively, we could use a dictionary mapping value to its prefix list. Since max_val is small, easier to preallocate.
Now final answer.
We'll provide explanation and code.
But as ChatGPT we need to output final code.
I'll include comments.
Now write final message. ```python
import sys
def main():
data = sys.stdin.buffer
code
# read N
N = int(data.readline())
# read the array
arr = list(map(int, data.readline().split()))
# read Q
Q = int(data.readline())
MAX_VAL = 100 # given upper bound for array values and queries
# prefix[val][i] = number of occurrences of 'val' in arr[0 .. i-1]
# we keep rows for indices 0..MAX_VAL (0 unused)
prefix = [ [0]*(N+1) for _ in range(MAX_VAL+1) ]
# build prefix rows for each possible value 1..MAX_VAL
for val in range(1, MAX_VAL+1):
cnt = 0
row = prefix[val]
# scan the array once
for i, x in enumerate(arr):
if x == val:
cnt += 1
row[i+1] = cnt
out = sys.stdout.write
# answer queries
for _ in range(Q):
line = data.readline()
# in case of blank lines (should not happen)
while line.strip() == b'':
line = data.readline()
X_str, Y_str, val_str = line.split()
X = int(X_str)
Y = int(Y_str)
v = int(val_str)
if v < 1 or v > MAX_VAL:
out('0\n')
continue
# count in [X, Y] = prefix[v][Y+1] - prefix[v][X]
ans = prefix[v][Y+1] - prefix[v][X]
out(str(ans) + '\n')