洛谷P4867 Gty的二逼妹子序列 莫隊套數據結構


洛谷P4867 Gty的二逼妹子序列


標籤

  • 莫隊套數據結構

前言

  • 我的csdn和博客園是同步的,歡迎來訪danzh-博客園~
  • 聽說莫隊和分塊更配哦~

簡明題意

  • 給定序列,需要多次詢問[l,r]區間中權值在[a,b]範圍內的種數。

思路

  • 權值種數很自然想到莫隊。然鵝,再加一個限制條件,權值位於[a,b]的種數。這樣一來,其實很容易想到權值線段樹。仍然是用莫隊維護,轉移的時候,只需要在線段樹中單點修改爲1.然後轉移完了後(轉移完了是指莫隊的四個while進行完),這顆權值線段樹維護的就是[l,r]中所有的數是否出現,所以這時候直接查詢[a,b]就好了。然鵝這樣會T兩個點,無奈只能優化。
  • 我們先不考慮別的,只考慮莫隊的複雜度。假設有n組詢問,那麼莫隊的轉移複雜度是O(nn)O(n\sqrt{n}),而查詢的複雜度是O(n)O(n)的。然後,這裏需要用數據結構維護莫隊的轉移,查詢時也需要用那個數據結構查詢。假設我們用線段樹維護莫隊,那麼轉移和查詢的複雜度就分別成爲了O(nlognn)O(nlogn\sqrt{n}),O(nlogn)O(nlogn)
  • 這裏發現O(nn)O(n\sqrt{n})是遠遠大於O(nlogn)O(nlogn)的。如果用線段樹,兩者都乘上log,他倆的複雜度仍然沒有得到均攤。因此,我們是要用一種數據結構,讓轉移的複雜度降低,而查詢的複雜度可以適當增高。沒懂的話下面從另一個角度解釋了一番
  • 注意到我們需要的數據結構是要支持單點修改,區間查詢的。而莫隊轉移時,需要乘以單點修改的複雜度,查詢是需要乘以區間查詢的複雜度。所以我們寫一個單點修改複雜度低,區間查詢複雜度適當高的數據結構。顯然,分塊!
  • 對於單點修改,區間查詢,分塊可以很容易做到O(1)O(1)修改,O(n)O(\sqrt{n})查詢,然後莫隊的轉移:O(nn)O(n\sqrt{n}),莫隊的查詢O(nn)O(n\sqrt{n}),然後就得到均攤啦,就能開心的AC了。

注意事項

  • 詢問比序列的長度n大的時候,注意不要把query也開成maxn了!!!!

總結

  • 詢問比序列的長度n大的時候,注意不要把query也開成maxn了!!!!
  • 莫隊總的轉移的複雜度是O(nn)O(n\sqrt{n}),總的查詢的複雜度是O(n)O(n)。如果這時候我們要用數據結構維護莫隊,可以選擇修改時複雜度低,查詢時複雜度稍高的數據結構。(這樣的數據結構一般是分塊)

AC代碼

莫隊+權值線段樹(33分)
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 10;

int pos[maxn];
struct Query
{
	int l, r, id, a, b;
	bool operator < (const Query& a)const
	{
		if (pos[l] == pos[a.l])
			return r < a.r;
		return pos[l] < pos[a.l];
	}
}; Query query[maxn * 10];

int n, q, a[maxn];

struct w_tree
{
	struct Node
	{
		int l, r, sum;
	}; Node tree[maxn * 4];

	void build(int o, int l, int r)
	{
		tree[o].l = l, tree[o].r = r;
		if (l == r)
			return;

		int mid = (l + r) / 2;
		build(o * 2, l, mid);
		build(o * 2 + 1, mid + 1, r);
	}

	void change(int o, int x, int type)
	{
		tree[o].sum += type;
		if (tree[o].l == tree[o].r)
			return;

		int mid = (tree[o].l + tree[o].r) / 2;
		if (x <= mid)
			change(o * 2, x, type);
		else
			change(o * 2 + 1, x, type);
	}

