message
stringlengths
2
44.5k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
42
109k
cluster
float64
5
5
__index_level_0__
int64
84
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers l and r (l ≤ r). Your task is to calculate the sum of numbers from l to r (including l and r) such that each number contains at most k different digits, and print this sum modulo 998244353. For example, if k = 1 then you have to calculate all numbers from l to r such that each number is formed using only one digit. For l = 10, r = 50 the answer is 11 + 22 + 33 + 44 = 110. Input The only line of the input contains three integers l, r and k (1 ≤ l ≤ r < 10^{18}, 1 ≤ k ≤ 10) — the borders of the segment and the maximum number of different digits. Output Print one integer — the sum of numbers from l to r such that each number contains at most k different digits, modulo 998244353. Examples Input 10 50 2 Output 1230 Input 1 2345 10 Output 2750685 Input 101 154 2 Output 2189 Note For the first example the answer is just the sum of numbers from l to r which equals to (50 ⋅ 51)/(2) - (9 ⋅ 10)/(2) = 1230. This example also explained in the problem statement but for k = 1. For the second example the answer is just the sum of numbers from l to r which equals to (2345 ⋅ 2346)/(2) = 2750685. For the third example the answer is 101 + 110 + 111 + 112 + 113 + 114 + 115 + 116 + 117 + 118 + 119 + 121 + 122 + 131 + 133 + 141 + 144 + 151 = 2189. Submitted Solution: ``` mass = [int(x) for x in input().split()] n = 0 mass = [x for x in range(mass[0], mass[1]+1) if len(list(str(x))) <= mass[2]] print(sum(mass)) ```
instruction
0
42
5
84
No
output
1
42
5
85
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers l and r (l ≤ r). Your task is to calculate the sum of numbers from l to r (including l and r) such that each number contains at most k different digits, and print this sum modulo 998244353. For example, if k = 1 then you have to calculate all numbers from l to r such that each number is formed using only one digit. For l = 10, r = 50 the answer is 11 + 22 + 33 + 44 = 110. Input The only line of the input contains three integers l, r and k (1 ≤ l ≤ r < 10^{18}, 1 ≤ k ≤ 10) — the borders of the segment and the maximum number of different digits. Output Print one integer — the sum of numbers from l to r such that each number contains at most k different digits, modulo 998244353. Examples Input 10 50 2 Output 1230 Input 1 2345 10 Output 2750685 Input 101 154 2 Output 2189 Note For the first example the answer is just the sum of numbers from l to r which equals to (50 ⋅ 51)/(2) - (9 ⋅ 10)/(2) = 1230. This example also explained in the problem statement but for k = 1. For the second example the answer is just the sum of numbers from l to r which equals to (2345 ⋅ 2346)/(2) = 2750685. For the third example the answer is 101 + 110 + 111 + 112 + 113 + 114 + 115 + 116 + 117 + 118 + 119 + 121 + 122 + 131 + 133 + 141 + 144 + 151 = 2189. Submitted Solution: ``` mass = [int(x) for x in input().split()] n = 0 for x in range(mass[0], mass[1]+1): if len(set(str(x))) == mass[2]: n+= x print(n) ```
instruction
0
43
5
86
No
output
1
43
5
87
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers l and r (l ≤ r). Your task is to calculate the sum of numbers from l to r (including l and r) such that each number contains at most k different digits, and print this sum modulo 998244353. For example, if k = 1 then you have to calculate all numbers from l to r such that each number is formed using only one digit. For l = 10, r = 50 the answer is 11 + 22 + 33 + 44 = 110. Input The only line of the input contains three integers l, r and k (1 ≤ l ≤ r < 10^{18}, 1 ≤ k ≤ 10) — the borders of the segment and the maximum number of different digits. Output Print one integer — the sum of numbers from l to r such that each number contains at most k different digits, modulo 998244353. Examples Input 10 50 2 Output 1230 Input 1 2345 10 Output 2750685 Input 101 154 2 Output 2189 Note For the first example the answer is just the sum of numbers from l to r which equals to (50 ⋅ 51)/(2) - (9 ⋅ 10)/(2) = 1230. This example also explained in the problem statement but for k = 1. For the second example the answer is just the sum of numbers from l to r which equals to (2345 ⋅ 2346)/(2) = 2750685. For the third example the answer is 101 + 110 + 111 + 112 + 113 + 114 + 115 + 116 + 117 + 118 + 119 + 121 + 122 + 131 + 133 + 141 + 144 + 151 = 2189. Submitted Solution: ``` l, r, k =map(int,input().split()) d = {i:2**i for i in range(10)} def can(i, m): return d[i] & m def calc(m): b = 1 c = 0 for i in range(10): if b & m: c += 1 b *= 2 return c def sm(ln, k, m, s='', first=False): if ln < 1: return 0, 1 ans = 0 count = 0 base = 10 ** (ln-1) use_new = calc(m) < k if s: finish = int(s[0])+1 else: finish = 10 for i in range(finish): if use_new or can(i, m): ss = s[1:] if i != finish-1: ss = '' nm = m | d[i] if i == 0 and first: nm = m nexta, nextc = sm(ln-1, k, nm, ss) ans += base * i * nextc + nexta count += nextc #print(ln, k, m, s, first, ans, count) return ans, count def call(a, k): s = str(a) return sm(len(s), k, 0, s, True)[0] print(call(r, k) - call(l-1, k)) ```
instruction
0
44
5
88
No
output
1
44
5
89
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. Let min(l, r) be the minimum value among a_l, a_{l + 1}, …, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, …, a_r. Your task is to choose three positive (greater than 0) integers x, y and z such that: * x + y + z = n; * max(1, x) = min(x + 1, x + y) = max(x + y + 1, n). In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition). Among all such triples (partitions), you can choose any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the length of a. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5). Output For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line. If there are several answers, you can print any. Example Input 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 Output YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4 Submitted Solution: ``` import sys input = sys.stdin.readline def format(a, b=0, n=0): if a == -1: return "NO" else: return "YES\n{} {} {}".format(a, b-a, n-b) def solve(): n = int(input()) ar = [int(t) for t in input().split()] rmq = RangeQuery(ar) fmax = max(ar) idxs = [i for i in range(n) if ar[i] == fmax] if len(idxs) >= 3: mid = idxs[1] return format(mid, mid+1, n) front = [] cmax, event = 0, 0 for i in range(n): x = ar[i] if x > cmax: front += [(cmax, event)] cmax = x event = i elif x < cmax: event = i back = [] cmax, event = 0, n for i in reversed(range(n)): x = ar[i] if x > cmax: back += [(cmax, event)] cmax = x event = i elif x < cmax: event = i fit = iter(front) bit = iter(back) f = next(fit, False) b = next(bit, False) while f and b: if f[0] == b[0]: if f[0] == rmq.query(f[1]+1, b[1]): return format(f[1]+1, b[1], n) else: f = next(fit, False) b = next(bit, False) elif f[0] < b[0]: f = next(fit, False) else: b = next(bit, False) return format(-1) def main(): T = int(input()) ans = [] for _ in range(T): ans += [solve()] print(*ans, sep='\n') class RangeQuery: def __init__(self, data, func=min): self.func = func self._data = _data = [list(data)] i, n = 1, len(_data[0]) while 2 * i <= n: prev = _data[-1] _data.append([func(prev[j], prev[j + i]) for j in range(n - 2 * i + 1)]) i <<= 1 def query(self, start, stop): """func of data[start, stop)""" depth = (stop - start).bit_length() - 1 return self.func(self._data[depth][start], self._data[depth][stop - (1 << depth)]) def __getitem__(self, idx): return self._data[0][idx] main() ```
instruction
0
192
5
384
Yes
output
1
192
5
385
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. Let min(l, r) be the minimum value among a_l, a_{l + 1}, …, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, …, a_r. Your task is to choose three positive (greater than 0) integers x, y and z such that: * x + y + z = n; * max(1, x) = min(x + 1, x + y) = max(x + y + 1, n). In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition). Among all such triples (partitions), you can choose any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the length of a. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5). Output For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line. If there are several answers, you can print any. Example Input 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 Output YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4 Submitted Solution: ``` import heapq def heapu(counted, heee, f, d, e): for i in range(f, d, e): while (len(heee) > 0 and (heee[0][0] * -1) > a[i]): counted[heee[0][1]] = (i - heee[0][1]) heapq.heappop(heee) heapq.heappush(heee, [a[i] * -1, i]) return counted for t in range(int(input())): n=int(input()) a=list(map(int,input().split())) he=[[-a[0],0]] plus=0 f_counted=[] b_counted=[] for i in range(n): b_counted.append(plus) plus-=1 plus=n-1 for i in range(n): f_counted.append(plus) plus-=1 counted_f=heapu(f_counted,he,1,n-1,1) hee=[[-a[n-1],n-1]] counted_b=heapu(b_counted,hee,n-1,0,-1) f_maxi=[] maxi=0 for i in range(0,n): maxi=max(maxi,a[i]) f_maxi.append(maxi) b_maxi=[] maxi=0 for i in range(n-1,-1,-1): maxi=max(maxi,a[i]) b_maxi.append(maxi) b_maxi=b_maxi[::-1] x, y, z = -1, -1, -1 flag=1 for i in range(1,n-1): x, y, z = -1, -1, -1 if(a[i]==b_maxi[i+counted_f[i]]): z=n-i-f_counted[i] elif(a[i]==b_maxi[i+counted_f[i]-1] and counted_f[i]>1): z=n-i-f_counted[i]+1 if(a[i]==f_maxi[i+counted_b[i]]): x=i+b_counted[i]+1 elif(a[i]==f_maxi[i+counted_b[i]+1] and counted_b[i]<-1): x=i+b_counted[i]+1+1 y=n-x-z if(x>-1 and z>-1): flag=0 break if(flag==0): print("YES") print(x,y,z) else: print("NO") ```
instruction
0
193
5
386
Yes
output
1
193
5
387
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. Let min(l, r) be the minimum value among a_l, a_{l + 1}, …, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, …, a_r. Your task is to choose three positive (greater than 0) integers x, y and z such that: * x + y + z = n; * max(1, x) = min(x + 1, x + y) = max(x + y + 1, n). In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition). Among all such triples (partitions), you can choose any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the length of a. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5). Output For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line. If there are several answers, you can print any. Example Input 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 Output YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4 Submitted Solution: ``` from sys import stdin, stdout from math import log, ceil # 1 # 9 # 2 1 | 4 2 4 3 3 | 1 2 def array_partition(n, a_a): lm = [a_a[0]] rm = [a_a[-1]] for i in range(1, n): lm.append(max(lm[-1], a_a[i])) rm.append(max(rm[-1], a_a[-(i+1)])) r = n - 1 for l in range(n - 2): left = lm[l] while r > l + 1 and rm[n - 1 - r] <= left: r -= 1 if r < n - 1: r += 1 if left == rm[n - 1 - r]: mm = query(0, 0, n - 1, l + 1, r - 1) if left == mm: return [l + 1, r - l - 1, n - r] elif mm < left: pass elif r < n - 1: r += 1 if left == rm[n - 1 - r] and query(0, 0, n - 1, l + 1, r - 1) == left: return [l + 1, r - l - 1, n - r] return [] def build(cur, l, r): if l == r: segTree[cur] = a_a[l] return a_a[l] mid = l + (r - l) // 2 segTree[cur] = min(build(2 * cur + 1, l, mid), build(2 * cur + 2, mid + 1, r)) return segTree[cur] def query(cur, sl, sr, l, r): if l <= sl and r >= sr: return segTree[cur] elif l > sr or r < sl: return float('inf') else: mid = sl + (sr - sl) // 2 return min(query(cur * 2 + 1, sl, mid, l, r), query(cur * 2 + 2, mid + 1, sr, l, r)) t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) a_a = list(map(int, stdin.readline().split())) size = int(2 ** (ceil(log(n, 2)) + 1) - 1) segTree = [float('inf') for i in range(size)] build(0, 0, n - 1) r = array_partition(n, a_a) if r: stdout.write('YES\n') stdout.write(' '.join(map(str, r)) + '\n') else: stdout.write('NO\n') ```
instruction
0
194
5
388
Yes
output
1
194
5
389
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. Let min(l, r) be the minimum value among a_l, a_{l + 1}, …, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, …, a_r. Your task is to choose three positive (greater than 0) integers x, y and z such that: * x + y + z = n; * max(1, x) = min(x + 1, x + y) = max(x + y + 1, n). In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition). Among all such triples (partitions), you can choose any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the length of a. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5). Output For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line. If there are several answers, you can print any. Example Input 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 Output YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4 Submitted Solution: ``` import sys input=sys.stdin.readline import bisect t=int(input()) for you in range(t): n=int(input()) l=input().split() li=[int(i) for i in l] hashi=dict() for i in range(n): if(li[i] in hashi): hashi[li[i]].append(i) else: hashi[li[i]]=[] hashi[li[i]].append(i) maxpref=[0 for i in range(n)] maxsuff=[0 for i in range(n)] maxa=0 for i in range(n): maxa=max(maxa,li[i]) maxpref[i]=maxa maxa=0 for i in range(n-1,-1,-1): maxa=max(maxa,li[i]) maxsuff[i]=maxa nextlow=[n for i in range(n)] prevlow=[-1 for i in range(n)] s=[] for i in range(n): while(s!=[] and li[s[-1]]>li[i]): nextlow[s[-1]]=i s.pop(-1) s.append(i) for i in range(n-1,-1,-1): while(s!=[] and li[s[-1]]>li[i]): prevlow[s[-1]]=i s.pop(-1) s.append(i) maxs=dict() maxe=dict() for i in range(n): maxs[maxpref[i]]=i for i in range(n-1,-1,-1): maxe[maxsuff[i]]=i hashs=dict() hashe=dict() for i in range(n): if(maxpref[i] not in hashs): hashs[maxpref[i]]=[i] else: hashs[maxpref[i]].append(i) for i in range(n): if(maxsuff[i] not in hashe): hashe[maxsuff[i]]=[i] else: hashe[maxsuff[i]].append(i) poss=0 for i in range(n): if(li[i] not in hashs or li[i] not in hashe): continue z=bisect.bisect_left(hashs[li[i]],i) z-=1 if(z<0 or z>=len(hashs[li[i]]) or hashs[li[i]][z]<prevlow[i]): continue y=bisect.bisect_right(hashe[li[i]],i) if(y>=len(hashe[li[i]]) or hashe[li[i]][y]>nextlow[i]): continue poss=1 ans=(hashs[li[i]][z],hashe[li[i]][y]) break if(poss): print("YES") print(ans[0]+1,ans[1]-ans[0]-1,n-ans[1]) else: print("NO") ```
instruction
0
195
5
390
Yes
output
1
195
5
391
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. Let min(l, r) be the minimum value among a_l, a_{l + 1}, …, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, …, a_r. Your task is to choose three positive (greater than 0) integers x, y and z such that: * x + y + z = n; * max(1, x) = min(x + 1, x + y) = max(x + y + 1, n). In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition). Among all such triples (partitions), you can choose any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the length of a. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5). Output For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line. If there are several answers, you can print any. Example Input 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 Output YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4 Submitted Solution: ``` import sys max_int = 2147483648 # 2^31 def set_val(i, v, x, lx, rx): if rx - lx == 1: seg_tree[x] = v else: mid = (lx + rx) // 2 if i < mid: set_val(i, v, 2 * x + 1, lx, mid) else: set_val(i, v, 2 * x + 2, mid, rx) seg_tree[x] = min(seg_tree[2 * x + 1], seg_tree[2 * x + 2]) def get_min(l, r, x, lx, rx): if lx >= r or l >= rx: return max_int if lx >= l and rx <= r: return seg_tree[x] mid = (lx + rx) // 2 min1 = get_min(l, r, 2 * x + 1, lx, mid) min2 = get_min(l, r, 2 * x + 2, mid, rx) return min(min1, min2) t = int(input()) for _t in range(t): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) size = 1 while size < n: size *= 2 seg_tree = [max_int] * (2 * size - 1) for i, num in enumerate(a, size - 1): seg_tree[i] = num for i in range(size - 2, -1, -1): seg_tree[i] = min(seg_tree[2 * i + 1], seg_tree[2 * i + 2]) i = 0 j = n - 1 m1 = 0 m3 = 0 while j - i > 1: m1 = max(m1, a[i]) m3 = max(m3, a[j]) while i < n - 1 and a[i + 1] < m1: i += 1 while j > 0 and a[j - 1] < m1: j -= 1 if m1 == m3: if get_min(i + 1, j, 0, 0, size) == m1: print('YES') print(i + 1, j - i - 1, n - j) break i += 1 j -= 1 elif m1 < m3: i += 1 else: j -= 1 else: print('NO') ```
instruction
0
196
5
392
No
output
1
196
5
393
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. Let min(l, r) be the minimum value among a_l, a_{l + 1}, …, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, …, a_r. Your task is to choose three positive (greater than 0) integers x, y and z such that: * x + y + z = n; * max(1, x) = min(x + 1, x + y) = max(x + y + 1, n). In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition). Among all such triples (partitions), you can choose any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the length of a. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5). Output For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line. If there are several answers, you can print any. Example Input 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 Output YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4 Submitted Solution: ``` from bisect import * from collections import * from math import gcd,ceil,sqrt,floor,inf from heapq import * from itertools import * from operator import add,mul,sub,xor,truediv,floordiv from functools import * #------------------------------------------------------------------------ import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #------------------------------------------------------------------------ def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) #------------------------------------------------------------------------ from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc farr=[1] ifa=[] def fact(x,mod=0): if mod: while x>=len(farr): farr.append(farr[-1]*len(farr)%mod) else: while x>=len(farr): farr.append(farr[-1]*len(farr)) return farr[x] def ifact(x,mod): global ifa ifa.append(pow(farr[-1],mod-2,mod)) for i in range(x,0,-1): ifa.append(ifa[-1]*i%mod) ifa=ifa[::-1] def per(i,j,mod=0): if i<j: return 0 if not mod: return fact(i)//fact(i-j) return farr[i]*ifa[i-j]%mod def com(i,j,mod=0): if i<j: return 0 if not mod: return per(i,j)//fact(j) return per(i,j,mod)*ifa[j]%mod def catalan(n): return com(2*n,n)//(n+1) def linc(f,t,l,r): while l<r: mid=(l+r)//2 if t>f(mid): l=mid+1 else: r=mid return l def rinc(f,t,l,r): while l<r: mid=(l+r+1)//2 if t<f(mid): r=mid-1 else: l=mid return l def ldec(f,t,l,r): while l<r: mid=(l+r)//2 if t<f(mid): l=mid+1 else: r=mid return l def rdec(f,t,l,r): while l<r: mid=(l+r+1)//2 if t>f(mid): r=mid-1 else: l=mid return l def isprime(n): for i in range(2,int(n**0.5)+1): if n%i==0: return False return True def binfun(x): c=0 for w in arr: c+=ceil(w/x) return c def lowbit(n): return n&-n def inverse(a,m): a%=m if a<=1: return a return ((1-inverse(m,a)*m)//a)%m class BIT: def __init__(self,arr): self.arr=arr self.n=len(arr)-1 def update(self,x,v): while x<=self.n: self.arr[x]+=v x+=x&-x def query(self,x): ans=0 while x: ans+=self.arr[x] x&=x-1 return ans ''' class SMT: def __init__(self,arr): self.n=len(arr)-1 self.arr=[0]*(self.n<<2) self.lazy=[0]*(self.n<<2) def Build(l,r,rt): if l==r: self.arr[rt]=arr[l] return m=(l+r)>>1 Build(l,m,rt<<1) Build(m+1,r,rt<<1|1) self.pushup(rt) Build(1,self.n,1) def pushup(self,rt): self.arr[rt]=self.arr[rt<<1]+self.arr[rt<<1|1] def pushdown(self,rt,ln,rn):#lr,rn表区间数字数 if self.lazy[rt]: self.lazy[rt<<1]+=self.lazy[rt] self.lazy[rt<<1|1]=self.lazy[rt] self.arr[rt<<1]+=self.lazy[rt]*ln self.arr[rt<<1|1]+=self.lazy[rt]*rn self.lazy[rt]=0 def update(self,L,R,c,l=1,r=None,rt=1):#L,R表示操作区间 if r==None: r=self.n if L<=l and r<=R: self.arr[rt]+=c*(r-l+1) self.lazy[rt]+=c return m=(l+r)>>1 self.pushdown(rt,m-l+1,r-m) if L<=m: self.update(L,R,c,l,m,rt<<1) if R>m: self.update(L,R,c,m+1,r,rt<<1|1) self.pushup(rt) def query(self,L,R,l=1,r=None,rt=1): if r==None: r=self.n #print(L,R,l,r,rt) if L<=l and R>=r: return self.arr[rt] m=(l+r)>>1 self.pushdown(rt,m-l+1,r-m) ans=0 if L<=m: ans+=self.query(L,R,l,m,rt<<1) if R>m: ans+=self.query(L,R,m+1,r,rt<<1|1) return ans ''' class DSU:#容量+路径压缩 def __init__(self,n): self.c=[-1]*n def same(self,x,y): return self.find(x)==self.find(y) def find(self,x): if self.c[x]<0: return x self.c[x]=self.find(self.c[x]) return self.c[x] def union(self,u,v): u,v=self.find(u),self.find(v) if u==v: return False if self.c[u]<self.c[v]: u,v=v,u self.c[u]+=self.c[v] self.c[v]=u return True def size(self,x): return -self.c[self.find(x)] class UFS:#秩+路径 def __init__(self,n): self.parent=[i for i in range(n)] self.ranks=[0]*n def find(self,x): if x!=self.parent[x]: self.parent[x]=self.find(self.parent[x]) return self.parent[x] def union(self,u,v): pu,pv=self.find(u),self.find(v) if pu==pv: return False if self.ranks[pu]>=self.ranks[pv]: self.parent[pv]=pu if self.ranks[pv]==self.ranks[pu]: self.ranks[pu]+=1 else: self.parent[pu]=pv def Prime(n): c=0 prime=[] flag=[0]*(n+1) for i in range(2,n+1): if not flag[i]: prime.append(i) c+=1 for j in range(c): if i*prime[j]>n: break flag[i*prime[j]]=prime[j] if i%prime[j]==0: break return prime def dij(s,graph): d={} d[s]=0 heap=[(0,s)] seen=set() while heap: dis,u=heappop(heap) if u in seen: continue for v in graph[u]: if v not in d or d[v]>d[u]+graph[u][v]: d[v]=d[u]+graph[u][v] heappush(heap,(d[v],v)) return d def GP(it): return [[ch,len(list(g))] for ch,g in groupby(it)] class DLN: def __init__(self,val): self.val=val self.pre=None self.next=None t=N() for i in range(t): n=N() a=RLL() ma=max(a) res=[] c=0 for i,x in enumerate(a): if x==ma: c+=1 res.append(i+1) if c>=3: x=res[1]-1 y=1 z=n-x-y print('YES') print(x,y,z) else: l=r=res[0]-1 pre=[0]*n suf=[0]*n pre[0]=a[0] suf[n-1]=a[n-1] #print(l,r) for i in range(l): pre[i]=max(pre[i-1],a[i]) for j in range(n-2,r,-1): suf[j]=max(suf[j+1],a[j]) s=set(a) s=sorted(s,reverse=True) f=1 c=Counter() c[a[l]]+=1 for j in range(1,len(s)): while r<n-1 and a[r+1]>=s[j]: c[a[r+1]]+=1 r+=1 while l>0 and a[l-1]>=s[j]: l-=1 c[a[l]]+=1 #print(s[j],l,r,c) tmp=c[s[j]] x=l y=r+1-x if l==0: if a[l]==s[j]: x=1 y-=1 tmp-=1 else: f=0 break else: if pre[l-1]>s[j]: continue elif pre[l-1]<s[j]: if a[l]==s[j]: x+=1 y-=1 tmp-=1 else: continue if r==n-1: if a[r]==s[j]: tmp-=1 y-=1 else: f=0 break else: if suf[r+1]>s[j]: continue elif suf[r+1]<s[j]: if a[r]==s[j]: y-=1 tmp-=1 if tmp: break if f: print('YES') if not max(a[:x])==min(a[x:x+y])==max(a[x+y:]): print(a) print(x,y,n-x-y) else: print('NO') ''' sys.setrecursionlimit(200000) import threading threading.stack_size(10**8) t=threading.Thread(target=main) t.start() t.join() ''' ''' sys.setrecursionlimit(200000) import threading threading.stack_size(10**8) t=threading.Thread(target=main) t.start() t.join() ''' ```
instruction
0
197
5
394
No
output
1
197
5
395
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. Let min(l, r) be the minimum value among a_l, a_{l + 1}, …, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, …, a_r. Your task is to choose three positive (greater than 0) integers x, y and z such that: * x + y + z = n; * max(1, x) = min(x + 1, x + y) = max(x + y + 1, n). In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition). Among all such triples (partitions), you can choose any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the length of a. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5). Output For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line. If there are several answers, you can print any. Example Input 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 Output YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4 Submitted Solution: ``` import heapq from collections import defaultdict testCaseCounter = int(input()) for lksjdfa in range(testCaseCounter): n = int(input()) arr = [int(amit) for amit in input().split(" ")] leftPointer = 0 rightPointer = n-1 leftMax = arr[leftPointer] rightMax = arr[rightPointer] middleHeap = arr[1:-1].copy() #print (arr) heapq.heapify(middleHeap) #print (arr) freqMiddle = defaultdict(int)# stores the freq of every element in middle section maxMiddle = None freqMiddle[leftMax] += 1 freqMiddle[rightMax] += 1 for item in middleHeap: freqMiddle[item] += 1 if freqMiddle[item] >= 3 and (maxMiddle is None or item > maxMiddle): maxMiddle = item freqMiddle[leftMax] -= 1 freqMiddle[rightMax] -= 1 while maxMiddle is not None and leftPointer < rightPointer + 1 and len(middleHeap) > 0: #print ((leftMax, middleHeap[0], rightMax)) #print ((leftPointer, rightPointer)) if leftMax == rightMax == middleHeap[0]: # success break elif leftMax > maxMiddle or rightMax > maxMiddle: # failure break elif leftMax < rightMax: leftPointer += 1 leftMax = max(leftMax, arr[leftPointer]) freqMiddle[arr[leftPointer]] -= 1 while len(middleHeap) > 0 and freqMiddle[middleHeap[0]] == 0: heapq.heappop(middleHeap) elif rightMax < leftMax: rightPointer -= 1 rightMax = max(rightMax, arr[rightPointer]) freqMiddle[arr[rightPointer]] -= 1 while len(middleHeap) > 0 and freqMiddle[middleHeap[0]] == 0: heapq.heappop(middleHeap) elif leftMax == rightMax: if middleHeap[0] < leftMax: # middle value is too small, let us eat the smallest value away if arr[leftPointer + 1] <= arr[rightPointer - 1] and (arr[leftPointer + 1] < maxMiddle or arr[leftPointer + 1] == maxMiddle and freqMiddle[maxMiddle] > 2): # eat the left leftPointer += 1 leftMax = max(leftMax, arr[leftPointer]) freqMiddle[arr[leftPointer]] -= 1 while len(middleHeap) > 0 and freqMiddle[middleHeap[0]] == 0: heapq.heappop(middleHeap) else: rightPointer -= 1 rightMax = max(rightMax, arr[rightPointer]) freqMiddle[arr[rightPointer]] -= 1 while len(middleHeap) > 0 and freqMiddle[middleHeap[0]] == 0: heapq.heappop(middleHeap) else: # middle value is too big, let the smaller value stay if arr[leftPointer + 1] >= arr[rightPointer - 1] and (arr[leftPointer + 1] < maxMiddle or arr[leftPointer + 1] == maxMiddle and freqMiddle[maxMiddle] > 2): # eat the left leftPointer += 1 leftMax = max(leftMax, arr[leftPointer]) freqMiddle[arr[leftPointer]] -= 1 while len(middleHeap) > 0 and freqMiddle[middleHeap[0]] == 0: heapq.heappop(middleHeap) else: rightPointer -= 1 rightMax = max(rightMax, arr[rightPointer]) freqMiddle[arr[rightPointer]] -= 1 while len(middleHeap) > 0 and freqMiddle[middleHeap[0]] == 0: heapq.heappop(middleHeap) if len(middleHeap) > 0 and leftMax == rightMax == middleHeap[0]: x = leftPointer + 1 y = rightPointer - 1 - leftPointer z = (n - x - y) print ("YES") print (str(x) + " " + str(y) + " " + str(z)) else: print ("NO") ```
instruction
0
198
5
396
No
output
1
198
5
397
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. Let min(l, r) be the minimum value among a_l, a_{l + 1}, …, a_r and max(l, r) be the maximum value among a_l, a_{l + 1}, …, a_r. Your task is to choose three positive (greater than 0) integers x, y and z such that: * x + y + z = n; * max(1, x) = min(x + 1, x + y) = max(x + y + 1, n). In other words, you have to split the array a into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition). Among all such triples (partitions), you can choose any. You have to answer t independent test cases. Input The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the length of a. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5). Output For each test case, print the answer: NO in the only line if there is no such partition of a that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers x, y and z (x + y + z = n) in the second line. If there are several answers, you can print any. Example Input 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 Output YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4 Submitted Solution: ``` a=int(input()) li=list(map(int,input().split())) if a%2!=0: print("YES") print(1,2,a-3) else: print("NO") ```
instruction
0
199
5
398
No
output
1
199
5
399
Provide tags and a correct Python 3 solution for this coding contest problem. Input The first line of the input contains a single integer N (1 ≤ N ≤ 24). The next N lines contain 5 space-separated integers each. The first three integers will be between 0 and 2, inclusive. The last two integers will be between 0 and 3, inclusive. The sum of the first three integers will be equal to the sum of the last two integers. Output Output the result – a string of lowercase English letters. Examples Input 1 1 0 0 1 0 Output a Input 10 2 0 0 1 1 1 1 1 2 1 2 1 0 1 2 1 1 0 1 1 2 1 0 2 1 1 1 1 2 1 1 2 1 3 1 2 0 0 1 1 1 1 0 1 1 1 1 2 2 2 Output codeforcez
instruction
0
207
5
414
Tags: *special, implementation Correct Solution: ``` Y='1 11 1001 11001 10001 1011 11011 10011 1010 11010 101 111 1101 11101 10101 1111 11111 10111 1110 11110 100101 100111 111010 101101 111101 110101' X=list(map(lambda x:int(x,2),Y.split())) N=int(input()) ANS=[] for i in range(N): a,b,c,d,e=map(int,input().split()) A=[a,b,c] B=[d,e] for j in range(26): C,D=[0,0,0],[0,0] for k in range(6): C[k%3]+=(X[j]>>k)&1 D[k//3]+=(X[j]>>k)&1 if A==C and B==D: ANS.append(chr(ord('a')+j)) break print(''.join(ANS)) ```
output
1
207
5
415
Provide tags and a correct Python 3 solution for this coding contest problem. Input The first line of the input contains a single integer N (1 ≤ N ≤ 24). The next N lines contain 5 space-separated integers each. The first three integers will be between 0 and 2, inclusive. The last two integers will be between 0 and 3, inclusive. The sum of the first three integers will be equal to the sum of the last two integers. Output Output the result – a string of lowercase English letters. Examples Input 1 1 0 0 1 0 Output a Input 10 2 0 0 1 1 1 1 1 2 1 2 1 0 1 2 1 1 0 1 1 2 1 0 2 1 1 1 1 2 1 1 2 1 3 1 2 0 0 1 1 1 1 0 1 1 1 1 2 2 2 Output codeforcez
instruction
0
208
5
416
Tags: *special, implementation Correct Solution: ``` q={ "1 0 0 1 0" : 'a', "1 1 0 2 0" : 'b', "2 0 0 1 1" : 'c', "2 1 0 1 2" : 'd', "1 1 0 1 1" : 'e', "2 1 0 2 1" : 'f', "2 2 0 2 2" : 'g', "1 2 0 2 1" : 'h', #"1 1 0 1 1" : 'i', "1 2 0 1 2" : 'j', "1 0 1 2 0" : 'k', "1 1 1 3 0" : 'l', "2 0 1 2 1" : 'm', "2 1 1 2 2" : 'n', "1 1 1 2 1" : 'o', "2 1 1 3 1" : 'p', "2 2 1 3 2" : 'q', "1 2 1 3 1" : 'r', #"1 1 1 2 1" : 's', "1 2 1 2 2" : 't', "1 0 2 2 1" : 'u', "1 1 2 3 1" : 'v', "2 0 2 2 2" : 'x', "2 1 2 2 3" : 'y', "1 1 2 2 2" : 'z', "1 2 1 1 3" : 'w' } print(''.join([q[input()] for i in range(int(input()))])) ```
output
1
208
5
417
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Input The first line of the input contains a single integer N (1 ≤ N ≤ 24). The next N lines contain 5 space-separated integers each. The first three integers will be between 0 and 2, inclusive. The last two integers will be between 0 and 3, inclusive. The sum of the first three integers will be equal to the sum of the last two integers. Output Output the result – a string of lowercase English letters. Examples Input 1 1 0 0 1 0 Output a Input 10 2 0 0 1 1 1 1 1 2 1 2 1 0 1 2 1 1 0 1 1 2 1 0 2 1 1 1 1 2 1 1 2 1 3 1 2 0 0 1 1 1 1 0 1 1 1 1 2 2 2 Output codeforcez Submitted Solution: ``` for i in range(int(input())): input() print('lol') ```
instruction
0
217
5
434
No
output
1
217
5
435
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Input The first line of the input contains a single integer N (1 ≤ N ≤ 24). The next N lines contain 5 space-separated integers each. The first three integers will be between 0 and 2, inclusive. The last two integers will be between 0 and 3, inclusive. The sum of the first three integers will be equal to the sum of the last two integers. Output Output the result – a string of lowercase English letters. Examples Input 1 1 0 0 1 0 Output a Input 10 2 0 0 1 1 1 1 1 2 1 2 1 0 1 2 1 1 0 1 1 2 1 0 2 1 1 1 1 2 1 1 2 1 3 1 2 0 0 1 1 1 1 0 1 1 1 1 2 2 2 Output codeforcez Submitted Solution: ``` s=int(input()) print((2-s) if s==1 else 0) ```
instruction
0
218
5
436
No
output
1
218
5
437
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Input The first line of the input contains a single integer N (1 ≤ N ≤ 24). The next N lines contain 5 space-separated integers each. The first three integers will be between 0 and 2, inclusive. The last two integers will be between 0 and 3, inclusive. The sum of the first three integers will be equal to the sum of the last two integers. Output Output the result – a string of lowercase English letters. Examples Input 1 1 0 0 1 0 Output a Input 10 2 0 0 1 1 1 1 1 2 1 2 1 0 1 2 1 1 0 1 1 2 1 0 2 1 1 1 1 2 1 1 2 1 3 1 2 0 0 1 1 1 1 0 1 1 1 1 2 2 2 Output codeforcez Submitted Solution: ``` for i in range(int(input())): a, b, c, d, e = map(int, input().split()) if c==0: print(chr(a+b+d+e+95), end="") else: print(chr(a+b+c*10+d+e+96),end="") ```
instruction
0
220
5
440
No
output
1
220
5
441
Provide a correct Python 3 solution for this coding contest problem. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767
instruction
0
542
5
1,084
"Correct Solution: ``` class LazySegmentTree(): __slots__ = ["merge","merge_unit","operate","merge_operate","operate_unit","n","data","lazy"] def __init__(self,n,init,merge,merge_unit,operate,merge_operate,operate_unit): self.merge=merge self.merge_unit=merge_unit self.operate=operate self.merge_operate=merge_operate self.operate_unit=operate_unit self.n=(n-1).bit_length() self.data=[merge_unit for i in range(1<<(self.n+1))] self.lazy=[operate_unit for i in range(1<<(self.n+1))] if init: for i in range(n): self.data[2**self.n+i]=init[i] for i in range(2**self.n-1,0,-1): self.data[i]=self.merge(self.data[2*i],self.data[2*i+1]) def propagate(self,v): ope = self.lazy[v] if ope == self.operate_unit: return self.lazy[v]=self.operate_unit self.data[(v<<1)]=self.operate(self.data[(v<<1)],ope) self.data[(v<<1)+1]=self.operate(self.data[(v<<1)+1],ope) self.lazy[v<<1]=self.merge_operate(self.lazy[(v<<1)],ope) self.lazy[(v<<1)+1]=self.merge_operate(self.lazy[(v<<1)+1],ope) def propagate_above(self,i): m=i.bit_length()-1 for bit in range(m,0,-1): v=i>>bit self.propagate(v) def remerge_above(self,i): while i: c = self.merge(self.data[i],self.data[i^1]) i>>=1 self.data[i]=self.operate(c,self.lazy[i]) def update(self,l,r,x): l+=1<<self.n r+=1<<self.n l0=l//(l&-l) r0=r//(r&-r)-1 self.propagate_above(l0) self.propagate_above(r0) while l<r: if l&1: self.data[l]=self.operate(self.data[l],x) self.lazy[l]=self.merge_operate(self.lazy[l],x) l+=1 if r&1: self.data[r-1]=self.operate(self.data[r-1],x) self.lazy[r-1]=self.merge_operate(self.lazy[r-1],x) l>>=1 r>>=1 self.remerge_above(l0) self.remerge_above(r0) def query(self,l,r): l+=1<<self.n r+=1<<self.n l0=l//(l&-l) r0=r//(r&-r)-1 self.propagate_above(l0) self.propagate_above(r0) res=self.merge_unit while l<r: if l&1: res=self.merge(res,self.data[l]) l+=1 if r&1: res=self.merge(res,self.data[r-1]) l>>=1 r>>=1 return res>>32 import sys input = sys.stdin.buffer.readline mod = 998244353 mask = 2**32 - 1 def merge(x,y): s = ((x>>32) + (y>>32)) % mod num = (x&mask) + (y&mask) return (s<<32) + num merge_unit = 0 def operate(x,ope): s,num = x>>32,x&mask b,c = ope>>32,ope&mask s = (b*s + c*num) % mod return (s<<32)+num def merge_operate(x,y): b1,c1 = x>>32,x&mask b2,c2 = y>>32,y&mask return (((b1*b2)%mod)<<32)+((b2*c1+c2)%mod) operate_unit = 1<<32 N,Q = map(int,input().split()) a = list(map(int,input().split())) a = [(a[i]<<32)+1 for i in range(N)] LST = LazySegmentTree(N,a,merge,merge_unit,operate,merge_operate,operate_unit) for _ in range(Q): query = list(map(int,input().split())) if query[0] == 0: gomi,l,r,b,c = query LST.update(l,r,(b<<32)+c) else: gomi,l,r = query print(LST.query(l,r)) ```
output
1
542
5
1,085
Provide a correct Python 3 solution for this coding contest problem. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767
instruction
0
543
5
1,086
"Correct Solution: ``` class LazySegmentTree: def __init__(self, op_X, e_X, mapping, compose, id_M, N, array=None): #__slots__ = ["op_X","e_X","mapping","compose","id_M","N","log","N0","data","lazy"] # それぞれ Xの演算、単位元、f(x), f\circ g, Xの恒等変換 self.e_X = e_X; self.op_X = op_X; self.mapping = mapping; self.compose = compose; self.id_M = id_M self.N = N self.log = (N-1).bit_length() self.N0 = 1<<self.log self.data = [e_X]*(2*self.N0) self.lazy = [id_M]*self.N0 if array: assert N == len(array) self.data[self.N0:self.N0+self.N] = array for i in range(self.N0-1,0,-1): self.update(i) # 1点更新 def point_set(self, p, x): p += self.N0 for i in range(self.log, 0,-1): self.push(p>>i) self.data[p] = x for i in range(1, self.log + 1): self.update(p>>i) # 1点取得 def point_get(self, p): p += self.N0 for i in range(self.log, 0, -1): self.push(p>>i) return self.data[p] # 半開区間[L,R)をopでまとめる def prod(self, l, r): if l == r: return self.e_X l += self.N0 r += self.N0 for i in range(self.log, 0, -1): if (l>>i)<<i != l: self.push(l>>i) if (r>>i)<<i != r: self.push(r>>i) sml = smr = self.e_X while l < r: if l & 1: sml = self.op_X(sml, self.data[l]) l += 1 if r & 1: r -= 1 smr = self.op_X(self.data[r], smr) l >>= 1 r >>= 1 return self.op_X(sml, smr) # 全体をopでまとめる def all_prod(s): return self.data[1] # 1点作用 def apply(self, p, f): p += self.N0 for i in range(self.log, 0, -1): self.push(p>>i) self.data[p] = self.mapping(f, self.data[p]) for i in range(1, self.log + 1): self.update(p>>i) # 区間作用 def apply(self, l, r, f): if l == r: return l += self.N0 r += self.N0 for i in range(self.log, 0, -1): if (l>>i)<<i != l: self.push(l>>i) if (r>>i)<<i != r: self.push((r-1)>>i) l2, r2 = l, r while l < r: if l & 1: self.all_apply(l, f) l += 1 if r & 1: r -= 1 self.all_apply(r, f) l >>= 1 r >>= 1 l, r = l2, r2 for i in range(1, self.log + 1): if (l>>i)<<i != l: self.update(l>>i) if (r>>i)<<i != r: self.update((r-1)>>i) """ 始点 l を固定 f(x_l*...*x_{r-1}) が True になる最大の r つまり TTTTFFFF となるとき、F となる最小の添え字 存在しない場合 n が返る f(e_M) = True でないと壊れる """ def max_right(self, l, g): if l == self.N: return self.N l += self.N0 for i in range(self.log, 0, -1): self.push(l>>i) sm = self.e_X while True: while l&1 == 0: l >>= 1 if not g(self.op_X(sm, self.data[l])): while l < self.N0: self.push(l) l *= 2 if g(self.op_X(sm, self.data[l])): sm = self.op_X(sm, self.data[l]) l += 1 return l - self.N0 sm = self.op_X(sm, self.data[l]) l += 1 if l&-l == l: break return self.N """ 終点 r を固定 f(x_l*...*x_{r-1}) が True になる最小の l つまり FFFFTTTT となるとき、T となる最小の添え字 存在しない場合 r が返る f(e_M) = True でないと壊れる """ def min_left(self, r, g): if r == 0: return 0 r += self.N0 for i in range(self.log, 0, -1): self.push((r-1)>>i) sm = self.e_X while True: r -= 1 while r>1 and r&1: r >>= 1 if not g(self.op_X(self.data[r], sm)): while r < self.N0: self.push(r) r = 2*r + 1 if g(self.op_X(self.data[r], sm)): sm = self.op_X(self.data[r], sm) r -= 1 return r + 1 - self.N0 sm = self.op_X(self.data[r], sm) if r&-r == r: break return 0 # 以下内部関数 def update(self, k): self.data[k] = self.op_X(self.data[2*k], self.data[2*k+1]) def all_apply(self, k, f): self.data[k] = self.mapping(f, self.data[k]) if k < self.N0: self.lazy[k] = self.compose(f, self.lazy[k]) def push(self, k): #propagate と同じ if self.lazy[k] == self.id_M: return self.data[2*k ] = self.mapping(self.lazy[k], self.data[2*k]) self.data[2*k+1] = self.mapping(self.lazy[k], self.data[2*k+1]) if 2*k < self.N0: self.lazy[2*k] = self.compose(self.lazy[k], self.lazy[2*k]) self.lazy[2*k+1] = self.compose(self.lazy[k], self.lazy[2*k+1]) self.lazy[k] = self.id_M e_X = 0 id_M = 1<<32 def op_X(X,Y): return ((((X+Y)>>32)%MOD)<<32) + ((X+Y)&MASK) def compose(D,C): a = C>>32; b = C&MASK c = D>>32; d = D&MASK return (a*c%MOD<<32) + (c*b + d)%MOD def mapping(C,X): x = X>>32; v = X&MASK a = C>>32; b = C&MASK return ((x*a + v*b)%MOD<<32) + v import sys readline = sys.stdin.readline MOD = 998244353 MASK = (1<<32)-1 n,q = map(int, readline().split()) a = [(int(i)<<32)+1 for i in input().split()] seg = LazySegmentTree(op_X, e_X, mapping, compose, id_M, n, array=a) for _ in range(q): *V, = map(int, readline().split()) if V[0]==0: _,l,r,b,c = V seg.apply(l,r,(b<<32)+c) else: _,l,r = V print(seg.prod(l,r)>>32) ```
output
1
543
5
1,087
Provide a correct Python 3 solution for this coding contest problem. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767
instruction
0
544
5
1,088
"Correct Solution: ``` class LazySegmentTree(): def __init__(self, op, e, mapping, composition, ie, init_array): self.op = op self.e = e self.mapping = mapping self.composition = composition self.ie = ie l = len(init_array) def ceil_pow2(n): x = 0 while (1 << x) < n: x += 1 return x self.log = ceil_pow2(l) self.size = 1 << self.log # self.size = 1 << (l - 1).bit_length() # self.h = self.size.bit_length() self.d = [e() for _ in range(2*self.size)] self.lz = [ie() for _ in range(self.size)] for i, a in enumerate(init_array): self.d[i+self.size] = a for i in range(self.size-1, 0, -1): self.__update(i) def set(self, p, x): p += self.size for i in range(self.log, 0, -1): self.__push(p >> i) self.d[p] = x for i in range(1, self.log+1): self.__update(p >> i) def __getitem__(self, p): p += self.size for i in range(self.log, 0, -1): self.__push(p >> i) return self.d[p] # @profile def prod(self, l, r): if l == r: return self.e() l += self.size r += self.size for i in range(self.log, 0, -1): if ((l >> i) << i) != l: self.__push(l >> i) if ((r >> i) << i) != r: self.__push(r >> i) sml = self.e() smr = self.e() while l < r: if l & 1: sml = self.op(sml, self.d[l]) l += 1 if r & 1: r -= 1 smr = self.op(self.d[r], smr) l >>= 1 r >>= 1 return self.op(sml, smr) # def apply(self, p, f): # p += self.size # for i in range(self.h, 0, -1): # self.__push(p >> i) # self.d[p] = self.mapping(f, self.d[p]) # for i in range(1, self.h+1): # self.__update(p >> i) # @profile def apply(self, l, r, f): if l == r: return l += self.size r += self.size for i in range(self.log, 0, -1): if ((l >> i) << i) != l: self.__push(l >> i) if ((r >> i) << i) != r: self.__push((r-1) >> i) l2 = l r2 = r while l < r: if l & 1: self.__all_apply(l, f) l += 1 if r & 1: r -= 1 self.__all_apply(r, f) l >>= 1 r >>= 1 l = l2 r = r2 for i in range(1, self.log+1): if ((l >> i) << i) != l: self.__update(l >> i) if ((r >> i) << i) != r: self.__update((r-1) >> i) def __update(self, k): self.d[k] = self.op(self.d[2*k], self.d[2*k+1]) def __all_apply(self, k, f): self.d[k] = self.mapping(f, self.d[k]) if k < self.size: self.lz[k] = self.composition(f, self.lz[k]) def __push(self, k): self.__all_apply(2*k, self.lz[k]) self.__all_apply(2*k+1, self.lz[k]) self.lz[k] = self.ie() MOD = 998244353 def e(): return 0 def op(s, t): sv, sn = s >> 32, s % (1 << 32) tv, tn = t >> 32, t % (1 << 32) return (((sv + tv) % MOD) << 32) + sn + tn def mapping(f, a): fb, fc = f >> 32, f % (1 << 32) av, an = a >> 32, a % (1 << 32) return (((fb * av + fc * an) % MOD) << 32) + an def composition(f, g): fb, fc = f >> 32, f % (1 << 32) gb, gc = g >> 32, g % (1 << 32) return ((fb * gb % MOD) << 32) + ((gc * fb + fc) % MOD) def ie(): return 1 << 32 import sys input = sys.stdin.buffer.readline N, Q = map(int, input().split()) A = list(map(lambda x: (int(x) << 32) + 1, input().split())) # op = lambda l, r: ((l[0] + r[0])%M, (l[1] + r[1])%M) # e = lambda: (0, 0) # mapping = lambda l, r: ((r[0] * l[0] + r[1] * l[1])%M, r[1]) # composition = lambda l, r: ((r[0] * l[0])%M, (r[1] * l[0] + l[1])%M) # ie = lambda: (1, 0) st = LazySegmentTree(op, e, mapping, composition, ie, A) for _ in range(Q): k, *q = map(int, input().split()) if k == 0: l, r, b, c = q st.apply(l, r, (b << 32) + c) else: l, r = q a = st.prod(l, r) print(a >> 32) # # @profile # def atcoder_practice_k(): # if __name__ == "__main__": # atcoder_practice_k() ```
output
1
544
5
1,089
Provide a correct Python 3 solution for this coding contest problem. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767
instruction
0
545
5
1,090
"Correct Solution: ``` import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") ### 遅延評価セグメント木 class LazySegmentTree: def __init__(self, n, a=None): """初期化 num : n以上の最小の2のべき乗 """ num = 1 while num<=n: num *= 2 self.num = num self.seg = [ninf] * (2*self.num-1) self.lazy = [f0] * (2*self.num-1) self.ls = [0]*(2*self.num-1) self.rs = [0]*(2*self.num-1) self.ls[0] = 0 self.rs[0] = self.num for i in range(self.num-1): self.ls[2*i+1] = self.ls[i] self.rs[2*i+1] = (self.ls[i] + self.rs[i])//2 self.ls[2*i+2] = (self.ls[i] + self.rs[i])//2 self.rs[2*i+2] = self.rs[i] if a is not None: # O(n)で初期化 assert len(a)==n for i in range(n): self.seg[num-1+i] = a[i] for k in range(num-2, -1, -1): self.seg[k] = op(self.seg[2*k+1], self.seg[2*k+2]) def eval(self, k): if self.lazy[k]==f0: return if k<self.num-1: self.lazy[k*2+1] = composition(self.lazy[k], self.lazy[k*2+1]) self.lazy[k*2+2] = composition(self.lazy[k], self.lazy[k*2+2]) self.seg[k] = mapping(self.lazy[k], self.seg[k]) self.lazy[k] = f0 def eval_all(self): for i in range(2*self.num-1): self.eval(i) def update(self,a,b,x=None,f=None): """A[a]...A[b-1]をxに更新する """ if f is None: # 更新クエリ f = lambda y: x k = 0 q = [k] # k>=0なら行きがけ順 # 重なる区間を深さ優先探索 while q: k = q.pop() l,r = self.ls[k], self.rs[k] if k>=0: self.eval(k) if r<=a or b<=l: continue elif a<=l and r<=b: self.lazy[k] = composition(f, self.lazy[k]) self.eval(k) else: q.append(~k) q.append(2*k+1) q.append(2*k+2) else: k = ~k self.seg[k] = op(self.seg[2*k+1], self.seg[2*k+2]) def query(self,a,b): k = 0 l = 0 r = self.num q = [k] ans = ninf # 重なる区間を深さ優先探索 while q: k = q.pop() l,r = self.ls[k], self.rs[k] self.eval(k) if r<=a or b<=l: continue elif a<=l and r<=b: ans = op(ans, self.seg[k]) else: q.append(2*k+2) q.append(2*k+1) # print(q, ans, l,r,a,b, self.seg[k]) return ans n,q = list(map(int, input().split())) a = list(map(int, input().split())) # ninf = -10**9 # op = max # mapping = lambda f,x: f(x) # composition = lambda f1, f2: f1 if f1 is not None else f2 M = 998244353 ninf = 0 B = 32 f0 = 1<<B mask = (1<<B)-1 def op(x,y): x0,x1 = x>>B, x&mask y0,y1 = y>>B, y&mask return (((x0+y0)%M)<<B) + x1+y1 def mapping(f,x): x0,x1 = x>>B, x&mask f0,f1 = f>>B, f&mask return (((f0*x0 + f1*x1)%M)<<B) + x1 def composition(f,g): g0,g1 = g>>B, g&mask f0,f1 = f>>B, f&mask return (((f0*g0)%M)<<B) + (g1*f0 + f1)%M # op = lambda x,y: ((x[0]+y[0])%M, (x[1]+y[1])) # mapping = lambda f,x: ((f[0]*x[0] + f[1]*x[1])%M, x[1]) # composition = lambda f1, f2: ((f1[0]*f2[0])%M, (f2[1]*f1[0]+f1[1])%M) # f0 = (1,0) sg = LazySegmentTree(n, [((item<<B)+1) for item in a]) ans = [] for _ in range(q): t = tuple(map(int, input().split())) if t[0]==0: _, l,r,b,c = t sg.update(l,r,f=((b<<B)+c)) else: _,s,t = t ans.append(sg.query(s,t)>>B) write("\n".join(map(str, ans))) ```
output
1
545
5
1,091
Provide a correct Python 3 solution for this coding contest problem. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767
instruction
0
546
5
1,092
"Correct Solution: ``` class LazySegmentTree(): def __init__(self, op, e, mapping, composition, ie, init_array): self.op = op self.e = e self.mapping = mapping self.composition = composition self.ie = ie l = len(init_array) def ceil_pow2(n): x = 0 while (1 << x) < n: x += 1 return x self.log = ceil_pow2(l) self.size = 1 << self.log self.d = [e() for _ in range(2*self.size)] self.lz = [ie() for _ in range(self.size)] for i, a in enumerate(init_array): self.d[i+self.size] = a for i in range(self.size-1, 0, -1): self.__update(i) def set(self, p, x): p += self.size for i in range(self.log, 0, -1): self.__push(p >> i) self.d[p] = x for i in range(1, self.log+1): self.__update(p >> i) def __getitem__(self, p): p += self.size for i in range(self.log, 0, -1): self.__push(p >> i) return self.d[p] def prod(self, l, r): if l == r: return self.e() l += self.size r += self.size for i in range(self.log, 0, -1): if ((l >> i) << i) != l: self.__push(l >> i) if ((r >> i) << i) != r: self.__push(r >> i) sml = self.e() smr = self.e() while l < r: if l & 1: sml = self.op(sml, self.d[l]) l += 1 if r & 1: r -= 1 smr = self.op(self.d[r], smr) l >>= 1 r >>= 1 return self.op(sml, smr) def apply(self, l, r, f): if l == r: return l += self.size r += self.size for i in range(self.log, 0, -1): if ((l >> i) << i) != l: self.__push(l >> i) if ((r >> i) << i) != r: self.__push((r-1) >> i) l2 = l r2 = r while l < r: if l & 1: self.__all_apply(l, f) l += 1 if r & 1: r -= 1 self.__all_apply(r, f) l >>= 1 r >>= 1 l = l2 r = r2 for i in range(1, self.log+1): if ((l >> i) << i) != l: self.__update(l >> i) if ((r >> i) << i) != r: self.__update((r-1) >> i) def __update(self, k): self.d[k] = self.op(self.d[2*k], self.d[2*k+1]) def __all_apply(self, k, f): self.d[k] = self.mapping(f, self.d[k]) if k < self.size: self.lz[k] = self.composition(f, self.lz[k]) def __push(self, k): self.__all_apply(2*k, self.lz[k]) self.__all_apply(2*k+1, self.lz[k]) self.lz[k] = self.ie() MOD = 998244353 def e(): return 0 def op(s, t): sv, sn = s >> 32, s % (1 << 32) tv, tn = t >> 32, t % (1 << 32) return (((sv + tv) % MOD) << 32) + sn + tn def mapping(f, a): fb, fc = f >> 32, f % (1 << 32) av, an = a >> 32, a % (1 << 32) return (((fb * av + fc * an) % MOD) << 32) + an def composition(f, g): fb, fc = f >> 32, f % (1 << 32) gb, gc = g >> 32, g % (1 << 32) return ((fb * gb % MOD) << 32) + ((gc * fb + fc) % MOD) def ie(): return 1 << 32 def atcoder_practice_k(): import sys input = sys.stdin.buffer.readline N, Q = map(int, input().split()) A = list(map(lambda x: (int(x)<<32) +1, input().split())) st = LazySegmentTree(op, e, mapping, composition, ie, A) for _ in range(Q): k, *q = map(int, input().split()) if k == 0: l, r, b, c = q st.apply(l, r, (b<<32) + c) else: l, r = q a = st.prod(l, r) print(a >> 32) if __name__ == "__main__": atcoder_practice_k() ```
output
1
546
5
1,093
Provide a correct Python 3 solution for this coding contest problem. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767
instruction
0
547
5
1,094
"Correct Solution: ``` class LazySegmentTree(): def __init__(self, op, e, mapping, composition, ie, init_array): self.op = op self.e = e self.mapping = mapping self.composition = composition self.ie = ie l = len(init_array) def ceil_pow2(n): x = 0 while (1 << x) < n: x += 1 return x self.h = ceil_pow2(l) self.size = 1 << self.h # self.size = 1 << (l - 1).bit_length() # self.h = self.size.bit_length() # self.d = [self.e() for _ in range(2*self.size)] # self.lz = [self.ie() for _ in range(self.size)] self.d = [self.e()] * (2*self.size) self.lz = [self.ie()] * self.size for i, a in enumerate(init_array): self.d[i+self.size] = a for i in range(self.size-1, 0, -1): self.__update(i) def set(self, p, x): p += self.size for i in range(self.h, 0, -1): self.__push(p >> i) self.d[p] = x for i in range(1, self.h+1): self.__update(p >> i) def __getitem__(self, p): p += self.size for i in range(self.h, 0, -1): self.__push(p >> i) return self.d[p] def prod(self, l, r): if l == r: return self.e() l += self.size r += self.size for i in range(self.h, 0, -1): if ((l >> i) << i) != l: self.__push(l >> i) if ((r >> i) << i) != r: self.__push(r >> i) sml = self.e() smr = self.e() while l < r: if l & 1: sml = self.op(sml, self.d[l]) l += 1 if r & 1: r -= 1 smr = self.op(self.d[r], smr) l >>= 1 r >>= 1 return self.op(sml, smr) # def apply(self, p, f): # p += self.size # for i in range(self.h, 0, -1): # self.__push(p >> i) # self.d[p] = self.mapping(f, self.d[p]) # for i in range(1, self.h+1): # self.__update(p >> i) def apply(self, l, r, f): if l == r: return l += self.size r += self.size for i in range(self.h, 0, -1): if ((l >> i) << i) != l: self.__push(l >> i) if ((r >> i) << i) != r: self.__push((r-1) >> i) l2 = l r2 = r while l < r: if l & 1: self.__all_apply(l, f) l += 1 if r & 1: r -= 1 self.__all_apply(r, f) l >>= 1 r >>= 1 l = l2 r = r2 for i in range(1, self.h+1): if ((l >> i) << i) != l: self.__update(l >> i) if ((r >> i) << i) != r: self.__update((r-1) >> i) def __update(self, k): self.d[k] = self.op(self.d[2*k], self.d[2*k+1]) def __all_apply(self, k, f): self.d[k] = self.mapping(f, self.d[k]) if k < self.size: self.lz[k] = self.composition(f, self.lz[k]) def __push(self, k): self.__all_apply(2*k, self.lz[k]) self.__all_apply(2*k+1, self.lz[k]) self.lz[k] = self.ie() def atcoder_practice_k(): import sys input = sys.stdin.buffer.readline N, Q = map(int, input().split()) A = list(map(lambda x: (int(x)<<32) +1, input().split())) MOD = 998244353 def e(): return 0 def op(s, t): sv, sn = s >> 32, s % (1 << 32) tv, tn = t >> 32, t % (1 << 32) return (((sv + tv) % MOD) << 32) + sn + tn def mapping(f, a): fb, fc = f >> 32, f % (1 << 32) av, an = a >> 32, a % (1 << 32) return (((fb * av + fc * an) % MOD) << 32) + an def composition(f, g): fb, fc = f >> 32, f % (1 << 32) gb, gc = g >> 32, g % (1 << 32) return ((fb * gb % MOD) << 32) + ((gc * fb + fc) % MOD) def ie(): return 1 << 32 # op = lambda l, r: ((l[0] + r[0])%M, (l[1] + r[1])%M) # e = lambda: (0, 0) # mapping = lambda l, r: ((r[0] * l[0] + r[1] * l[1])%M, r[1]) # composition = lambda l, r: ((r[0] * l[0])%M, (r[1] * l[0] + l[1])%M) # ie = lambda: (1, 0) st = LazySegmentTree(op, e, mapping, composition, ie, A) for _ in range(Q): k, *q = map(int, input().split()) if k == 0: l, r, b, c = q st.apply(l, r, (b<<32) + c) else: l, r = q a = st.prod(l, r) print(a >> 32) if __name__ == "__main__": atcoder_practice_k() ```
output
1
547
5
1,095
Provide a correct Python 3 solution for this coding contest problem. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767
instruction
0
548
5
1,096
"Correct Solution: ``` import sys sys.setrecursionlimit(10**7) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり class LazySegTree(): # モノイドに対して適用可能、Nが2冪でなくても良い def __init__(self,N,X_func,A_func,operate,X_unit,A_unit): self.N = N self.X_func = X_func self.A_func = A_func self.operate = operate self.X_unit = X_unit self.A_unit = A_unit self.X = [self.X_unit]*(2*self.N) self.A = [self.A_unit]*(2*self.N) self.size = [0]*(2*self.N) def build(self,init_value): # 初期値を[N,2N)に格納 for i in range(self.N): self.X[self.N+i] = init_value[i] self.size[self.N+i] = 1 for i in range(self.N-1,0,-1): self.X[i] = self.X_func(self.X[i << 1],self.X[i << 1 | 1]) self.size[i] = self.size[i << 1] + self.size[i << 1 | 1] def update(self,i,x): # i番目(0-index)の値をxに変更 i += self.N self.X[i] = x i >>= 1 while i: self.X[i] = self.X_func(self.X[i << 1],self.X[i << 1 | 1]) i >>= 1 def eval_at(self,i): # i番目で作用を施した値を返す return self.operate(self.X[i],self.A[i],self.size[i]) def eval_above(self,i): # i番目より上の値を再計算する i >>= 1 while i: self.X[i] = self.X_func(self.eval_at(i << 1),self.eval_at(i << 1 | 1)) i >>= 1 def propagate_at(self,i): # i番目で作用を施し、1つ下に作用の情報を伝える self.X[i] = self.operate(self.X[i],self.A[i],self.size[i]) self.A[i << 1] = self.A_func(self.A[i << 1],self.A[i]) self.A[i << 1 | 1] = self.A_func(self.A[i << 1 | 1], self.A[i]) self.A[i] = self.A_unit def propagate_above(self,i): # i番目より上で作用を施す H = i.bit_length() for h in range(H,0,-1): self.propagate_at(i >> h) def fold(self,L,R): # [L,R)の区間取得 L += self.N R += self.N L0 = L // (L & -L) R0 = R // (R & -R) - 1 self.propagate_above(L0) self.propagate_above(R0) vL = self.X_unit vR = self.X_unit while L < R: if L & 1: vL = self.X_func(vL,self.eval_at(L)) L += 1 if R & 1: R -= 1 vR = self.X_func(self.eval_at(R),vR) L >>= 1 R >>= 1 return self.X_func(vL,vR) def operate_range(self,L,R,x): # [L,R)にxを作用 L += self.N R += self.N L0 = L // (L & -L) R0 = R // (R & -R) - 1 self.propagate_above(L0) self.propagate_above(R0) while L < R: if L & 1: self.A[L] = self.A_func(self.A[L],x) L += 1 if R & 1: R -= 1 self.A[R] = self.A_func(self.A[R],x) L >>= 1 R >>= 1 self.eval_above(L0) self.eval_above(R0) mod = 998244353 def X_func(x,y): return (x+y) % mod def A_func(a,b): b0,c0 = a b1,c1 = b return ((b0*b1) % mod,(b1*c0+c1) % mod) def operate(x,a,r): # 右作用 b,c = a return (b*x+c*r) % mod X_unit = 0 A_unit = (1,0) N,Q = MI() LST = LazySegTree(N,X_func,A_func,operate,X_unit,A_unit) A = LI() LST.build(A) for i in range(Q): X = LI() if X[0] == 0: l,r,b,c = X[1:] LST.operate_range(l,r,(b,c)) else: l,r = X[1:] print(LST.fold(l,r)) ```
output
1
548
5
1,097
Provide a correct Python 3 solution for this coding contest problem. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767
instruction
0
549
5
1,098
"Correct Solution: ``` mod = 998244353 eps = 10**-9 def main(): import sys input = sys.stdin.buffer.readline def op(a, b): return (a + b) % mod e = 0 mask = 2**32 - 1 def mapping(a, x, seg_len): b = x >> 32 c = x & mask return ((a * b)%mod + (c * seg_len)%mod)%mod def composition(x, y): b1 = x >> 32 c1 = x & mask b2 = y >> 32 c2 = y & mask return (((b1 * b2)%mod) << 32) + ((b2 * c1)%mod + c2)%mod id = 1 << 32 class LazySegTree: # Range update query def __init__(self, A, op=op, e=e, mapping=mapping, composition=composition, id=id, initialize=True): self.N = len(A) self.LV = (self.N - 1).bit_length() self.N0 = 1 << self.LV self.op = op self.e = e self.mapping = mapping self.composition = composition self.id = id if initialize: self.data = [self.e] * self.N0 + A + [self.e] * (self.N0 - self.N) for i in range(self.N0 - 1, 0, -1): self.data[i] = op(self.data[i * 2], self.data[i * 2 + 1]) else: self.data = [self.e] * (self.N0 * 2) self.lazy = [id] * (self.N0 * 2) def _ascend(self, i): for _ in range(i.bit_length() - 1): i >>= 1 self.data[i] = self.op(self.data[i * 2], self.data[i * 2 + 1]) def _descend(self, idx): lv = idx.bit_length() seg_len = 1 << self.LV for j in range(lv - 1, 0, -1): seg_len >>= 1 i = idx >> j x = self.lazy[i] if x == self.id: continue self.lazy[i * 2] = self.composition(self.lazy[i * 2], x) self.lazy[i * 2 + 1] = self.composition(self.lazy[i * 2 + 1], x) self.lazy[i] = self.id self.data[i * 2] = self.mapping(self.data[i * 2], x, seg_len) self.data[i * 2 + 1] = self.mapping(self.data[i * 2 + 1], x, seg_len) # open interval [l, r) def apply(self, l, r, x): l += self.N0 - 1 r += self.N0 - 1 self._descend(l // (l & -l)) self._descend(r // (r & -r) - 1) l_ori = l r_ori = r seg_len = 1 while l < r: if l & 1: self.data[l] = self.mapping(self.data[l], x, seg_len) self.lazy[l] = self.composition(self.lazy[l], x) l += 1 if r & 1: r -= 1 self.data[r] = self.mapping(self.data[r], x, seg_len) self.lazy[r] = self.composition(self.lazy[r], x) l >>= 1 r >>= 1 seg_len <<= 1 self._ascend(l_ori // (l_ori & -l_ori)) self._ascend(r_ori // (r_ori & -r_ori) - 1) # open interval [l, r) def query(self, l, r): l += self.N0 - 1 r += self.N0 - 1 self._descend(l // (l & -l)) self._descend(r // (r & -r) - 1) ret = self.e while l < r: if l & 1: ret = self.op(ret, self.data[l]) l += 1 if r & 1: ret = self.op(self.data[r - 1], ret) r -= 1 l >>= 1 r >>= 1 return ret N, Q = map(int, input().split()) A = list(map(int, input().split())) ST = LazySegTree(A) for _ in range(Q): q = list(map(int, input().split())) if q[0] == 0: l, r, b, c = q[1:] l += 1 r += 1 ST.apply(l, r, (b << 32) + c) else: l, r = q[1:] l += 1 r += 1 print(ST.query(l, r)) if __name__ == '__main__': main() ```
output
1
549
5
1,099
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767 Submitted Solution: ``` import sys readline = sys.stdin.readline class Lazysegtree: def __init__(self, A, fx, ex, fm, initialize = True): #作用の単位元をNoneにしている #fa(operator, idx)の形にしている、data[idx]の長さに依存する場合などのため self.N = len(A) self.N0 = 2**(self.N-1).bit_length() self.ex = ex self.fx = fx self.fm = fm self.lazy = [None]*(2*self.N0) if initialize: self.data = [self.ex]*self.N0 + A + [self.ex]*(self.N0 - self.N) for i in range(self.N0-1, -1, -1): self.data[i] = self.fx(self.data[2*i], self.data[2*i+1]) else: self.data = [self.ex]*(2*self.N0) def fa(self, ope, idx): """ TO WRITE """ k = idx.bit_length() return (ope[0]*self.data[idx] + (self.N0>>(k-1))*ope[1])%MOD def __repr__(self): s = 'data' l = 'lazy' cnt = 1 for i in range(self.N0.bit_length()): s = '\n'.join((s, ' '.join(map(str, self.data[cnt:cnt+(1<<i)])))) l = '\n'.join((l, ' '.join(map(str, self.lazy[cnt:cnt+(1<<i)])))) cnt += 1<<i return '\n'.join((s, l)) def _ascend(self, k): k = k >> 1 c = k.bit_length() for j in range(c): idx = k >> j self.data[idx] = self.fx(self.data[2*idx], self.data[2*idx+1]) def _descend(self, k): k = k >> 1 idx = 1 c = k.bit_length() for j in range(1, c+1): idx = k >> (c - j) if self.lazy[idx] is None: continue self.data[2*idx] = self.fa(self.lazy[idx], 2*idx) self.data[2*idx+1] = self.fa(self.lazy[idx], 2*idx+1) if self.lazy[2*idx] is not None: self.lazy[2*idx] = self.fm(self.lazy[idx], self.lazy[2*idx]) else: self.lazy[2*idx] = self.lazy[idx] if self.lazy[2*idx+1] is not None: self.lazy[2*idx+1] = self.fm(self.lazy[idx], self.lazy[2*idx+1]) else: self.lazy[2*idx+1] = self.lazy[idx] self.lazy[idx] = None def query(self, l, r): L = l+self.N0 R = r+self.N0 self._descend(L//(L & -L)) self._descend(R//(R & -R)-1) sl = self.ex sr = self.ex while L < R: if R & 1: R -= 1 sr = self.fx(self.data[R], sr) if L & 1: sl = self.fx(sl, self.data[L]) L += 1 L >>= 1 R >>= 1 return self.fx(sl, sr) def operate(self, l, r, x): L = l+self.N0 R = r+self.N0 Li = L//(L & -L) Ri = R//(R & -R) self._descend(Li) self._descend(Ri-1) while L < R : if R & 1: R -= 1 self.data[R] = self.fa(x, R) if self.lazy[R] is None: self.lazy[R] = x else: self.lazy[R] = self.fm(x, self.lazy[R]) if L & 1: self.data[L] = self.fa(x, L) if self.lazy[L] is None: self.lazy[L] = x else: self.lazy[L] = self.fm(x, self.lazy[L]) L += 1 L >>= 1 R >>= 1 self._ascend(Li) self._ascend(Ri-1) MOD = 998244353 def fx(x, y): return (x+y)%MOD def fm(o, p): return ((o[0]*p[0])%MOD, (o[0]*p[1] + o[1])%MOD) N, Q = map(int, readline().split()) A = list(map(int, readline().split())) T = Lazysegtree(A, fx, 0, fm, initialize = True) Ans = [] for _ in range(Q): t, *s = readline().split() if t == '0': l, r, b, c = map(int, s) T.operate(l, r, (b, c)) else: l, r = map(int, s) Ans.append(T.query(l, r)) print('\n'.join(map(str, Ans))) ```
instruction
0
550
5
1,100
Yes
output
1
550
5
1,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767 Submitted Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip() # LazySegmentTree # Thank "zkou" ! class LazySegmentTree: # f(X, X) -> X # g(X, M) -> X # h(M, M) -> def __init__(self, n, p, x_unit, m_unit, f, g, h): self.n = n self.seg = p*2 self.x_unit = x_unit self.m_unit = m_unit self.f = f self.g = g self.h = h for i in range(self.n-1, 0, -1): self.seg[i] = self.f(self.seg[i<<1], self.seg[(i<<1)+1]) self.lazy = [m_unit] * (self.n * 2) def update(self, l, r, x): l += self.n r += self.n ll = l // (l & -l) rr = r // (r & -r) - 1 for shift in range(ll.bit_length()-1, 0, -1): i = ll >> shift self.lazy[i << 1] = self.h(self.lazy[i << 1], self.lazy[i]) self.lazy[(i << 1)+1] = self.h(self.lazy[(i << 1)+1], self.lazy[i]) self.seg[i] = self.g(self.seg[i], self.lazy[i]) self.lazy[i] = self.m_unit for shift in range(rr.bit_length()-1, 0, -1): i = rr >> shift self.lazy[i << 1] = self.h(self.lazy[i << 1], self.lazy[i]) self.lazy[(i << 1)+1] = self.h(self.lazy[(i << 1)+1], self.lazy[i]) self.seg[i] = self.g(self.seg[i], self.lazy[i]) self.lazy[i] = self.m_unit while l < r: if l & 1: self.lazy[l] = self.h(self.lazy[l], x) l += 1 if r & 1: r -= 1 self.lazy[r] = self.h(self.lazy[r], x) l >>= 1 r >>= 1 while ll > 1: ll >>= 1 self.seg[ll] = self.f(self.g(self.seg[ll << 1], self.lazy[ll << 1]), self.g(self.seg[(ll << 1)+1], self.lazy[(ll << 1)+1])) self.lazy[ll] = self.m_unit while rr > 1: rr >>= 1 self.seg[rr] = self.f(self.g(self.seg[rr << 1], self.lazy[rr << 1]), self.g(self.seg[(rr << 1)+1], self.lazy[(rr << 1)+1])) self.lazy[rr] = self.m_unit def query(self, l, r): l += self.n r += self.n ll = l // (l & -l) rr = r // (r & -r) - 1 for shift in range(ll.bit_length()-1, 0, -1): i = ll >> shift self.lazy[i << 1] = self.h(self.lazy[i << 1], self.lazy[i]) self.lazy[(i << 1)+1] = self.h(self.lazy[(i << 1)+1], self.lazy[i]) self.seg[i] = self.g(self.seg[i], self.lazy[i]) self.lazy[i] = self.m_unit for shift in range(rr.bit_length()-1, 0, -1): i = rr >> shift self.lazy[i << 1] = self.h(self.lazy[i << 1], self.lazy[i]) self.lazy[(i << 1)+1] = self.h(self.lazy[(i << 1)+1], self.lazy[i]) self.seg[i] = self.g(self.seg[i], self.lazy[i]) self.lazy[i] = self.m_unit ans_l = ans_r = self.x_unit while l < r: if l & 1: ans_l = self.f(ans_l, self.g(self.seg[l], self.lazy[l])) l += 1 if r & 1: r -= 1 ans_r = self.f(self.g(self.seg[r], self.lazy[r]), ans_r) l >>= 1 r >>= 1 return self.f(ans_l, ans_r) mod=998244353 mask=(1<<32)-1 n,q=map(int,input().split()) a=[(i<<32)+1 for i in map(int,input().split())] def f(a,b): m=a+b return (((m>>32)%mod)<<32)+(m&mask) def g(a,x): a1=a>>32 a2=a&mask x1=x>>32 x2=x&mask return (((a1*x1+a2*x2)%mod)<<32)+a2 def h(x,y): x1=x>>32 x2=x&mask y1=y>>32 y2=y&mask a=x1*y1%mod b=(x2*y1+y2)%mod return (a<<32)+b seg=LazySegmentTree(n,a,0,(1<<32),f,g,h) for _ in range(q): f,*query=map(int,input().split()) if f==0: l,r,b,c=query seg.update(l,r,(b<<32)+c) else: l,r=query print(seg.query(l,r)>>32) ```
instruction
0
551
5
1,102
Yes
output
1
551
5
1,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767 Submitted Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip() # SegmentTree class SegmentTree: # f(X, X) -> X # g(X, M) -> X # h(M, M) -> def __init__(self, n, p, unit, f, g, h): num = 2**((n-1).bit_length()) seg = [unit]*(num*2) self.lazy = [None]*(num*2) for i in range(n): seg[num+i] = p[i] for i in range(num-1, 0, -1): seg[i] = f(seg[i << 1], seg[(i << 1)+1]) self.num = num self.seg = seg self.unit = unit self.flag = False self.f = f self.g = g self.h = h def gindex(self, l, r): l += self.num r += self.num lm = (l//(l & -l)) >> 1 rm = (r//(r & -r)) >> 1 mm = max(lm, rm) r -= 1 while l < r: if r <= rm: yield r if l <= lm: yield l l >>= 1 r >>= 1 while l: if l <= mm: yield l l >>= 1 def propagates(self, ids): num = self.num g = self.g h = self.h for i in reversed(ids): if self.lazy[i] is None: continue if (i << 1) < num: self.lazy[i << 1] = h(self.lazy[i << 1], self.lazy[i]) self.lazy[(i << 1)+1] = h(self.lazy[(i << 1)+1], self.lazy[i]) self.seg[i << 1] = g(self.seg[i << 1], self.lazy[i]) self.seg[(i << 1)+1] = g(self.seg[(i << 1)+1], self.lazy[i]) self.lazy[i] = None def query(self, l, r): f = self.f if self.flag: *ids, = self.gindex(l, r) self.propagates(ids) ansl = ansr = self.unit l += self.num r += self.num-1 if l == r: return self.seg[l] while l < r: if l & 1: ansl = f(ansl, self.seg[l]) l += 1 if r & 1 == 0: ansr = f(self.seg[r], ansr) r -= 1 l >>= 1 r >>= 1 if l == r: ansl = f(ansl, self.seg[l]) return f(ansl, ansr) def update1(self, i, x): if self.flag: *ids, = self.gindex(i, i+1) self.propagates(ids) i += self.num f = self.f self.seg[i] = x while i: i >>= 1 self.seg[i] = f(self.seg[i << 1], self.seg[(i << 1)+1]) def update2(self, l, r, x): self.flag = True *ids, = self.gindex(l, r) self.propagates(ids) num = self.num f = self.f g = self.g h = self.h l += num r += num-1 if l == r: self.seg[l] = g(self.seg[l], x) for i in ids: self.seg[i] = f(self.seg[i << 1], self.seg[(i << 1)+1]) return while l < r: if l & 1: if l < num: self.lazy[l] = h(self.lazy[l], x) self.seg[l] = g(self.seg[l], x) l += 1 if r & 1 == 0: if r < num: self.lazy[r] = h(self.lazy[r], x) self.seg[r] = g(self.seg[r], x) r -= 1 l >>= 1 r >>= 1 #x = f(x, x) if l == r: self.lazy[l] = h(self.lazy[l], x) self.seg[l] = g(self.seg[l], x) for i in ids: self.seg[i] = f(self.seg[i << 1], self.seg[(i << 1)+1]) def update(self, i, x): if type(i) is int: self.update1(i, x) else: self.update2(i[0], i[1], x) mod=998244353 d=10**9 n,q=map(int,input().split()) a=list(map(int,input().split())) aa=[d*i+1 for i in a] def f(a,b): a1,a2=divmod(a,d) b1,b2=divmod(b,d) c1=(a1+b1)%mod c2=(a2+b2)%mod return c1*d+c2 def g(a,x): a1,a2=divmod(a,d) x1,x2=divmod(x,d) c1=(a1*x1+a2*x2)%mod c2=a2 return c1*d+c2 def h(x,y): if x is None: return y x1,x2=divmod(x,d) y1,y2=divmod(y,d) z1=(x1*y1)%mod z2=(x2*y1+y2)%mod return z1*d+z2 seg=SegmentTree(n,aa,0,f,g,h) for _ in range(q): que=list(map(int,input().split())) if len(que)==5: _,l,r,b,c=que seg.update((l,r),b*d+c) else: _,l,r=que print(seg.query(l,r)//d) ```
instruction
0
552
5
1,104
Yes
output
1
552
5
1,105
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767 Submitted Solution: ``` #!usr/bin/env python3 from collections import defaultdict, deque, Counter, OrderedDict from bisect import bisect_left, bisect_right from functools import reduce, lru_cache from heapq import heappush, heappop, heapify import itertools import math, fractions import sys, copy def L(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline().rstrip()) def SL(): return list(sys.stdin.readline().rstrip()) def LI(): return list(map(int, sys.stdin.readline().split())) def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()] def LS(): return [list(x) for x in sys.stdin.readline().split()] def R(n): return [sys.stdin.readline().strip() for _ in range(n)] def LR(n): return [L() for _ in range(n)] def IR(n): return [I() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def LIR1(n): return [LI1() for _ in range(n)] def SR(n): return [SL() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] def perm(n, r): return math.factorial(n) // math.factorial(r) def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r)) def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)] dire = [[1, 0], [0, 1], [-1, 0], [0, -1]] dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]] alphabets = "abcdefghijklmnopqrstuvwxyz" ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" MOD = 998244353 INF = float("inf") sys.setrecursionlimit(1000000) # モノイドに対して適用可能、Nが2冪でなくても良い # class LazySegTree(): def __init__(self, initial_values, monoid_func, composition, operate, monoid_identity, operator_identity): self.N = len(initial_values) self.monoid_func = monoid_func self.composition = composition # composition(f, g) => g(f(x))の順であることに注意 self.operate = operate #右作用 operate(a, f) => f(a), 雑に可換な処理を書こうとするとバグるので注意 self.monoid_identity = monoid_identity self.operator_identity = operator_identity self.data = [self.monoid_identity]*(2*self.N) self.lazy = [self.operator_identity]*(2*self.N) self.size = [0]*(2*self.N) for i, ai in enumerate(initial_values): self.data[self.N+i] = ai self.size[self.N+i] = 1 for i in range(self.N-1,0,-1): self.data[i] = self.monoid_func(self.data[i << 1], self.data[i << 1 | 1]) self.size[i] = self.size[i << 1] + self.size[i << 1 | 1] def update(self,i,x): # i番目(0-index)の値をxに変更 i += self.N self.data[i] = x while i>1: i >>= 1 self.data[i] = self.monoid_func(self.data[i << 1], self.data[i << 1 | 1]) def eval_at(self,i): # i番目で作用を施した値を返す return self.operate(self.data[i],self.lazy[i],self.size[i]) def eval_above(self,i): # i番目より上の値を再計算する while i > 1: i >>= 1 self.data[i] = self.monoid_func(self.eval_at(i << 1),self.eval_at(i << 1 | 1)) def propagate_at(self,i): # i番目で作用を施し、1つ下に作用の情報を伝える self.data[i] = self.operate(self.data[i],self.lazy[i],self.size[i]) self.lazy[i << 1] = self.composition(self.lazy[i << 1],self.lazy[i]) self.lazy[i << 1 | 1] = self.composition(self.lazy[i << 1 | 1], self.lazy[i]) self.lazy[i] = self.operator_identity def propagate_above(self,i): # i番目より上で作用を施す H = i.bit_length() for h in range(H,0,-1): self.propagate_at(i >> h) def fold(self, L, R): # [L,R)の区間取得 L += self.N R += self.N L0 = L // (L & -L) R0 = R // (R & -R) - 1 self.propagate_above(L0) self.propagate_above(R0) vL = self.monoid_identity vR = self.monoid_identity while L < R: if L & 1: vL = self.monoid_func(vL,self.eval_at(L)) L += 1 if R & 1: R -= 1 vR = self.monoid_func(self.eval_at(R),vR) L >>= 1 R >>= 1 return self.monoid_func(vL,vR) #重たい def apply_range(self,L,R,x): # [L,R)にxを作用 L += self.N R += self.N L0 = L // (L & -L) R0 = R // (R & -R) - 1 self.propagate_above(L0) self.propagate_above(R0) while L < R: if L & 1: self.lazy[L] = self.composition(self.lazy[L], x) L += 1 if R & 1: R -= 1 self.lazy[R] = self.composition(self.lazy[R], x) L >>= 1 R >>= 1 self.eval_above(L0) self.eval_above(R0) # shift = 1 << 32 # mask = (1 << 32) - 1 def monoid_func(s, t): sv, sn = s >> 32, s % (1 << 32) tv, tn = t >> 32, t % (1 << 32) return (((sv + tv) % MOD) << 32) + sn + tn # g(f(x))の順であることに注意 def composition(f, g): fb, fc = f >> 32, f % (1 << 32) gb, gc = g >> 32, g % (1 << 32) return ((fb * gb % MOD) << 32) + ((gb * fc + gc) % MOD) #右作用 #雑に可換な処理を書こうとするとバグるので注意 def operate(a,f,_): fb, fc = f >> 32, f % (1 << 32) av, an = a >> 32, a % (1 << 32) return (((fb * av + fc * an) % MOD) << 32) + an def main(): monoid_identity = 0 operator_identity = 1 << 32 N, Q = LI() A = [(ai << 32) + 1 for ai in LI()] LST = LazySegTree(A, monoid_func, composition, operate, monoid_identity, operator_identity) for q in LIR(Q): if q[0] == 0: _, l, r, b, c = q LST.apply_range(l,r,(b<<32)+c) else: _, l, r = q print(LST.fold(l,r) >> 32) if __name__ == '__main__': main() ```
instruction
0
553
5
1,106
Yes
output
1
553
5
1,107
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767 Submitted Solution: ``` #!/usr/bin/env python3 import sys sys.setrecursionlimit(10**6) INF = 10 ** 9 + 1 # sys.maxsize # float("inf") MOD = 998244353 def set_depth(depth): global DEPTH, SEGTREE_SIZE, NONLEAF_SIZE DEPTH = depth SEGTREE_SIZE = 1 << DEPTH NONLEAF_SIZE = 1 << (DEPTH - 1) def set_width(width): set_depth((width - 1).bit_length() + 1) def get_size(pos): ret = pos.bit_length() return (1 << (DEPTH - ret)) def up(pos): pos += SEGTREE_SIZE // 2 return pos // (pos & -pos) def up_propagate(table, pos, binop): while pos > 1: pos >>= 1 table[pos] = binop( table[pos * 2], table[pos * 2 + 1] ) def full_up(table, binop): for i in range(NONLEAF_SIZE - 1, 0, -1): table[i] = binop( table[2 * i], table[2 * i + 1]) def force_down_propagate( action_table, value_table, pos, action_composite, action_force, action_unity ): max_level = pos.bit_length() - 1 size = NONLEAF_SIZE for level in range(max_level): size //= 2 i = pos >> (max_level - level) action = action_table[i] if action != action_unity: action_table[i * 2] = action_composite( action, action_table[i * 2]) action_table[i * 2 + 1] = action_composite( action, action_table[i * 2 + 1]) action_table[i] = action_unity value_table[i * 2] = action_force( action, value_table[i * 2], size) value_table[i * 2 + 1] = action_force( action, value_table[i * 2 + 1], size) def force_range_update( value_table, action_table, left, right, action, action_force, action_composite, action_unity ): """ action_force: action, value, cell_size => new_value action_composite: new_action, old_action => composite_action """ left += NONLEAF_SIZE right += NONLEAF_SIZE while left < right: if left & 1: value_table[left] = action_force( action, value_table[left], get_size(left)) action_table[left] = action_composite(action, action_table[left]) left += 1 if right & 1: right -= 1 value_table[right] = action_force( action, value_table[right], get_size(right)) action_table[right] = action_composite(action, action_table[right]) left //= 2 right //= 2 def range_reduce(table, left, right, binop, unity): ret_left = unity ret_right = unity left += NONLEAF_SIZE right += NONLEAF_SIZE while left < right: if left & 1: ret_left = binop(ret_left, table[left]) left += 1 if right & 1: right -= 1 ret_right = binop(table[right], ret_right) left //= 2 right //= 2 return binop(ret_left, ret_right) def lazy_range_update( action_table, value_table, start, end, action, action_composite, action_force, action_unity, value_binop): "update [start, end)" L = up(start) R = up(end) force_down_propagate( action_table, value_table, L, action_composite, action_force, action_unity) force_down_propagate( action_table, value_table, R, action_composite, action_force, action_unity) # print("action", file=sys.stderr) # debugprint(action_table) # print("value", file=sys.stderr) # debugprint(value_table) # print(file=sys.stderr) force_range_update( value_table, action_table, start, end, action, action_force, action_composite, action_unity) up_propagate(value_table, L, value_binop) up_propagate(value_table, R, value_binop) def lazy_range_reduce( action_table, value_table, start, end, action_composite, action_force, action_unity, value_binop, value_unity ): "reduce [start, end)" force_down_propagate( action_table, value_table, up(start), action_composite, action_force, action_unity) force_down_propagate( action_table, value_table, up(end), action_composite, action_force, action_unity) return range_reduce(value_table, start, end, value_binop, value_unity) def debug(*x): print(*x, file=sys.stderr) def main(): # parse input N, Q = map(int, input().split()) AS = list(map(int, input().split())) set_width(N + 1) value_unity = 0 value_table = [value_unity] * SEGTREE_SIZE value_table[NONLEAF_SIZE:NONLEAF_SIZE + len(AS)] = AS action_unity = None action_table = [action_unity] * SEGTREE_SIZE def action_force(action, value, size): if action == action_unity: return value b, c = action return (value * b + c * size) % MOD def action_composite(new_action, old_action): if new_action == action_unity: return old_action if old_action == action_unity: return new_action b1, c1 = old_action b2, c2 = new_action return (b1 * b2, b2 * c1 + c2) def value_binop(a, b): return (a + b) % MOD full_up(value_table, value_binop) for _q in range(Q): q, *args = map(int, input().split()) if q == 0: l, r, b, c = args lazy_range_update( action_table, value_table, l, r, (b, c), action_composite, action_force, action_unity, value_binop) else: l, r = args print(lazy_range_reduce( action_table, value_table, l, r, action_composite, action_force, action_unity, value_binop, value_unity)) T1 = """ 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 """ TEST_T1 = """ >>> as_input(T1) >>> main() 15 404 41511 4317767 """ def _test(): import doctest doctest.testmod() g = globals() for k in sorted(g): if k.startswith("TEST_"): doctest.run_docstring_examples(g[k], g, name=k) def as_input(s): "use in test, use given string as input file" import io f = io.StringIO(s.strip()) g = globals() g["input"] = lambda: bytes(f.readline(), "ascii") g["read"] = lambda: bytes(f.read(), "ascii") input = sys.stdin.buffer.readline read = sys.stdin.buffer.read if sys.argv[-1] == "-t": print("testing") _test() sys.exit() main() ```
instruction
0
554
5
1,108
No
output
1
554
5
1,109
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767 Submitted Solution: ``` class lazy_segment_tree: def __init__(self, N, operator_M, e_M, compose, funcval, ID_M=None): __slots__ = ["compose", "ID_M","funcval","op_M","e_M","height","N","N0","dat","laz"] self.compose = compose #作用素の合成(右から) self.ID_M = ID_M #恒等作用素 self.funcval = funcval #関数適用 self.op_M = operator_M # Mの演算 self.e_M = e_M # Mの単位元 #################################### self.height = (N-1).bit_length() #木の段数 self.N = N self.N0 = N0 = 1<<self.height #木の横幅 >= N self.dat = [self.e_M]*(2*N0) #データの木 self.laz = [self.ID_M]*(2*N0) #作用素の木 """ length = [N0>>1]*N0 for i in range(2,N0): length[i] = length[i>>1]>>1 self.length = length #区間の長さ """ # 長さNの配列 initial で dat を初期化 def build(self, initial): self.dat[self.N0:self.N0+len(initial)] = initial[:] for k in range(self.N0-1,0,-1): self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1]) # dat[k] に laz[k] を適用して、laz[k] を伝播 def propagate(self,k): if self.laz[k] == self.ID_M: return; self.dat[k] = self.funcval(self.dat[k],self.laz[k]) if self.N0 <= k: self.laz[k] = self.ID_M return else: self.laz[2*k ] = self.compose(self.laz[2*k ],self.laz[k]) self.laz[2*k+1] = self.compose(self.laz[2*k+1],self.laz[k]) self.laz[k] = self.ID_M return # 遅延をすべて解消する def propagate_all(self): for i in range(1,2*self.N0): self.propagate(i) # laz[k]およびその上に位置する作用素をすべて伝播 def thrust(self,k): for i in range(self.height,-1,-1): self.propagate(k>>i) # 2回連続で伝播するときに、ムダのないようにする def thrust2(self,L,R): for i in range(self.height,0,-1): self.propagate(L>>i) if (L>>i) != R>>i: self.propagate(R>>i) # 区間[l,r]に関数 f を作用 def update(self, l,r,f): l += self.N0; r += self.N0 #まず伝播させる #self.thrust(l>>1); self.thrust(r>>1) と同じ意味 self.thrust2(l,r) L = l; R = r+1 #[l.r]に対応する区間たちに関数 f を適用 while L < R: if R & 1: R -= 1 self.laz[R] = self.compose(self.laz[R], f) if L & 1: self.laz[L] = self.compose(self.laz[L], f) L += 1 L >>= 1; R >>= 1 # 再計算 #self.recalc_dat(l); self.recalc_dat(r) self.recalc_dat2(l,r) #def reflect(self,k): # if self.laz[k] == self.ID_M: return self.dat[k] # else: return self.funcval(self.dat[k],self.laz[k]) # dat[k] を更新したときに上部を再計算 def recalc_dat(self,k): k >>= 1 while k: self.dat[k] = self.op_M(self.funcval(self.dat[2*k],self.laz[2*k]),self.funcval(self.dat[2*k+1],self.laz[2*k+1])) #self.dat[k] = self.op_M(self.reflect(2*k),self.reflect(2*k+1))) k >>= 1 # 2回連続で再計算するときに、ムダのないようにする def recalc_dat2(self,l,r): l >>= 1; r >>= 1 while l: #self.dat[l] = self.op_M(self.reflect(2*l),self.reflect(2*l+1)) self.dat[l] = self.op_M(self.funcval(self.dat[2*l],self.laz[2*l]),self.funcval(self.dat[2*l+1],self.laz[2*l+1])) if l != r: #self.dat[r] = self.op_M(self.reflect(2*r),self.reflect(2*r+1)) self.dat[r] = self.op_M(self.funcval(self.dat[2*r],self.laz[2*r]),self.funcval(self.dat[2*r+1],self.laz[2*r+1])) l >>= 1; r >>= 1 # val[k] を取得。 def point_get(self, k): k += self.N0 res = self.dat[k] while k: if self.laz[k] != self.ID_M: res = self.funcval(res, self.laz[k]) k >>= 1 return res # values[k] = x 代入する def point_set(self, k,x): k += self.N0 self.thrust(k) self.dat[k] = x self.recalc_dat(k) # 区間[L,R]をopでまとめる def query(self, L,R): L += self.N0; R += self.N0 + 1 self.thrust2(L,R) #self.thrust(L); self.thrust(R-1) sl = sr = self.e_M while L < R: if R & 1: R -= 1 sr = self.op_M(self.funcval(self.dat[R],self.laz[R]),sr) if L & 1: sl = self.op_M(sl,self.funcval(self.dat[L],self.laz[L])) L += 1 L >>= 1; R >>= 1 return self.op_M(sl,sr) ########################################### import sys readline = sys.stdin.readline n,q = map(int, readline().split()) *a, = map(int, readline().split()) MOD = 998244353 operator_M = lambda P,Q: ((P[0]+Q[0])%MOD,P[1]+Q[1]) e_M = (0,0) compose = lambda C1,C2: (C1[0]*C2[0]%MOD, + (C2[0]*C1[1]+C2[1])%MOD) funcval = lambda Val,Coeff: ((Coeff[0]*Val[0] + Coeff[1]*Val[1])%MOD, Val[1]) ID_M = (1,0) seg = lazy_segment_tree(n,operator_M, e_M, compose, funcval, ID_M) seg.build([(ai,1) for ai in a]) for _ in range(q): *V, = map(int, readline().split()) if V[0]==0: _,l,r,b,c = V seg.update(l,r-1,(b,c)) else: _,l,r = V print(seg.query(l,r-1)[0]) if n==5 and q==7: print(1) ```
instruction
0
555
5
1,110
No
output
1
555
5
1,111
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767 Submitted Solution: ``` import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict mod = 998244353; INF = float("inf") sec = pow(2, mod - 2, mod) # 5 3 # 1 1 1 1 1 # 0 0 4 10 3 # 0 1 5 20 3 # 1 2 3 def getlist(): return list(map(int, input().split())) class lazySegTree(object): # N:処理する区間の長さ def __init__(self, N): self.N = N self.LV = (N - 1).bit_length() self.N0 = 2 ** self.LV self.initVal = 0 self.data = [0] * (2 * self.N0) self.lazybeta = [1] * (2 * self.N0) self.lazy = [0] * (2 * self.N0) def calc(self, a, b): return a + b def initialize(self, A): for i in range(self.N): self.data[self.N0 - 1 + i] = A[i] for i in range(self.N0 - 2, -1, -1): self.data[i] = self.calc(self.data[2 * i + 1], self.data[2 * i + 2]) def gindex(self, l, r): L = (l + self.N0) >> 1; R = (r + self.N0) >> 1 lc = 0 if l & 1 else (L & -L).bit_length() rc = 0 if r & 1 else (R & -R).bit_length() for i in range(self.LV): if rc <= i: yield R if L < R and lc <= i: yield L L >>= 1; R >>= 1 def propagates(self, *ids): for i in reversed(ids): w = self.lazybeta[i - 1] v = self.lazy[i - 1] if w == 1 and (not v): continue val = (v * sec) % mod self.lazybeta[2 * i - 1] = (self.lazybeta[2 * i - 1] * w) % mod self.lazybeta[2 * i] = (self.lazybeta[2 * i] * w) % mod self.lazy[2 * i - 1] = (self.lazy[2 * i - 1] * w + val) % mod self.lazy[2 * i] = (self.lazy[2 * i] * w + val) % mod self.data[2 * i - 1] = (self.data[2 * i - 1] * w + val) % mod self.data[2 * i] = (self.data[2 * i] * w + val) % mod self.lazybeta[i - 1] = 1; self.lazy[i - 1] = 0 def update(self, l, r, b, c): # 1行にまとめてよい? *ids, = self.gindex(l, r + 1) self.propagates(*ids) L = self.N0 + l; R = self.N0 + r + 1 v = c w = b while L < R: if R & 1: R -= 1 self.lazybeta[R - 1] *= w; self.lazy[R - 1] = (w * self.lazy[R - 1] + v) % mod self.data[R - 1] = (self.data[R - 1] * w + v) % mod if L & 1: self.lazybeta[L - 1] *= w; self.lazy[L - 1] = (w * self.lazy[L - 1] + v) % mod self.data[L - 1] = (self.data[L - 1] * w + v) % mod L += 1 L >>= 1; R >>= 1; v <<= 1 for i in ids: self.data[i - 1] = self.calc(self.data[2 * i - 1], self.data[2 * i]) def query(self, l, r): self.propagates(*self.gindex(l, r + 1)) L = self.N0 + l; R = self.N0 + r + 1 s = self.initVal while L < R: if R & 1: R -= 1 s = self.calc(s, self.data[R - 1]) if L & 1: s = self.calc(s, self.data[L - 1]) L += 1 L >>= 1; R >>= 1 return s def main(): N, Q = getlist() A = getlist() lSeg = lazySegTree(N) lSeg.initialize(A) for _ in range(Q): q = getlist() if q[0] == 0: null, l, r, b, c = q lSeg.update(l, r - 1, b, c) else: null, l, r = q res = lSeg.query(l, r - 1) print(res % mod) # print(lSeg.data) # print(lSeg.lazybeta) # print(lSeg.lazy) if __name__ == '__main__': main() ```
instruction
0
556
5
1,112
No
output
1
556
5
1,113
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types. * `0 l r b c`: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c. * `1 l r`: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353. Constraints * 1 \leq N, Q \leq 500000 * 0 \leq a_i, c < 998244353 * 1 \leq b < 998244353 * 0 \leq l < r \leq N * All values in Input are integer. Input Input is given from Standard Input in the following format: N Q a_0 a_1 ... a_{N - 1} \textrm{Query}_0 \textrm{Query}_1 : \textrm{Query}_{Q - 1} Output For each query of the latter type, print the answer. Example Input 5 7 1 2 3 4 5 1 0 5 0 2 4 100 101 1 0 3 0 1 3 102 103 1 2 5 0 2 5 104 105 1 0 5 Output 15 404 41511 4317767 Submitted Solution: ``` import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict mod = 998244353; INF = float("inf") sec = pow(2, mod - 2, mod) def getlist(): return list(map(int, input().split())) class lazySegTree(object): # N:処理する区間の長さ def __init__(self, N): self.N = N self.LV = (N - 1).bit_length() self.N0 = 2 ** self.LV self.data = [0] * (2 * self.N0) self.lazybeta = [1] * (2 * self.N0) self.lazy = [0] * (2 * self.N0) def initialize(self, A): for i in range(self.N): self.data[self.N0 - 1 + i] = A[i] for i in range(self.N0 - 2, -1, -1): self.data[i] = self.data[2 * i + 1] + self.data[2 * i + 2] def gindex(self, l, r): L = (l + self.N0) >> 1; R = (r + self.N0) >> 1 lc = 0 if l & 1 else (L & -L).bit_length() rc = 0 if r & 1 else (R & -R).bit_length() for i in range(self.LV): if rc <= i: yield R if L < R and lc <= i: yield L L >>= 1; R >>= 1 def propagates(self, *ids): for i in reversed(ids): w = self.lazybeta[i - 1] v = self.lazy[i - 1] if w == 1 and (not v): continue val = (v * sec) % mod self.lazybeta[2 * i - 1] = (self.lazybeta[2 * i - 1] * w) % mod self.lazybeta[2 * i] = (self.lazybeta[2 * i] * w) % mod self.lazy[2 * i - 1] = (self.lazy[2 * i - 1] * w + val) % mod self.lazy[2 * i] = (self.lazy[2 * i] * w + val) % mod self.data[2 * i - 1] = (self.data[2 * i - 1] * w + val) % mod self.data[2 * i] = (self.data[2 * i] * w + val) % mod self.lazybeta[i - 1] = 1; self.lazy[i - 1] = 0 def update(self, l, r, b, c): *ids, = self.gindex(l, r + 1) self.propagates(*ids) L = self.N0 + l; R = self.N0 + r + 1 v = c; w = b while L < R: if R & 1: R -= 1 self.lazybeta[R - 1] = (self.lazybeta[R - 1] * w) % mod self.lazy[R - 1] = (w * self.lazy[R - 1] + v) % mod self.data[R - 1] = (self.data[R - 1] * w + v) % mod if L & 1: self.lazybeta[L - 1] = (self.lazybeta[L - 1] * w) % mod self.lazy[L - 1] = (w * self.lazy[L - 1] + v) % mod self.data[L - 1] = (self.data[L - 1] * w + v) % mod L += 1 L >>= 1; R >>= 1; v <<= 1 for i in ids: self.data[i - 1] = (self.data[2 * i - 1] + self.data[2 * i]) % mod def query(self, l, r): self.propagates(*self.gindex(l, r + 1)) L = self.N0 + l; R = self.N0 + r + 1 s = 0 while L < R: if R & 1: R -= 1 s += self.data[R - 1] if L & 1: s += self.data[L - 1] L += 1 L >>= 1; R >>= 1 s %= mod return s def main(): N, Q = getlist() A = getlist() lSeg = lazySegTree(N) lSeg.initialize(A) for _ in range(Q): q = getlist() if q[0] == 0: null, l, r, b, c = q lSeg.update(l, r - 1, b, c) else: null, l, r = q res = lSeg.query(l, r - 1) print(res) if __name__ == '__main__': main() # 5 3 # 1 1 1 1 1 # 0 0 4 10 3 # 0 1 5 20 3 # 1 2 3 ```
instruction
0
557
5
1,114
No
output
1
557
5
1,115
Provide a correct Python 3 solution for this coding contest problem. There are S sheep and W wolves. If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep. If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Constraints * 1 \leq S \leq 100 * 1 \leq W \leq 100 Input Input is given from Standard Input in the following format: S W Output If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Examples Input 4 5 Output unsafe Input 100 2 Output safe Input 10 10 Output unsafe
instruction
0
558
5
1,116
"Correct Solution: ``` S,W = [int(i) for i in input().split()] print(["safe","unsafe"][W>=S]) ```
output
1
558
5
1,117
Provide a correct Python 3 solution for this coding contest problem. There are S sheep and W wolves. If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep. If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Constraints * 1 \leq S \leq 100 * 1 \leq W \leq 100 Input Input is given from Standard Input in the following format: S W Output If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Examples Input 4 5 Output unsafe Input 100 2 Output safe Input 10 10 Output unsafe
instruction
0
559
5
1,118
"Correct Solution: ``` s,w=map(int,input().split(' ')) print("unsafe") if w>=s else print("safe") ```
output
1
559
5
1,119
Provide a correct Python 3 solution for this coding contest problem. There are S sheep and W wolves. If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep. If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Constraints * 1 \leq S \leq 100 * 1 \leq W \leq 100 Input Input is given from Standard Input in the following format: S W Output If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Examples Input 4 5 Output unsafe Input 100 2 Output safe Input 10 10 Output unsafe
instruction
0
560
5
1,120
"Correct Solution: ``` s, w = map(int, input().split()) r = 'unsafe' if s <= w else 'safe' print(r) ```
output
1
560
5
1,121
Provide a correct Python 3 solution for this coding contest problem. There are S sheep and W wolves. If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep. If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Constraints * 1 \leq S \leq 100 * 1 \leq W \leq 100 Input Input is given from Standard Input in the following format: S W Output If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Examples Input 4 5 Output unsafe Input 100 2 Output safe Input 10 10 Output unsafe
instruction
0
561
5
1,122
"Correct Solution: ``` s,w=(map(int,input().split())) if s<=w: print("unsafe") else: print("safe") ```
output
1
561
5
1,123
Provide a correct Python 3 solution for this coding contest problem. There are S sheep and W wolves. If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep. If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Constraints * 1 \leq S \leq 100 * 1 \leq W \leq 100 Input Input is given from Standard Input in the following format: S W Output If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Examples Input 4 5 Output unsafe Input 100 2 Output safe Input 10 10 Output unsafe
instruction
0
562
5
1,124
"Correct Solution: ``` s,t = map(int,input().split()) if t >= s: print("unsafe") else: print("safe") ```
output
1
562
5
1,125
Provide a correct Python 3 solution for this coding contest problem. There are S sheep and W wolves. If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep. If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Constraints * 1 \leq S \leq 100 * 1 \leq W \leq 100 Input Input is given from Standard Input in the following format: S W Output If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Examples Input 4 5 Output unsafe Input 100 2 Output safe Input 10 10 Output unsafe
instruction
0
563
5
1,126
"Correct Solution: ``` s,w = map(int,input().split()) print('unsafe' if s<=w else 'safe') ```
output
1
563
5
1,127
Provide a correct Python 3 solution for this coding contest problem. There are S sheep and W wolves. If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep. If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Constraints * 1 \leq S \leq 100 * 1 \leq W \leq 100 Input Input is given from Standard Input in the following format: S W Output If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Examples Input 4 5 Output unsafe Input 100 2 Output safe Input 10 10 Output unsafe
instruction
0
564
5
1,128
"Correct Solution: ``` s, w = map(int, input().split()) print(['safe', 'unsafe'][w >= s]) ```
output
1
564
5
1,129
Provide a correct Python 3 solution for this coding contest problem. There are S sheep and W wolves. If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep. If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Constraints * 1 \leq S \leq 100 * 1 \leq W \leq 100 Input Input is given from Standard Input in the following format: S W Output If the wolves will attack the sheep, print `unsafe`; otherwise, print `safe`. Examples Input 4 5 Output unsafe Input 100 2 Output safe Input 10 10 Output unsafe
instruction
0
565
5
1,130
"Correct Solution: ``` s, w = map(int, input().split()) print('safe') if s > w else print('unsafe') ```
output
1
565
5
1,131
Provide a correct Python 3 solution for this coding contest problem. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: * Operation A: The displayed value is doubled. * Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. Constraints * 1 \leq N, K \leq 10 * All input values are integers. Input Input is given from Standard Input in the following format: N K Output Print the minimum possible value displayed in the board after N operations. Examples Input 4 3 Output 10 Input 10 10 Output 76
instruction
0
623
5
1,246
"Correct Solution: ``` N =int(input()) K =int(input()) a =1 for i in range(N): a += min(a,K) print(a) ```
output
1
623
5
1,247
Provide a correct Python 3 solution for this coding contest problem. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: * Operation A: The displayed value is doubled. * Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. Constraints * 1 \leq N, K \leq 10 * All input values are integers. Input Input is given from Standard Input in the following format: N K Output Print the minimum possible value displayed in the board after N operations. Examples Input 4 3 Output 10 Input 10 10 Output 76
instruction
0
624
5
1,248
"Correct Solution: ``` n=int(input()) k=int(input()) a=1 for i in range(n): a += min(a,k) print(a) ```
output
1
624
5
1,249
Provide a correct Python 3 solution for this coding contest problem. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: * Operation A: The displayed value is doubled. * Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. Constraints * 1 \leq N, K \leq 10 * All input values are integers. Input Input is given from Standard Input in the following format: N K Output Print the minimum possible value displayed in the board after N operations. Examples Input 4 3 Output 10 Input 10 10 Output 76
instruction
0
625
5
1,250
"Correct Solution: ``` n = int(input()) k = (int(input())) a = [(1<<x)+k*(n-x) for x in range(n+1)] print(min(a)) ```
output
1
625
5
1,251
Provide a correct Python 3 solution for this coding contest problem. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: * Operation A: The displayed value is doubled. * Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. Constraints * 1 \leq N, K \leq 10 * All input values are integers. Input Input is given from Standard Input in the following format: N K Output Print the minimum possible value displayed in the board after N operations. Examples Input 4 3 Output 10 Input 10 10 Output 76
instruction
0
626
5
1,252
"Correct Solution: ``` N=int(input()) K=int(input()) data=1 for i in range (N): data=min(data+K,data*2) print(data) ```
output
1
626
5
1,253
Provide a correct Python 3 solution for this coding contest problem. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: * Operation A: The displayed value is doubled. * Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. Constraints * 1 \leq N, K \leq 10 * All input values are integers. Input Input is given from Standard Input in the following format: N K Output Print the minimum possible value displayed in the board after N operations. Examples Input 4 3 Output 10 Input 10 10 Output 76
instruction
0
627
5
1,254
"Correct Solution: ``` n=int(input());k=int(input());r=1 for _ in[0]*n:r+=min(r,k) print(r) ```
output
1
627
5
1,255
Provide a correct Python 3 solution for this coding contest problem. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: * Operation A: The displayed value is doubled. * Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. Constraints * 1 \leq N, K \leq 10 * All input values are integers. Input Input is given from Standard Input in the following format: N K Output Print the minimum possible value displayed in the board after N operations. Examples Input 4 3 Output 10 Input 10 10 Output 76
instruction
0
628
5
1,256
"Correct Solution: ``` N = int(input()) K = int(input()) a = 1 for n in range(N): a+=min(a,K) print(a) ```
output
1
628
5
1,257
Provide a correct Python 3 solution for this coding contest problem. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: * Operation A: The displayed value is doubled. * Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. Constraints * 1 \leq N, K \leq 10 * All input values are integers. Input Input is given from Standard Input in the following format: N K Output Print the minimum possible value displayed in the board after N operations. Examples Input 4 3 Output 10 Input 10 10 Output 76
instruction
0
629
5
1,258
"Correct Solution: ``` N = int(input()) K = int(input()) tmp = 1 for _ in range(N): tmp = min(tmp*2,tmp+K) print(tmp) ```
output
1
629
5
1,259
Provide a correct Python 3 solution for this coding contest problem. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: * Operation A: The displayed value is doubled. * Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. Constraints * 1 \leq N, K \leq 10 * All input values are integers. Input Input is given from Standard Input in the following format: N K Output Print the minimum possible value displayed in the board after N operations. Examples Input 4 3 Output 10 Input 10 10 Output 76
instruction
0
630
5
1,260
"Correct Solution: ``` N = int(input()) K = int(input()) n = 1 for i in range(N): n = min(n * 2, n + K) print(n) ```
output
1
630
5
1,261
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: * Operation A: The displayed value is doubled. * Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. Constraints * 1 \leq N, K \leq 10 * All input values are integers. Input Input is given from Standard Input in the following format: N K Output Print the minimum possible value displayed in the board after N operations. Examples Input 4 3 Output 10 Input 10 10 Output 76 Submitted Solution: ``` N = int(input()) K = int(input()) ans = 1 for _ in range(N): ans += min(ans, K) print(ans) ```
instruction
0
631
5
1,262
Yes
output
1
631
5
1,263
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Square1001 has seen an electric bulletin board displaying the integer 1. He can perform the following operations A and B to change this value: * Operation A: The displayed value is doubled. * Operation B: The displayed value increases by K. Square1001 needs to perform these operations N times in total. Find the minimum possible value displayed in the board after N operations. Constraints * 1 \leq N, K \leq 10 * All input values are integers. Input Input is given from Standard Input in the following format: N K Output Print the minimum possible value displayed in the board after N operations. Examples Input 4 3 Output 10 Input 10 10 Output 76 Submitted Solution: ``` N=int(input()) K=int(input()) x=1 for _ in range(N): x=min(2*x,x+K) print(x) ```
instruction
0
632
5
1,264
Yes
output
1
632
5
1,265