給定陣列A[N],有Q個詢問,每個詢問給出l[i]和r[i],問區間[l[i],r[i]]內有多少個不同的數?
1<=N,Q<=5E5; 1<=A[i]<=N; 1<=l[i]<=r[i]<=N
分析:對詢問按右端點從小到大排序,然後從左到右依次處理每個A[i],將下標i的位置置為1,如果前面出現過A[i],則把上一次出現的位置置為0,然後處理右端點為i的詢問,其實就是個字首和問題,可以用BIT來維護,這裡用的平衡樹。
#include <bits/stdc++.h>
using i64 = long long;
template <typename TYPE>
struct Treap {
struct Node {
TYPE data;
int rnd, siz, dup, son[2];
Node() { memset(&data, 0, sizeof(data)); rnd = siz = son[0] = son[1] = 0; }
Node(const TYPE &d, int cnt=1) { init(d, cnt); }
void init(const TYPE & d, int cnt) { data = d; dup = siz = cnt; rnd = rand(); son[0] = son[1] = 0; }
};
Treap(int multi=1):multiple(multi) { reset(); }
void setmulti(int multi) { multiple = multi; }
int newnode(const TYPE & d, int cnt) {
if (!reuse.empty()) {
int r = reuse.front();
reuse.pop_front();
node[r].init(d, cnt);
return r;
}
node.push_back({d, cnt});
return node.size() - 1;
}
void reset() { root = 0; node.resize(1); reuse.clear(); }
void maintain(int x) {
node[x].siz = node[x].dup;
if (node[x].son[0]) {
node[x].siz += node[node[x].son[0]].siz;
}
if (node[x].son[1]) {
node[x].siz += node[node[x].son[1]].siz;
}
}
void rotate(int d, int &r) {
int k = node[r].son[d^1];
node[r].son[d^1] = node[k].son[d];
node[k].son[d] = r;
maintain(r);
maintain(k);
r = k;
}
void insert(const TYPE &data, int cnt, int &r) {
if (r) {
if (!(data < node[r].data) && !(node[r].data < data)) {
if (multiple) {
node[r].dup += cnt;
maintain(r);
}
} else {
int d = data < node[r].data ? 0 : 1;
// avoid push_back -> realloc
int u = node[r].son[d];
insert(data, cnt, u);
node[r].son[d] = u;
if (node[node[r].son[d]].rnd > node[r].rnd) {
rotate(d^1, r);
} else {
maintain(r);
}
}
} else {
r = newnode(data, cnt);
}
}
TYPE kth(int k) {
assert(1 <= k && k <= size());
for (int r = root; r; ) {
int x = node[r].son[0] ? node[node[r].son[0]].siz : 0;
int y = node[r].dup;
if (k <= x) {
r = node[r].son[0];
} else if (k <= x + y) {
return node[r].data;
} else {
k -= x + y;
r = node[r].son[1];
}
}
assert(k == 0);
return 0;
}
TYPE Kth(int k) {
assert(1 <= k && k <= size());
for (int r = root; r; ) {
int x = node[r].son[1] ? node[node[r].son[1]].siz : 0;
int y = node[r].dup;
if (k <= x) {
r = node[r].son[1];
} else if (k <= x + y) {
return node[r].data;
} else {
k -= x + y;
r = node[r].son[0];
}
}
assert(k == 0);
return 0;
}
void erase(const TYPE& data, int cnt, int & r) {
if (r == 0) return;
int d = -1;
if (data < node[r].data) {
d = 0;
} else if (node[r].data < data) {
d = 1;
}
if (d == -1) {
node[r].dup -= cnt;
if (node[r].dup > 0) {
maintain(r);
} else {
if (node[r].son[0] == 0) {
reuse.push_back(r);
r = node[r].son[1];
} else if (node[r].son[1] == 0) {
reuse.push_back(r);
r = node[r].son[0];
} else {
int dd = node[node[r].son[0]].rnd > node[node[r].son[1]].rnd ? 1 : 0;
rotate(dd, r);
erase(data, cnt, node[r].son[dd]);
}
}
} else {
erase(data, cnt, node[r].son[d]);
}
if (r) maintain(r);
}
int ltcnt(const TYPE &data) {
int ans = 0;
for (int r = root; r; ) {
int x = node[r].son[0] ? node[node[r].son[0]].siz : 0;
if (node[r].data < data) {
ans += node[r].dup + x;
r = node[r].son[1];
} else if (data < node[r].data) {
r = node[r].son[0];
} else {
ans += x;
break;
}
}
return ans;
}
int gtcnt(const TYPE &data) {
int ans = 0;
for (int r = root; r; ) {
int x = node[r].son[1] ? node[node[r].son[1]].siz : 0;
if (node[r].data < data) {
r = node[r].son[1];
} else if (data < node[r].data) {
ans += node[r].dup + x;
r = node[r].son[0];
} else {
ans += x;
break;
}
}
return ans;
}
int count(const TYPE &data) {
for (int r = root; r; ) {
if (data < node[r].data) {
r = node[r].son[0];
} else if (node[r].data < data) {
r = node[r].son[1];
} else {
return node[r].dup;
}
}
return 0;
}
std::pair<bool,TYPE> prev(const TYPE &data) {
std::pair<bool,TYPE> ans = {false, 0};
for (int r = root; r; ) {
if (node[r].data < data) {
if (ans.first) {
ans.second = std::max(ans.second, node[r].data);
} else {
ans = {true, node[r].data};
}
r = node[r].son[1];
} else {
r = node[r].son[0];
}
}
return ans;
}
std::pair<bool,TYPE> next(const TYPE &data) {
std::pair<bool,TYPE> ans = {false, 0};
for (int r = root; r; ) {
if (data < node[r].data) {
if (ans.first) {
ans.second = std::min(ans.second, node[r].data);
} else {
ans = {true, node[r].data};
}
r = node[r].son[0];
} else {
r = node[r].son[1];
}
}
return ans;
}
void dofetch(int r, std::vector<std::pair<TYPE,int>> &vec) const {
if (r) {
dofetch(node[r].son[0], vec);
vec.push_back({node[r].data, node[r].dup});
dofetch(node[r].son[1], vec);
}
}
void fetch(std::vector<std::pair<TYPE,int>> &vec) const {
dofetch(root, vec);
}
void merge(const Treap<TYPE> &rhs) {
std::vector<std::pair<TYPE,int>> vp;
rhs.fetch(vp);
for (auto &pr : vp) {
insert(pr.first, pr.second);
}
}
std::vector<Node> node;
std::deque<int> reuse;
int root, multiple;
void insert(const TYPE& data, int cnt=1) { insert(data, cnt, root); }
void erase(const TYPE& data, int cnt=1) { erase(data, cnt, root); }
int size() const { return root ? node[root].siz : 0; }
int lecnt(const TYPE& data) { return size() - gtcnt(data); }
int gecnt(const TYPE& data) { return size() - ltcnt(data); }
};
struct Node {
int l, r, x, ans;
};
void solve() {
int N, Q;
std::cin >> N >> Q;
std::vector<int> c(N + 1);
for (int i = 1; i <= N; i++) {
std::cin >> c[i];
}
std::vector<Node> qry(Q + 1);
for (int i = 1; i <= Q; i++) {
std::cin >> qry[i].l >> qry[i].r;
qry[i].x = i;
}
std::sort(qry.begin() + 1, qry.end(), [](Node &u, Node &v){
return u.r < v.r;
});
std::vector<int> lst(N + 1);
Treap<int> tr;
for (int i = 1, j = 1; i <= N; i++) {
if (lst[c[i]]) {
tr.erase(lst[c[i]]);
}
tr.insert(i);
while (j <= Q && qry[j].r <= i) {
qry[j].ans = tr.lecnt(qry[j].r) - tr.ltcnt(qry[j].l);
j += 1;
}
lst[c[i]] = i;
}
std::sort(qry.begin() + 1, qry.end(), [](Node &u, Node &v){
return u.x < v.x;
});
for (int i = 1; i <= Q; i++) {
std::cout << qry[i].ans << "\n";
}
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
std::cout << std::fixed << std::setprecision(10);
int t = 1;
// std::cin >> t;
while (t--) solve();
return 0;
}
本題也有線上做法,需要用到可持久化線段樹。