	int ask(int o, int l, int r)
	{
		if (tree[o].l == l && tree[o].r == r)
			return tree[o].sum;

		int mid = (tree[o].l + tree[o].r) / 2;
		if (r <= mid)
			return ask(o * 2, l, r);
		else if (l > mid)
			return ask(o * 2 + 1, l, r);
		else
			return ask(o * 2, l, mid) + ask(o * 2 + 1, mid + 1, r);
	}
}; w_tree tree;

int cnt[maxn];
void remove(int id)
{
	if (cnt[a[id]]-- == 1)
		tree.change(1, a[id], -1);
}
void add(int id)
{
	if (cnt[a[id]]++ == 0)
	tree.change(1, a[id], 1);
}

int ans0[maxn * 10];
void solve()
{
	scanf("%d%d", &n, &q);
	tree.build(1, 1, n);
	int len = sqrt(n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]), pos[i] = (i - 1) / len + 1;

	for (int i = 1; i <= q; i++)
	{
		auto& it = query[i];
		scanf("%d%d%d%d", &it.l, &it.r, &it.a, &it.b);
		it.id = i;
	}
	sort(query + 1, query + 1 + q);

	int l = 1, r = 0;
	for (int i = 1; i <= q; i++)
	{
		int L = query[i].l, R = query[i].r, a = query[i].a, b = query[i].b;
		while (l < L) remove(l++);
		while (l > L) add(--l);
		while (r < R) add(++r);
		while (r > R) remove(r--);

		ans0[query[i].id] = tree.ask(1, a, b);
	} 

	for (int i = 1; i <= q; i++)
		printf("%d\n", ans0[i]);
}

int main()
{
	freopen("1.txt", "r", stdin);
	freopen("Testout.txt", "w", stdout);
	solve();
	return 0;
}
莫隊+分塊
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 10;

int pos[maxn];
struct Query
{
	int l, r, id, a, b;
	bool operator < (const Query& a)const
	{
		if (pos[l] == pos[a.l])
			return r < a.r;
		return pos[l] < pos[a.l];
	}
}; Query query[maxn * 10];

int n, q, a[maxn];
int len;

struct FK
{
	int cnt[maxn], b[maxn];

	void change(int x, int type)
	{
		cnt[a[x]] += type;
		b[pos[a[x]]] += type;
	}

	int ask(int l, int r)
	{
		int ans = 0;

		for (int i = l; i <= min(pos[l] * len, r); i++)
			ans += cnt[i];

		if (pos[l] != pos[r])
			for (int i = pos[r] * len - len + 1; i <= r; i++)
				ans += cnt[i];

		for (int i = pos[l] + 1; i <= pos[r] - 1; i++)
			ans += b[i];

		return ans;
	}

}; FK fk;

int cnt[maxn];
void remove(int id)
{
	if (cnt[a[id]]-- == 1)
		fk.change(id, -1);
}
void add(int id)
{
	if (cnt[a[id]]++ == 0)
		fk.change(id, 1);
}

int ans0[maxn * 10];
void solve()
{
	scanf("%d%d", &n, &q);
	len = sqrt(n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]), pos[i] = (i - 1) / len + 1;

	for (int i = 1; i <= q; i++)
	{
		auto& it = query[i];
		scanf("%d%d%d%d", &it.l, &it.r, &it.a, &it.b);
		it.id = i;
	}
	sort(query + 1, query + 1 + q);

	int l = 1, r = 0;
	for (int i = 1; i <= q; i++)
	{
		int L = query[i].l, R = query[i].r, a = query[i].a, b = query[i].b;
		while (l < L) remove(l++);
		while (l > L) add(--l);
		while (r < R) add(++r);
		while (r > R) remove(r--);

		ans0[query[i].id] = fk.ask(a, b);
	} 

	for (int i = 1; i <= q; i++)
		printf("%d\n", ans0[i]);
}

int main()
{
	freopen("Testin.txt", "r", stdin);
	//freopen("1.txt", "r", stdin);
	//freopen("Testout.txt", "w", stdout);
	solve();
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章