莫队算法模板

形如hdu 5381:点击打开链接

使用莫队需要满足的条件:

1、询问之间必须互不影响,是独立的。

2、询问必须是一个区间,不能有别的因子,也就是必须形如 l, r。

莫队是干什么的:

把询问: [1,3] [2,2] [3,5] [2,4] 排序,排序的结果就是得到一棵树,树上的每个节点对应着一个询问。

维护一个区间,然后dfs这棵树,用这个区间在树上移动,通过给区间增加(删除)一段区间使得旧区间变成当前访问的区间。

我需要做什么:

完成两个函数:增加一个区间: add(int l, int r){},删除一个区间: del(int l, inr){}

注意:

1、如果当前区间是空的,即l > r, 那么新增加的点需要特殊处理。

2、add(int l, int r), del的函数复杂度必须为 O(r-l)

3、莫队模板的复杂度:O(query*log(query))


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>  
#include <iostream>  
#include <algorithm>  
#include <sstream>  
#include <stdlib.h>  
#include <string.h>  
#include <limits.h>  
#include <vector>  
#include <string>  
#include <time.h>  
#include <math.h>  
#include <iomanip>  
#include <queue>  
#include <stack>  
#include <set>  
#include <map>  
const int inf = 1e9;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x < 0) { putchar('-'); x = -x; }
	if (x > 9) pt(x / 10);
	putchar(x % 10 + '0');
}
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
typedef pair<int, int> pii;
template <class T>
inline T gcd(T a, T b) {
	if (a > b)swap(a, b);
	while (a)b %= a, swap(a, b);return b;
}
vector<int>G[N];
int l[N], r[N];
class MST {
	struct Edge {
		int from, to, dis;
		Edge(int _from = 0, int _to = 0, int _dis = 0) :from(_from), to(_to), dis(_dis) {}
		bool operator < (const Edge &x) const { return dis < x.dis; }
	}edge[N << 3];
	int f[N], tot;
	int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
	bool Union(int x, int y) {
		x = find(x); y = find(y);
		if (x == y)return false;
		if (x > y)swap(x, y);
		f[x] = y;
		return true;
	}
public:
	void init(int n) {
		for (int i = 0; i <= n; i++)f[i] = i;
		tot = 0;
	}
	void add(int u, int v, int dis) {
		edge[tot++] = Edge(u, v, dis);
	}
	ll work() {//计算最小生成树,返回花费  
		sort(edge, edge + tot);
		ll cost = 0;
		for (int i = 0; i < tot; i++)
			if (Union(edge[i].from, edge[i].to)) {
				cost += edge[i].dis;
				G[edge[i].from].push_back(edge[i].to);
				G[edge[i].to].push_back(edge[i].from);
			}
		return cost;
	}
}mst;
struct Point {//二维平面的点  
	int x, y, id;
	bool operator < (const Point&a) const {
		return x == a.x ? y < a.y : x < a.x;
	}
}p[N];
bool cmp_id(const Point&a, const Point&b) {
	return a.id < b.id;
}
class BIT {//树状数组  
	int c[N], id[N], maxn;
	int lowbit(int x) { return x&-x; }
public:
	void init(int n) {
		maxn = n + 10;
		fill(c, c + maxn + 1, inf);
		fill(id, id + maxn + 1, -1);
	}
	void updata(int x, int val, int _id) {
		while (x) {
			if (val < c[x]) { c[x] = val; id[x] = _id; }
			x -= lowbit(x);
		}
	}
	int query(int x) {
		int val = inf, _id = -1;
		while (x <= maxn) {
			if (val > c[x]) { val = c[x]; _id = id[x]; }
			x += lowbit(x);
		}
		return _id;
	}
}tree;
inline bool cmp(int *x, int *y) { return *x < *y; }
class Manhattan_MST {
	int A[N], B[N];
public:
	ll work(int l, int r) {
		for (int i = l; i <= r; i++)G[i].clear();
		mst.init(r);
		for (int dir = 1; dir <= 4; dir++) {
			if (dir % 2 == 0)for (int i = l; i <= r; i++)swap(p[i].x, p[i].y);
			else if (dir == 3)for (int i = l; i <= r; i++)p[i].y = -p[i].y;
			sort(p + l, p + r + 1);
			for (int i = l; i <= r; i++) A[i] = B[i] = p[i].y - p[i].x; //离散化  
			sort(B + l, B + r + 1);
			int sz = unique(B + l, B + r + 1) - B;
			//初始化反树状数组  
			tree.init(sz);
			for (int i = r; i >= l; i--)
			{
				int pos = lower_bound(B + l, B + sz, A[i]) - B;
				int id = tree.query(pos);
				if (id != -1)
					mst.add(p[i].id, p[id].id, abs(p[i].x - p[id].x) + abs(p[i].y - p[id].y));
				tree.updata(pos, p[i].x + p[i].y, i);
			}
		}
		for (int i = l; i <= r; i++)p[i].y = -p[i].y;
		return mst.work();
	}
}m_mst;

int n, m, a[N];
ll ans[N], now;
void add(int x, int y) { 
	for (int i = x; i <= y; i++)
	{

	}
}
void del(int x, int y) {
	for (int i = x; i <= y; i++)
	{

	}
}
void dfs(int u, int fa) {
	if (fa != u)
		add(l[u], r[u]);
	else
	{
		if (l[u] < l[fa]) add(l[u], l[fa] - 1);
		if (r[u] > r[fa]) add(r[fa] + 1, r[u]);
		if (l[u] > l[fa]) del(l[fa], l[u] - 1);
		if (r[u] < r[fa]) del(r[u] + 1, r[fa]);
	}
	ans[u] = now;
	for (int v : G[u]) if (v != fa)dfs(v, u);
	if (fa != u)
	{
		if (l[u] > l[fa]) add(l[fa], l[u] - 1);
		if (r[u] < r[fa]) add(r[u] + 1, r[fa]);
		if (l[u] < l[fa]) del(l[u], l[fa] - 1);
		if (r[u] > r[fa]) del(r[fa] + 1, r[u]);
	}
}
int main() {
	rd(n);
	for (int i = 1; i <= n; i++)rd(a[i]);
	rd(m);
	for (int i = 1; i <= m; i++) {
		rd(p[i].x); rd(p[i].y); p[i].id = i;
		l[i] = p[i].x; r[i] = p[i].y;
	}
	m_mst.work(1, m);
	dfs(1, 1);
	for (int i = 1; i <= m; i++)pt(ans[i]), puts("");
	return 0;
}


你可能感兴趣的:(莫队算法模板)