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

上一个出现的位置,每次将上上位置出现的+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. Dev的DocumentManager 相关问题

    1.改变DocumentManager包含的窗体的排列方式 if (this.documentManager1.View.Type != ViewType.NativeMdi) { this.docu ...

  2. 例题6-10 The Falling Leaves,UVA699

    这道题我的思路是先通过递归构建树,然后进行遍历将位置和保存在map映射中,最后按顺序输出map集合中的值. 至于如何遍历,我是依次尝试了宽度优先遍历和深度优先遍历,当然这都是可以的.不过期间写错了很多 ...

  3. CSS3 column-rule-style 属性

    CSS column-rule-style属性用于在多列布局中指定列与列之间通过column rule属性设置的分隔线的样式.column-rule是列与列之间的一条垂直分隔线,你可以使用column ...

  4. 【转】代码编辑器(一)-TSynCompletionProposal用法

    注意,本系列均转载自http://blog.163.com/zom1995@126/ 网上有人给我一个SynEdit这个东西,因为我很喜欢自己编个代码编辑器,但要是用Delphi直接弄的,就我现在这样 ...

  5. C# A窗口内容显示在B窗口中的方法

    HeScripts script = new HeScripts(); //A窗口中实例化B窗口 string okscripts = "test"; //设置字段内容 scrip ...

  6. [terry笔记]Oracle会话追踪(二):TKPROF

    接上一笔记[terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046 http://www.cnblogs.com/kkterry/p/3279282.html ...

  7. Microsoft Azure 的一些限制 Global

    Azure Subscription and Service Limits, Quotas, and Constraints http://azure.microsoft.com/en-us/docu ...

  8. iOS学习之Object-C语言简单的通讯录管理系统

    用这几天学的OC的知识,写了一个实现简单功能的通讯录管理系统,在这里分享给大家: 通讯录管理系统 *  需求: 1.定义联系人类Contact.实例变量:姓名(拼音,首字母大写).性别.电话号码.住址 ...

  9. Question about pairing/bonding?

    Except that on android you can bypass the pairing dialog if you know the PIN in advance through a di ...

  10. Content Template & DataTemplate 区别

    转一篇很好的博客: http://www.cnblogs.com/lzhp/p/3250786.html 介绍 listbox 的 Template.ItemsPanel.ItemContainerS ...