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 ...
随机推荐
- 洛谷 P2719 搞笑世界杯
题目传送门 解题思路: f[i][j]表示买i张A票,j张B票的概率. AC代码: #include<iostream> #include<cstdio> using name ...
- C语言之结构体概述
C语言之结构体概述1.结构体类型是一种自定义类型(1)C语言中有2种类型:原生类型和自定义类型.2.结构体使用时先定义结构体类型再用类型定义变量(1)结构体定义时需要先定义结构体类型,再用类型来定义变 ...
- 82.常用的返回QuerySet对象的方法使用详解:all,select_related
1. all: 返回这个ORM模型的QuerySet对象. articles = Article.objects.all() print(articles) 2.select_related: 查找数 ...
- 循环(while,break,continue),转义字符
01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 -- 从上向下,顺序执行代码 分支 -- 根据条件判断,决定执行代码的 分支 循环 -- 让 特定代码 重复 执行 02. while ...
- unable to execute /bin/mv: Argument list too long
四种解决”Argument list too long”参数列表过长的办法 转自 http://hi.baidu.com/cpuramdisk/item/5aa49ce00c0757aecf2d4f2 ...
- re模块3
#分组 () print(re.findall("(ad)/(vv)","adddad/vvdddddddddd")) print(re.findall(&qu ...
- MySQL优化查询相关
[查询优化相关] 1.如何定位相关慢的查询: a.可以开启慢查询日志,也可以使用show profiles 去记录相关查询到一个临时表再分析. b.show processlist 看看有没有大量等 ...
- Android aar同步Failed to resolve: :nuisdk:
在app.gradle中android.dependencies同一级别下加入: repositories { flatDir { dirs 'libs' } }
- Hadoop的常用指令
-help:查看帮助 hadoop fs -help rm -rm [-f] [-r|-R] [-skipTrash] <src> ... : Delete all files that ...
- 条款02:尽量以const,enum,inline替换#define
目录 1. 总结 2. 使用const常量或enum替换宏常量 class外部的常量指针 class专属常量 1. 总结 对于单纯常量,最好以const常量或enum替换#define 对于宏代码段, ...