fork download
  1. program squareunion;
  2. const
  3. MAXN = 100000;
  4. type elenco= Array [1..MAXN] of LongInt;
  5. var
  6. N, i,j, base, altezza : LongInt;
  7. R, X, Inizio, Fine :elenco;
  8. ans, areaoverl : Int64;
  9.  
  10. Procedure scambia (var a,b: Longint);
  11. var x:Longint;
  12. begin
  13. x:=a;
  14. a:=b;
  15. b:=x;
  16. end;
  17.  
  18. Procedure ordinamento (estremoi,estremos: Longint; var v : elenco; var u:elenco; var w:elenco; ordinato:boolean);
  19. var inf, sup, medio:Longint;
  20. pivot :Longint;
  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. scambia(w[inf],w[sup]);
  37. inf:=inf+1;
  38. sup:=sup-1;
  39. end;
  40. until inf>sup;
  41. if (estremoi<sup) then ordinamento(estremoi,sup,v,u,w,ordinato);
  42. if (inf<estremos) then ordinamento(inf,estremos,v,u,w,ordinato);
  43. end;
  44.  
  45.  
  46.  
  47. begin
  48. {
  49.   uncomment the two following lines if you want to read/write from files
  50.   assign(input, 'input.txt'); reset(input);
  51.   assign(output, 'output.txt'); rewrite(output);
  52. }
  53.  
  54. ReadLn(N);
  55.  
  56. for i:=1 to N do
  57. Read(X[i]);
  58. ReadLn();
  59. ans:=0;
  60. for i:=1 to N do begin Read(R[i]); ans:=ans+sqr(2*R[i]); end;
  61. ReadLn();
  62. for i:=1 to N do
  63. begin
  64. Inizio[i]:=X[i]-R[i];
  65. Fine[i]:=X[i]+R[i];
  66. end;
  67. ordinamento(1,N,Inizio, Fine, R, true);
  68. i:=1;
  69. while i<N do
  70. begin
  71. if Fine[i]<=Inizio[i+1] then begin areaoverl:=0; i:=i+1; end
  72. else
  73. begin
  74. j:=i+1;
  75. areaoverl := 0;
  76. while (Fine[i]>Inizio[j]) and (j<=N) do
  77. begin
  78. if Fine[i]>Fine[j] then areaoverl:=sqr(2*R[j])
  79. else
  80. begin
  81. base:=Fine[i]-Inizio[j];
  82. if R[i]<=R[j] then altezza:=2*R[i]
  83. else altezza:=2*R[j];
  84. areaoverl:=base*altezza;
  85.  
  86. end;
  87. j:=j+1;
  88. ans:= ans-areaoverl;
  89. end;
  90. i:=j-1;
  91. end;
  92. end;
  93. WriteLn(ans);
  94. end.
Success #stdin #stdout 0s 5296KB
stdin
3
1 2 5
1 1 1
stdout
10