查询种类数,维护区间内仅有一个或两个此种元素
一般都考虑离线询问
将询问的区间按照右端点小在前排序
然后用
a
i
a_i
ai 出现的位置
p
r
e
[
a
i
]
pre[a_i]
pre[ai] 代替
a
i
a_i
ai,标志着一个种类,即使给定
a
i
<
=
n
a_i<=n
ai<=n 也不能用
a
i
a_i
ai 作为树状数组更新时的位置,因为们树状数组查询时一般是询问下标
i
i
i 之前的种类数有多少,因此必须用下标表示这个数
[SDOI2009]HH的项链
题意:查询区间元素种类个数
思路:
先将求区间元素种类数问题转化成求区间和问题
我们可以先将所有的询问离线下来,用
v
e
c
t
o
r
vector
vector 离线下来所有的查询,
v
[
i
]
v[i]
v[i] 存储所有右端点为
i
i
i 的查询区间
从左往右扫,枚举的
i
i
i 是区间右端点
开一个
p
r
e
pre
pre 数组,不断更新
1
−
i
1-i
1−i 的区间长度时要去除重复的元素
每出现一个新的花朵,就在位置
i
i
i 上 +1
如果出现重复的,就先把
p
r
e
[
a
[
i
]
]
pre[a[i]]
pre[a[i]] 上一个出现这朵花的位置,删去+1,然后再给当前位置 +1
遇到被查询的右端点时,计算答案即可
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const int maxn = 1e6 + 9;
ll n, m;
int a[maxn], c[maxn];
int ans[maxn], pre[maxn];
struct node
{
int l, id;
};// 固定r
// 在i位置上加k
void update(int i, int k){
while(i <= n) c[i] += k, i += i & (-i);
}
int getsum(int i, int ans = 0){
while(i){
ans += c[i], i -= i & (-i);
}return ans;
}
vector <node> v[maxn];
void work()
{
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
cin >> m;
for(int i = 1; i <= m; ++i){
int l, r;cin >> l >> r;
v[r].push_back({l, i});
}
for(int i = 1; i <= n; ++i){
if(pre[a[i]]) update(pre[a[i]], -1);
update(i, 1);
pre[a[i]] = i;
for(auto x : v[i]){
ans[x.id] = getsum(i) - getsum(x.l - 1);
}
}
for(int i = 1; i <= m; ++i) cout << ans[i] << endl;
}
int main()
{
ios::sync_with_stdio(0);
work();
return 0;
}
[HEOI2012采花
思路:
思路与上题一致
只需要多维护一个出现的位置即可
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const int maxn = 1e6 + 9;
ll n, m, k;
int a[maxn], c[maxn];
int ans[maxn], pre[maxn][2]; // pre[w][0]:上一个w出现的位置 pre[w][1]: 再上一个w出现的位置;
struct node
{
int l, id;
};// 固定r
// 在i位置上加k
void update(int i, int k){
while(i <= n) c[i] += k, i += i & (-i);
}
int getsum(int i, int ans = 0){
while(i){
ans += c[i], i -= i & (-i);
}return ans;
}
vector <node> v[maxn];
void work()
{
cin >> n >> k >> m;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= m; ++i){
int l, r;cin >> l >> r;
v[r].push_back({l, i});
}
for(int i = 1; i <= n; ++i){// 枚举右端点
if(pre[a[i]][1]) update(pre[a[i]][1], -1), update(pre[a[i]][0], 1);
else if(pre[a[i]][0]) update(pre[a[i]][0], 1);
pre[a[i]][1] = pre[a[i]][0];// 更新一下最后两个花色为i的花的位置
pre[a[i]][0] = i;
for(auto x : v[i]){
ans[x.id] = getsum(i) - getsum(x.l - 1);
}
}
for(int i = 1; i <= m; ++i) cout << ans[i] << endl;
}
int main()
{
ios::sync_with_stdio(0);
work();
return 0;
}
Different Integers
题意:
给定端点
l
,
r
l,r
l,r,查询区间
[
1
,
l
]
,
[
r
,
n
]
[1,l],[r,n]
[1,l],[r,n] 合并起来的区间种类数
思路:
考虑离线询问
对于每个答案我们这样求解:
总元素个数
−
-
− 仅出现在
[
1
,
r
−
1
]
[1,r-1]
[1,r−1] 范围的元素,删除同时加入树状数组中,然后就可以
g
e
t
s
u
m
getsum
getsum 查询
[
1
,
l
]
[1,l]
[1,l] 的元素个数,加上剩余的
c
n
t
cnt
cnt 的即为
a
n
s
ans
ans
首先求一下所有元素种类数
然后维护一下每个元素最后一次出现的位置来判断一个元素在后边是否还有
code:
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn = 1e5 + 9;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
ll n, m;
int a[maxn];
int c[maxn];
int pre[maxn], fir[maxn], cnt, ans[maxn];
struct node
{
int l, r, id;
bool operator<(const node &B)const{
return r < B.r;
}
}d[maxn];
void add(int i, int x){
while(i <= n) c[i] += x, i += i & (-i);
}
int getsum(int i, int ans = 0){
while(i) ans += c[i], i -= i & (-i); return ans;
}
void work()
{
for(int i = 1; i <= n; ++i) pre[i] = fir[i] = 0, c[i] = 0;
cnt = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
if(!fir[a[i]]) fir[a[i]] = i, ++cnt;
pre[a[i]] = i;
}
for(int i = 1; i <= m; ++i)
{
int l, r;scanf("%d %d", &d[i].l, &d[i].r);
d[i].id = i;
}
sort(d + 1, d + 1 + m);
int r = 1;// 扫描右端点的指针
for(int i = 1; i <= m; ++i)
{
while(r < d[i].r)
{
if(pre[a[r]] == r){
--cnt;
add(fir[a[r]], 1);
}
++r;
}
ans[d[i].id] = cnt + getsum(d[i].l);
}
for(int i = 1; i <= m; ++i)
cout << ans[i] << endl;
}
int main()
{
//ios::sync_with_stdio(0);
while(~scanf("%lld %lld", &n, &m))
work();
return 0;
}
求答案的代码稍微不同
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define eps 1e-6
using namespace std;
const int maxn = 1e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
int pre[maxn], lst[maxn], a[maxn], ans[maxn];
int cnt;
int c[maxn];
struct node{
int l, r, id;
bool operator<(const node &B){
return r < B.r;
}
}d[maxn];
void update(int i, int k){
while(i <= n) c[i] += k, i += i & (-i);
}
int qry(int i){
int ans = 0;while(i) ans += c[i], i -= i & (-i);
return ans;
}
void init(){
cnt = 0;
for(int i = 1; i <= n; ++i) pre[i] = lst[i] = c[i] = 0;
}
void work()
{
init();
for(int i = 1; i <= n; ++i){
cin >> a[i];
if(!pre[a[i]]) pre[a[i]] = i, ++cnt;
lst[a[i]] = i;
}
for(int i = 1; i <= m; ++i){
cin >> d[i].l >> d[i].r;
d[i].id = i;
}
sort(d + 1, d + 1 + m);
int j = 1;// 扫描询问数组的指针
for(int i = 1; i <= n; ++i){// 枚举右端点
while(i == d[j].r && j <= m){// 因为是 [1,r-1],所以先求答案再更新,还有就是这里是while,而不是if
ans[d[j].id] = cnt + qry(d[j].l);
++j;
}
if(lst[a[i]] == i){
update(pre[a[i]], 1);
--cnt;
}
}
for(int i = 1; i <= m; ++i) cout << ans[i] << endl;
}
int main()
{
ios::sync_with_stdio(0);
// int TT;cin>>TT;while(TT--)
while(cin >> n >> m)
work();
return 0;
}
Galahad
题意:
给定一个序列长度为
n
n
n,
q
q
q 个查询,每次询问区间
[
l
,
r
]
[l,r]
[l,r] 的和是多少,要求值相同的数仅加一次
思路:
离线询问
右端点小的在前排序
种类数我们每次更新成
1
1
1,
q
r
y
(
x
)
qry(x)
qry(x) 可以直接得到
[
1
,
x
]
[1,x]
[1,x] 的种类数
本题稍微不同就是要求和,而且每个数仅贡献一次,这就相当于把求种类数的
u
p
d
a
t
e
(
x
,
1
)
update(x,1)
update(x,1) 改成
u
p
d
a
t
e
(
x
,
a
i
)
update(x,a_i)
update(x,ai) 即可
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define eps 1e-6
using namespace std;
const int maxn = 5e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
int pre[maxn], a[maxn];
ll c[maxn], ans[maxn];
struct node{
int l, r, id;
bool operator<(const node &B){
return r < B.r;
}
}d[maxn];
void update(int i, int k){
while(i <= n) c[i] += k, i += i & (-i);
}
ll qry(int i){
ll ans = 0;while(i) ans += c[i], i -= i & (-i);
return ans;
}
void work()
{
cin >> n >> m;
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
for(int i = 1; i <= m; ++i){
cin >> d[i].l >> d[i].r;
d[i].id = i;
}
sort(d + 1, d + 1 + m);
int j = 1;
for(int i = 1; i <= n; ++i){
if(pre[a[i]]) update(pre[a[i]], -a[i]);
pre[a[i]] = i;update(i, a[i]);
while(i == d[j].r && j <= m){// 注意是while而不是if
ans[d[j].id] = qry(i) - qry(d[j].l - 1);
++j;
}
}
for(int i = 1; i <= m; ++i) cout << ans[i] << endl;
}
int main()
{
ios::sync_with_stdio(0);
// int TT;cin>>TT;while(TT--)
work();
return 0;
}