zoukankan      html  css  js  c++  java
  • bzoj 2743 树状数组离线查询

    我们按照询问的右端点排序,然后对于每一个位置,记录同颜色

    上一个出现的位置,每次将上上位置出现的+1,上次出现的-1,然后

    用树状数组维护就好了

    /**************************************************************
        Problem: 2743
        User: BLADEVIL
        Language: Pascal
        Result: Accepted
        Time:11072 ms
        Memory:70540 kb
    ****************************************************************/
     
    //By BLADEVIL
    var
        n, m, t, l                      :longint;
        pre, other, num                 :array[0..2000005] of longint;
        last, ans, first, a, pred, c    :array[0..2000005] of longint;
         
    procedure connect(x,y,tot:longint);
    begin
        inc(l);
        pre[l]:=last[x];
        last[x]:=l;
        other[l]:=y;
        num[l]:=tot;
    end;
      
    procedure add(x,y:longint);
    begin
        while (x<=n) do
        begin
            inc(c[x],y);
            x:=x+(x and -x);
        end;
    end;
      
    function ask(x:longint):longint;
    var    
        sum                             :longint;
    begin
        sum:=0;
        while x>0 do
        begin
            sum:=sum+c[x];
            x:=x-(x and -x);
        end;
        exit(sum);
    end;
     
    procedure init;
    var
        i, j                            :longint;
        x, y                            :longint;
         
    begin
        readln(n,t,m);
        for i:=1 to n do
        begin
            read(a[i]);
            if first[a[i]]<>0 then pred[i]:=first[a[i]];
            first[a[i]]:=i;
        end;
         
        for i:=1 to m do
        begin
            readln(x,y);
            connect(y,x,i);
        end;
    end;
     
    procedure main;
    var
        i                               :longint;
        q, p                            :longint;
         
    begin
        for i:=1 to n do
        begin
            add(pred[pred[i]]+1,1);
            add(pred[i]+1,-1);
            q:=last[i];
            while q<>0 do
            begin
                p:=other[q];
                ans[num[q]]:=ask(p);
                q:=pre[q];
            end;
        end;
        for i:=1 to m do writeln(ans[i]);
    end;
      
    begin
        init;
        main;
    end.
  • 相关阅读:
    一文说清 InnoDB 的事务机制
    MySQL 索引结构
    Mysql索引(一篇就够le)
    对Elasticsearch生命周期的思考
    elasticsearch备份和还原(基于hdfs)
    想写一篇jvm的工具入门
    elasticsearch跨集群数据迁移
    [论文阅读]阿里DIN深度兴趣网络之总体解读
    [源码阅读] 阿里SOFA服务注册中心MetaServer(3)
    [源码阅读] 阿里SOFA服务注册中心MetaServer(2)
  • 原文地址:https://www.cnblogs.com/BLADEVIL/p/3496023.html
Copyright © 2011-2022 走看看