洛谷P1972:[SDOI2009]HH的项链(莫队/线段树)

题目传送门:https://www.luogu.org/problem/show?pid=1972

分析:本题有很多种做法,有O(n*log(n))的线段树,也有O(n*sqrt(n))的莫队。线段树的做法:

http://blog.csdn.net/kscla/article/details/70227098

下面贴一下莫队的代码(其实就是个暴力,注意每一次要先让R指针右移,再移动L指针,不然可能会出现L>R的情况,然后WA):

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn=50010;
const int maxm=200100;
const int maxv=1000010;

struct data
{
	int L,R,t;
} ask[maxm];

int a[maxn];
int num[maxv];
int temp[maxm];

int n,m;
int sn;

int Get(int x)
{
	if (!x) return 0;
	return (x-1)/sn+1;
}

bool Comp(data x,data y)
{
	int dx=Get(x.L);
	int dy=Get(y.L);
	return ( dxask[i].L)
		{
			nL--;
			if (!num[ a[nL] ]) ans++;
			num[ a[nL] ]++;
		}
		
		while (nL

你可能感兴趣的:(莫队,可持久化数据结构)