10ptsMnZn求调
查看原帖
10ptsMnZn求调
428889
Xile楼主2024/1/25 17:36

忽略快读的部分,代码主体在中间,输入之后记得CRTL + Z

/**
 * @author: Xile
 * @date: 2024-01-25 16:38:26
 **/

#include <bits/stdc++.h>
#include <bits/extc++.h>

#define priority_queue __gnu_pbds::priority_queue
#define unordered_map __gnu_pbds::gp_hash_table

template<class T>T Min(const T &a, const T &b) {return a < b ? a : b;}
template<class T>T Max(const T &a, const T &b) {return a > b ? a : b;}

using namespace std;

namespace Fast_I {
	char *_Buf, *_Start_ptr, *_End_ptr;
	std::streambuf* inbuf;
	unsigned int Size;
	bool _Ok;
	
	struct Fast_Istream {
		operator bool() { return _Ok; }
		Fast_Istream(std::streambuf*, unsigned int);
		Fast_Istream(unsigned int);
		Fast_Istream(const char*, unsigned int);
		Fast_Istream& operator>>(char&);
		Fast_Istream& operator>>(char*);
		Fast_Istream& operator>>(bool&);
		Fast_Istream& operator>>(short&);
		Fast_Istream& operator>>(int&);
		Fast_Istream& operator>>(long&);
		Fast_Istream& operator>>(long long&);
		Fast_Istream& operator>>(unsigned short&);
		Fast_Istream& operator>>(unsigned int&);
		Fast_Istream& operator>>(unsigned long&);
		Fast_Istream& operator>>(unsigned long long&);
		Fast_Istream& operator>>(float&);
		Fast_Istream& operator>>(double&);
		Fast_Istream& operator>>(long double&);
		Fast_Istream& operator>>(std::string&);
		template <typename Typex>
		void operator()(Typex& _Val) { *this >> _Val; }
		template <typename Typex, typename... More>
		void operator()(Typex&, More&...);
		std::streambuf* rdbuf() { return inbuf; }
		void rdbuf(std::streambuf* _inbuf) { inbuf = _inbuf; }
		void rdbuf(const char*);
		void pop();
		char peek();
	};
}  // namespace Fast_I
namespace Fast_O {
	std::string buf;
	std::streambuf* outbuf;
	
	struct Fast_Ostream {
		Fast_Ostream(std::streambuf*, unsigned int);
		Fast_Ostream(std::streambuf* out) { outbuf = out; }
		Fast_Ostream(const char*, unsigned int);
		Fast_Ostream(unsigned int);
		void flush();
		~Fast_Ostream();
		void endl() { buf.push_back('\n'); }
		template <typename Typex>
		void endl(const Typex& _Val);
		template <typename Typex, typename... More>
		void endl(const Typex&, const More&...);
		template <typename Typex>
		void operator()(const Typex& _Val);
		template <typename Typex, typename... More>
		void operator()(const Typex&, const More&...);
		Fast_Ostream& operator<<(char);
		Fast_Ostream& operator<<(const char*);
		Fast_Ostream& operator<<(const std::string&);
		Fast_Ostream& operator<<(bool);
		Fast_Ostream& operator<<(short);
		Fast_Ostream& operator<<(int);
		Fast_Ostream& operator<<(long);
		Fast_Ostream& operator<<(long long);
		Fast_Ostream& operator<<(unsigned short);
		Fast_Ostream& operator<<(unsigned int);
		Fast_Ostream& operator<<(unsigned long);
		Fast_Ostream& operator<<(unsigned long long);
		std::streambuf* rdbuf() { return outbuf; }
		void rdbuf(std::streambuf* _outbuf) { outbuf = _outbuf; }
		void rdbuf(const char*);
	};
}  // namespace Fast_O
namespace Fast_IO {
	Fast_I::Fast_Istream fin(std::cin.rdbuf(), 1048576);  // 1 MB
	Fast_O::Fast_Ostream fout(std::cout.rdbuf());         // 鈭?
}  // namespace Fast_IO

