CVJETICI - Cvjetici

Tác giả: ll931110

Ngôn ngữ: Pascal

{$MODE DELPHI}
Program CVJETICI;
Const
  input  = '';
  output = '';
  maxn = 100000;
Var
  a: array[1..maxn] of integer;
  n: integer;

Procedure add(v,k: integer);
Begin
  While v <= maxn do
    Begin
      a[v]:= a[v] + k;
      v:= v + (v and -v);
    End;
End;

Function calc(v: integer): integer;
Var
  res: integer;
Begin
  res:= 0;
  While v > 0 do
    Begin
      res:= res + a[v];
      v:= v - (v and -v);
    End;
  calc:= res;
End;

Procedure solve;
Var
  fi,fo: text;
  i,u,v,x,y: integer;
Begin
  Assign(fi, input);
    Reset(fi);

  Assign(fo, output);
    Rewrite(fo);

  Readln(fi, n);
  For i:= 1 to n do
    Begin
      Readln(fi, u, v);

      x:= calc(u);
      y:= calc(v);
      Writeln(fo, x + y);

      add(u,-x);
      add(u + 1,x);

      add(v,-y);
      add(v + 1,y);

      add(u + 1,1);
      add(v,-1);
    End;

  Close(fo);
  Close(fi);
End;

Begin
  solve;
End.

Download