SPOJ DQUERY - D-query (树状数组求区间种类数)
作者:互联网
https://www.spoj.com/problems/DQUERY/
求区间种类数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1000000;
const int MAXM = 30010;
struct Q{
int l, r, id;
}ask[MAXN];
int c[MAXN + 10], a[MAXM], last[MAXN + 10];
void add(int p, int val){
for(int i = p; i <= MAXN; i += i & (-i))
c[i] += val;
}
int sum(int p){
int ans = 0;
for(int i = p; i >= 1; i -= i & (-i))
ans += c[i];
return ans;
}
int ans[MAXN];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
cin >> q;
for(int i = 0; i < q; ++i)
cin >> ask[i].l >> ask[i].r, ask[i].id = i;
sort(ask, ask + q, [](Q &x, Q &y)->bool{return x.r < y.r;});
int now = 0;
for(int i = 1; i <= n; ++i){
if(last[a[i]]) add(last[a[i]], -1);
add(i, 1);
last[a[i]] = i;
while(ask[now].r == i){
ans[ask[now].id] = sum(i) - sum(ask[now].l - 1);
++now;
}
}
for(int i = 0; i < q; ++i) cout << ans[i] << '\n';
return 0;
}
标签:int,cin,DQUERY,SPOJ,MAXN,ans,ask,query,id 来源: https://www.cnblogs.com/Zeronera/p/14471757.html