P4113 [HEOI2012]采花 ( 树状数组 + 离线 )

这篇博客介绍了如何使用树状数组(也称为 Fenwick Tree)解决区间内不同颜色数量的计数问题。文章通过分析HH的项链问题,提出了一种优化策略,即只关注每个数在区间最右边的第二个出现。通过对区间按右边界排序,维护last1和last2数组以跟踪每个颜色的出现位置,并使用树状数组更新和查询前缀和,从而高效地计算出区间内的不同颜色数量。这种方法适用于处理多个区间查询,具有较高的时间效率。
摘要由CSDN通过智能技术生成
题目链接:点击进入
题目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

思路

跟此题相似:HH的项链
对于若干个询问的区间 [ l , r ] ,如果他们的 r 都相等的话,那么对于数组中出现的同一个数字,因为要求只有每个数出现至少两次才可以计入答案,那么我们只需要关心出现在最右边的第二个的即可。( r 固定情况下,只需要看 l 与这第二个的大小关系来判断这个数可不可以计入答案 )
对 [ 1 , r ] 保留所有数离 r 第二近的那个,剩下的扔掉。那么此时求前缀和 sum , sum [ r ] 就是 [ 1 , r ] 的不同颜色的数量,那么询问区间 [ l, r ] 答案就是 sum [ r ] - sum [ l - 1 ] 。
因此,我们可以对所有查询的区间按照 r 来排序,然后再来维护一个树状数组,同时维护一个 last1 数组与 last2 数组,分别记录颜色 x 最近第一次出现的位置与最近第二次出现的位置。我们每次修改将上一次的第二个最近位置-1,然后将新的最近第二次位置的+1。
位置加入就是 add ( i ,1 ) ,位置删除就是 add ( i , -1 ) ;
思路借鉴于:wangjyqh

代码
// Problem: P4113 [HEOI2012]采花
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4113
// Memory Limit: 500 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize(3)//O3
//#pragma GCC optimize(2)//O2
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131 
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define lowbit(x) x & -x
#define inf 0x3f3f3f3f
//#define int long long
//#define double long double
//#define rep(i,x,y) for(register int i = x; i <= y;++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const int maxn=2e6+10;
const int mod=1e9+7;
const double eps=1e-9;
const int N=5e3+10;
/*--------------------------------------------*/
inline int read()
{
    int k = 0, f = 1 ;
    char c = getchar() ;
    while(!isdigit(c)){if(c == '-') f = -1 ;c = getchar() ;}
    while(isdigit(c)) k = (k << 1) + (k << 3) + c - 48 ,c = getchar() ;
    return k * f ;
}
/*--------------------------------------------*/

int n,m,limit,c[maxn],a[maxn];
int last1[maxn],last2[maxn],ans[maxn];
struct node
{
	int l;
	int r;
	int pos;
	bool operator <(const node &t)const
	{
		return r<t.r;
	}
}p[maxn];
void add(int x,int val)
{
	for(int i=x;i<=n;i+=lowbit(i)) c[i]+=val;
}
int getsum(int x)
{
	int sum=0;
	for(int i=x;i>=1;i-=lowbit(i)) sum+=c[i];
	return sum;
}

int main() 
{
	// ios::sync_with_stdio(false);
	// cin.tie(0);cout.tie(0);
	scanf("%d%d%d",&n,&limit,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&p[i].l,&p[i].r);
		p[i].pos=i;
	}
	sort(p+1,p+m+1);
	int j=1;
	for(int i=1;i<=n;i++)
	{
		if(!last1[a[i]]) last1[a[i]]=i;
		else
		{
			if(!last2[a[i]])
			{
				last2[a[i]]=last1[a[i]];
				last1[a[i]]=i;
				add(last2[a[i]],1);
			}
			else
			{
				add(last2[a[i]],-1);
				last2[a[i]]=last1[a[i]];
				last1[a[i]]=i;
				add(last2[a[i]],1);
			}
		}	
		while(j<=m&&p[j].r==i)
		{
			ans[p[j].pos]=getsum(p[j].r)-getsum(p[j].l-1);
			j++;
		}
	}
	for(int i=1;i<=m;i++)
		printf("%d\n",ans[i]);
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值