#define read Fast_IO::fin
#define write Fast_IO::fout
#define debug cout << "fuck"

typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef pair<int, int> pii;

const int N = 1e5 + 5;

int n, v[N], w[N];

struct Edge{int fr, to, next, val;}edge[N << 1];
int head[N], tot;
void add_edge(int x, int y, int v) {edge[++tot] = {x, y, head[x], v}; head[x] = tot;}

struct Segment_tree{
	struct node{int l, r, maxv, add, cov;}tr[N << 2];
#define ls (now << 1)
#define rs (ls | 1)
#define mid ((tr[now].l + tr[now].r) >> 1)
	void spread(int now) {
		if (tr[now].cov != -1) {
			tr[ls].add = tr[rs].add = 0;
			tr[ls].cov = tr[rs].cov = tr[now].cov;
			tr[ls].maxv = tr[rs].maxv = tr[now].cov;
			tr[now].cov = -1;
		}
		if (tr[now].add) {
			tr[ls].add += tr[now].add;
			tr[rs].add += tr[now].add;
			tr[ls].maxv += tr[now].add;
			tr[rs].maxv += tr[now].add;
			tr[now].add = 0;
		}
	}
	void update(int now) {tr[now].maxv = Max(tr[ls].maxv, tr[rs].maxv);}
	void build(int now, int l, int r) {
		tr[now].l = l, tr[now].r = r, tr[now].cov = -1;
		if (l == r) {
			tr[now].maxv = w[l];
			return;
		}
		build(ls, l, mid);
		build(rs, mid + 1, r);
		update(now);
	}
	void modify(int now, int ql, int qr, int op, int v) {
		if (tr[now].l >= ql && tr[now].r <= qr) {
			if (op == 1) tr[now].maxv = tr[now].cov = v;
			if (op == 2) tr[now].add += v, tr[now].maxv += v;
			if (op == 3) tr[now].maxv = v;
			return ;
		}
		spread(now);
		if (ql <= mid) modify(ls, ql, qr, op, v);
		if (qr > mid) modify(rs, ql, qr, op, v);
		update(now);
	}
	int query(int now, int ql, int qr) {
		if (tr[now].l >= ql && tr[now].r <= qr) return tr[now].maxv;
		spread(now);
		int res = 0;
		if (ql <= mid) res = query(ls, ql, qr);
		if (qr > mid) res = Max(res, query(rs, ql, qr));
		return res;
	}
}ST;

int siz[N], dep[N], fat[N], son[N], top[N], id[N], idx;
void dfs1(int x) {
	siz[x] = 1;
	for (int i = head[x]; i; i = edge[i].next) {
		int y = edge[i].to;
		if (y == fat[x]) continue;
		dep[y] = dep[x] + 1;
		fat[y] = x;
		v[y] = edge[i].val;
		dfs1(y);
		siz[x] += siz[y];
		if (siz[y] > siz[son[x]]) son[x] = y;
	}
}
void dfs2(int x, int topf) {
	top[x] = topf;
	id[x] = ++idx;
	w[idx] = v[x];
	if (son[x]) dfs2(son[x], topf);
	for (int i = head[x]; i; i = edge[i].next) {
		int y = edge[i].to;
		if (y == fat[x] || y == son[x]) continue;
		dfs2(y, y);
	} 
}
void modify1(int x, int y, int v) {
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) swap(x, y);
		ST.modify(1, id[top[x]], id[x], 1, v);
		x = fat[top[x]];
	}
	if (dep[x] < dep[y]) swap(x, y);
	if (x != y) ST.modify(1, id[y] + 1, id[x], 1, v);
}
void modify2(int x, int y, int v) {
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) swap(x, y);
		ST.modify(1, id[top[x]], id[x], 2, v);
		x = fat[top[x]];
	}
	if (dep[x] < dep[y]) swap(x, y);
	if (x != y) ST.modify(1, id[y] + 1, id[x], 2, v);
}
int query(int x, int y) {
	int ans = 0;
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) swap(x, y);
		ans = Max(ans, ST.query(1, id[top[x]], id[x]));
		x = fat[top[x]];
	}
	if (dep[x] < dep[y]) swap(x, y);
	if (x != y) ans = Max(ans, ST.query(1, id[y] + 1, id[x]));
	return ans;
}

