题意
给出n个数,m个询问,每个询问给出一个区间,需要回答这个区间中不同的数的个数
题目
{assign var=“code” value=“DQUERY”} {if KaTeX parse error: Expected 'EOF', got '}' at position 8: par==""}̲ {assign var=pa…locale} {/if}
English Vietnamese
{if $par==“vn”} {literal}
Truy vấn-d
Cho một dãy số n phần tử a1, a2, …, an và một số các truy vấn-d. Một truy vấn-d là một cặp (i, j) (1 ≤ i ≤ j ≤ n). Với mỗi truy vấn-d (i, j), bạn cần trả về số phần tử phân biệt nằm trong dãy con ai, ai+1, …, aj.
Dữ liệu
Dòng 1: n (1 ≤ n ≤ 30000).
Dòng 2: n số a1, a2, …, an (1 ≤ ai ≤ 106).
Dòng 3: q (1 ≤ q ≤ 200000), số lượng truy vấn- d.
Trong q dòng sau, mỗi dòng chứa 2 số i, j biểu thị một truy vấn-d (1 ≤ i ≤ j ≤ n).
Kết quả
Với mỗi truy vấn-d (i, j), in ra số phần tử phân biệt thuộc dãy con ai, ai+1, …, aj trên một dòng.
Ví dụ
Dữ liệu
5
1 1 2 1 3
3
1 5
2 4
3 5
Kết quả
3
2
3
{/literal}{elseif ($par==“en” || $par=="")}{literal}
Given a sequence of n numbers a1, a2, …, an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, …, aj.
Input
Line 1: n (1 ≤ n ≤ 30000).
Line 2: n numbers a1, a2, …, an (1 ≤ ai ≤ 106).
Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).
Output
For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, …, aj in a single line.
Example
Input
5
1 1 2 1 3
3
1 5
2 4
3 5
Output
3
2
3
{/literal} {/if}
分析
分析:主席树的经典运用(注意不是权值线段树而是位置线段树),将主席树看作拥有n个历史版本的线段树, 每个线段树表示[1,n]的区间,
节点权值为建造该线段树为止该区间的贡献。
当拿到一个新的元素时,如果这个元素没被插入过,我们就直接插入。反之则删除这个数再插入
一次这个数。也就是说,我们既能保证序列中有相同元素时,先插入位置靠后的(前面的删掉再
插入相当于把插入位置向后挪),又能保证每一个元素在线段树中只有一个。
AC代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int M=3e4+10;
const int N=1e6+10;
int book[N],root[M];///每个数列的每个位置作为叶子节点
struct node
{int l,r,sum;
} s[M*40];
int tot,n,m,t,tmp,l,r;
void build(int l,int r,int &x)
{int now=++tot;s[now].sum=0;s[now].l=s[now].r=0;if(l==r)return ;int mid=(l+r)>>1;build(l,mid,s[now].l);build(mid+1,r,s[now].r);
}
void update(int l,int r,int &x,int y,int pos,int v)
{s[++tot]=s[y];s[tot].sum+=v;//这样对于查询区间[l,r]首先需要找到第r个线段树,该树记录的是[1,r]中各区间的贡献x=tot;if(l==r)return ;int mid=(l+r)>>1;if(pos<=mid)///对于相同的数,先更新最左边的位置update(l,mid,s[x].l,s[y].l,pos,v);elseupdate(mid+1,r,s[x].r,s[y].r,pos,v);
}
int query(int pos,int x,int l,int r)
{if(l==r)return s[x].sum;int mid=(l+r)>>1;if(pos<=mid)return s[s[x].r].sum+query(pos,s[x].l,l,mid);elsereturn query(pos,s[x].r,mid+1,r);
}
int main()
{tot=-1;scanf("%d",&n);build(1,n,root[0]);for(int i=1; i<=n; i++)///对于构造第i个线段树{scanf("%d",&m);if(!book[m])update(1,n,root[i],root[i-1],i,1);///如果book[m]的值没有出现,则只将这次出现的位置权值+1, elseelse{update(1,n,tmp,root[i-1],book[m],-1);///对于构造第i个线段树,如果a[i]的值已经出现过了,就将上一个出现的位置权值-1update(1,n,root[i],tmp,i,1);///再将这次出现的位置权值+1}book[m]=i;///相同的数产生的贡献,只记录在最末尾的为止上,这样就不会重复。}scanf("%d",&t);while(t--){scanf("%d%d",&l,&r);printf("%d\n",query(l,root[r],1,n));///这样我们在查询区间[l,r]时,只需要查询一下第r棵线段树中[l,r]的区间和即可。}return 0;
}