My solution is based on some divide and conquer and hashing(it would be simple if u read disjoint sparse table tutorial before)
Lets build a tree,where the root node ans the query for all ranges(l,r) such that l< mid and r> mid,mid=n/2
So in this node ,precalculate the prime factorisation of ranges (i,mid-1) where i>=l and (mid,j) where j>mid ,calculate it mod2 and mod3
So lets say for (l,mid-1) we have prime factor(power raised to mod2) as 2,3,5(no use of 0 powers)
Then for (l,r) to be a perfect square (mid,r) must have the same prime factors mod2(ie 2,3,5)
So one can just hash the prime factor sequence in some way and for checking (l,r) ,compare hash value of (l,mid-1),(mid,r)
For mod3,we must have sth like 2^1,3^2 on one side and 2^2,3^1 on other
So for mod3,one should hash the complement(3-x) in left side ie if it is 2^2,3^1 then convert to 2^1,3^2 and hash it,while for the right side hash the uncomplemented form
So again to check if its a cube one can compare complemented of(l,mid-1) and uncomplemented of (mid,r)
However this node could only ans for the ranges (l,r) such l < mid ,r > mid
So do this thing repeatedly for left and right halves
Preprocessing:O(nlogn)
Query:O(logn)
(U can ans it in O(1) using some bit operations,however i dont remember it correctly)
Sorry i haven’t written the code,its just an idea.