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