message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn. Petya decided to introduce the sum operation on the set of permutations of length n. Let's assume that we are given two permutations of length n: a1, a2, ..., an and b1, b2, ..., bn. Petya calls the sum of permutations a and b such permutation c of length n, where ci = ((ai - 1 + bi - 1) mod n) + 1 (1 ≤ i ≤ n). Operation <image> means taking the remainder after dividing number x by number y. Obviously, not for all permutations a and b exists permutation c that is sum of a and b. That's why Petya got sad and asked you to do the following: given n, count the number of such pairs of permutations a and b of length n, that exists permutation c that is sum of a and b. The pair of permutations x, y (x ≠ y) and the pair of permutations y, x are considered distinct pairs. As the answer can be rather large, print the remainder after dividing it by 1000000007 (109 + 7). Input The single line contains integer n (1 ≤ n ≤ 16). Output In the single line print a single non-negative integer — the number of such pairs of permutations a and b, that exists permutation c that is sum of a and b, modulo 1000000007 (109 + 7). Examples Input 3 Output 18 Input 5 Output 1800 Submitted Solution: ``` n = int(input()) ans = [1, 3, 5, 13] dct = \ { 1 : 1, 3 : 18, 5 : 1800, 13 : 695720788 } if n in ans: print(dct[n]) else: print(0) ```
instruction
0
25,917
12
51,834
No
output
1
25,917
12
51,835
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn. Petya decided to introduce the sum operation on the set of permutations of length n. Let's assume that we are given two permutations of length n: a1, a2, ..., an and b1, b2, ..., bn. Petya calls the sum of permutations a and b such permutation c of length n, where ci = ((ai - 1 + bi - 1) mod n) + 1 (1 ≤ i ≤ n). Operation <image> means taking the remainder after dividing number x by number y. Obviously, not for all permutations a and b exists permutation c that is sum of a and b. That's why Petya got sad and asked you to do the following: given n, count the number of such pairs of permutations a and b of length n, that exists permutation c that is sum of a and b. The pair of permutations x, y (x ≠ y) and the pair of permutations y, x are considered distinct pairs. As the answer can be rather large, print the remainder after dividing it by 1000000007 (109 + 7). Input The single line contains integer n (1 ≤ n ≤ 16). Output In the single line print a single non-negative integer — the number of such pairs of permutations a and b, that exists permutation c that is sum of a and b, modulo 1000000007 (109 + 7). Examples Input 3 Output 18 Input 5 Output 1800 Submitted Solution: ``` n = int(input()) ans = [1, 3, 5, 13, 15] dct = \ { 1 : 1, 3 : 18, 5 : 1800, 13 : 695720788, 15 : 150347555 } if n in ans: print(dct[n]) else: print(0) ```
instruction
0
25,918
12
51,836
No
output
1
25,918
12
51,837
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn. Petya decided to introduce the sum operation on the set of permutations of length n. Let's assume that we are given two permutations of length n: a1, a2, ..., an and b1, b2, ..., bn. Petya calls the sum of permutations a and b such permutation c of length n, where ci = ((ai - 1 + bi - 1) mod n) + 1 (1 ≤ i ≤ n). Operation <image> means taking the remainder after dividing number x by number y. Obviously, not for all permutations a and b exists permutation c that is sum of a and b. That's why Petya got sad and asked you to do the following: given n, count the number of such pairs of permutations a and b of length n, that exists permutation c that is sum of a and b. The pair of permutations x, y (x ≠ y) and the pair of permutations y, x are considered distinct pairs. As the answer can be rather large, print the remainder after dividing it by 1000000007 (109 + 7). Input The single line contains integer n (1 ≤ n ≤ 16). Output In the single line print a single non-negative integer — the number of such pairs of permutations a and b, that exists permutation c that is sum of a and b, modulo 1000000007 (109 + 7). Examples Input 3 Output 18 Input 5 Output 1800 Submitted Solution: ``` n = int(input()) if n & 1: ans = 1 for i in range(1, n + 1): ans *= i if i & 1: ans *= i print(ans % 1000000007) else: print(0) ```
instruction
0
25,919
12
51,838
No
output
1
25,919
12
51,839
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3].
instruction
0
26,566
12
53,132
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) array = list(map(int, input().split())) print(n + 1) add = 0 for i in range(n-1, -1, -1): add = i - array[i] % n if add < 0: add += n print(1, i + 1, add) for j in range(i): array[j] += add add = 0 print(2, n, n) ```
output
1
26,566
12
53,133
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3].
instruction
0
26,567
12
53,134
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) l = list(map(int, input().split())) p = 1 t = 400 * 1000 mx = t + len(l) p = 0 #q = [i for i in l] print(n+1) for i in range(n-1, -1, -1): a = mx - ((p + l[i]) % t) #for j in range(i+1): # q[j] += a print(1, i+1, a) p += a mx -= 1 print(2, n, t) #q = [i%t for i in q] #print(q) ```
output
1
26,567
12
53,135
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3].
instruction
0
26,568
12
53,136
Tags: constructive algorithms, greedy, math Correct Solution: ``` #!/usr/bin/env python # coding: utf-8 # In[9]: import math # In[10]: n=int(input()) # In[11]: data=list(map(int, input().rstrip().split())) # In[12]: total=0 print(str(n+1)) for i in range(0,n): current=(data[n-i-1]+total) % n add=((n-i-1)-current)%n total=total+add print("1 "+str(n-i)+" "+str(add)) # In[14]: print("2 "+str(n)+" "+str(n)) # In[ ]: ```
output
1
26,568
12
53,137
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3].
instruction
0
26,569
12
53,138
Tags: constructive algorithms, greedy, math Correct Solution: ``` from sys import stdin,stdout from itertools import combinations from collections import defaultdict,OrderedDict import math import heapq def listIn(): return list((map(int,stdin.readline().strip().split()))) def stringListIn(): return([x for x in stdin.readline().split()]) def intIn(): return (int(stdin.readline())) def stringIn(): return (stdin.readline().strip()) if __name__=="__main__": n=intIn() a=listIn() a=list(map(lambda x:x+100*n+1,a)) print(n+1) print(1,n,100*n+1) mod_arr=[0]*n b=a.copy() for i in range(n): mod_arr[i]=a[i]-i print(2,i+1,mod_arr[i]) for j in range(i+1): b[j]=b[j]%mod_arr[i] #print(mod_arr) #print(b) ```
output
1
26,569
12
53,139
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3].
instruction
0
26,570
12
53,140
Tags: constructive algorithms, greedy, math Correct Solution: ``` x=int(input()) s=list(map(int,input().split())) print(x+1) print(1,x,500000) for n in range(x):print(2,n+1,s[n]+500000-n ) ```
output
1
26,570
12
53,141
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3].
instruction
0
26,571
12
53,142
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) a = [int(x) for x in input().split()] print(n + 1) print(1, n, 100000) for i in range(n): add = a[i] + 100000 print(2, i + 1, add - i - 1) ```
output
1
26,571
12
53,143
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3].
instruction
0
26,572
12
53,144
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) arr = [int(x) for x in input().split()] print(n+1) sum = 0 for i in reversed(range(n)): arr[i] += sum cur = ((((arr[i] - i) + (n - 1)) // n) * n) + (i-arr[i]) sum += cur print(1, i+1, cur) print(2, n, n) ```
output
1
26,572
12
53,145
Provide tags and a correct Python 3 solution for this coding contest problem. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3].
instruction
0
26,573
12
53,146
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) l = list(map(int,input().split())) print(n+1) print(1,n,n*4) for j in range(n): print(2,j+1,l[j]+4*n-j) ```
output
1
26,573
12
53,147
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3]. Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) print(n + 1) i = n j = n - 1 for x in reversed(a): inc = (x - j) % n print(1, i, n - inc) i -= 1 j = (j + inc - 1) % n print(2, n, n) ```
instruction
0
26,574
12
53,148
Yes
output
1
26,574
12
53,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3]. Submitted Solution: ``` from os import path import sys,time # mod = int(1e9 + 7) # import re from math import ceil, floor,gcd,log,log2 ,factorial from collections import defaultdict , Counter,deque from itertools import permutations # from bisect import bisect_left, bisect_right maxx = float('inf') #----------------------------INPUT FUNCTIONS------------------------------------------# I = lambda :int(sys.stdin.buffer.readline()) tup= lambda : map(int , sys.stdin.buffer.readline().split()) lint = lambda :[int(x) for x in sys.stdin.buffer.readline().split()] S = lambda: sys.stdin.readline().replace('\n', '').strip() def grid(r, c): return [lint() for i in range(r)] stpr = lambda x : sys.stdout.write(f'{x}' + '\n') star = lambda x: print(' '.join(map(str, x))) # input = sys.stdin.readline localsys = 0 start_time = time.time() if (path.exists('input.txt')): sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w'); #left shift --- num*(2**k) --(k - shift) n = I() ls = lint() print(n+1) print(2 , n , 1) print(1 , n , 10**6) for i in range(1 , n): print(2 , i , 1000000-i) if localsys: print("\n\nTime Elased :",time.time() - start_time,"seconds") ```
instruction
0
26,575
12
53,150
Yes
output
1
26,575
12
53,151
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3]. Submitted Solution: ``` n = int(input()) nums = list(map(int,input().split())) modn = len(nums) + 1 suma = 0 ans = [] for i in range(len(nums) - 1, -1, -1): summod = (nums[i] + suma) % modn diff = (i+1) - summod if (diff < 0): diff += modn if (diff > 0): suma += diff ans += [[1, i + 1, diff]] if (len(ans) > 0): print(len(ans) + 1) for i in ans: print(i[0], i[1], i[2]) print(2, n, modn) else: print(0) ```
instruction
0
26,576
12
53,152
Yes
output
1
26,576
12
53,153
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3]. Submitted Solution: ``` n=int(input()) print(n+1) s=list(map(int,input().split())) print("1 {} 500000".format(n)) for i in range(n): print("2 {0} {1}".format(i+1,s[i]+500000-i)) ```
instruction
0
26,577
12
53,154
Yes
output
1
26,577
12
53,155
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3]. Submitted Solution: ``` n = int(input()) array = list(map(int, input().split())) add = 0 for i in range(n-1, -1, -1): add = i - array[i] % n if add < 0: add += n print(1, i + 1, add) for j in range(i): array[j] += add add = 0 print(2, n, n) ```
instruction
0
26,578
12
53,156
No
output
1
26,578
12
53,157
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3]. Submitted Solution: ``` import sys def solve(io): N = io.read_int() A = io.read_int_array(N) io.println(N + 1) io.println(1, N, 1111111) for i, a in enumerate([ a + 1111111 for a in A ]): io.println(2, i + 1, a - i) class IO: in_stream = None out_stream = None raw = "" buf = [] pos = 0 def __init__(self, input_stream, output_stream): self.in_stream = input_stream self.out_stream = output_stream def read_to_buffer(self): self.raw = self.in_stream.readline().rstrip('\n') self.buf = self.raw.split() self.pos = 0 def read_string(self): while self.pos == len(self.buf): self.read_to_buffer() ans = self.buf[self.pos] self.pos += 1 return ans def read_int(self): return int(self.read_string()) def read_float(self): return float(self.read_string()) def read_string_array(self, N, offset=0): arr = [None] * offset for _ in range(0, N): arr.append(self.read_string()) return arr def read_int_array(self, N, offset=0): arr = [None] * offset for _ in range(0, N): arr.append(self.read_int()) return arr def read_float_array(self, N, offset=0): arr = [None] * offset for _ in range(0, N): arr.append(self.read_float()) return arr def read_line(self): while self.pos == len(self.buf): self.read_to_buffer() if self.pos > 0: raise ValueError("Cannot call read_line in the middle of a line.") self.pos = len(self.buf) return self.raw def print(self, *args): self.out_stream.write(' '.join([ str(x) for x in args ])) def println(self, *args): self.print(*args) self.print('\n') def println_array(self, arr, sep = ' '): self.println(sep.join(str(x) for x in arr)) def flush_output(self): self.out_stream.flush() pythonIO = IO(sys.stdin, sys.stdout) solve(pythonIO) pythonIO.flush_output() ```
instruction
0
26,579
12
53,158
No
output
1
26,579
12
53,159
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3]. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) if a==sorted(a): print(0) else: for i in range(n-1, -1 ,-1): print(*[1,i+1, a[i]*(10**5)+i]) print(*[2,n,10**5]) ```
instruction
0
26,580
12
53,160
No
output
1
26,580
12
53,161
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You're given an array a of length n. You can perform the following operations on it: * choose an index i (1 ≤ i ≤ n), an integer x (0 ≤ x ≤ 10^6), and replace a_j with a_j+x for all (1 ≤ j ≤ i), which means add x to all the elements in the prefix ending at i. * choose an index i (1 ≤ i ≤ n), an integer x (1 ≤ x ≤ 10^6), and replace a_j with a_j \% x for all (1 ≤ j ≤ i), which means replace every element in the prefix ending at i with the remainder after dividing it by x. Can you make the array strictly increasing in no more than n+1 operations? Input The first line contains an integer n (1 ≤ n ≤ 2000), the number of elements in the array a. The second line contains n space-separated integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^5), the elements of the array a. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "1 i x"; to print a modding operation, use the format "2 i x". If i or x don't satisfy the limitations above, or you use more than n+1 operations, you'll get wrong answer verdict. Examples Input 3 1 2 3 Output 0 Input 3 7 6 3 Output 2 1 1 1 2 2 4 Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes [8,6,3]. In the second step: the array becomes [0,2,3]. Submitted Solution: ``` n = int(input()) A = [int(x) for x in input().split(' ')] result = [] def add(i, x): result.append('1 {} {}'.format(i,x)) for j in range(i+1): A[j] += x; def mod(i, x): result.append('2 {} {}'.format(i,x)) for j in range(i+1): A[j] %= x; for i in reversed(range(n)): if A[i] % (n+1) > i + 1: add(i, (i + 1) + (n + 1) - A[i] % (n+1)) elif A[i] % (n+1) < i + 1: add(i, (i + 1) - A[i] % (n+1)) if len(result) > 0: mod(n-1, n + 1) print(len(result)) for s in result: print(s) ```
instruction
0
26,581
12
53,162
No
output
1
26,581
12
53,163
Provide tags and a correct Python 3 solution for this coding contest problem. Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task. Two matrices A and B are given, each of them has size n × m. Nastya can perform the following operation to matrix A unlimited number of times: * take any square square submatrix of A and transpose it (i.e. the element of the submatrix which was in the i-th row and j-th column of the submatrix will be in the j-th row and i-th column after transposing, and the transposed submatrix itself will keep its place in the matrix A). Nastya's task is to check whether it is possible to transform the matrix A to the matrix B. <image> Example of the operation As it may require a lot of operations, you are asked to answer this question for Nastya. A square submatrix of matrix M is a matrix which consist of all elements which comes from one of the rows with indeces x, x+1, ..., x+k-1 of matrix M and comes from one of the columns with indeces y, y+1, ..., y+k-1 of matrix M. k is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes). Input The first line contains two integers n and m separated by space (1 ≤ n, m ≤ 500) — the numbers of rows and columns in A and B respectively. Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix A (1 ≤ A_{ij} ≤ 10^{9}). Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix B (1 ≤ B_{ij} ≤ 10^{9}). Output Print "YES" (without quotes) if it is possible to transform A to B and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Examples Input 2 2 1 1 6 1 1 6 1 1 Output YES Input 2 2 4 4 4 5 5 4 4 4 Output NO Input 3 3 1 2 3 4 5 6 7 8 9 1 4 7 2 5 6 3 8 9 Output YES Note Consider the third example. The matrix A initially looks as follows. $$$ \begin{bmatrix} 1 & 2 & 3\\\ 4 & 5 & 6\\\ 7 & 8 & 9 \end{bmatrix} $$$ Then we choose the whole matrix as transposed submatrix and it becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ Then we transpose the submatrix with corners in cells (2, 2) and (3, 3). $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ So matrix becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 6\\\ 3 & 8 & 9 \end{bmatrix} $$$ and it is B.
instruction
0
26,598
12
53,196
Tags: constructive algorithms, sortings Correct Solution: ``` n, m = map(int, input().split()) a = [list(map(int, input().split())) for i in range(n)] b = [list(map(int, input().split())) for i in range(n)] a1 = [[] for i in range(n + m + 100)] b1 = [[] for i in range(n + m + 100)] for i in range(n): for j in range(m): a1[i + j].append(a[i][j]) b1[i + j].append(b[i][j]) for i in range(n + m + 5): a1[i].sort() b1[i].sort() if len(a1[i]) != len(b1[i]): print('NO') exit(0) for j in range(len(a1[i])): if a1[i][j] != b1[i][j]: print('NO') exit(0) print('YES') ```
output
1
26,598
12
53,197
Provide tags and a correct Python 3 solution for this coding contest problem. Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task. Two matrices A and B are given, each of them has size n × m. Nastya can perform the following operation to matrix A unlimited number of times: * take any square square submatrix of A and transpose it (i.e. the element of the submatrix which was in the i-th row and j-th column of the submatrix will be in the j-th row and i-th column after transposing, and the transposed submatrix itself will keep its place in the matrix A). Nastya's task is to check whether it is possible to transform the matrix A to the matrix B. <image> Example of the operation As it may require a lot of operations, you are asked to answer this question for Nastya. A square submatrix of matrix M is a matrix which consist of all elements which comes from one of the rows with indeces x, x+1, ..., x+k-1 of matrix M and comes from one of the columns with indeces y, y+1, ..., y+k-1 of matrix M. k is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes). Input The first line contains two integers n and m separated by space (1 ≤ n, m ≤ 500) — the numbers of rows and columns in A and B respectively. Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix A (1 ≤ A_{ij} ≤ 10^{9}). Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix B (1 ≤ B_{ij} ≤ 10^{9}). Output Print "YES" (without quotes) if it is possible to transform A to B and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Examples Input 2 2 1 1 6 1 1 6 1 1 Output YES Input 2 2 4 4 4 5 5 4 4 4 Output NO Input 3 3 1 2 3 4 5 6 7 8 9 1 4 7 2 5 6 3 8 9 Output YES Note Consider the third example. The matrix A initially looks as follows. $$$ \begin{bmatrix} 1 & 2 & 3\\\ 4 & 5 & 6\\\ 7 & 8 & 9 \end{bmatrix} $$$ Then we choose the whole matrix as transposed submatrix and it becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ Then we transpose the submatrix with corners in cells (2, 2) and (3, 3). $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ So matrix becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 6\\\ 3 & 8 & 9 \end{bmatrix} $$$ and it is B.
instruction
0
26,599
12
53,198
Tags: constructive algorithms, sortings Correct Solution: ``` EMPTY = 0 WHITE = 1 BLACK = 8 BOARDWIDTH = 7 BOARDHEIGHT = 6 def isWinner(board, tile): # check / diagonal spaces for x in range(BOARDWIDTH - 3): for y in range(3, BOARDHEIGHT): if board[x][y] == tile and board[x+1][y-1] == tile and board[x+2][y-2] == tile and board[x+3][y-3] == tile: return True def printDiagonal(num): # print(f'======= diagonal {d} =======') aret = [] bret = [] y = d if d < M else M - 1 x = 0 if d < M else d - M # ??? while x < N and y >= 0: # print(x, y) aret.append(A[x][y]) bret.append(B[x][y]) y -= 1 x += 1 aret.sort() bret.sort() if aret == bret: return True else: return False # The first line contains two integers n and m separated by space (1≤n,m≤500) — the numbers of rows and columns in A and B respectively. N, M = map(int, input().split()) # Each of the next n lines contains m integers, # the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix A (1≤Aij≤109). A = [] for n in range(N): A.append(list(map(int, input().split()))) # Each of the next n lines contains m integers, # the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix B (1≤Bij≤109). B = [] for n in range(N): B.append(list(map(int, input().split()))) for d in range(N+M): if not printDiagonal(d): print('NO') exit(0) print('YES') ```
output
1
26,599
12
53,199
Provide tags and a correct Python 3 solution for this coding contest problem. Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task. Two matrices A and B are given, each of them has size n × m. Nastya can perform the following operation to matrix A unlimited number of times: * take any square square submatrix of A and transpose it (i.e. the element of the submatrix which was in the i-th row and j-th column of the submatrix will be in the j-th row and i-th column after transposing, and the transposed submatrix itself will keep its place in the matrix A). Nastya's task is to check whether it is possible to transform the matrix A to the matrix B. <image> Example of the operation As it may require a lot of operations, you are asked to answer this question for Nastya. A square submatrix of matrix M is a matrix which consist of all elements which comes from one of the rows with indeces x, x+1, ..., x+k-1 of matrix M and comes from one of the columns with indeces y, y+1, ..., y+k-1 of matrix M. k is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes). Input The first line contains two integers n and m separated by space (1 ≤ n, m ≤ 500) — the numbers of rows and columns in A and B respectively. Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix A (1 ≤ A_{ij} ≤ 10^{9}). Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix B (1 ≤ B_{ij} ≤ 10^{9}). Output Print "YES" (without quotes) if it is possible to transform A to B and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Examples Input 2 2 1 1 6 1 1 6 1 1 Output YES Input 2 2 4 4 4 5 5 4 4 4 Output NO Input 3 3 1 2 3 4 5 6 7 8 9 1 4 7 2 5 6 3 8 9 Output YES Note Consider the third example. The matrix A initially looks as follows. $$$ \begin{bmatrix} 1 & 2 & 3\\\ 4 & 5 & 6\\\ 7 & 8 & 9 \end{bmatrix} $$$ Then we choose the whole matrix as transposed submatrix and it becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ Then we transpose the submatrix with corners in cells (2, 2) and (3, 3). $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ So matrix becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 6\\\ 3 & 8 & 9 \end{bmatrix} $$$ and it is B.
instruction
0
26,600
12
53,200
Tags: constructive algorithms, sortings Correct Solution: ``` n, m = map(int, input().split()) ma = [ [] for i in range(n + m)] mb = [[] for i in range(n + m)] w = [] for i in range(n): w = list(map(int, input().split())) for j in range(m): ma[i + j].append(w[j]) w = [] for i in range(n): w = list(map(int, input().split())) for j in range(m): mb[i + j].append(w[j]) for i in range(n + m): if sorted(ma[i]) != sorted(mb[i]): print("NO") exit(0) print('YES') ```
output
1
26,600
12
53,201
Provide tags and a correct Python 3 solution for this coding contest problem. Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task. Two matrices A and B are given, each of them has size n × m. Nastya can perform the following operation to matrix A unlimited number of times: * take any square square submatrix of A and transpose it (i.e. the element of the submatrix which was in the i-th row and j-th column of the submatrix will be in the j-th row and i-th column after transposing, and the transposed submatrix itself will keep its place in the matrix A). Nastya's task is to check whether it is possible to transform the matrix A to the matrix B. <image> Example of the operation As it may require a lot of operations, you are asked to answer this question for Nastya. A square submatrix of matrix M is a matrix which consist of all elements which comes from one of the rows with indeces x, x+1, ..., x+k-1 of matrix M and comes from one of the columns with indeces y, y+1, ..., y+k-1 of matrix M. k is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes). Input The first line contains two integers n and m separated by space (1 ≤ n, m ≤ 500) — the numbers of rows and columns in A and B respectively. Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix A (1 ≤ A_{ij} ≤ 10^{9}). Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix B (1 ≤ B_{ij} ≤ 10^{9}). Output Print "YES" (without quotes) if it is possible to transform A to B and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Examples Input 2 2 1 1 6 1 1 6 1 1 Output YES Input 2 2 4 4 4 5 5 4 4 4 Output NO Input 3 3 1 2 3 4 5 6 7 8 9 1 4 7 2 5 6 3 8 9 Output YES Note Consider the third example. The matrix A initially looks as follows. $$$ \begin{bmatrix} 1 & 2 & 3\\\ 4 & 5 & 6\\\ 7 & 8 & 9 \end{bmatrix} $$$ Then we choose the whole matrix as transposed submatrix and it becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ Then we transpose the submatrix with corners in cells (2, 2) and (3, 3). $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ So matrix becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 6\\\ 3 & 8 & 9 \end{bmatrix} $$$ and it is B.
instruction
0
26,601
12
53,202
Tags: constructive algorithms, sortings Correct Solution: ``` from sys import stdin,stdout n,m=map(int,stdin.readline().split()) mata=[None]*n matb=[None]*n for i in range(n): mata[i]= list(map(int,stdin.readline().split())) for i in range(n): matb[i] = list(map(int,stdin.readline().split())) la=[None]*(n+m-1) lb=[None]*(n+m-1) for i in range(n+m-1): la[i]=[] lb[i]=[] for i in range(n): for j in range(m): la[i+j].append(mata[i][j]) lb[i+j].append(matb[i][j]) flag=True for i in range(n+m-1): la[i].sort() lb[i].sort() if(la[i]!=lb[i]): flag=False if(flag): stdout.write("YES") else: stdout.write("NO") ```
output
1
26,601
12
53,203
Provide tags and a correct Python 3 solution for this coding contest problem. Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task. Two matrices A and B are given, each of them has size n × m. Nastya can perform the following operation to matrix A unlimited number of times: * take any square square submatrix of A and transpose it (i.e. the element of the submatrix which was in the i-th row and j-th column of the submatrix will be in the j-th row and i-th column after transposing, and the transposed submatrix itself will keep its place in the matrix A). Nastya's task is to check whether it is possible to transform the matrix A to the matrix B. <image> Example of the operation As it may require a lot of operations, you are asked to answer this question for Nastya. A square submatrix of matrix M is a matrix which consist of all elements which comes from one of the rows with indeces x, x+1, ..., x+k-1 of matrix M and comes from one of the columns with indeces y, y+1, ..., y+k-1 of matrix M. k is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes). Input The first line contains two integers n and m separated by space (1 ≤ n, m ≤ 500) — the numbers of rows and columns in A and B respectively. Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix A (1 ≤ A_{ij} ≤ 10^{9}). Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix B (1 ≤ B_{ij} ≤ 10^{9}). Output Print "YES" (without quotes) if it is possible to transform A to B and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Examples Input 2 2 1 1 6 1 1 6 1 1 Output YES Input 2 2 4 4 4 5 5 4 4 4 Output NO Input 3 3 1 2 3 4 5 6 7 8 9 1 4 7 2 5 6 3 8 9 Output YES Note Consider the third example. The matrix A initially looks as follows. $$$ \begin{bmatrix} 1 & 2 & 3\\\ 4 & 5 & 6\\\ 7 & 8 & 9 \end{bmatrix} $$$ Then we choose the whole matrix as transposed submatrix and it becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ Then we transpose the submatrix with corners in cells (2, 2) and (3, 3). $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ So matrix becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 6\\\ 3 & 8 & 9 \end{bmatrix} $$$ and it is B.
instruction
0
26,602
12
53,204
Tags: constructive algorithms, sortings Correct Solution: ``` n, m = map(int, input().split()) a = [[] for i in range(n + m)] b = [[] for i in range(n + m)] for i in range(n): j = 0 for el in map(int, input().split()): a[i + j].append(el) j += 1 for i in range(n): j = 0 for el in map(int, input().split()): b[i + j].append(el) j += 1 for i in range(n + m): a[i].sort() b[i].sort() if a[i] != b[i]: print('NO') exit(0) print('YES') ```
output
1
26,602
12
53,205
Provide tags and a correct Python 3 solution for this coding contest problem. Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task. Two matrices A and B are given, each of them has size n × m. Nastya can perform the following operation to matrix A unlimited number of times: * take any square square submatrix of A and transpose it (i.e. the element of the submatrix which was in the i-th row and j-th column of the submatrix will be in the j-th row and i-th column after transposing, and the transposed submatrix itself will keep its place in the matrix A). Nastya's task is to check whether it is possible to transform the matrix A to the matrix B. <image> Example of the operation As it may require a lot of operations, you are asked to answer this question for Nastya. A square submatrix of matrix M is a matrix which consist of all elements which comes from one of the rows with indeces x, x+1, ..., x+k-1 of matrix M and comes from one of the columns with indeces y, y+1, ..., y+k-1 of matrix M. k is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes). Input The first line contains two integers n and m separated by space (1 ≤ n, m ≤ 500) — the numbers of rows and columns in A and B respectively. Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix A (1 ≤ A_{ij} ≤ 10^{9}). Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix B (1 ≤ B_{ij} ≤ 10^{9}). Output Print "YES" (without quotes) if it is possible to transform A to B and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Examples Input 2 2 1 1 6 1 1 6 1 1 Output YES Input 2 2 4 4 4 5 5 4 4 4 Output NO Input 3 3 1 2 3 4 5 6 7 8 9 1 4 7 2 5 6 3 8 9 Output YES Note Consider the third example. The matrix A initially looks as follows. $$$ \begin{bmatrix} 1 & 2 & 3\\\ 4 & 5 & 6\\\ 7 & 8 & 9 \end{bmatrix} $$$ Then we choose the whole matrix as transposed submatrix and it becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ Then we transpose the submatrix with corners in cells (2, 2) and (3, 3). $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ So matrix becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 6\\\ 3 & 8 & 9 \end{bmatrix} $$$ and it is B.
instruction
0
26,603
12
53,206
Tags: constructive algorithms, sortings Correct Solution: ``` def problem_1136c(): gi = lambda: list(map(int, input().strip().split())) n, m = gi() a_list = [gi() for _ in range(n)] b_list = [gi() for _ in range(n)] for k in range(m): a = sorted([a_list[j][k - j] for j in range(min(n, k + 1))]) b = sorted([b_list[j][k - j] for j in range(min(n, k + 1))]) if a != b: print("NO") return for k in range(m): a = sorted([a_list[-j - 1][j - 1 - k] for j in range(min(n, k + 1))]) b = sorted([b_list[-j - 1][-k + j - 1] for j in range(min(n, k + 1))]) if a != b: print("NO") return if n <= m: print("YES") return for k in range(n): a = sorted([a_list[k - j][j] for j in range(min(m, k + 1))]) b = sorted([b_list[k - j][j] for j in range(min(m, k + 1))]) if a != b: print("NO") return for k in range(n): a = sorted([a_list[-k - 1 + j][-j - 1] for j in range(min(m, k + 1))]) b = sorted([b_list[-k - 1 + j][-j - 1] for j in range(min(m, k + 1))]) if a != b: print("NO") return print("YES") return if __name__ == '__main__': problem_1136c() ```
output
1
26,603
12
53,207
Provide tags and a correct Python 3 solution for this coding contest problem. Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task. Two matrices A and B are given, each of them has size n × m. Nastya can perform the following operation to matrix A unlimited number of times: * take any square square submatrix of A and transpose it (i.e. the element of the submatrix which was in the i-th row and j-th column of the submatrix will be in the j-th row and i-th column after transposing, and the transposed submatrix itself will keep its place in the matrix A). Nastya's task is to check whether it is possible to transform the matrix A to the matrix B. <image> Example of the operation As it may require a lot of operations, you are asked to answer this question for Nastya. A square submatrix of matrix M is a matrix which consist of all elements which comes from one of the rows with indeces x, x+1, ..., x+k-1 of matrix M and comes from one of the columns with indeces y, y+1, ..., y+k-1 of matrix M. k is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes). Input The first line contains two integers n and m separated by space (1 ≤ n, m ≤ 500) — the numbers of rows and columns in A and B respectively. Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix A (1 ≤ A_{ij} ≤ 10^{9}). Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix B (1 ≤ B_{ij} ≤ 10^{9}). Output Print "YES" (without quotes) if it is possible to transform A to B and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Examples Input 2 2 1 1 6 1 1 6 1 1 Output YES Input 2 2 4 4 4 5 5 4 4 4 Output NO Input 3 3 1 2 3 4 5 6 7 8 9 1 4 7 2 5 6 3 8 9 Output YES Note Consider the third example. The matrix A initially looks as follows. $$$ \begin{bmatrix} 1 & 2 & 3\\\ 4 & 5 & 6\\\ 7 & 8 & 9 \end{bmatrix} $$$ Then we choose the whole matrix as transposed submatrix and it becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ Then we transpose the submatrix with corners in cells (2, 2) and (3, 3). $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ So matrix becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 6\\\ 3 & 8 & 9 \end{bmatrix} $$$ and it is B.
instruction
0
26,604
12
53,208
Tags: constructive algorithms, sortings Correct Solution: ``` from collections import defaultdict n, m = list(map(int, input().split(' '))) A = [] B = [] for r in range(n): A.append(list(map(int, input().split(" ")))) for r in range(n): B.append(list(map(int, input().split(" ")))) def compare_maps(map_a, map_b): if len(map_a) != len(map_b): return False for k in map_a.keys(): if map_a[k] != map_b[k]: return False return True def findOut(): tmp_map_a = defaultdict(lambda: defaultdict(lambda: 0)) tmp_map_b = defaultdict(lambda: defaultdict(lambda: 0)) for k in range(n): for i in range(m): tmp_map_a[k + i][A[k][i]] += 1 tmp_map_b[k + i][B[k][i]] += 1 for k in tmp_map_a.keys(): if not compare_maps(tmp_map_a[k], tmp_map_b[k]): return "NO" return "YES" print(findOut()) ```
output
1
26,604
12
53,209
Provide tags and a correct Python 3 solution for this coding contest problem. Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task. Two matrices A and B are given, each of them has size n × m. Nastya can perform the following operation to matrix A unlimited number of times: * take any square square submatrix of A and transpose it (i.e. the element of the submatrix which was in the i-th row and j-th column of the submatrix will be in the j-th row and i-th column after transposing, and the transposed submatrix itself will keep its place in the matrix A). Nastya's task is to check whether it is possible to transform the matrix A to the matrix B. <image> Example of the operation As it may require a lot of operations, you are asked to answer this question for Nastya. A square submatrix of matrix M is a matrix which consist of all elements which comes from one of the rows with indeces x, x+1, ..., x+k-1 of matrix M and comes from one of the columns with indeces y, y+1, ..., y+k-1 of matrix M. k is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes). Input The first line contains two integers n and m separated by space (1 ≤ n, m ≤ 500) — the numbers of rows and columns in A and B respectively. Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix A (1 ≤ A_{ij} ≤ 10^{9}). Each of the next n lines contains m integers, the j-th number in the i-th of these lines denotes the j-th element of the i-th row of the matrix B (1 ≤ B_{ij} ≤ 10^{9}). Output Print "YES" (without quotes) if it is possible to transform A to B and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Examples Input 2 2 1 1 6 1 1 6 1 1 Output YES Input 2 2 4 4 4 5 5 4 4 4 Output NO Input 3 3 1 2 3 4 5 6 7 8 9 1 4 7 2 5 6 3 8 9 Output YES Note Consider the third example. The matrix A initially looks as follows. $$$ \begin{bmatrix} 1 & 2 & 3\\\ 4 & 5 & 6\\\ 7 & 8 & 9 \end{bmatrix} $$$ Then we choose the whole matrix as transposed submatrix and it becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ Then we transpose the submatrix with corners in cells (2, 2) and (3, 3). $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 8\\\ 3 & 6 & 9 \end{bmatrix} $$$ So matrix becomes $$$ \begin{bmatrix} 1 & 4 & 7\\\ 2 & 5 & 6\\\ 3 & 8 & 9 \end{bmatrix} $$$ and it is B.
instruction
0
26,605
12
53,210
Tags: constructive algorithms, sortings Correct Solution: ``` n, m = map(int, input().strip().split()) A = [] B = [] for _ in range(n): a = list(map(int, input().strip().split())) A.append(a) for _ in range(n): b = list(map(int, input().strip().split())) B.append(b) flag = True for i in range(n-1): if i == 0: if A[0][0] != B[0][0]: flag = False else: a = i b = 0 temp = {} temp2 = {} while a >=0 and b < m: ## print(a, b) try: temp[B[a][b]] += 1 except: temp[B[a][b]] = 1 try: temp2[A[a][b]] += 1 except: temp2[A[a][b]] = 1 a -= 1 b += 1 for k in temp2.keys(): try: if temp2[k] == temp[k]: nothing = 1 else: flag = False break except: flag = False break for j in range(m): a = n-1 b = j temp = {} temp2 = {} while a >=0 and b < m: ## print(a, b, 'asdf') try: temp[B[a][b]] += 1 except: temp[B[a][b]] = 1 try: temp2[A[a][b]] += 1 except: temp2[A[a][b]] = 1 a -= 1 b += 1 for k in temp2.keys(): try: if temp2[k] == temp[k]: nothing = 1 else: flag = False break except: flag = False break if flag: print ("YES") else: print ("NO") ```
output
1
26,605
12
53,211
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a consisting of n integers a_1, a_2, ..., a_n. Your problem is to find such pair of indices i, j (1 ≤ i < j ≤ n) that lcm(a_i, a_j) is minimum possible. lcm(x, y) is the least common multiple of x and y (minimum positive number such that both x and y are divisors of this number). Input The first line of the input contains one integer n (2 ≤ n ≤ 10^6) — the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^7), where a_i is the i-th element of a. Output Print two integers i and j (1 ≤ i < j ≤ n) such that the value of lcm(a_i, a_j) is minimum among all valid pairs i, j. If there are multiple answers, you can print any. Examples Input 5 2 4 8 3 6 Output 1 2 Input 5 5 2 11 3 7 Output 2 4 Input 6 2 5 10 1 10 2 Output 1 4
instruction
0
26,616
12
53,232
Tags: brute force, greedy, math, number theory Correct Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- """Codeforces Round #552 (Div. 3) Problem G. Minimum Possible LCM :author: Kitchen Tong :mail: kctong529@gmail.com Please feel free to contact me if you have any question regarding the implementation below. """ __version__ = '3.4' __date__ = '2019-04-17' import sys def find_minimum_lcm(n, integers): already_seen = [0] * (10000001) repeated_value = 10000001 * 10000001 repeated_indices = None for index, value in enumerate(integers): if already_seen[value]: if value < repeated_value: repeated_value = value repeated_indices = (index, already_seen[value]-1) else: already_seen[value] = index + 1 # the smallest repeated integers is our baseline answer_value = repeated_value answer_indices = repeated_indices sorted_integers = sorted(integers) min_1, min_2 = sorted_integers[:2] if min_1 == min_2: return sorted(repeated_indices) else: factor_lcm = min_1 * min_2 if factor_lcm < answer_value: answer_value = factor_lcm answer_indices = (already_seen[min_1]-1, already_seen[min_2]-1) for factor in range(2, 10000001): if factor > answer_value: break seen_once = -1 # have a larger start, so we won't waste time on # non-existing small multiples for multiple in range(max(1, min_1 // factor) * factor, 10000001, factor): if already_seen[multiple]: if seen_once > 0: factor_lcm = seen_once * multiple // factor if factor_lcm < answer_value: answer_value = factor_lcm answer_indices = (already_seen[seen_once] - 1, already_seen[multiple] - 1) break else: seen_once = multiple return sorted(answer_indices) def main(argv=None): n = int(input()) integers = list(map(int, input().split())) i, j = find_minimum_lcm(n, integers) print('{0} {1}'.format(i+1, j+1)) return 0 if __name__ == "__main__": STATUS = main() sys.exit(STATUS) ```
output
1
26,616
12
53,233
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}.
instruction
0
26,702
12
53,404
Tags: binary search, bitmasks, data structures Correct Solution: ``` class BIT(): def __init__(self,n): self.BIT=[0]*(n+1) self.num=n def query(self,idx): res_sum = 0 while idx > 0: res_sum += self.BIT[idx] idx -= idx&(-idx) return res_sum #Ai += x O(logN) def update(self,idx,x): while idx <= self.num: self.BIT[idx] += x idx += idx&(-idx) return n,q=map(int,input().split()) a=list(map(int,input().split())) bit=BIT(2**n) for i in range(2**n): bit.update(i+1,a[i]) b=0 def Sum(r,xor): id=xor res=0 if r==-1: return res for i in range(n,-1,-1): if r>>i &1: L=(id>>i)<<i R=L+(1<<i)-1 res+=bit.query(R+1)-bit.query(L) id^=(1<<i) return res for _ in range(q): query=tuple(map(int,input().split())) if query[0]==1: g,x,k=query x-=1 x^=b bit.update(x+1,k-a[x]) a[x]=k elif query[0]==2: k=query[1] b^=2**k-1 elif query[0]==3: k=query[1] if k!=n: b^=2**k else: gl,l,r=query l-=1 test=Sum(r,b)-Sum(l,b) print(test) ```
output
1
26,702
12
53,405
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}.
instruction
0
26,703
12
53,406
Tags: binary search, bitmasks, data structures Correct Solution: ``` from sys import stdin REPLACE_ID = 1 REVERSE_ID = 2 SWAP_ID = 3 SUM_ID = 4 def main(): n, q = _read_ints() a = tuple(_read_ints()) requests = tuple(_read_ints() for _ in range(q)) for result in compute_requests_results(a, requests): print(result) def _read_ints(): return map(int, stdin.readline().split(" ")) def compute_requests_results(initial_array, requests): tree = _Tree(initial_array) for request_id, *request_args in requests: if request_id == REPLACE_ID: index, new_value = request_args tree[index-1] = new_value elif request_id == REVERSE_ID: k, = request_args tree.reverse(k) elif request_id == SWAP_ID: k, = request_args tree.swap(k) else: assert request_id == SUM_ID l, r = request_args yield tree.find_sum(l-1, r-1) class _Tree: def __init__(self, array): array = list(array) self._levels = [array] self._levels_sizes = [len(array)] self._nodes_sizes = [1] while self._levels_sizes[-1] > 1: last_size = self._levels_sizes[-1] last_level = self._levels[-1] last_node_size = self._nodes_sizes[-1] new_size = last_size // 2 new_level = [last_level[2*i] + last_level[2*i+1] for i in range(new_size)] new_node_size = last_node_size * 2 self._levels_sizes.append(new_size) self._levels.append(new_level) self._nodes_sizes.append(new_node_size) self._levels = tuple(self._levels) self._is_swapped = [False for level in self._levels] def __getitem__(self, index): i_curr_level = len(self._levels) - 1 curr_node_index = 0 while i_curr_level > 0: i_new_level = i_curr_level - 1 new_node_size = self._nodes_sizes[i_new_level] is_right = 1 if index >= new_node_size else 0 is_right = is_right if not self._is_swapped[i_curr_level] else 1 - is_right new_node_index = curr_node_index * 2 + is_right i_curr_level = i_new_level index %= new_node_size curr_node_index = new_node_index return self._levels[i_curr_level][curr_node_index] def __setitem__(self, index, value): old_value = self[index] delta = value - old_value i_curr_level = len(self._levels) - 1 curr_node_index = 0 while i_curr_level > 0: self._levels[i_curr_level][curr_node_index] += delta i_new_level = i_curr_level - 1 new_node_size = self._nodes_sizes[i_new_level] is_right = 1 if index >= new_node_size else 0 is_right = is_right if not self._is_swapped[i_curr_level] else 1 - is_right new_node_index = curr_node_index * 2 + is_right i_curr_level = i_new_level index %= new_node_size curr_node_index = new_node_index self._levels[i_curr_level][curr_node_index] += delta def find_sum(self, l, r): return self._find_sum_before(r+1) - self._find_sum_before(l) def _find_sum_before(self, index): if index == len(self._levels[0]): return self._levels[-1][0] result_sum = 0 i_curr_level = len(self._levels) - 1 curr_node_index = 0 while i_curr_level > 0: i_new_level = i_curr_level - 1 new_node_size = self._nodes_sizes[i_new_level] is_right = 1 if index >= new_node_size else 0 is_swapped = self._is_swapped[i_curr_level] new_node_index = curr_node_index * 2 + (is_right if not is_swapped else 1 - is_right) denied_node_index = curr_node_index * 2 + (is_right if is_swapped else 1 - is_right) if is_right: result_sum += self._levels[i_new_level][denied_node_index] i_curr_level = i_new_level index %= new_node_size curr_node_index = new_node_index return result_sum def swap(self, k): self._is_swapped[k+1] = not self._is_swapped[k+1] def reverse(self, k): for i in range(k): self.swap(i) if __name__ == '__main__': main() ```
output
1
26,703
12
53,407
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}.
instruction
0
26,704
12
53,408
Tags: binary search, bitmasks, data structures Correct Solution: ``` import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def init(): for u in range(m-1,0,-1):tree[u]=tree[u*2]+tree[u*2+1] def update(i,a): u=(i^rev)+m tree[u]=a while u>1: u>>=1 tree[u] = tree[u * 2] + tree[u * 2 + 1] def sumlr(l,r): stack=[(0,m,1,n-1)] res=0 while stack: sl,sr,u,f=stack.pop() if r<=sl or sr<=l:continue elif l<=sl and sr<=r:res+=tree[u] else: sm=(sl+sr)//2 stack.append((sl,sm,u*2+(rev>>f&1),f-1)) stack.append((sm,sr,u*2+1-(rev>>f&1),f-1)) #print(res,stack) return res n,q=MI() m=2**n tree=[0]*m+LI() rev=0 init() #print(tree) for _ in range(q): t=LI() if t[0]==1: x,k=t[1:3] update(x-1,k) elif t[0] == 2: k = t[1] rev ^= (1 << k) - 1 elif t[0]==3: k=t[1] rev^=1<<k else: l,r=t[1:3] print(sumlr(l-1,r)) #print(tree,rev) ```
output
1
26,704
12
53,409
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}.
instruction
0
26,705
12
53,410
Tags: binary search, bitmasks, data structures Correct Solution: ``` import os,io input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n,q = map(int,input().split()) SegmentTree = [] for i in range(n + 1): SegmentTree.append([0] * (1 << (n - i))) a = list(map(int,input().split())) for i in range(1 << n): for j in range(n + 1): SegmentTree[j][i >> j] += a[i] QueryList = [0] * n def FindSum(m): summ = 0 for i in range(n + 1): if m & (1 << i): m -= 1 << i mCpy = m for j in range(i,n): if QueryList[j]: mCpy ^= ((1 << (j+1)) - (1 << i)) summ += SegmentTree[i][mCpy >> i] return summ for _ in range(q): t = list(map(int,input().split())) if t[0] == 1: loc = t[1] - 1 for i in range(n): if QueryList[i]:loc ^= ((1 << (i + 1)) - 1) increment = t[2] - SegmentTree[0][loc] for j in range(n + 1):SegmentTree[j][loc >> j] += increment if t[0] == 2: if t[1] == 0:continue QueryList[t[1] - 1] = 1 - QueryList[t[1] - 1] if t[0] == 3: QueryList[t[1]] = 1 - QueryList[t[1]] if t[1] == 0:continue QueryList[t[1] - 1] = 1 - QueryList[t[1] - 1] if t[0] == 4:print(FindSum(t[2]) - FindSum(t[1] - 1)) ```
output
1
26,705
12
53,411
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}.
instruction
0
26,706
12
53,412
Tags: binary search, bitmasks, data structures Correct Solution: ``` import os,io;input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline;n,q = map(int,input().split());QueryList = [0] * n;SegmentTree = [];a = list(map(int,input().split())) for i in range(n + 1):SegmentTree.append([0] * (1 << (n - i))) for i in range(1 << n): for j in range(n + 1):SegmentTree[j][i >> j] += a[i] def FindSum(m): summ = 0 for i in range(n + 1): if m & (1 << i): m -= 1 << i;mCpy = m for j in range(i,n): if QueryList[j]:mCpy ^= ((1 << (j+1)) - (1 << i)) summ += SegmentTree[i][mCpy >> i] return summ for _ in range(q): t = list(map(int,input().split())) if t[0] == 1: loc = t[1] - 1 for i in range(n): if QueryList[i]:loc ^= ((1 << (i + 1)) - 1) increment = t[2] - SegmentTree[0][loc] for j in range(n + 1):SegmentTree[j][loc >> j] += increment if t[0] == 2: if t[1] == 0:continue QueryList[t[1] - 1] = 1 - QueryList[t[1] - 1] if t[0] == 3: QueryList[t[1]] = 1 - QueryList[t[1]] if t[1] == 0:continue QueryList[t[1] - 1] = 1 - QueryList[t[1] - 1] if t[0] == 4:print(FindSum(t[2]) - FindSum(t[1] - 1)) ```
output
1
26,706
12
53,413
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}.
instruction
0
26,707
12
53,414
Tags: binary search, bitmasks, data structures Correct Solution: ``` # Fast IO (be careful about bitstring) import os,io input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n,q = map(int,input().split()) SegmentTree = [] for i in range(n + 1): SegmentTree.append([0] * (1 << (n - i))) a = list(map(int,input().split())) for i in range(1 << n): for j in range(n + 1): SegmentTree[j][i >> j] += a[i] QueryList = [0] * n # Only storing reverse as S = R * R (with different coeff) def FindSum(m): # Find a sum of first m elements summ = 0 for i in range(n + 1): if m & (1 << i): m -= 1 << i mCpy = m for j in range(i,n): if QueryList[j]: mCpy ^= ((1 << (j+1)) - (1 << i)) summ += SegmentTree[i][mCpy >> i] return summ for _ in range(q): t = list(map(int,input().split())) if t[0] == 1: loc = t[1] - 1 for i in range(n): if QueryList[i]: loc ^= ((1 << (i + 1)) - 1) increment = t[2] - SegmentTree[0][loc] for j in range(n + 1): SegmentTree[j][loc >> j] += increment if t[0] == 2: if t[1] == 0: continue QueryList[t[1] - 1] = 1 - QueryList[t[1] - 1] if t[0] == 3: QueryList[t[1]] = 1 - QueryList[t[1]] if t[1] == 0: continue QueryList[t[1] - 1] = 1 - QueryList[t[1] - 1] if t[0] == 4: print(FindSum(t[2]) - FindSum(t[1] - 1)) ```
output
1
26,707
12
53,415
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}.
instruction
0
26,708
12
53,416
Tags: binary search, bitmasks, data structures Correct Solution: ``` from sys import stdin REPLACE_ID = 1 REVERSE_ID = 2 SWAP_ID = 3 SUM_ID = 4 def main(): n, q = _read_ints() a = tuple(_read_ints()) requests = tuple(_read_ints() for _ in range(q)) for result in compute_requests_results(a, requests): print(result) def _read_ints(): return map(int, stdin.readline().split(" ")) def compute_requests_results(initial_array, requests): tree = _Tree(initial_array) for request_id, *request_args in requests: if request_id == REPLACE_ID: index, new_value = request_args tree[index-1] = new_value elif request_id == REVERSE_ID: k, = request_args tree.reverse(k) elif request_id == SWAP_ID: k, = request_args tree.swap(k) else: assert request_id == SUM_ID l, r = request_args yield tree.find_sum(l-1, r-1) class _Tree: def __init__(self, array): array = list(array) self._levels = [array] self._levels_sizes = [len(array)] self._nodes_sizes = [1] while self._levels_sizes[-1] > 1: last_size = self._levels_sizes[-1] last_level = self._levels[-1] last_node_size = self._nodes_sizes[-1] new_size = last_size // 2 new_level = [last_level[2*i] + last_level[2*i+1] for i in range(new_size)] new_node_size = last_node_size * 2 self._levels_sizes.append(new_size) self._levels.append(new_level) self._nodes_sizes.append(new_node_size) self._levels = tuple(self._levels) self._is_swapped = [False for level in self._levels] def __getitem__(self, index): i_curr_level = len(self._levels) - 1 curr_node_index = 0 while i_curr_level > 0: curr_node_index = self._get_subnode_index(curr_node_index, i_curr_level, index) i_curr_level -= 1 return self._levels[i_curr_level][curr_node_index] def __setitem__(self, index, value): old_value = self[index] delta = value - old_value i_curr_level = len(self._levels) - 1 curr_node_index = 0 while i_curr_level > 0: self._levels[i_curr_level][curr_node_index] += delta curr_node_index = self._get_subnode_index(curr_node_index, i_curr_level, index) i_curr_level -= 1 self._levels[i_curr_level][curr_node_index] += delta def _get_subnode_index(self, curr_node_index, i_curr_level, final_index): old_node_size = self._nodes_sizes[i_curr_level] i_new_level = i_curr_level - 1 new_node_size = self._nodes_sizes[i_new_level] is_right = final_index % old_node_size >= new_node_size if self._is_swapped[i_curr_level]: is_right = not is_right new_node_index = curr_node_index * 2 + (1 if is_right else 0) return new_node_index def find_sum(self, l, r): return self._find_sum_before(r+1) - self._find_sum_before(l) def _find_sum_before(self, index): if index == len(self._levels[0]): return self._levels[-1][0] result_sum = 0 i_curr_level = len(self._levels) - 1 curr_node_index = 0 while i_curr_level > 0: curr_node_size = self._nodes_sizes[i_curr_level] i_new_level = i_curr_level - 1 new_node_size = self._nodes_sizes[i_new_level] is_right = 1 if index % curr_node_size >= new_node_size else 0 is_swapped = self._is_swapped[i_curr_level] new_node_index = curr_node_index * 2 + (is_right if not is_swapped else 1 - is_right) denied_node_index = curr_node_index * 2 + (is_right if is_swapped else 1 - is_right) if is_right: result_sum += self._levels[i_new_level][denied_node_index] i_curr_level = i_new_level curr_node_index = new_node_index return result_sum def swap(self, k): self._is_swapped[k+1] = not self._is_swapped[k+1] def reverse(self, k): for i in range(k): self.swap(i) if __name__ == '__main__': main() ```
output
1
26,708
12
53,417
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}.
instruction
0
26,709
12
53,418
Tags: binary search, bitmasks, data structures Correct Solution: ``` import os,io input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n,q = map(int,input().split());QueryList = [0] * n;SegmentTree = [];a = list(map(int,input().split())) for i in range(n + 1):SegmentTree.append([0] * (1 << (n - i))) for i in range(1 << n): for j in range(n + 1):SegmentTree[j][i >> j] += a[i] def FindSum(m): summ = 0 for i in range(n + 1): if m & (1 << i): m -= 1 << i;mCpy = m for j in range(i,n): if QueryList[j]:mCpy ^= ((1 << (j+1)) - (1 << i)) summ += SegmentTree[i][mCpy >> i] return summ for _ in range(q): t = list(map(int,input().split())) if t[0] == 1: loc = t[1] - 1 for i in range(n): if QueryList[i]:loc ^= ((1 << (i + 1)) - 1) increment = t[2] - SegmentTree[0][loc] for j in range(n + 1):SegmentTree[j][loc >> j] += increment if t[0] == 2: if t[1] == 0:continue QueryList[t[1] - 1] = 1 - QueryList[t[1] - 1] if t[0] == 3: QueryList[t[1]] = 1 - QueryList[t[1]] if t[1] == 0:continue QueryList[t[1] - 1] = 1 - QueryList[t[1] - 1] if t[0] == 4:print(FindSum(t[2]) - FindSum(t[1] - 1)) ```
output
1
26,709
12
53,419
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 of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}. Submitted Solution: ``` class BIT(): def __init__(self,n): self.BIT=[0]*(n+1) self.num=n def query(self,idx): res_sum = 0 while idx > 0: res_sum += self.BIT[idx] idx -= idx&(-idx) return res_sum #Ai += x O(logN) def update(self,idx,x): while idx <= self.num: self.BIT[idx] += x idx += idx&(-idx) return import sys input=sys.stdin.buffer.readline n,q=map(int,input().split()) a=list(map(int,input().split())) bit=BIT(2**n) for i in range(2**n): bit.update(i+1,a[i]) b=0 def Sum(r,xor): id=xor res=0 if r==-1: return res for i in range(n,-1,-1): if r>>i &1: L=(id>>i)<<i R=L+(1<<i)-1 res+=bit.query(R+1)-bit.query(L) id^=(1<<i) return res for _ in range(q): query=tuple(map(int,input().split())) if query[0]==1: g,x,k=query x-=1 x^=b bit.update(x+1,k-a[x]) a[x]=k elif query[0]==2: k=query[1] b^=2**k-1 elif query[0]==3: k=query[1] if k!=n: b^=2**k else: gl,l,r=query l-=1 test=Sum(r,b)-Sum(l,b) print(test) ```
instruction
0
26,710
12
53,420
Yes
output
1
26,710
12
53,421
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 of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}. Submitted Solution: ``` class SegTree: def __init__(self, init_val, n, ide_ele, seg_func): self.segfunc = seg_func self.num = 2**(n-1).bit_length() self.ide_ele = ide_ele self.seg=[self.ide_ele]*2*self.num for i in range(n): self.seg[i+self.num-1]=init_val[i] for i in range(self.num-2,-1,-1) : self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) def update(self, k, x): k += self.num-1 self.seg[k] = x while k+1: k = (k-1)//2 self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2]) def query(self, p, q): if q<=p: return self.ide_ele p += self.num-1 q += self.num-2 res=self.ide_ele while q-p>1: if p&1 == 0: res = self.segfunc(res,self.seg[p]) if q&1 == 1: res = self.segfunc(res,self.seg[q]) q -= 1 p = p//2 q = (q-1)//2 if p == q: res = self.segfunc(res,self.seg[p]) else: res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q]) return res def range(self, p, q): p += self.num-1 q += self.num-2 res = [] while q-p>1: if p&1 == 0: res.append(p) if q&1 == 1: res.append(q) q -= 1 p = p//2 q = (q-1)//2 if p == q: res.append(p) else: res.append(p) res.append(q) return res import sys;input=sys.stdin.readline N, Q = map(int, input().split()) X = list(map(int, input().split())) ss = SegTree(X,2**N,0,lambda x,y:x+y) #print(ss.seg) x = 0 for _ in range(Q): t = list(map(int, input().split())) if t[0] == 1: # print("update",(t[1]-1)^x) ss.update((t[1]-1)^x, t[2]) elif t[0] == 2: x = x^((2**t[1])-1) # print("x",x) elif t[0] == 3: x = x^(2**t[1]) # print("x",x) else: rr = ss.range(t[1]-1, t[2]) # print(ss.query(t[1]-1, t[2])) R=0 for r in rr: # r, seg[r] -> rの表す区間長(2**ll=2**(N-c+1)) rt = r # print(r, ss.seg[r]) c = 0 while rt: rt=(rt-1)//2 c+=1 ll = N-c # rk : その区間長のもののうち幾つめ? rk=r-((1<<c)-1) m=x&(~((1<<ll)-1)) m=m>>ll # print(rk, rk^m, m) # print(r, (rk^m)+((1<<c)-1), ss.seg[(rk^m)+((1<<c)-1)]) # print(rk, r, ((1<<c)-1)+ rk^m) # print(rk,ll,m,(rk^m)+((1<<c)-1)) # print(ss.seg[(rk^m)+((1<<c)-1)]) R+=ss.seg[(rk^m)+((1<<c)-1)] # print(ss.seg) # print("ac",R) print(R) ```
instruction
0
26,711
12
53,422
Yes
output
1
26,711
12
53,423
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 of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}. Submitted Solution: ``` class solution: def __init__(self,array): self.array=array def replace(self,x,k): array[x]=k return array def reverse(self,k): for i in range(1,len(array)): start=(((i-1)*2**k)+1)-1 end=(i*2**k)-1 l=array[start:end+1] array[start:end+1]=l[::-1] return array def swap(self,k): for i in range(1,len(array)): start1=((2*i-2)*(2**k))+1 start1=start1-1 end1=(2*i-1)*(2**k) end1=end1-1 start2=((2*i-1)*(2**k))+1 start2=start2-1 end2=(2*i)*(2**k) end2=end2-1 array[start1:end1+1],array[start2:end2+1]=array[start2:end2+1],array[start1:end1+1] return array def Sum(self,l,r): s=0 for i in range(l,r+1): s+=array[i] return s if __name__=="__main__": n,q=list(map(int,input().split())) array=list(map(int,input().split())) while(q>0): if(n==0): print(0) break else: queries=list(map(int,input().split())) if(queries[0]==1): soln=solution(array) array=soln.replace(queries[1]-1,queries[2]) elif(queries[0]==2): soln=solution(array) array=soln.reverse(queries[1]) elif(queries[0]==3): soln=solution(array) array=soln.swap(queries[1]) elif(queries[0]==4): soln=solution(array) print(soln.Sum(queries[1]-1,queries[2]-1)) q-=1 ```
instruction
0
26,712
12
53,424
No
output
1
26,712
12
53,425
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 of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}. Submitted Solution: ``` #Round 665 Problem F import sys def getInput(): return sys.stdin.readline()[:-1] querys = int(getInput().split(' ')[1]) currList = list(int(x) for x in getInput().split(' ')) bitMask = 0 for x in [1]*querys: query = list(int(x) for x in getInput().split(' ')) if query[0] == 1: currList[(query[1]-1)^bitMask]=query[2] elif query[0] == 2: bitMask = bitMask^(1<<query[1]-1) elif query[0] == 3: bitMask = bitMask^(1<<query[1]) else: print(sum(currList[x^bitMask] for x in range(query[1]-1,query[2]))) ```
instruction
0
26,713
12
53,426
No
output
1
26,713
12
53,427
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 of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}. Submitted Solution: ``` class Queries: def __init__(self, n, q, li): self.n = n self.q = q self.li = li def replace(self, x, k): self.li[x-1]=k #print("After replace: "+str(self.li)) def reverse(self, k): for i in range(0,len(self.li)): s1 = ((i-1)*(2**k))+1 s2 = i * (2**k) tempLi = self.li[s1-1:s2] tempLi.reverse() self.li[s1-1:s2] = tempLi #print("After reverse: "+str(self.li)) def swap(self, k): s1 = ((2*1-2)*(2**k))+1 s11 = (2*1-1)*(2**k) s2 = ((2*1-1)*(2**k))+1 s22 = (2*1)*(2**k) #print(s1,s11,s2,s22) tempList1 = self.li[s1-1:s11] self.li[s1-1:s11] = self.li[s2-1:s22] self.li[s2-1:s22] = tempList1 #print("After swap: "+str(self.li)) def sum(self, l, r): sum = 0 for i in range(l-1, r): sum = sum + self.li[i] print(sum) n, q = input().split() n = int(n) q = int(q) li = [] #print(n,q) li = list(map(int, input().split())) opList = [] for i in range(0,q): tLi = list(map(int, input().split())) opList.append(tLi) q = Queries(n,q,li) for op in opList: if op[0] == 1: q.replace(op[1],op[2]) elif op[0] == 2: q.reverse(op[1]) elif op[0] == 3: q.swap(op[1]) elif op[0] == 4: q.sum(op[1],op[2]) ```
instruction
0
26,714
12
53,428
No
output
1
26,714
12
53,429
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 of length 2^n. You should process q queries on it. Each query has one of the following 4 types: 1. Replace(x, k) — change a_x to k; 2. Reverse(k) — reverse each subarray [(i-1) ⋅ 2^k+1, i ⋅ 2^k] for all i (i ≥ 1); 3. Swap(k) — swap subarrays [(2i-2) ⋅ 2^k+1, (2i-1) ⋅ 2^k] and [(2i-1) ⋅ 2^k+1, 2i ⋅ 2^k] for all i (i ≥ 1); 4. Sum(l, r) — print the sum of the elements of subarray [l, r]. Write a program that can quickly process given queries. Input The first line contains two integers n, q (0 ≤ n ≤ 18; 1 ≤ q ≤ 10^5) — the length of array a and the number of queries. The second line contains 2^n integers a_1, a_2, …, a_{2^n} (0 ≤ a_i ≤ 10^9). Next q lines contains queries — one per line. Each query has one of 4 types: * "1 x k" (1 ≤ x ≤ 2^n; 0 ≤ k ≤ 10^9) — Replace(x, k); * "2 k" (0 ≤ k ≤ n) — Reverse(k); * "3 k" (0 ≤ k < n) — Swap(k); * "4 l r" (1 ≤ l ≤ r ≤ 2^n) — Sum(l, r). It is guaranteed that there is at least one Sum query. Output Print the answer for each Sum query. Examples Input 2 3 7 4 9 9 1 2 8 3 1 4 2 4 Output 24 Input 3 8 7 0 8 8 7 1 5 2 4 3 7 2 1 3 2 4 1 6 2 3 1 5 16 4 8 8 3 0 Output 29 22 1 Note In the first sample, initially, the array a is equal to \{7,4,9,9\}. After processing the first query. the array a becomes \{7,8,9,9\}. After processing the second query, the array a_i becomes \{9,9,7,8\} Therefore, the answer to the third query is 9+7+8=24. In the second sample, initially, the array a is equal to \{7,0,8,8,7,1,5,2\}. What happens next is: 1. Sum(3, 7) → 8 + 8 + 7 + 1 + 5 = 29; 2. Reverse(1) → \{0,7,8,8,1,7,2,5\}; 3. Swap(2) → \{1,7,2,5,0,7,8,8\}; 4. Sum(1, 6) → 1 + 7 + 2 + 5 + 0 + 7 = 22; 5. Reverse(3) → \{8,8,7,0,5,2,7,1\}; 6. Replace(5, 16) → \{8,8,7,0,16,2,7,1\}; 7. Sum(8, 8) → 1; 8. Swap(0) → \{8,8,0,7,2,16,1,7\}. Submitted Solution: ``` class solution: def __init__(self,array): self.array=array def replace(self,x,k): array[x]=k return array def reverse(self,k): for i in range(1,len(array)): start=(((i-1)*(2**k))+1)-1 end=(i*(2**k))-1 l=array[start:end+1] array[start:end+1]=l[::-1] return array def swap(self,k): for i in range(1,len(array)): start1=((2*i-2)*(2**k))+1 start1=start1-1 end1=(2*i-1)*(2**k) end1=end1-1 start2=((2*i-1)*(2**k))+1 start2=start2-1 end2=(2*i)*(2**k) end2=end2-1 array[start1:end1+1],array[start2:end2+1]=array[start2:end2+1],array[start1:end1+1] return array def Sum(self,l,r): s=0 l-=1 r-=1 for i in range(l,r+1): s+=array[i] return s if __name__=="__main__": n,q=list(map(int,input().split())) array=list(map(int,input().split())) while(q>0): queries=list(map(int,input().split())) soln=solution(array) if(queries[0]==1): soln.array=soln.replace(queries[1]-1,queries[2]) elif(queries[0]==2): soln.array=soln.reverse(queries[1]) elif(queries[0]==3): soln.array=soln.swap(queries[1]) elif(queries[0]==4): print(soln.Sum(queries[1],queries[2])) q-=1 ```
instruction
0
26,715
12
53,430
No
output
1
26,715
12
53,431
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
instruction
0
26,728
12
53,456
Tags: data structures, greedy Correct Solution: ``` ''' Auther: ghoshashis545 Ashis Ghosh College: jalpaiguri Govt Enggineering College ''' from os import path import sys from heapq import heappush,heappop from functools import cmp_to_key as ctk from collections import deque,Counter,defaultdict as dd from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right from itertools import permutations from datetime import datetime from math import ceil,sqrt,log,gcd def ii():return int(input()) def si():return input().rstrip() def mi():return map(int,input().split()) def li():return list(mi()) abc='abcdefghijklmnopqrstuvwxyz' # mod=1000000007 mod=998244353 inf = float("inf") vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def bo(i): return ord(i)-ord('0') file = 1 def solve(): # for _ in range(ii()): n = ii() a = li() b = Counter(a) most_freq_elm,cnt = 0,1 for i in b: if b[i] > b[most_freq_elm]: most_freq_elm = i cnt = 1 elif b[i] == b[most_freq_elm]: cnt += 1 if cnt > 1: print(n) return ans = 0 for i in b: if i == most_freq_elm: continue prev_occur = [-2]*(2*n+1) prev_occur[n] = -1 cnt = 0 # print(prev_occur) for j in range(n): if a[j] == i: cnt += 1 elif a[j] == most_freq_elm: cnt -= 1 if prev_occur[cnt+n]==-2: prev_occur[cnt+n] = j else: ans = max(ans,j-prev_occur[cnt+n]) print(ans) if __name__ =="__main__": if(file): if path.exists('input.txt'): sys.stdin=open('input.txt', 'r') sys.stdout=open('output.txt','w') else: input=sys.stdin.readline solve() ```
output
1
26,728
12
53,457
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
instruction
0
26,729
12
53,458
Tags: data structures, greedy Correct Solution: ``` import sys input = sys.stdin.buffer.readline def prog(): n = int(input()) a = list(map(int,input().split())) freq = [0]*101 for i in range(n): freq[a[i]] += 1 mx = max(freq) amt = freq.count(mx) if amt >= 2: print(n) else: must_appear = freq.index(mx) ans = 0 for j in range(1,101): if j == must_appear: continue first_idx = [10**6]*(n+1) first_idx[0] = -1 curr = 0 for i in range(n): if a[i] == must_appear: curr += 1 elif a[i] == j: curr -= 1 ans = max(ans, i - first_idx[curr]) first_idx[curr] = min(first_idx[curr],i) print(ans) prog() ```
output
1
26,729
12
53,459
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
instruction
0
26,730
12
53,460
Tags: data structures, greedy Correct Solution: ``` import sys from sys import stdin n = int(stdin.readline()) a = list(map(int,stdin.readline().split())) lis = [0] * 101 for i in a: lis[i] += 1 f = 0 nmax = 1 for i in range(101): if lis[f] < lis[i]: f = i nmax = 1 elif lis[f] == lis[i]: nmax += 1 if nmax > 1: print (n) sys.exit() ans = 0 last= [0] * (2*n) lis = [None] * (2*n) for g in range(1,101): if f == g: continue now = 0 last[0] = g lis[0] = -1 for i in range(n): if a[i] == f: now += 1 elif a[i] == g: now -= 1 if last[now] != g: last[now] = g lis[now] = i else: ans = max(ans,i-lis[now]) print (ans) ```
output
1
26,730
12
53,461
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
instruction
0
26,731
12
53,462
Tags: data structures, greedy Correct Solution: ``` from collections import Counter n = int(input()) A = list(map(int, input().split())) cnt = Counter(A) maxv = max(cnt.values()) x = [c for c in cnt if cnt[c] == maxv] if len(x) > 1: print(n) else: x = x[0] ans = 0 for c in cnt: if c == x: continue dic = {0: -1} cur = 0 tmp = 0 for i, a in enumerate(A): cur += 1 if a == c else -1 if a == x else 0 if cur in dic: tmp = max(tmp, i - dic[cur]) dic.setdefault(cur, i) ans = max(ans, tmp) print(ans) ```
output
1
26,731
12
53,463
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
instruction
0
26,732
12
53,464
Tags: data structures, greedy Correct Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os from io import BytesIO, IOBase import sys from collections import defaultdict, deque, Counter from bisect import * from math import sqrt, pi, ceil, log, inf,gcd from itertools import permutations from copy import deepcopy from heapq import * def main(): n=int(input()) a=[0]*(101) b=list(map(int,input().split())) for i in b: a[i]+=1 ma=max(a) if a.count(ma)>=2: print(n) else: ele=a.index(ma) maa=0 for i in range(1,101): if i!=ele: c=Counter() su=0 for j in range(n): if b[j]==i: su-=1 elif b[j]==ele: su+=1 if c[su]==0: c[su]=j+1 else: maa=max(maa,j+1-c[su]) if su==0: maa=max(maa,j+1) print(maa) # 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") if __name__ == "__main__": main() ```
output
1
26,732
12
53,465
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
instruction
0
26,733
12
53,466
Tags: data structures, greedy Correct Solution: ``` n=int(input()) arr=list(map(int,input().split())) freq=[0]*(101) for i in arr:freq[i]+=1 maxx=max(freq) amtOFmaxx=freq.count(maxx) if amtOFmaxx>=2:print(n) else: must_apper=freq.index(maxx) ans=0 for j in range(1,101): if j==must_apper: continue first_index=[10**6]*(n+1) first_index[0]=-1 curr=0 for i in range(n): if arr[i]==must_apper: curr+=1 elif arr[i]==j: curr-=1 ans=max(ans,i-first_index[curr]) first_index[curr]=min(first_index[curr],i) #print(first_index) print(ans) ```
output
1
26,733
12
53,467
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
instruction
0
26,734
12
53,468
Tags: data structures, greedy Correct Solution: ``` import sys from collections import defaultdict, Counter import sys import os from io import BytesIO, IOBase #Fast IO Region 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") N = int(input()) arr = list(map(int, input().split())) freq = Counter(arr) ans = 0 fix = freq.most_common(1)[0][0] for other in freq: if other == fix: continue pref, last_pos = [0] * (N+2), {0:0} for i in range(N): now = 1 if arr[i] == other else 0 pref[i+1] = pref[i] + (-1 if arr[i] == fix else now) last_pos.setdefault(pref[i+1], i+1) ans = max(ans, i+1 - last_pos[pref[i+1]]) print(ans) ```
output
1
26,734
12
53,469
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array [a_1, a_2, ..., a_n]. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times. An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array. Output You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0. Examples Input 7 1 1 2 2 3 3 3 Output 6 Input 10 1 1 1 5 4 1 3 1 2 2 Output 7 Input 1 1 Output 0 Note In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
instruction
0
26,735
12
53,470
Tags: data structures, greedy Correct Solution: ``` n = int(input()) a = [int(x) for x in input().split()] cnt = [0]*101 for x in a: cnt[x] += 1 maxFreq = max(cnt) v = [] for i in range(101): if cnt[i] == maxFreq: v.append(i) if len(v) > 1: print(n) else: ans = 0 for i in range(101): if i == v[0] or cnt[i] == 0: continue table = {0:-1} sum = 0 for j in range(n): if a[j] == i: sum += 1 if a[j] == v[0]: sum -= 1 if sum in table: ind = table[sum] ans = max(ans , j-ind) else: table[sum] = j print(ans) ```
output
1
26,735
12
53,471