莫队算法和主席树算法
我认为这两个算法解决的问题相似,都是求区间内不同数的个数,或者是求区间内第k小,第k大的数。
DQUERY - D-query - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
P1972 [SDOI2009] HH的项链 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
主席树:(过不了P1972)
从右向左建立主席树,如果一个值还没出现过,则直接插入,否则删除后再重新插入。之后要查询 [l, r] 时,选用 l 位置的主席树,这时树中的数据是 [l, n] 范围内的,因此查询时需要传入 r 作为挡板,仅统计小于等于 r 的个数,这样就可以实现 [l, r] 区间不同数的查询。
#include<bits/stdc++.h>
typedef long long LL;
const int MAXX = 10000010;
int n, m;
int cnt, lst[MAXX], tp[MAXX], rot[MAXX];
struct PTree {
int l, r, sum;
}t[MAXX];
inline int read() {
int num = 0, f = 1; char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
while (isdigit(ch)) {num = num * 10 + ch - '0'; ch = getchar();}
return num * f;
}
void Build(int &u, int x, int l, int r) {
t[++cnt] = t[u];
u = cnt; ++ t[u].sum;
if (l == r) return ;
int mid = (l + r) >> 1;
if (x <= mid) Build(t[u].l, x, l, mid);
else Build(t[u].r, x, mid + 1, r);
}
int query(int i, int j, int q, int l, int r) {
if (r <= q) return t[j].sum - t[i].sum;
int mid = (l + r) >> 1, temp = query(t[i].l, t[j].l, q, l, mid);
if (mid < q) temp += query(t[i].r, t[j].r, q, mid + 1, r);
return temp;
}
int main() {
n = read(); int x, l, r;
for (int i = 1; i <= n; ++ i) {
x = read();
lst[i] = tp[x];
tp[x] = i;
}
rot[0] = 0;
for (int i = 1; i <= n; ++ i) {
rot[i] = rot[i - 1];
Build(rot[i], lst[i], 0, n);
}
m = read();
for (int i = 1; i <= m; ++ i) {
l = read(), r = read();
printf("%d\n", query(rot[l - 1], rot[r], l - 1, 0, n));
}
return 0;
}
莫队算法:
#include <bits/stdc++.h>
inline int read() {
int x = 0;
bool flag = true;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
flag = false;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
if(flag)
return x;
return ~(x - 1);
}
inline int lowbit(int x) {
return x & (-x);
}
const int maxn = (int)1e6 + 5;
const int maxq = (int)1e6 + 5;
const int maxv = (int)1e6 + 5;
int a[maxn], lst[maxv], c[maxn];
int n;
inline void add(int x, int v) {
for (; x <= n; x += lowbit(x))
c[x] += v;
return ;
}
inline int get(int x) {
int sum = 0;
for (; x; x -= lowbit(x))
sum += c[x];
return sum;
}
typedef std :: pair <int, int> pii;
std :: vector <pii> qst[maxn];
int ans[maxq];
int main() {
n = read();
for (int i = 1; i <= n; ++i)
a[i] = read();
std :: fill(lst + 1, lst + maxv - 3, n + 1);
int q = read();
for (int i = 1; i <= q; ++i) {
int l = read(), r = read();
qst[r].emplace_back(l, i);
}
for (int r = 1; r <= n; ++r) {
int x = a[r];
add(lst[x], -1);
add(r, 1);
lst[x] = r;
for (pii q : qst[r]) {
int l = q.first, id = q.second;
ans[id] = get(r) - get(l - 1);
}
}
for (int i = 1; i <= q; ++i)
printf("%d\n", ans[i]);
return 0;
}
树状数组:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 1000119
int num[maxn],tree[maxn],booll[maxn],nnn[maxn],N,ww;;
struct tt
{
int l,r;
int pos;
};
tt ask[maxn];
bool cmp(tt x,tt y)
{
return x.r<y.r;
}
int lowbit(int n)
{
return n&(-n);
}
void add(int n,int now)
{
while(n<=N)
{
tree[n]+=now;
n+=lowbit(n);
}
}
int sum(int n)
{
int ans=0;
while(n!=0)
{
ans+=tree[n];
n-=lowbit(n);
}
return ans;
}
int main()
{
scanf("%d",&N);
for(int i=1;i<=N;i++)
scanf("%d",&num[i]);
scanf("%d",&ww);
for(int i=1;i<=ww;i++)
{
scanf("%d%d",&ask[i].l,&ask[i].r);
ask[i].pos=i;
}
sort(ask+1,ask+1+ww,cmp);
int next=1;
for(int i=1;i<=ww;i++)
{
for(int j=next;j<=ask[i].r;j++)
{
if(booll[num[j]])
add(booll[num[j]],-1);
add(j,1);
booll[num[j]]=j;
}
next=ask[i].r+1;
nnn[ask[i].pos]=sum(ask[i].r)-sum(ask[i].l-1);
}
for(int i=1;i<=ww;i++)
cout<<nnn[i]<<endl;
return 0;
}