signed main() {
	
	read(n);
	for (int i = 1, x, y, v; i < n; i++) read(x, y, v), add_edge(x, y, v), add_edge(y, x, v);
	
	dfs1(1);
	dfs2(1, 1);
	ST.build(1, 1, n);
	
	string op;
	int x, y, z;
	while (1) {
		read(op);
		if (op == "Stop") break;
		if (op == "Max") {
			read(x, y);
			write(query(x, y), '\n');
		}
		if (op == "Cover") {
			read(x, y, z);
			modify1(x, y, z);
		}
		if (op == "Change") {
			read(x, z);
			int k = (x << 1);
			int y = dep[edge[k].to] < dep[edge[k].fr] ? edge[k].fr : edge[k].to;

			ST.modify(1, id[y], id[y], 3, z);
		}
		if (op == "Add") {
			read(x, y, z);
			modify2(x, y, z);
		}
	}
	
	return 0;
}

namespace Fast_I {
	Fast_Istream::Fast_Istream(std::streambuf* in, unsigned int Sz) {
		_Ok = 1;
		Fast_I::Size = Sz;
		inbuf = in;
		_Start_ptr = _End_ptr = _Buf = new char[Sz];
	}
	Fast_Istream::Fast_Istream(const char* in, unsigned int Sz) {
		_Ok = 1;
		Fast_I::Size = Sz;
		rdbuf(in);
		_Start_ptr = _End_ptr = _Buf = new char[Sz];
	}
	Fast_Istream::Fast_Istream(unsigned int Sz) {
		_Ok = 1;
		Fast_I::Size = Sz;
		_Start_ptr = _End_ptr = _Buf = new char[Sz];
	}
	void Fast_Istream::rdbuf(const char* File) {
		static std::ifstream __In__(File);
		rdbuf(__In__.rdbuf());
	}
	void Get_Char(char& _Val) {
		if (_Start_ptr == _End_ptr) {
			_Start_ptr = _Buf;
			_End_ptr = _Buf + inbuf->sgetn(_Buf, Size);
		}
		if (_Start_ptr == _End_ptr) {
			_Val = -1;
			_Ok = 0;
		} else {
			_Val = *_Start_ptr++;
		}
	}
	Fast_Istream& Fast_Istream::operator>>(char& _Val) {
		if (_Ok) {
			Get_Char(_Val);
			while (_Val == 32 || _Val == 10 || _Val == 13 || _Val == 8 || _Val == 9 ||
				_Val == 7 || _Val == 12 || _Val == 11) {
				Get_Char(_Val);
			}
		}
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(char* _Val) {
		if (_Ok) {
			Get_Char(*_Val);
			while (*_Val == 32 || *_Val == 10 || *_Val == 13 || *_Val == 8 ||
				*_Val == 9 || *_Val == 7 || *_Val == 12 || *_Val == 11) {
				Get_Char(*_Val);
			}
			while (*_Val != 32 && *_Val != 10 && *_Val && *_Val != -1 && *_Val != 9 &&
				*_Val != 11 && *_Val != 12) {
				Get_Char(*++_Val);
			}
			*_Val = 0;
			--_Start_ptr;
		}
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(std::string& _Val) {
		if (_Ok) {
			char c;
			Get_Char(c);
			while (c == 32 || c == 10 || c == 13 || c == 8 || c == 9 || c == 7 ||
				c == 12 || c == 11) {
				Get_Char(c);
			}
			for (_Val.clear();
				c != 32 && c != 10 && c && c != -1 && c != 9 && c != 11 && c != 12;
				Get_Char(c)) {
				_Val.push_back(c);
			}
			--_Start_ptr;
		}
		return *this;
	}
	template <typename Typex>
	void Get_Int(Typex& _Val) {
		if (_Ok) {
			char ch;
			bool _F = 0;
			for (Get_Char(ch); (ch < 48 || ch > 57) && ch != -1; Get_Char(ch)) {
				_F = ch == 45;
			}
			for (_Val = 0; ch > 47 && ch < 58 && ch != -1; Get_Char(ch)) {
				_Val = _Val * 10 + (ch ^ 48);
			}
			if (_F) {
				_Val = ~_Val + 1;
			}
			--_Start_ptr;
		}
	}
	template <typename Typex>
	void Get_Unsigned(Typex& _Val) {
		if (_Ok) {
			char ch;
			Get_Char(ch);
			while ((ch < 48 || ch > 57) && ch != -1) {
				Get_Char(ch);
			}
			for (_Val = 0; ch > 47 && ch < 58 && ch != -1; Get_Char(ch)) {
				_Val = _Val * 10 + (ch ^ 48);
			}
			--_Start_ptr;
		}
	}
	template <typename Typex>
	void Get_Double(Typex& _Val) {
		if (_Ok) {
			char ch;
			bool _F = 0;
			for (Get_Char(ch); (ch < 48 || ch > 57) && ch != -1; Get_Char(ch)) {
				_F = ch == 45;
			}
			for (_Val = 0; ch > 47 && ch < 58 && ch != -1; Get_Char(ch)) {
				_Val = _Val * 10 + (ch ^ 48);
			}
			if (ch == 46) {
				unsigned long long _Pow = 1;
				for (Get_Char(ch); ch > 47 && ch < 58 && ch != -1; Get_Char(ch)) {
					_Val += Typex((ch ^ 48) * 1.0 / (_Pow *= 10));
				}
			}
			if (_F) {
				_Val = -_Val;
			}
			--_Start_ptr;
		}
	}
	Fast_Istream& Fast_Istream::operator>>(bool& _Val) {
		if (_Ok) {
			char ch;
			Get_Char(ch);
			while (ch == 32 || ch == 10 || ch == 13 || ch == 8 || ch == 9 || ch == 7 ||
				ch == 12 || ch == 11) {
				Get_Char(ch);
			}
			while (ch != 32 && ch != 10 && ch && ch != -1 && ch != 9 && ch != 11 &&
				ch != 12) {
				_Val |= ch != 48;
				Get_Char(ch);
			}
			--_Start_ptr;
		}
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(short& _Val) {
		Get_Int(_Val);
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(int& _Val) {
		Get_Int(_Val);
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(long& _Val) {
		Get_Int(_Val);
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(long long& _Val) {
		Get_Int(_Val);
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(unsigned short& _Val) {
		Get_Unsigned(_Val);
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(unsigned int& _Val) {
		Get_Unsigned(_Val);
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(unsigned long& _Val) {
		Get_Unsigned(_Val);
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(unsigned long long& _Val) {
		Get_Unsigned(_Val);
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(float& _Val) {
		Get_Double(_Val);
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(double& _Val) {
		Get_Double(_Val);
		return *this;
	}
	Fast_Istream& Fast_Istream::operator>>(long double& _Val) {
		Get_Double(_Val);
		return *this;
	}
	template <typename Typex, typename... More>
	void Fast_Istream::operator()(Typex& _Val, More&... _More) {
		*this >> _Val;
		operator()(_More...);
	}
	void Fast_Istream::pop() {
		char ch;
		Get_Char(ch);
	}
	char Fast_Istream::peek() {
		if (_Start_ptr == _End_ptr) {
			_Start_ptr = _Buf;
			_End_ptr = _Buf + inbuf->sgetn(_Buf, Size);
		}
		if (_Start_ptr == _End_ptr) {
			_Ok = 0;
			return -1;
		} else {
			return *_Start_ptr;
		}
	}
}  // namespace Fast_I
namespace Fast_O {
	Fast_Ostream::Fast_Ostream(std::streambuf* out, unsigned int Size) {
		buf.reserve(Size);
		outbuf = out;
	}
	Fast_Ostream::Fast_Ostream(const char* File, unsigned int Size) {
		buf.reserve(Size);
		rdbuf(File);
	}
	void Fast_Ostream::rdbuf(const char* File) {
		static std::ofstream __Out__(File);
		rdbuf(__Out__.rdbuf());
	}
	Fast_Ostream::Fast_Ostream(unsigned int Size) {
		buf.reserve(Size);
	}
	void Fast_Ostream::flush() {
		outbuf->sputn(buf.data(), buf.size());
		buf.clear();
	}
	Fast_Ostream::~Fast_Ostream() {
		flush();
	}
	Fast_Ostream& Fast_Ostream::operator<<(char _Val) {
		buf.push_back(_Val);
		return *this;
	}
	Fast_Ostream& Fast_Ostream::operator<<(const char* _Val) {
		while (*_Val) {
			buf.push_back(*_Val++);
		}
		return *this;
	}
	Fast_Ostream& Fast_Ostream::operator<<(const std::string& _Val) {
		for (auto&& i : _Val) {
			buf.push_back(i);
		}
		return *this;
	}
	template <typename Typex>
	void Put_Unsigned(Typex _Val) {
		char* _Stack = (char*)malloc(sizeof(Typex) * 3);
		unsigned S_top = 0;
		while (_Val) {
			_Stack[++S_top] = (_Val % 10) ^ 48;
			_Val /= 10;
		}
		if (!S_top) {
			buf.push_back('0');
		}
		while (S_top) {
			buf.push_back(_Stack[S_top--]);
		}
		free(_Stack);
	}
	void Put_Int(long long _Val) {
		if (_Val < 0) {
			buf.push_back('-');
			Put_Unsigned(~_Val + 1);
		} else {
			Put_Unsigned(_Val);
		}
	}
	Fast_Ostream& Fast_Ostream::operator<<(bool _Val) {
		buf.push_back(_Val ? '1' : '0');
		return *this;
	}
	Fast_Ostream& Fast_Ostream::operator<<(short _Val) {
		Put_Int(_Val);
		return *this;
	}
	Fast_Ostream& Fast_Ostream::operator<<(int _Val) {
		Put_Int(_Val);
		return *this;
	}
	Fast_Ostream& Fast_Ostream::operator<<(long _Val) {
		Put_Int(_Val);
		return *this;
	}
	Fast_Ostream& Fast_Ostream::operator<<(long long _Val) {
		Put_Int(_Val);
		return *this;
	}
	Fast_Ostream& Fast_Ostream::operator<<(unsigned short _Val) {
		Put_Unsigned(_Val);
		return *this;
	}
	Fast_Ostream& Fast_Ostream::operator<<(unsigned int _Val) {
		Put_Unsigned(_Val);
		return *this;
	}
	Fast_Ostream& Fast_Ostream::operator<<(unsigned long _Val) {
		Put_Unsigned(_Val);
		return *this;
	}
	Fast_Ostream& Fast_Ostream::operator<<(unsigned long long _Val) {
		Put_Unsigned(_Val);
		return *this;
	}
	template <typename Typex>
	void Fast_Ostream::endl(const Typex& _Val) {
		*this << _Val << '\n';
	}
	template <typename Typex, typename... More>
	void Fast_Ostream::endl(const Typex& _Val, const More&... _More) {
		*this << _Val;
		endl(_More...);
	}
	template <typename Typex>
	void Fast_Ostream::operator()(const Typex& _Val) {
		*this << _Val;
	}
	template <typename Typex, typename... More>
	void Fast_Ostream::operator()(const Typex& _Val, const More&... _More) {
		*this << _Val;
		operator()(_More...);
	}
}  // namespace Fast_O
2024/1/25 17:36
加载中...