树链剖分+线段树 0pts求调
查看原帖
树链剖分+线段树 0pts求调
674147
vanueber楼主2024/10/5 19:30

rt,求助大佬

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <ctime>
#include <random>

using namespace std;

const int INF = 0x3f3f3f3f;

inline int read()
{
	int w = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		w = (w << 1) + (w << 3) + (ch ^ 48);
		ch = getchar();
	}
	return w * f;
}

inline void write(int x)
{
	if (x < 0)
	{
		putchar('-');
		x = -x;
	}
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
const int maxn = 1e5 + 5;
int n;
vector<int> G[maxn];
int gu[maxn], gv[maxn], gw[maxn], val[maxn];

int f[maxn], sz[maxn], hson[maxn], dep[maxn];
void dfs1(int u, int fa, int depth)
{
	dep[u] = depth;
	sz[u] = 1;
	int hson_size = -1;
	for (int v : G[u])
	{
		if (v == fa) continue;
		f[v] = u;
		dfs1(v, u, depth + 1);
		sz[u] += sz[v];
		if (sz[v] > hson_size)
		{
			hson_size = sz[v];
			hson[u] = v;
		}
	}
}

int top[maxn], nval[maxn], dfn[maxn], cnt;
void dfs2(int u, int fa)
{
	top[u] = fa, dfn[u] = ++cnt, nval[cnt] = val[u];
	if (hson[u] == 0) return;
	dfs2(hson[u], fa);
	for (int v : G[u])
	{
		if (v == hson[u] || v == f[u]) continue;
		dfs2(v, v);
	}
}

struct node
{
	int l, r;
	int maxx;
	int add, chg;
} tr[maxn << 2];
int ls(int p)
{
	return p << 1;
}
int rs(int p)
{
	return p << 1 | 1;
}
void pushup(int p)
{
	tr[p].maxx = max(tr[ls(p)].maxx, tr[rs(p)].maxx);
}
void pushdown(int p)
{
	if (tr[p].chg != -1)
	{
		tr[ls(p)].maxx = tr[p].chg;
		tr[rs(p)].maxx = tr[p].chg;

		tr[ls(p)].add = 0;
		tr[rs(p)].add = 0;

		tr[ls(p)].chg = tr[p].chg;
		tr[rs(p)].chg = tr[p].chg;

		tr[p].chg = -1;
	}
	if (tr[p].add)
	{
		tr[ls(p)].add += tr[p].add;
		tr[rs(p)].add += tr[p].add;
		tr[ls(p)].maxx += tr[p].add;
		tr[rs(p)].maxx += tr[p].add;
		tr[p].add = 0;
	}
}
void build(int p, int l, int r)
{
	tr[p].l = l, tr[p].r = r;
	if (l == r)
	{
		tr[p].maxx = nval[l];
		tr[p].chg = -1;
		return;
	}
	int mid = l + r >> 1;
	build(ls(p), l, mid);
	build(rs(p), mid + 1, r);
	pushup(p);
}
void change(int p, int l, int r, int k)
{
	if (l <= tr[p].l && tr[p].r <= r)
	{
		tr[p].chg = k;
		tr[p].maxx = k;
		tr[p].add = 0;
		return;
	}
	pushdown(p);
	int mid = tr[p].l + tr[p].r >> 1;
	if (mid >= l) change(ls(p), l, r, k);
	if (mid < r) change(rs(p), l, r, k);
	pushup(p);
}
void add(int p, int l, int r, int k)
{
	if (l <= tr[p].l && tr[p].r <= r)
	{
		tr[p].add += k;
		tr[p].maxx += k;
		return;
	}
	pushdown(p);
	int mid = tr[p].l + tr[p].r >> 1;
	if (mid >= l) add(ls(p), l, r, k);
	if (mid < r) add(rs(p), l, r, k);
	pushup(p);
}
int query(int p, int l, int r)
{
	if (l <= tr[p].l && tr[p].r <= r)
	{
		return tr[p].maxx;
	}
	pushdown(p);
	int mid = tr[p].l + tr[p].r >> 1;
	int maxx = -INF;
	if (mid >= l) maxx = max(maxx, query(ls(p), l, r));
	if (mid < r) maxx = max(maxx, query(rs(p), l, r));
	return maxx;
}

void add_path(int u, int v, int k)
{
	while (top[u] != top[v])
	{
		if (dep[top[u]] < dep[top[v]]) swap(u, v);
		add(1, dfn[top[u]], dfn[u], k);
		u = f[top[u]];
	}
	if (dep[u] < dep[v]) swap(u, v);
	add(1, dfn[v] + 1, dfn[u], k);
}
void change_path(int u, int v, int k)
{
	while (top[u] != top[v])
	{
		if (dep[top[u]] < dep[top[v]]) swap(u, v);
		change(1, dfn[top[u]], dfn[u], k);
		u = f[top[u]];
	}
	if (dep[u] < dep[v]) swap(u, v);
	change(1, dfn[v] + 1, dfn[u], k);
}
int query_path(int u, int v)
{
	int maxx = -1;
	while (top[u] != top[v])
	{
		if (dep[top[u]] < dep[top[v]]) swap(u, v);
		maxx = max(maxx, query(1, dfn[top[u]], dfn[u]));
		u = f[top[u]];
	}
	if (dep[u] < dep[v]) swap(u, v);
	maxx = max(maxx, query(1, dfn[v] + 1, dfn[u]));
	return maxx;
}

string opt;

int main()
{
	n = read();
	for (int i = 1; i < n; ++i)
	{
		gu[i] = read(), gv[i] = read(), gw[i] = read();
		G[gu[i]].push_back(gv[i]);
		G[gv[i]].push_back(gu[i]);
	}
	dfs1(1, 0, 1);
	for (int i = 1; i < n; ++i)
	{
		int u = gu[i], v = gv[i];
		if (f[u] == v)
		{
			val[u] = gw[i];
		}
		else
		{
			val[v] = gw[i];
		}
	}
	dfs2(1, 1);
	build(1, 1, n);
	while (cin >> opt)
	{
		if (opt == "Stop")
		{
			return 0;
		}
		else if (opt == "Change")
		{
			static int k, w;
			k = read(), w = read();
			if (f[gu[k]] == gv[k])
			{
				change(1, dfn[gu[k]], dfn[gu[k]], w);
			}
			else change(1, dfn[gv[k]], dfn[gu[k]], w);
		}
		else if (opt == "Cover")
		{
			static int u, v, w;
			u = read(), v = read(), w = read();
			change_path(u, v, w);
		}
		else if (opt == "Add")
		{
			static int u, v, w;
			u = read(), v = read(), w = read();
			add_path(u, v, w);
		}
		else
		{
			static int u, v;
			u = read(), v = read();
			write(query_path(u, v));
			puts("");
		}
	}
	return 0;
}


2024/10/5 19:30
加载中...