Different Integers
题目描述
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 a 1, 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.
备注
* 1 ≤ n, q ≤ 105
* 1 ≤ ai ≤ n
* 1 ≤ li, ri ≤ n
* The number of test cases does not exceed 10.
示例1:
输入
3 2
1 2 1
1 2
1 3
4 1
1 2 3 4
1 3
输出
2
1
3
题意
输入长度为n的数列,且数列中的元素大小小于n。求[0,li]U[ri,n)区间内不同元素的个数。
思路:运用树状数组+离散的方法求区间内不同数的个数
(如果还没有接触过树状数组的可以先看这两篇文章
树状数组简单易懂的详解 +【专题】树状数组+视频 SWPU-ACM每周算法讲堂-树状数组)
其中建树的函数 add 与 sum 原理是相通的
通过这个树状数组的图我们可以发现其子树的特点与 二进制 相关
奇数 2^n +1 和 2^n 是属于 2^(n+1) 的所属子树
比如 2 3 属于 4 的子树
c[2^n] 的子树范围为 【2^(n-1), 2^n -1】
比如 C【8】的直系子树有 C【4】c[6],c[7];
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxnn=100010;
const int maxn=2*maxnn;
struct ql//区间查询结构体
{
int l,r,pos;//区间[l,r] 第几个问题(用来顺序输出)
} lr[maxn];
int loc[maxnn],ans[maxnn],n;//loc[] 记录a[i]值 出现的下标位置 ans[]记录问题的答案
int a[maxn],c[maxn],fir[maxn],Next[maxn];//fir用来 记录出现一次的个数,next用来记录上一次出现的位置
//a[] 原数组 c[] 树状数组
bool cmp(ql p, ql q) // 根据左端点p.l 排序
{
return p.l <q.l ;
}
int lowbit(int x) // 树状数组处理
{
return x & -x;
}
void add(int x, int v) // 单点更新//更新位置 位置价值
{
while (x < 2*n)//x的影响子树范围
{
c[x] += v;
x += lowbit(x);
}
}
int sum(int x) // 树状数组处理
{
int s = 0;
while (x)
{
s += c[x];//加上x的所属子树范围的数
x -= lowbit(x);//sum与add中的子树相对应
}
return s;
}
int main()
{
int q;
while(scanf("%d%d",&n,&q)!=EOF)
{
memset(a, 0, sizeof(a));
memset(loc, 0, sizeof(loc));
memset(Next, 0, sizeof(Next));
memset(ans, 0, sizeof(ans));
memset(c, 0, sizeof(c));//初始化变量,但fir数组需要初始化为1.
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
a[i+n]=a[i];//加倍数组,使两个区间变为一个区间
fir[i]=1;
fir[i+n]=1;//fir数组初始化为1
}
for(int i=2*n; i>0; i--)
{
fir[loc[a[i]]]=0;//i从2*n-->1 当出现两个相同的数字时 fir归0//如果之前a[i]记录过 后面 fir[i] 则为0
Next[i]=loc[a[i]];//因为i从2*n-->1 所以当出现两个以上相同数字时,记录后一个出现的位置
loc[a[i]]=i;
}
// 如果不懂可以输出 则明白
// puts("AA");
// for(int i=1;i<=2*n;i++)
// printf("%d\t",fir[i]);puts("BB");
// for(int i=1;i<=2*n;i++)
// printf("%d\t",Next[i]);
for (int i = 1; i <= 2 * n; i++)
{
if (fir[i])
add(i, 1);//当fir=1时,加入树状数组
}
for (int i = 1; i <= q; i++)//输入区间,使其变为一个区间,并对区间进行排序
{
scanf("%d %d", &lr[i].l, &lr[i].r);
lr[i].l += n;
int temp;//求区间 [1,L]+[R,n] 转换为求区间 [R,L+n] 中不同的数
temp=lr[i].l;
lr[i].l =lr[i].r;
lr[i].r=temp;//取一个区间
lr[i].pos = i;//记录点
}
sort(lr+1,lr+q+1,cmp);//数组排序
int k = 1;//从左开始往右到x
for (int i = 1; i <= 2 * n && k <= q;)//遍历所有子树
{
if (i == lr[k].l)
{
ans[lr[k].pos] = sum(lr[k].r) - sum(lr[k].l) + 1;//树状数组从r到l
k++;//查询下一个
}
else
{
if (fir[i]) //在1-x区间内去除fir的值,同时将fir的值归到下一个相同值的位置
{
fir[i] = 0;
add(i, -1);// 树状数组 i子树 去除1
fir[Next[i]] = 1;//将fir的值归到下一个相同值的位置
add(Next[i], 1);//树状数组 下一个相同值的子树位置 加1
}
i++;//到下一棵子树搜索
}
}
for (int i = 1; i <= q; i++)
printf("%d\n", ans[i]);//输出从l到r的树状数组之和
}
}