1632D - New Year Concert - CodeForces Solution


binary search data structures greedy math number theory two pointers *2000

Please click on ads to support us..

Python Code:

from sys import stdin , setrecursionlimit
input = stdin.readline

inp = lambda : list(map(int,input().split()))

class sparsetable:

    def __init__(self , function , bound):
        self.b = None
        self.block = None
        self.n = None
        self.size = None
        self.bound = bound
        self.function = function

    def construct(self , a):

        self.n = len(a)
        for i in range(30):
            if(self.n < (1 << i)):
                self.size = i
                break

        self.b = [[self.bound for i in range(self.size)] for j in range(self.n)]
        self.block = [-1 for i in range(self.n + 1)]

        for i in range(self.n):
            self.b[i][0] = a[i]
        
        for j in range(1 , self.size):
            for i in range(self.n - (1 << (j - 1))):
                self.b[i][j] = self.function(self.b[i][j - 1] , self.b[i + (1 << (j - 1))][j - 1])
        
        for i in range(1 , self.n + 1):
            for j in range(self.size - 1 , -1 , -1):
                if(i >= (1 << j)):
                    self.block[i] = j
                    break

    def query(self , l , r):

                dist = r - l + 1

        ans = self.function(self.b[l][self.block[dist]] , self.b[r - (1 << self.block[dist]) + 1][self.block[dist]])
        return ans
        
    
    def log_query(self , l , r):

                dist = r - l + 1

        ans = self.bound
        for i in range(self.size - 1 , -1 , -1):
            if(dist >> i & 1):
                ans = self.function(ans , b[l][i])
                l += (1 << i)

        return ans

def gcd(a , b):
    if(b == 0):return a
    return gcd(b , a % b)

def answer():

    b = sparsetable(gcd , 0)
    b.construct(a)
    
    ans , done = 0 , -1
    for i in range(n):
 
        l , h = 1 , i - done
        while(l <= h):
            mid = (l + h)//2
            g = b.query(i - mid + 1 , i)
 
            if(g == mid):
                done = i
                ans += 1
                break
                
            if(g > mid):l = mid + 1
            else:h = mid - 1
            
        print(ans , end = ' ')
    
 
for T in range(1):
 
    n = int(input())
    a = list(map(int,input().split()))
    
    answer()


Comments

Submit
0 Comments
More Questions

1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance