SPOJ DQUERY D-query(主席树 区间不同数个数)

题意:问你区间有几个不同的数

思路:主席树nb。我们知道主席树每一个root都存着一棵权值线段树,现在我们在每个root中存位置,也就是01表示这个位置存不存在。然后我们用一个fa[a[i]]表示a[i]这个数在前面出现的位置。如果没有在前面出现过,那么我们直接把这个位置变成1,;如果出现过了,我们就把当前root节点下的线段树的fa[a[i]]位置减掉。也就是说我每个数字存在的位置现在只出现当前区间的最右边的那个位置,那我直接每次询问L R区间个数,那么我直接遍历root[R]线段树的L R区间,看一下有几个位置就好了。

代码:

#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + ;
const int M = maxn * ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int a[maxn], root[maxn], tot;
int n, m;
struct node{
int lson, rson;
int sum;
}T[maxn * ];
int fa[maxn];
void init(){
tot = ;
memset(fa, -, sizeof(fa));
memset(T, , sizeof(T));
} void update(int l, int r, int &now, int pre, int pos, int v){
T[++tot] = T[pre], now = tot;
T[now].sum += v;
if(l == r) return;
int m = (l + r) >> ;
if(pos <= m)
update(l, m, T[now].lson, T[pre].lson, pos, v);
else
update(m + , r, T[now].rson, T[pre].rson, pos, v);
}
int query(int l, int r, int L, int R, int now){
if(L <= l && R >= r){
return T[now].sum;
}
int m = (l + r) >> , sum = ;
if(L <= m)
sum += query(l, m, L, R, T[now].lson);
if(R > m)
sum += query(m + , r, L, R, T[now].rson);
return sum;
}
int main(){
int ca = ;
init();
scanf("%d", &n);
int cnt = 1e6;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
if(fa[a[i]] == -){
update(, cnt, root[i], root[i - ], i, );
fa[a[i]] = i;
}
else{
update(, cnt, root[i], root[i - ], i, );
update(, cnt, root[i], root[i], fa[a[i]], -);
fa[a[i]] = i;
}
}
scanf("%d", &m);
while(m--){
int L, R;
scanf("%d%d", &L, &R);
printf("%d\n", query(, cnt, L, R, root[R]));
} return ;
}
上一篇:Oracle存储过程基本语法介绍


下一篇:Java线程并发中常见的锁