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

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

用树状数组维护就好了

/**************************************************************
    Problem:
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time: ms
    Memory: kb
****************************************************************/
 
//By BLADEVIL
var
    n, m, t, l                      :longint;
    pre, other, num                 :array[..] of longint;
    last, ans, first, a, pred, c    :array[..] 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:=;
    while x> 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:= to n do
    begin
        read(a[i]);
        if first[a[i]]<> then pred[i]:=first[a[i]];
        first[a[i]]:=i;
    end;
     
    for i:= to m do
    begin
        readln(x,y);
        connect(y,x,i);
    end;
end;
 
procedure main;
var
    i                               :longint;
    q, p                            :longint;
     
begin
    for i:= to n do
    begin
        add(pred[pred[i]]+,);
        add(pred[i]+,-);
        q:=last[i];
        while q<> do
        begin
            p:=other[q];
            ans[num[q]]:=ask(p);
            q:=pre[q];
        end;
    end;
    for i:= to m do writeln(ans[i]);
end;
  
begin
    init;
    main;
end.

bzoj 2743 树状数组离线查询的更多相关文章

  1. HDU 4630 No Pain No Game 树状数组+离线查询

    思路参考 这里. #include <cstdio> #include <cstring> #include <cstdlib> #include <algo ...

  2. 【树状数组+离线查询】HDU 3333 Turing Tree

    https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/H [题意] 给定一个数组,查询任意区间内不同数字之和. (n<=30000 ...

  3. HDU 3333 树状数组离线查询

    题目大意: 询问区间内不同种类的数的数值之和 这里逐个添加最后在线查询,会因为相同的数在区间内导致冲突 我们总是希望之后添加的数不会影响前面,那么我们就在添加到第i个数的时候,把所有在1~i 的区间的 ...

  4. BZOJ 2743 树状数组

    不能用分块. #include <bits/stdc++.h> using namespace std; ; struct Info{int l,r,Id;}Q[Maxn]; int a[ ...

  5. SPOJ DQUERY树状数组离线or主席树

    D-query Time Limit: 227MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submit Status ...

  6. D-query SPOJ 树状数组+离线

    D-query SPOJ 树状数组+离线/莫队算法 题意 有一串正数,求一定区间中有多少个不同的数 解题思路--树状数组 说明一下,树状数组开始全部是零. 首先,我们存下所有需要查询的区间,然后根据右 ...

  7. Necklace HDU - 3874 (线段树/树状数组 + 离线处理)

    Necklace HDU - 3874  Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...

  8. 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...

  9. BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) , ...

随机推荐

  1. php 显示内存 释放内存

    <?php //这只是个例子,下面的数字取决于你的系统 echo memory_get_usage() . "\n"; // 36640 $a = str_repeat(&q ...

  2. LotusPhp起步:经典的HelloWorld

    写了几篇LotusPhp,一直没有跑个程序,感觉好像步骤有点错,所以先上个经典的Demo,HelloWorld吧 先按推荐目录建好文件夹,如果懒的建,下面有下载的Demo包,解压就可以用,因为简单,也 ...

  3. 在 PHP 中结合 Ajax 技术进行图片上传

    前面几则日志中讲述了在 PHP 中上传文件,相信大家对 PHP 中如何进行文件上传已经初步掌握.本文来继续探讨在 PHP 中上传文件的技术,不同的是,本次上传将仅限于图片文件的上传,并且将采用 Aja ...

  4. mysql中的 IN和FIND_IN_SET的查询问题

    原来以为mysql可以进行这样的查询select id, list, name from table where 'daodao' IN (list);      (一)注:1. table含有三个字 ...

  5. Vmware为Ubuntu安装VmTools

    From:http://www.cnblogs.com/killerlegend/p/3632443.html Author:KillerLegend 1:首先打开Vmware并运行里面的Ubuntu ...

  6. 3.html5的文本元素

    如果你看了第一篇的内容,你会发现我的代码是这样的: 文本 <span>文本</span> <scolia>文本</scolia> <scolia ...

  7. LaTeX中无法显示中文问题

  8. Windows Phone 8.1SDK新特性预览

    前言    Windows Phone 8.1的预览版将在近期推送,WP 8.1的SDK也已经进入到RC阶段,可以从这里安装.本次更新的SDK被直接集成到了VS2013Update2里面,不再是单独的 ...

  9. virtualbox下 ubuntu 14.04设置外网独立IP

    安装时记得选择sshserver vim /etc/network/interfaces iface eth0 inet static address YOUR IP netmask 子网掩码 get ...

  10. 十一、从头到尾彻底解析Hash 表算法

    在研究MonetDB时深入的学习了hash算法,看了作者的文章很有感触,所以转发,希望能够使更多人受益! 十一.从头到尾彻底解析Hash 表算法 作者:July.wuliming.pkuoliver  ...