fork(1) download
  1. //package pawan;
  2. import java.util.*;
  3. import java.io.*;
  4. class mystruct
  5. {
  6. int freq;
  7. int data;
  8. int start;
  9. int end;
  10. }
  11. public class Main
  12. {
  13. public static ArrayList<ArrayList<Integer>>pf=new ArrayList<ArrayList<Integer>>();
  14. public static int[] arr;
  15. public static ArrayList<ArrayList<mystruct>>seg=new ArrayList<ArrayList<mystruct>>();
  16. public static int lowsearch(int low,int high,int no,ArrayList<Integer>list)
  17. {
  18. int mid,j;
  19. if(low>high)
  20. return -1;
  21. else if(list.get(high)<no)
  22. return -1;
  23. else
  24. {
  25. mid=(low+high)/2;
  26. if(list.get(mid)>=no)
  27. {
  28. j=lowsearch(low,mid-1,no,list);
  29. if(j<mid&&j!=-1)
  30. return j;
  31. else
  32. return mid;
  33. }
  34. else
  35. return lowsearch(mid+1,high,no,list);
  36. }
  37. }
  38. public static int highsearch(int low,int high,int no,ArrayList<Integer>list)
  39. {
  40. int mid,j;
  41. if(low>high)
  42. return -1;
  43. else if(list.get(low)>no)
  44. return -1;
  45. else
  46. {
  47. mid=(low+high)/2;
  48. if(list.get(mid)<=no)
  49. {
  50. j=highsearch(mid+1,high,no,list);
  51. if(j>=mid)
  52. return j;
  53. else
  54. return mid;
  55. }
  56. else
  57. return highsearch(low,mid-1,no,list);
  58. }
  59. }
  60. public static void fill(int i,int j,int current,ArrayList<Integer>list,ArrayList<mystruct>ans)
  61. {
  62. int mid;
  63. mystruct node,node1,node2;
  64. if(i==j)
  65. {
  66. node=ans.get(current);
  67. node.data=arr[list.get(i)];
  68. node.freq=1;
  69. node.start=i;
  70. node.end=i;
  71. return;
  72. }
  73. else
  74. {
  75. mid=(i+j)/2;
  76. fill(i,mid,2*current+1,list,ans);
  77. fill(mid+1,j,2*current+2,list,ans);
  78. node=ans.get(current);
  79. node1=ans.get(2*current+1);
  80. node2=ans.get(2*current+2);
  81. if(node1.data>node2.data)
  82. {
  83. node.data=node1.data;
  84. node.freq=node1.freq;
  85. node.start=i;
  86. node.end=j;
  87. }
  88. else if(node1.data<node2.data)
  89. {
  90. node.data=node2.data;
  91. node.freq=node2.freq;
  92. node.start=i;
  93. node.end=j;
  94. }
  95. else if(node1.data==node2.data)
  96. {
  97. node.data=node1.data;
  98. node.freq=node1.freq+node2.freq;
  99. node.start=i;
  100. node.end=j;
  101. }
  102. return ;
  103. }
  104. }
  105. public static void construct(ArrayList<Integer>list,int current)
  106. {
  107. int i,k,p;
  108. double x,y,z;
  109. p=list.size();
  110. x=(double)p;
  111. y=(double)2;
  112. z=Math.log10(x)/Math.log10(y);
  113. k=(int)z;
  114. y=(double)k;
  115. if(y!=z)
  116. y++;
  117. k=(int)Math.pow((double)2,y+1)-1;
  118. ArrayList<mystruct>ans;
  119. ans=seg.get(current);
  120. mystruct node;
  121. for(i=0;i<=k-1;i++)
  122. {
  123. node=new mystruct();
  124. ans.add(node);
  125. }
  126. fill(0,list.size()-1,0,list,ans);
  127. return ;
  128. }
  129. public static mystruct result(int i,int j,int current,int prime)
  130. {
  131. mystruct p,q,r;
  132. q=new mystruct();
  133. int x,y,a,c;
  134. p=seg.get(prime).get(current);
  135. x=p.start;
  136. y=p.end;
  137. if(x>=i&&y<=j)
  138. {
  139. q.data=p.data;
  140. q.freq=p.freq;
  141. return q;
  142. }
  143. else if(x>j||y<i)
  144. {
  145. q.data=-1;
  146. q.freq=0;
  147. return q;
  148. }
  149. else
  150. {
  151. q=result(i,j,2*current+1,prime);
  152. r=result(i,j,2*current+2,prime);
  153. a=q.data;
  154. c=r.data;
  155. if(a>c)
  156. return q;
  157. else if(c>a)
  158. return r;
  159. else if(c==a)
  160. {
  161. q.freq=q.freq+r.freq;
  162. return q;
  163. }
  164. }
  165. return q;
  166. }
  167. public static void main(String[] args)
  168. throws IOException
  169. {
  170. Integer i,j,k,l,m,n,p,q,a,b,c;
  171. String str;
  172. String[] string;
  173. str=buf.readLine();
  174. string=str.split(" ");
  175. n=Integer.parseInt(string[0]);
  176. m=Integer.parseInt(string[1]);
  177. str=buf.readLine();
  178. string=str.split(" ");
  179. j=string.length;
  180. arr=new int[j];
  181. for(i=0;i<=j-1;i++)
  182. arr[i]=Integer.parseInt(string[i]);
  183. //precomputation prime seive
  184. int[] primes=new int[100001];
  185. primes[0]=1;
  186. primes[1]=1;
  187. for(i=2;i<=100000;i++)
  188. {
  189. if(primes[i]==1)
  190. continue;
  191. else
  192. {
  193. for(j=2*i;j<=100000;j+=i)
  194. {
  195. primes[j]=1;
  196. }
  197. }
  198. }
  199. ArrayList<Integer>temp;
  200. for(i=0;i<=100000;i++)
  201. {
  202. temp=new ArrayList<Integer>();
  203. pf.add(temp);
  204. }
  205. //prime factorization of the array
  206. for(i=0;i<=n-1;i++)
  207. {
  208. l=arr[i];
  209. j=(int)Math.sqrt(l);
  210. for(k=1;k<=j;k++)
  211. {
  212. if(l%k!=0)
  213. continue;
  214. else
  215. {
  216. p=l/k;
  217. if(p==k)
  218. {
  219. if(primes[p]==0)
  220. pf.get(p).add(i);
  221. }
  222. else
  223. {
  224. if(primes[p]==0)
  225. pf.get(p).add(i);
  226. if(primes[k]==0)
  227. pf.get(k).add(i);
  228. }
  229. }
  230. }
  231. }
  232. //segment tree construction
  233. ArrayList<mystruct>temp1;
  234. for(i=0;i<=100000;i++)
  235. {
  236. temp1=new ArrayList<mystruct>();
  237. seg.add(temp1);
  238. }
  239. for(i=0;i<=100000;i++)
  240. {
  241. if(pf.get(i).size()>0)
  242. construct(pf.get(i),i);
  243. }
  244. ArrayList<Integer>temp2=new ArrayList<Integer>();
  245. mystruct help;
  246. int x,y;
  247. for(i=0;i<=m-1;i++)
  248. {
  249. p=-1;
  250. q=-1;
  251. x=-1;
  252. y=-1;
  253. str=buf.readLine();
  254. string=str.split(" ");
  255. a=Integer.parseInt(string[0]);
  256. b=Integer.parseInt(string[1]);
  257. c=Integer.parseInt(string[2]);
  258. k=(int)Math.sqrt((double)a);
  259. for(j=1;j<=k;j++)
  260. {
  261. if((a%j)!=0)
  262. continue;
  263. else
  264. {
  265. l=a/j;
  266. if(l==j)
  267. {
  268. if(primes[l]==0)
  269. temp2.add(l);
  270. }
  271. else
  272. {
  273. if(primes[l]==0)
  274. temp2.add(l);
  275. if(primes[j]==0)
  276. temp2.add(j);
  277. }
  278. }
  279. }
  280. //System.out.println(temp2.size());
  281. for(j=0;j<=temp2.size()-1;j++)
  282. {
  283. l=temp2.get(j);
  284. temp=pf.get(l);
  285. p=lowsearch(0,temp.size()-1,b-1,temp);
  286. q=highsearch(0,temp.size()-1,c-1,temp);
  287. //System.out.println(p+" "+q);
  288. if(p==-1||q==-1)
  289. {
  290. p=-1;
  291. q=-1;
  292. }
  293. else
  294. {
  295. help=result(p,q,0,l);
  296. p=help.data;
  297. q=help.freq;
  298. }
  299. if(p>x)
  300. {
  301. x=p;
  302. y=q;
  303. }
  304. }
  305. temp2.clear();
  306. System.out.println(x+" "+y);
  307. }
  308. }
  309. }
  310.  
Success #stdin #stdout 0.23s 381184KB
stdin
6 5
1 2 3 4 5 4
2 1 5
121 1 6
3 2 6
5 5 5
24 4 6
stdout
4 1
-1 -1
3 1
5 1
4 2