fork(1) download
  1. const fi='kquery.inp';
  2. fo='kquery.out';
  3. type mang=array[0..30000]of longint;
  4. manglon=array[0..200000]of longint;
  5. mangit=array[0..300000]of longint;
  6. var a,csa:mang;
  7. x,y,z,cstv,res:manglon;
  8. it:mangit;
  9. n,q:longint;
  10. procedure mo;
  11. begin
  12. assign(input,fi);reseT(input);
  13. assign(output,fo);rewrite(output);
  14. end;
  15. procedure dong;
  16. begin
  17. close(input);closE(output);
  18. end;
  19. procedure nhap;
  20. var i:longint;
  21. begin
  22. readln(n);
  23. for i:=1 to n do
  24. begin
  25. read(a[i]);
  26. csa[i]:=i;
  27. end;
  28. readln(q);
  29. for i:=1 to q do
  30. begin
  31. readln(x[i],y[i],z[i]);
  32. cstv[i]:=i;
  33. end;
  34. end;
  35. procedure swap(Var i,j:longint);
  36. var t:longint;
  37. begin
  38. t:=i;
  39. i:=j;
  40. j:=t;
  41. end;
  42. procedure qsa(l,r:longint);
  43. var i,j,m,xx:longint;
  44. begin
  45. i:=l;
  46. j:=r;
  47. m:=random(r-l)+1+l;
  48. xx:=a[m];
  49. repeat
  50. while (a[i]<xx) do inc(i);
  51. while (xx<a[j]) do dec(j);
  52. if i<=j then
  53. begin
  54. swap(a[i],a[j]);
  55. swap(csa[i],csa[j]);
  56. inc(i);
  57. dec(j);
  58. end;
  59. until i>j;
  60. if i<r then qsa(i,r);
  61. if l<j then qsa(l,j);
  62. end;
  63. procedure qstv(l,r:longint);
  64. var i,j,m,xx:longint;
  65. begin
  66. i:=l;
  67. j:=r;
  68. m:=random(r-l)+1+l;
  69. xx:=z[m];
  70. repeat
  71. while (z[i]<xx) do inc(i);
  72. while (xx<z[j]) do dec(j);
  73. if i<=j then
  74. begin
  75. swap(x[i],x[j]);
  76. swap(y[i],y[j]);
  77. swap(z[i],z[j]);
  78. swap(cstv[i],cstv[j]);
  79. inc(i);
  80. dec(j);
  81. end;
  82. until i>j;
  83. if i<r then qstv(i,r);
  84. if l<j then qstv(l,j);
  85. end;
  86. procedure update(u,w,l,r,i:longint);
  87. var mid:longint;
  88. begin
  89. if (r<u) or (u<l) then exit;
  90. if (u=l) and (u=r) then
  91. begin
  92. it[i]:=w;
  93. exit;
  94. end;
  95. mid:=(l+r) div 2;
  96. update(u,w,l,mid,i*2);
  97. update(u,w,mid+1,r,i*2+1);
  98. it[i]:=it[i*2]+it[i*2+1];
  99. end;
  100. function find(u,v,l,r,i:longint):longint;
  101. var mid:longint;
  102. begin
  103. if u>v then exit(0);
  104. if (r<u) or (v<l) then exit(0);
  105. if (u<=l) and (r<=v) then
  106. exit(it[i]);
  107. mid:=(l+r) div 2;
  108. exit(find(u,v,l,mid,i*2)+find(u,v,mid+1,r,i*2+1));
  109. end;
  110. procedure xuli;
  111. var i,j:longint;
  112. begin
  113. qsa(1,n);
  114. qstv(1,q);
  115. for i:=1 to n do update(i,1,1,n,1);
  116. j:=1;
  117. for i:=1 to q do
  118. begin
  119. while (j<=n) and (a[j]<=z[i]) do
  120. begin
  121. update(csa[j],0,1,n,1);
  122. inc(j);
  123. end;
  124. res[cstv[i]]:=(find(1,y[i],1,n,1)-find(1,x[i]-1,1,n,1));
  125. end;
  126. for i:=1 to q do writeln(res[i]);
  127. end;
  128. begin
  129. mo;
  130. nhap;
  131. xuli;
  132. dong;
  133. end.
  134.  
  135.  
Runtime error #stdin #stdout 0s 5600KB
stdin
Standard input is empty
stdout
Runtime error 2 at $080480E4
  $080480E4