首页 > 其他分享 >P5838 [USACO19DEC] Milk Visits G

P5838 [USACO19DEC] Milk Visits G

时间:2023-09-26 12:23:50浏览次数:38  
标签:USACO19DEC int pos Visits id sqrt mathcal Milk bitset

P5838 [USACO19DEC] Milk Visits G

Luogu P5838

Solution

提供一种奇特的 \(\mathcal O(\dfrac{n\sqrt n\log n}{\omega})\) 的做法。

树链剖分转化成序列问题。然后变成了询问一个区间 \(l,r\) 是否存在一个颜色 \(k\),显然可以 \(\mathcal O(n)\) 预处理 \(\mathcal O(\sqrt n)\) 判定,这样复杂度是 \(\mathcal O(n\sqrt n\log n)\) 的,空间复杂度 \(\mathcal O(n\sqrt n)\),很卡,没敢写。

注意到并不需要数颜色出现了多少次,因此考虑使用 bitset 维护一个颜色是否出现。

对于整块,开 \(\mathcal O(n)\) 个 bitset,其中第 \(i\) 个 bitset 的第 \(j\) 位表示颜色 \(i\) 是否在第 \(j\) 块出现。那么对于整块,可以使用 bitset 的位运算在 \(\mathcal O(\dfrac{\sqrt n}{\omega})\) 的时间内判定。

对于散块,考虑对每个块中的每一个颜色开一个长度为块长的 bitset ,第 \(i\) 位表示这个颜色在这个块中从左起的第 \(i\) 个位置出现了,那么询问也容易使用 bitset 的位运算做到 \(\mathcal O(\dfrac{\sqrt n}{\omega})\)。乍一看这部分的 bitset 大小是 \(\mathcal O(\dfrac{n^2}{\omega})\) 的,但是因为一个块中的颜色数量是 \(\mathcal O(\sqrt n)\) 的,所以可以将这个块内的颜色重新映射到 \([1,\sqrt n]\) 的范围(类似于离散化),然后空间复杂度就降为了 \(\mathcal O(\dfrac{n\sqrt n}{\omega})\)。颜色在每个块内的离散化可以使用 Hash 表做到线性处理。

总时间复杂度 \(\mathcal O(\dfrac{n\sqrt n\log n}{\omega})\),空间复杂度 \(\mathcal O(\dfrac{n\sqrt n}{\omega})\)。

#include<bits/stdc++.h>
#define For(i, a, b) for (int i = (a); i <= (b); ++i)
#define Rof(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
using i64 = long long;
constexpr int _N = 1e5 + 5, _B = 400 + 5;
bool ST;
int n, m;
vector<int> e[_N];
int dep[_N], fa[_N], id[_N], son[_N];
int top[_N], siz[_N], dfn;
int a[_N], b[_N];
void Dfs1(int x, int F) {
	dep[x] = dep[F] + 1, fa[x] = F, siz[x] = 1;
	int maxson = -1;
	for (int v : e[x]) if (v != F) {
		Dfs1(v, x);
		if (siz[v] > maxson) maxson = siz[v], son[x] = v;
		siz[x] += siz[v];
	}
}
void Dfs2(int x, int topf) {
	id[x] = ++dfn, top[x] = topf, b[dfn] = a[x];
	if (son[x]) Dfs2(son[x], topf);
	for (int v : e[x]) if (v != son[x] && v != fa[x])
		Dfs2(v, v);
}
int pos[_N], bl[_N], br[_N], len, cnt;
bitset<_B> ct[_N], tl[_B], bct[_B][_B];
int tot[_B];
#include<ext/pb_ds/assoc_container.hpp>
__gnu_pbds::gp_hash_table<int, int> H[_B];
bool ED;
void InitBlock() {
	len = sqrt(n), cnt = (n - 1) / len + 1;
	For(i, 1, cnt) bl[i] = br[i - 1] + 1, br[i] = min(i * len, n);
	For(i, 1, cnt) tl[i] = tl[i - 1], tl[i].set(i);
	For(i, 1, cnt) {
		For(j, bl[i], br[i]) {
			pos[j] = i;
			ct[b[j]].set(i);
			if (H[i].find(b[j]) == H[i].end())
				H[i][b[j]] = ++tot[i];
			int nc = H[i][b[j]];
			bct[i][nc].set(j - bl[i] + 1);
		}
	}
}
bool AskB(int id, int l, int r, int k) {
	if (H[id].find(k) == H[id].end()) return 0;
	int nc = H[id][k];
	return ((bct[id][nc] >> (l - bl[id])) & tl[r - l + 1]).any();
}
bool Query(int l, int r, int k) {
	if (pos[l] == pos[r])
		return AskB(pos[l], l, r, k);
	else
		return AskB(pos[l], l, br[pos[l]], k)
			|| AskB(pos[r], bl[pos[r]], r, k)
			|| ((ct[k] >> pos[l]) & tl[pos[r] - pos[l] - 1]).any();
}
bool QueryPath(int x, int y, int k) {
	bool res = 0;
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) swap(x, y);
		res = Query(id[top[x]], id[x], k);
		if (res) return res;
		x = fa[top[x]];
	}
	if (dep[x] > dep[y]) swap(x, y);
	return Query(id[x], id[y], k);
}
signed main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	For(i, 1, n) cin >> a[i];
	For(i, 2, n) {
		int x, y;
		cin >> x >> y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	Dfs1(1, 0), Dfs2(1, 1);
	InitBlock();
	For(i, 0, m - 1) {
		int x, y, k;
		cin >> x >> y >> k;
		cout << QueryPath(x, y, k);
	}
}

