[JLOI2015] 城池攻占 题解

前言

题目链接:洛谷

题目分析

其他人要么倍增,要么左偏树,那我就来讲讲朴实无华的 dfs 序加上线段树的做法。

首先发现题目中明确指出了作乘法的时候一定是乘上一个大于零的数,这是为什么呢?首先把可以占领当前城池的战斗力的不等式列出来:

\[h_j \le \left\{ \begin{array}{c} s_i \times v_j & & {a_j = 1}\\ s_i + v_j & & {a_j=0} \end{array} \right. \]

发现当 \(v_j > 0\) 时,不等式不会变号,得到如下式子:

\[s_i \ge \left\{ \begin{array}{c} \cfrac{h_j}{v_j} & & {a_j = 1}\\ h_j - v_j & & {a_j=0} \end{array} \right. \]

于是,我们发现,判断一大堆骑士能不能占领只用看其中的最小血量是否满足要求就行了。但是代码实现的时候为了避免丢失精度,不使用浮点数比较。对于当前城池,我们要知道起子树内所有能跳到这里来的骑士的最小血量,删去牺牲在这里的骑士,将留下来的骑士血量按照题意操作,在往上跳一步。子树的问题可以用 dfs 序转变成一个区间上的问题,区间最小值、区间乘、区间加、单点删除,明显可以用线段树维护。删除的时候将其战斗力设为 \(\infty\) 就不会对之后的造成影响。时间复杂度 \(\Theta(n \log n)\),令 \(n\)\(m\) 同阶。

实际代码实现起来码量很大?细节需要处理到位,特别是这题 dfs 序记的不是结点,而是士兵,所以会略有不同,具体见代码。

代码(已略去快读快写,码风清新,注释详尽)

dfs 序

//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast", "inline", "-ffast-math")
//#pragma GCC target("avx", "sse2", "sse3", "sse4", "mmx")
#include <iostream>
#include <cstdio>
#define debug(a) cerr << "Line: " << __LINE__ << " " << #a << endl
#define print(a) cerr << #a << "=" << (a) << endl
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define main Main(); signed main(){ return ios::sync_with_stdio(0), cin.tie(0), Main(); } signed Main
using namespace std;

#include <vector>

const int N = 300010;
const long long inf = 0x3f3f3f3f3f3f3f3fll;

int n, m;
vector<int> edge[N], man[N];
int ans1[N], ans2[N];
// 对于 ans2,转变成初始位置深度 - 死亡位置深度,根节点深度 1,没死的当做在深度为 0 的地方死了

int op[N], dpt[N];
long long h[N], v[N], s[N];

int L[N], R[N], val[N], timer;

void dfs(int now){
	L[now] = timer + 1;
	for (auto x: man[now]) val[++timer] = x;
	for (auto to: edge[now]) dfs(to);
	R[now] = timer;
}

// dfs 序记录子树所有士兵

struct Segment_Tree{
	#define lson (idx << 1    )
	#define rson (idx << 1 | 1)
	
	struct Tag{
		long long mul, add;
		Tag operator + (const Tag & o) const {
			return {mul * o.mul, add * o.mul + o.add};
		}
		inline void clear(){
			mul = 1, add = 0;
		}
	};
	// 懒惰标记
	
	struct Info{
		long long minn;
		int pos;
		Info operator + (const Info & o) const {
			if (minn == inf) return o;
			if (o.minn == inf) return *this;
			if (minn < o.minn) return *this;
			return o;
		}
		Info operator + (const Tag & o) const {
			if (minn == inf) return *this;
			return {minn * o.mul + o.add, pos};
		}
	};
	// 信息
	
	struct node{
		int l, r;
		Info info;
		Tag tag;
	} tree[N << 2];
	
	void pushup(int idx){
		tree[idx].info = tree[lson].info + tree[rson].info;
	}
	
	void build(int idx, int l, int r){
		tree[idx] = {l, r, inf, -1, 1, 0};
		if (l == r) return tree[idx].info = {s[val[l]], l}, void();
		int mid = (l + r) >> 1;
		build(lson, l, mid), build(rson, mid + 1, r), pushup(idx);
	}
	
	void pushtag(int idx, const Tag t){
		tree[idx].info = tree[idx].info + t;
		tree[idx].tag = tree[idx].tag + t;
	}
	
	void pushdown(int idx){
		pushtag(lson, tree[idx].tag), pushtag(rson, tree[idx].tag);
		tree[idx].tag.clear();
	}
	
	Info query(int idx, int l, int r){
		if (tree[idx].l > r || tree[idx].r < l) return {inf, -1};
		if (l <= tree[idx].l && tree[idx].r <= r) return tree[idx].info;
		return pushdown(idx), query(lson, l, r) + query(rson, l, r);
	}
	
	void modify(int idx, int l, int r, const Tag t){
		if (tree[idx].l > r || tree[idx].r < l) return;
		if (l <= tree[idx].l && tree[idx].r <= r) return pushtag(idx, t);
		pushdown(idx), modify(lson, l, r, t), modify(rson, l, r, t), pushup(idx);
	}
	
	void erase(int pos){
		modify(1, pos, pos, {0, inf});
	}
	
	void add(int l, int r, long long v){
		modify(1, l, r, {1, v});
	}
	
	void mul(int l, int r, long long v){
		modify(1, l, r, {v, 0});
	}
	
	void output(int idx){
		if (tree[idx].l == tree[idx].r){
			cerr << (tree[idx].info.minn == inf ? -1 : tree[idx].info.minn) << " \n"[tree[idx].l == timer];
			return;
		}
		pushdown(idx), output(lson), output(rson);
	}
	
	#undef lson
	#undef rson
} yzh;
// 貌似就是线段树 2 ?

void redfs(int now){
	if (L[now] > R[now]) return;
	for (auto to: edge[now]) redfs(to);
//	yzh.output(1);
	while (true){
		// 不断删去死了的士兵,注意到士兵最多删 m 次,故不会超时
		Segment_Tree::Info res = yzh.query(1, L[now], R[now]);
		if (res.pos == -1 || res.minn == inf) break;
		if (res.minn >= h[now]) break;
		ans2[val[res.pos]] -= dpt[now], yzh.erase(res.pos), ++ans1[now];
//		yzh.output(1);
	}
	if (op[now]) yzh.mul(L[now], R[now], v[now]);
	else         yzh.add(L[now], R[now], v[now]);
}
// 第二次深搜求得答案

signed main(){
	dpt[1] = 1, read(n, m);
	for (int i = 1; i <= n; ++i) read(h[i]);
	for (int i = 2, fa; i <= n; ++i) read(fa, op[i], v[i]), edge[fa].push_back(i), dpt[i] = dpt[fa] + 1;
	for (int i = 1, pos; i <= m; ++i) read(s[i], pos), man[pos].push_back(i), ans2[i] = dpt[pos];
	dfs(1), yzh.build(1, 1, timer), redfs(1);
	for (int i = 1; i <= n; ++i) write(ans1[i], '\n');
	for (int i = 1; i <= m; ++i) write(ans2[i], '\n');
	return 0;
}

倍增(虽然没讲,但是也给出吧?)

//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast", "inline", "-ffast-math")
//#pragma GCC target("avx", "sse2", "sse3", "sse4", "mmx")
#include <iostream>
#include <cstdio>
#define debug(a) cerr << "Line: " << __LINE__ << " " << #a << endl
#define print(a) cerr << #a << "=" << (a) << endl
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define main Main(); signed main(){ return ios::sync_with_stdio(0), cin.tie(0), Main(); } signed Main
using namespace std;

int n, m;
int op[300010];

int ans1[300010], ans2[300010];

int yzh[300010][20];
long long add[300010][20], mul[300010][20];
long long L[300010][20];

signed main(){
	read(n, m);
	for (int i = 1; i <= n; ++i) read(L[i][0]);
	for (int i = 2, op; i <= n; ++i){
		read(yzh[i][0], op), read(op ? mul[i][0] : (mul[i][0] = 1, add[i][0]));
	}
	for (int k = 1; k <= 19; ++k)
	for (int i = 1; i <= n; ++i) if (!!(yzh[i][k] = yzh[yzh[i][k - 1]][k - 1])){
		mul[i][k] = mul[i][k - 1] * mul[yzh[i][k - 1]][k - 1];
		add[i][k] = add[i][k - 1] * mul[yzh[i][k - 1]][k - 1] + add[yzh[i][k - 1]][k - 1];
		L[i][k] = max(L[i][k - 1], (L[yzh[i][k - 1]][k - 1] - add[i][k - 1] - 1) / mul[i][k - 1] + 1);
	}
	for (int i = 1, now; i <= m; ++i){
		long long val; read(val, now);
		for (int j = 19; j >= 0; --j)
		if (yzh[now][j] && L[now][j] <= val)
			ans2[i] += 1 << j, val = val * mul[now][j] + add[now][j], now = yzh[now][j];
		if (val >= L[now][0]) ++ans2[i];
		else ++ans1[now];
	}
	for (int i = 1; i <= n; ++i) write(ans1[i], '\n');
	for (int i = 1; i <= m; ++i) write(ans2[i], '\n');
	return 0;
}

左偏树

//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast", "inline", "-ffast-math")
//#pragma GCC target("avx", "sse2", "sse3", "sse4", "mmx")
#include <iostream>
#include <cstdio>
#define debug(a) cerr << "Line: " << __LINE__ << " " << #a << endl
#define print(a) cerr << #a << "=" << (a) << endl
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define main Main(); signed main(){ return ios::sync_with_stdio(0), cin.tie(0), Main(); } signed Main
using namespace std;

int n, m;
typedef int array[300010];
typedef long long Array[300010];
array lson, rson, root, a, dpt, fa, ans1, ans2, dis;
Array add, mul, h, s, v;

inline void pushtag(int x, long long mul, long long add){
	::add[x] = ::add[x] * mul + add, ::mul[x] *= mul;
	s[x] = s[x] * mul + add;
}

inline void pushdown(int x){
	if (lson[x]) pushtag(lson[x], mul[x], add[x]);
	if (rson[x]) pushtag(rson[x], mul[x], add[x]);
	add[x] = 0, mul[x] = 1;
}

int merge(int x, int y){
	if (!x || !y) return x | y;
	if (s[x] > s[y]) swap(x, y);
	pushdown(x), rson[x] = merge(rson[x], y);
	if (dis[lson[x]] < dis[rson[x]]) swap(lson[x], rson[x]);
	return dis[x] = dis[rson[x]] + 1, x;
}

signed main(){
	dpt[1] = 1, read(n, m);	
	for (int i = 1; i <= n; ++i) read(h[i]);
	for (int i = 2; i <= n; ++i) read(fa[i], a[i], v[i]), dpt[i] = dpt[fa[i]] + 1, mul[i] = 1;
	for (int i = 1, bl; i <= m; ++i) read(s[i], bl), root[bl] = merge(root[bl], i), ans2[i] = dpt[bl];
	for (int i = n; i >= 1; --i){
		while (root[i] && s[root[i]] < h[i]){
			ans2[root[i]] -= dpt[i], pushdown(root[i]), ++ans1[i];
			root[i] = merge(lson[root[i]], rson[root[i]]);
		}
		if (i == 1) break;
		if (root[i] == 0) continue;
		if (a[i]) pushtag(root[i], v[i], 0);
		else pushtag(root[i], 1, v[i]);
		pushdown(root[i]), root[fa[i]] = merge(root[fa[i]], root[i]);
	}
	for (int i = 1; i <= n; ++i) write(ans1[i], '\n');
	for (int i = 1; i <= m; ++i) write(ans2[i], '\n');
	return 0;
}

总结 & 后话

线段树无敌爱敲,另外两种做法码量小,速度快,虽然线段树码量大,速度慢,但是思路简单是个不错的选择!

posted @ 2024-03-14 18:46  XuYueming  阅读(11)  评论(0编辑  收藏  举报