牛客一 J题 树状数组

题目描述

Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l2, r2), ..., count(lq, rq) where count(i, j) is the number of different integers among a1, a2, ..., ai, aj, aj + 1, ..., an.

输入描述:

The input consists of several test cases and is terminated by end-of-file.

The first line of each test cases contains two integers n and q.

The second line contains n integers a1, a2, ..., an.

The i-th of the following q lines contains two integers li and ri.

输出描述:

For each test case, print q integers which denote the result.

示例

输入

3 2

1 2 1

1 2

1 3

4 1

1 2 3 4

1 3

输出

2

1

3

备注:

  • 1 ≤ n, q ≤ 105
  • 1 ≤ ai ≤ n
  • 1 ≤ li, ri ≤ n
  • The number of test cases does not exceed 10.

题解

这场比赛的签到题 有很多种方法:莫队,主席树等 不过我不会

标程是树状数组,树状数组可以对区间进行跳跃性的操作。首先是题意,题意要求我们找[0,l]与[r,n]两个区间加起来不同的数,我看到标程将数组扩大一倍,就相当于找[r,l]区间不同的数,不过我觉得没有这个必要。

首先把那三个函数写好,套模板。

我们可以标记每个数字出现的前后位置,当遍历到一个数的最后一个数字后,从这个数开始的位置向上更新,这样可以快速更新这个位置之前有多少种数,(l前有多少种数并且结束的-r前有多少开始并结束的)=空缺区间有多少种数开始并已经结束,然后总的减去这个数就可以

下面是在百度辛苦折腾下终于AC的代码

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
struct node
{
int l,r,num;
}f[maxn];
int s[maxn];
int pre[maxn];
int las[maxn];
int ans[maxn];
int tree[maxn];
int n;
bool cmp(node a,node b)
{
return a.r<b.r;
}
/* 树状数组函数模板*/
void add(int i, int v) {
while(i <= n+1) {
tree[i] += v;
i += i&-i;
}
} int query(int i) {
int ret = 0;
while(i) {
ret += tree[i];
i -= i&-i;
}
return ret;
}
/*******************/
int main() {
int q;
int tot=0;
while(scanf("%d%d",&n,&q)!=EOF){
memset(pre,0,sizeof(pre));
memset(tree,0,sizeof(tree));
memset(las,0,sizeof(las));
tot=0;
for(int i=1;i<=n;i++){
scanf("%d",&s[i]);
if(!pre[s[i]]){
tot++;
pre[s[i]]=i;
}
las[s[i]]=i;
}
for(int i=1;i<=q;i++){
scanf("%d %d",&f[i].l,&f[i].r);
f[i].num=i;
}
int nowl=1;
sort(f+1,f+1+q,cmp);
for(int i=1;i<=n;i++){
while(nowl<=q&&f[nowl].r==i){
int num=f[nowl].num;
ans[num]=tot-(query(f[nowl].r)-query(f[nowl].l));
nowl++; }
if(las[s[i]]==i) add(pre[s[i]],1);
}
for(int i=1;i<=q;i++) printf("%d\n",ans[i]);
}
}

Different Integers的更多相关文章

  1. [LeetCode] Sum of Two Integers 两数之和

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  2. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  3. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  4. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  5. LeetCode Sum of Two Integers

    原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...

  6. Nim Game,Reverse String,Sum of Two Integers

    下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...

  7. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  8. LeetCode 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  9. leetcode-【中等题】Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

  10. 解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译)

    解剖SQLSERVER 第十三篇    Integers在行压缩和页压缩里的存储格式揭秘(译) http://improve.dk/the-anatomy-of-row-amp-page-compre ...

随机推荐

  1. CentOS 7 连接不到网络解决方法

    使用VM12创建虚拟机并安装CentOS 7,但是安装完成后发现连接不到网络. ping jd.com发现不通 因为在创建虚拟机的时候我们选择的是NAT模式 这里给出NAT模式下对应的的解决方法: 一 ...

  2. ABP 切换mysql 数据库报错mysqlexception: incorrect string value: ‘\xe7\xae\x80\xe4\xbd\x93…’ for column display name

    刚折腾了ABP框架,为了跨平台,将SQL Server数据库换成了MySQL数据库,ABP框架上支持多语言,中间被字符集折腾的够呛,翻了N个博客,最后终于在StackOverFlow 上找到了最终的解 ...

  3. php中const和define的区别

    define部分:宏不仅可以用来代替常数值,还可以用来代替表达式,甚至是代码段.(宏的功能很强大,但也容易出错,所以其利弊大小颇有争议.)宏的语法为:#define 宏名称 宏值作为一种建议和一种广大 ...

  4. shell字符串大小写转换

    1.typeset  有两个选项 -l 代表小写 -u 代表大写. 用法: typeset -u name name='asdasdas' echo $name   typeset -l ame am ...

  5. 72)MFC测试动态共享库

    动态共享库: 首先我建立一个新的动态库: 然后不选择空项目了,因为我们普通的cpp文件 入口是main  win32入口是winmain  那么这个动态库的入口在哪里  我们就是为了看一看: 出来这样 ...

  6. 吴裕雄--天生自然MySQL学习笔记:MySQL 复制表

    如果需要完全的复制MySQL的数据表,包括表的结构,索引,默认值等. 如果仅仅使用CREATE TABLE ... SELECT 命令,是无法实现的. 如何完整的复制MySQL数据表,步骤如下: 使用 ...

  7. 吴裕雄--天生自然MySQL学习笔记:MySQL 正则表达式

    下表中的正则模式可应用于 REGEXP 操作符中. 实例 查找name字段中以'st'为开头的所有数据: mysql> SELECT name FROM person_tbl WHERE nam ...

  8. java程序无法连接Rabbitmq

    java程序不能连接到rabbitmq,提示有包括501.403.402等错误. 各种调试都没用,后来是尝试新建一个用户,在配置文件中使用新用户连接,最后才使程序成功运行.

  9. javaweb02

    第一个web服务器程序:开发部署到Tomcat服务器下运行 1).在eclipse新建一个Javaproject2).在java项目下创建web开发的目录结构 -Webcontent -WEB-INF ...

  10. 运行roscore出现unable to contact my own server无法启动小海龟的部分故障问题解决

    运行roscore后,出现下图这种情况(unable to contact my own server) 原因是找不到http://后面那些,ping不到域名或IP. 参考http://www.ros ...