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

上一个出现的位置,每次将上上位置出现的+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. CentOS7.0安装与配置Tomcat-7

    解决权限不够 #chmod a+x filename 安装说明 安装环境:CentOS-7.0.1406安装方式:源码安装 软件:apache-tomcat-7.0.29.tar.gz 下载地址:ht ...

  2. The Rotation Game (POJ 2286) 题解

    [问题描述] (由于是英文的,看不懂,这里就把大意给大家说一下吧……都是中国人,相信大家也不愿意看英文……) 如图,一个井字形的棋盘,中间有着1-3任意的数,有ABCDEFGH八个操作,每个操作意味着 ...

  3. python基础学习笔记第二天 内建方法(s t r)

    python的字符串内建函数 str.casefold()将字符串转换成小写,Unicode编码中凡是有对应的小写形式的,都会转换str.center()返回一个原字符串居中,并使用空格填充至长度 w ...

  4. python xml包使用记录

    <?xml version="1.0" encoding="utf-8" ?> <request> <functionID> ...

  5. UIViewAnimationOptions swift 2

    UIView.animateWithDuration(0.5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, ...

  6. IBM X3650 M4安装win 2008 Server操作指南

    由于IBM服务器是IBM原有的Linux系统,所以需要在此硬件上安装Win 2008 Server系统(以下简称win8),中间遇到了很多坑,在下面的描述中会阐述.以下是安装的整个步骤: 一.所需工具 ...

  7. wpa_supplicant测试

    Android系统中对于WIFI的设置集成到了“设置”中,其实跟手动设置差不多.这里介绍下如何手动连接WIFI,以方便以后调试WIFI. 第一步要做的就是要加载WIFI模块驱动了.当然如果你的WIFI ...

  8. c++基础(三):多态

    virtual:虚函数.C++通过虚函数实现多态."无论发送消息的对象属于什么类,它们均发送具有同一形式的消息,对消息的处理方式可能随接手消息的对象而变"的处理方式被称为多态性.& ...

  9. Object-c 语法 - 头文件引用(@class/#import/#include)

    一. Objective-C 中 #import 和 #include 的区别 预编译指令 Objective-C:#import:由gcc编译器支持 C,C++:#include 在 Objecti ...

  10. Linux下如何使CP命令不提示覆盖

    在Linux下使用CP命令,经常会提示是否覆盖,如果是太批量的文件覆盖,老是这么提示,会很烦的.那如何解决这个问题呢? 我们先来看一下原因吧! 一般我们使用的命令是cp -rf sourcefile ...