fork download
  1. program formula1;
  2. type elenco = array[1..2000] of qword;
  3. var N,Q,p,t,i,j, pos, ans :qword;
  4. a, b, c, valore, id :elenco;
  5.  
  6. function calcolavalore (aa,bb,cc,tt:qword): qword;
  7. begin
  8. calcolavalore:=aa*tt*tt+bb*tt+cc;
  9. end;
  10.  
  11. Procedure scambia (var aa,bb: qword);
  12. var x:qword;
  13. begin
  14. x:=aa;
  15. aa:=bb;
  16. bb:=x;
  17. end;
  18. Procedure ordinamento (estremoi,estremos: qword; var v : elenco;var u : elenco; ordinato:boolean);
  19. var inf, sup, medio:qword;
  20. pivot :qword;
  21. begin
  22. inf:=estremoi;
  23. sup:=estremos;
  24. medio:= (estremoi+estremos) div 2;
  25. pivot:=v[medio];
  26. repeat
  27. if (ordinato) then
  28. begin
  29. while (v[inf]>pivot) do inf:=inf+1;
  30. while (v[sup]<pivot) do sup:=sup-1;
  31. end;
  32. if inf<=sup then
  33. begin
  34. scambia(v[inf],v[sup]);
  35. scambia(u[inf],u[sup]);
  36. inf:=inf+1;
  37. sup:=sup-1;
  38. end;
  39. until inf>sup;
  40. if (estremoi<sup) then ordinamento(estremoi,sup,v,u,ordinato);
  41. if (inf<estremos) then ordinamento(inf,estremos,v,u,ordinato);
  42. end;
  43. Procedure ordinamento1 (estremoi,estremos: qword; var v : elenco; ordinato:boolean);
  44. var inf, sup, medio:qword;
  45. pivot :qword;
  46. begin
  47. inf:=estremoi;
  48. sup:=estremos;
  49. medio:= (estremoi+estremos) div 2;
  50. pivot:=v[medio];
  51. repeat
  52. if (ordinato) then
  53. begin
  54. while (v[inf]<pivot) do inf:=inf+1;
  55. while (v[sup]>pivot) do sup:=sup-1;
  56. end;
  57. if inf<=sup then
  58. begin
  59. scambia(v[inf],v[sup]);
  60. inf:=inf+1;
  61. sup:=sup-1;
  62. end;
  63. until inf>sup;
  64. if (estremoi<sup) then ordinamento1(estremoi,sup,v,ordinato);
  65. if (inf<estremos) then ordinamento1(inf,estremos,v,ordinato);
  66. end;
  67. procedure controlla (var v: elenco; var u : elenco);
  68. var h, w,z, inff,supp : qword;
  69. temp : elenco;
  70. begin
  71. h:=1;
  72. while ((v[h]<>v[pos]) and (h<=N)) do h:=h+1;
  73. inff:=h;
  74. w:=N;
  75. while ((v[w]<>v[pos]) and (w>=1)) do w:=w-1;
  76. supp:=w;
  77. z:=1;
  78. if inff=supp then ans:=u[pos]
  79. else
  80. begin
  81. for h:=inff to supp do
  82. begin
  83. temp[z]:=u[h];
  84. z:=z+1;
  85. end;
  86. ordinamento1(1,z-1,temp,true);
  87. ans:=temp[pos-inff+1] ;
  88. end;
  89. end;
  90.  
  91. begin
  92. (*assign(input, 'input.txt'); reset(input);
  93.   assign(output, 'output.txt'); rewrite(output);*)
  94. readln(N);
  95. for i:=1 to N do begin valore[i]:=0; id[i]:=i; end;
  96. for i:=1 to N do readln(a[i],b[i],c[i]);
  97. for i:=1 to N do valore[i]:=calcolavalore(a[i],b[i],c[i],0);
  98. ordinamento(1,N,valore, id, true);
  99. readln(Q);
  100. for i:=1 to Q do
  101. begin
  102. readln (p,t);
  103. pos:=p;
  104. controlla (valore, id);
  105. writeln(ans);
  106. end;
  107.  
  108. end.
Success #stdin #stdout 0s 5320KB
stdin
5 
2 4 6
3 3 8
4 0 5
1 4 1
7 7 4
5
4 0
1 1
4 2
4 3
5 4
stdout
5
2
5
5
4