标签:USACO19DEC,int,pos,Visits,id,sqrt,mathcal,Milk,bitset
From: https://www.cnblogs.com/hanx16msgr/p/17729819.html

相关文章

  • P5836 [USACO19DEC] Milk Visits S - 洛谷题解
     题目链接:[P5836] USACO19DEC] MilkVisitsS-洛谷|计算机科学教育新生态(luogu.com.cn)这道题可以用并查集来解决。题目中每个结点只有两个状态:H和G。那么我们可以推断出,只有当起点和终点间每个结点的状态相同但是起点(或者终点或起点到终点之间的某一点)与所需状态不同......
  • [8月摸鱼计划] MILKV DUO可以实现的功能及例程
    MILKVDUO是一个基于深度学习的计算机视觉库,它提供了许多功能和例程来处理图像和视觉任务。下面是几个MILKVDUO可以实现的功能以及相应的功能例程:图像分类(ImageClassification):功能:将输入的图像分为不同的类别或标签。例程:使用预训练的卷积神经网络(CNN)对图像进行分类,例如将猫和狗......
  • [USACO1.3]混合牛奶 Mixing Milk
    [USACO1.3]混合牛奶MixingMilk题目描述由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要。帮助Marry乳业找到最优的牛奶采购方案。Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格可能相同。此外,就像每头奶牛每天只能挤出固定数量......
  • Codeforces Round #225 (Div. 2)-C. Milking cows
    原题链接C.Milkingcowstimelimitpertestmemorylimitpertestinputoutputn cowssittinginarow,numberedfrom 1 to nIahubcandecidetheorderinwhichhemilksthecows.Buthemustmilkeachcowex......
  • Codeforces Round #225 (Div. 2) C. Milking cows Greedy
    Iahubhelpshisgrandfatheratthefarm.Todayhemustmilkthecows.Therearencowssittinginarow,numberedfrom1tonfromlefttoright.Eachcowiseitherfacingtotheleftorfacingtotheright.WhenIahubmilksacow,allthecowsthatseet......
  • POJ - 3616 Milking Time(DAG)
    题目大意:给出N头牛的产奶时间段和产奶量,每接完一头牛的奶后,需要休息R分钟问如何选择牛,才能使接到的产奶量达到最大解题思路:DAG,按产奶的结束时间大小排序#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;constintN=1010;structInterval{......
  • Kevin Rose创立新公司Milk,或对硅谷发展产生重大影响
    编者按:凯文.罗斯(KevinRose)——Pownce,Revision3,和Digg创始人,也曾投资过Twitter,Zynga。凯文.罗斯退出Digg之后重新创业,创立新公司Milk,这是一个类似创业孵化器但又不完全是的移动实验室。罗斯表示,新公司将着力解决移动互联网领域的重大问题,为重大创新服务,......
  • NC24370 [USACO 2012 Dec S]Milk Routing
    题目链接题目题目描述FarmerJohn'sfarmhasanoutdatednetworkofMpipes(1<=M<=500)forpumpingmilkfromthebarntohismilkstoragetank.Hewants......
  • P2744 「USACO5.3」量取牛奶 Milk Measuring
    将桶按容积大小从小到大排序,令\(f_{i,j}\)表示前\(i\)个桶能否量出\(j\)夸脱,如果可以就用vector存储最优方案。先枚举桶的种类再枚举夸脱数,转移看似只有两种:之前......
  • USACO Training Section 1.3 Mixing Milk
    简化题意二个整数\(n\),\(m\),表示需要牛奶的总量,和提供牛奶的农民个数。每行两个整数\(p_i\),\(a_i\),表示第\(i\)个农民牛奶的单价,和农民\(i\)一天最多能卖出的牛奶量......