链接 点击跳转
题解 【Different Integers】总的不同数字个数减去只在 ( l , r ) (l,r) (l,r)出现的数字种类,就是答案
代码
#include
#include
#include
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 100010
#define maxe 100010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;
i<=b;
i++)
#define drep(i,a,b) for(i=a;
i>=b;
i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "< pii;
typedef pair pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();
!isdigit(c);
c=getchar())if(c=='-')f=-f;
for(;
isdigit(c);
c=getchar())x=x*10+c-0x30;
return f*x;
}
struct BIT
{
ll bit[maxn], n;
void init(int N){n=N;
for(int i=1;
i<=n;
i++)bit[i]=0;
}
ll lowbit(ll x){return x&(-x);
}
void add(ll pos, ll v)
{
for(;
pos<=n;
pos+=lowbit(pos))bit[pos]+=v;
}
ll sum(ll pos)
{
ll ans(0);
for(;
pos;
pos-=lowbit(pos))ans+=bit[pos];
return ans;
}
}bit;
ll n, q, fst[maxn], lst[maxn], a[maxn], l[maxn], r[maxn], id[maxn], ans[maxn];
int main()
{
ll i;
while(~scanf("%lld%lld",&n,&q))
{
rep(i,1,n)a[i]=read();
rep(i,1,n)fst[i]=lst[i]=0;
rep(i,1,n)lst[a[i]]=i;
drep(i,n,1)fst[a[i]]=i;
bit.init(n);
vector v;
rep(i,1,n)if(fst[a[i]]==i)v.emb(pll(i,lst[a[i]]));
sort(v.begin(),v.end(),[](pll p1, pll p2){return p1>p2;
});
rep(i,1,q)l[i]=read(), r[i]=read(), id[i]=i;
sort(id+1,id+q+1,[](ll x, ll y){return l[x]>l[y];
});
auto it = v.begin();
rep(i,1,q)
{
while(it!=v.end() and it->first>l[id[i]])
{
bit.add(it->second,+1);
it++;
}
ans[id[i]] = bit.sum(r[id[i]]-1);
}
rep(i,1,q)printf("%lld\n",v.size()-ans[i]);
}
return 0;
}
推荐阅读
- 并查集|[CF938G] Shortest Path Queries
- 倍增|191026考试题解
- 圆方树|uoj#189. 【集训队互测2016】火车司机出秦川 //圆方树
- 树状数组|【HDU6635 Nonsense Time】树状数组维护最长上升子序列
- 51nod 1376 最长递增子序列的数量 树状数组