• bzoj1878 树状数组 要一点思考


    一开始快排敲错两次

     1 program hehe;
     2 var
     3  t,n,m,i,j,k:longint;
     4  c:array[0..1000000] of longint;
     5  pre,next,a,x:array[0..50005] of longint;
     6  ans,p,z,y:array[0..200005] of longint;
     7 
     8   procedure sort(l,r:longint);
     9   var
    10    zuo,you,mid:longint;
    11   begin
    12    zuo:=l;
    13    you:=r;
    14    mid:=y[(l+r)>>1];
    15    repeat
    16     while y[zuo]<mid do inc(zuo);
    17     while y[you]>mid do dec(you);
    18     if zuo<=you then
    19     begin
    20      t:=z[zuo];
    21      z[zuo]:=z[you];
    22      z[you]:=t;
    23      t:=y[zuo];
    24      y[zuo]:=y[you];
    25      y[you]:=t;
    26      t:=p[zuo];
    27      p[zuo]:=p[you];
    28      p[you]:=t;
    29      dec(you);
    30      inc(zuo);
    31     end;
    32    until zuo>you;
    33    if l<you then sort(l,you);
    34    if zuo<r then sort(zuo,r);
    35   end;
    36 
    37   function lowbit(a:longint):longint;
    38   begin
    39    exit(a and(-a));
    40   end;
    41 
    42   procedure add(a,b:longint);
    43   begin
    44    while a<=n do
    45    begin
    46     x[a]:=x[a]+b;
    47     a:=a+lowbit(a);
    48    end;
    49   end;
    50 
    51   function find(a:longint):longint;
    52   var
    53    s:longint;
    54   begin
    55    s:=0;
    56    while a>0 do
    57    begin
    58     s:=s+x[a];
    59     a:=a-lowbit(a);
    60    end;
    61    exit(s);
    62   end;
    63 
    64 begin
    65  readln(n);
    66  fillchar(a,sizeof(a),255);
    67  for i:=1 to n do
    68  begin
    69   read(a[i]);
    70   pre[i]:=c[a[i]];
    71   c[a[i]]:=i;
    72   next[pre[i]]:=i;
    73  end;
    74  read(m);
    75  for i:=1 to m do read(z[i],y[i]);
    76  for i:=1 to m do p[i]:=i;
    77  sort(1,m);
    78  for i:=1 to m do
    79  begin
    80   while j<y[i] do
    81   begin
    82    inc(j);
    83    add(pre[j]+1,1);
    84    add(j+1,-1)
    85   end;
    86   ans[p[i]]:=find(z[i]);
    87  end;
    88  for i:=1 to m do
    89  writeln(ans[i]);
    90 end.
    View Code

    1878: [SDOI2009]HH的项链

    Time Limit: 4 Sec  Memory Limit: 64 MB
    Submit: 2154  Solved: 1062
    [Submit][Status][Discuss]

    Description

    HH有一串由各种漂亮的贝壳组成的项链。HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义。HH不断地收集新的贝壳,因此, 他的项链变得越来越长。有一天,他突然提出了一个问题:某一段贝壳中,包含了多少种不同 的贝壳?这个问题很难回答。。。因为项链实在是太长了。于是,他只好求助睿智的你,来解 决这个问题。

    Input

    第一行:一个整数N,表示项链的长度。 第二行:N个整数,表示依次表示项链中贝壳的编号(编号为0到1000000之间的整数)。 第三行:一个整数M,表示HH询问的个数。 接下来M行:每行两个整数,L和R(1 ≤ L ≤ R ≤ N),表示询问的区间。

    Output

    M行,每行一个整数,依次表示询问对应的答案。

    Sample Input

    6
    1 2 3 4 3 5
    3
    1 2
    3 5
    2 6

    Sample Output

    2
    2
    4

    HINT


    对于20%的数据,N ≤ 100,M ≤ 1000;
    对于40%的数据,N ≤ 3000,M ≤ 200000;
    对于100%的数据,N ≤ 50000,M ≤ 200000。

    Source

    [Submit][Status][Discuss]
  • 相关阅读:
    Mobile phones(poj1195)
    Matrix(poj2155)
    1080
    1266
    codeforces626D . Jerry's Protest
    字符串格式化
    附加MySQL数据库的方法
    avaScript中变量的声明和赋值
    选择法排序
    JavaScript中变量的类型
  • 原文地址:https://www.cnblogs.com/chensiang/p/4586783.html
Copyright © 2020-2023  润新知