GCD problem

5

1 votes
Advanced Data Structures, Data Structures, Lazy Propagation in Interval/Segment Trees, Medium, Segment Trees
Problem

You are given an array of N integers. A function is defined as follows:   .

F(i,j)=G.C.D(Ai,Ai+1,...Aj)

You are also given Q queries of the form L R. For every query, you must answer the value:

 Ri=LRj=i+1F(i,j)
 

Input format

  • First line: Two integers N and M denoting the number of elements in the array and queries
  • Next line: N integers, Ai
  • Next M lines: Query in the mentioned format 

Output format
For each query print an integer in a new line denoting the answer to that query.

Constraints
1N,M2×105
1Ai3×108
1LRN

Time Limit: 2
Memory Limit: 256
Source Limit:
Editor Image

?