Different Integers
牛客一 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的更多相关文章
- [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 ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- 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 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- 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 ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- 解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译)
解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译) http://improve.dk/the-anatomy-of-row-amp-page-compre ...
随机推荐
- Arduino串口的一些高级用法
1.配置串口通信数据位.校验位.停止位通常我们使用Serial.begin(speed)来完成串口的初始化,这种方式,只能配置串口的波特率.而使用Serial.begin(speed, config) ...
- MySQL读写分离如何实现?
主要说下读写分离, 当我们的数据量很大时,数据库服务器的压力变大,这时候我们需要从架构方面来解决这一问题,在一个网站中读的操作很多,写的操作很少,这时候我们需要配置读写分离,把读操作和写操作分离出来, ...
- Map 查找表操作
package seday13; import java.util.HashMap; import java.util.Map; /** * @author xingsir * java.util.M ...
- XML文件读写编码不是UTF-8的问题
FileWriter和FileReader在写.读文件时,使用系统当前默认的编码方式. 在中文win下encoding基本是GB2312,在英文win下基本是ISO-8859-1.所以要创建一个UTF ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL UNION 操作符
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中.多个 SELECT 语句会删除重复的数据. 语法 MySQL UNION 操作符语法格式: SELECT ...
- 分享-QQ/微信/微博(环境搭建)
QQ环境搭建
- 机器学习分布式框架horovod安装 (Linux环境)
1.openmi 下载安装 下载连接: https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.1.tar.gz 安装命令 1 ...
- 解决Android Studio的安装问题
今天开始了android studio的下载与安装,我再官网上下载了Android studio,下载不难,运行出来可需要一定的时间,在中途中我遇到了一些问题 一:Build错误: 在我最开始下载完A ...
- Ansible-大保健
一.Ansible大纲 Ansible被红帽收购 1.什么是Ansible 2.Ansible特性\优点 3.Ansible基础架构 控制端\被控端\inventory\ad-hoc\playbook ...
- ServletContext实现网站计数器
在网站开发中,有很多功能需要使用ServletContext,比如: 1.网站计数器 2.网站在线用户的显示 3.简单的聊天系统 总之,如果是涉及到不用用户共享数据,而这些数据量不大,同时又不希望写入 ...