HDU5052 Yaoge’s maximum profit(LCT)

典型的LCT操作,但是维护的是一个序列最左边减最右边的最小值,所以要维护左边减右边的最小值del[0]和一个右边减左边的最小值del[1](因为rev标记swap的时候对应的值也要交换)。维护的时候del[0]可能是来自于左右儿子的del[0],也有可能是来自于左儿子的最小值减去右儿子及当前节点的值的最大值,还有就是当前节点减去右儿子的最大值,比赛的时候漏了当前节点减去右儿子的最大值因而WA了。。- -0

#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <assert.h>
using namespace std;

#define ll long long
#define maxn 200000
#define INF 0x3f3f3f3f

struct Node
{
	Node *p, *ch[2];
	bool rev;
	int val;
	int mx, mi;
	int add;
	int del[2];
	int size;
	bool isRoot;
	Node *fa;
	Node(){
		val = 0;
		rev = 0; mx = -INF; mi = INF;
		del[0] = del[1] = 0;
		add = 0;
		size = 0;
	}
	void setc(Node *c, int d){
		ch[d] = c;
		c->p = this;
	}
	bool d(){
		return p->ch[1] == this;
	}
	void upd(){
		size = ch[0]->size + ch[1]->size + 1;
		mx = max(max(ch[0]->mx, ch[1]->mx), val);
		mi = min(min(ch[0]->mi, ch[1]->mi), val);
		del[0] = min(ch[0]->del[0], ch[1]->del[0]);
		del[0] = min(del[0], ch[0]->mi - max(val, ch[1]->mx));
		del[0] = min(del[0], val - ch[1]->mx);

		del[1] = min(ch[0]->del[1], ch[1]->del[1]);
		del[1] = min(del[1], ch[1]->mi - max(val, ch[0]->mx));
		del[1] = min(del[1], val - ch[0]->mx);

	}
	void revIt(){
		swap(ch[0], ch[1]);
		swap(del[0], del[1]);
		rev ^= 1;
	}
	void addIt(int vx){
		add += vx;
		val += vx;
		mx += vx;
		mi += vx;
	}
	void relax();
	void setRoot(Node *f);
}Tnull, *null = &Tnull;

void Node::setRoot(Node *f){
	fa = f;
	isRoot = true;
	p = null;
}

void Node::relax(){
	if (add != 0){
		for (int i = 0; i < 2; i++){
			if (ch[i] != null) ch[i]->addIt(add);
		}
		add = 0;
	}
	if (rev){
		for (int i = 0; i < 2; i++){
			if (ch[i] != null) ch[i]->revIt();
		}
		rev = 0;
	}
}

Node mem[maxn], *C = mem;

Node *make(int v){
	C->val = v;
	C->mx = C->mi = v;
	C->del[0] = C->del[1] = 0;
	C->rev = 0; C->add = 0;
	C->ch[0] = C->ch[1] = null; C->isRoot = true;
	C->p = null;
	C->fa = null;
	return C++;
}

void rot(Node *t){
	Node *p = t->p;
	p->relax();
	t->relax();
	bool d = t->d();
	p->p->setc(t, p->d());
	p->setc(t->ch[!d], d);
	t->setc(p, !d);
	p->upd();
	if (p->isRoot){
		p->isRoot = false;
		t->isRoot = true;
		t->fa = p->fa;
	}
}

void pushTo(Node*t) {
	static Node*stk[maxn]; int top = 0;
	while (t != null) {
		stk[top++] = t;
		t = t->p;
	}
	for (int i = top - 1; i >= 0; --i) stk[i]->relax();
}

void splay(Node*u, Node*f = null) {
	pushTo(u);
	while (u->p != f) {
		if (u->p->p == f)
			rot(u);
		else
			u->d() == u->p->d() ? (rot(u->p), rot(u)) : (rot(u), rot(u));
	}
	u->upd();
}

Node *v[maxn];
vector<int> E[maxn];
int n, nQ;

int que[maxn], fa[maxn], qh = 0, qt = 0;
int wht[maxn];

void bfs()
{
	qh = qt = 0;
	que[qt++] = 1;
	fa[1] = -1;
	while (qh < qt){
		int u = que[qh++];
		for (int i = 0; i < E[u].size(); i++){
			int e = E[u][i];
			if (e != fa[u]){
				fa[e] = u;
				v[e]->fa = v[u];
				que[qt++] = e;
			}
		}
	}
}

Node *expose(Node *u)
{
	Node *v;
	for (v = null; u != null; v = u, u = u->fa){
		splay(u);
		u->ch[1]->setRoot(u);
		u->setc(v, 1);
		v->fa = u;
	}
	return v;
}

void makeRoot(Node *u)
{
	expose(u);
	splay(u);
	u->revIt();
}

void addPath(Node *u, Node *v, int ax)
{
	makeRoot(u);
	expose(v);
	splay(v);
	v->addIt(ax);
}

int deltaPath(Node *u, Node *v)
{
	makeRoot(u);
	expose(v);
	splay(v);
	return v->del[0];
}

int main()
{
	int T; cin >> T;
	while (T--){
		C = mem;
		scanf("%d", &n);
		for (int i = 0; i <= n; ++i) E[i].clear();
		for (int i = 1; i <= n; ++i){
			scanf("%d", wht + i);
			v[i] = make(wht[i]);
		}
		int ui, vi;
		for (int i = 0; i < n - 1; i++){
			scanf("%d%d", &ui, &vi);
			E[ui].push_back(vi); E[vi].push_back(ui);
		}
		bfs();
		scanf("%d", &nQ);
		int xi, yi, vv;
		Node *nx, *ny;
		while (nQ--){
			scanf("%d%d%d", &xi, &yi, &vv);
			nx = v[xi]; ny = v[yi];
			int ans = deltaPath(nx, ny);
			if (ans < 0) ans = -ans;
			else ans = 0;
			assert(ans >= 0);
			printf("%d\n", ans);
			addPath(nx, ny, vv);
		}
	}
	return 0;
}
时间: 2024-10-14 16:11:02

HDU5052 Yaoge’s maximum profit(LCT)的相关文章

HDU5052 Yaoge’s maximum profit(树链剖分)点权更新,经典题

Yaoge's maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 662    Accepted Submission(s): 182 Problem Description Yaoge likes to eat chicken chops late at night. Yaoge has eaten to

hdu 5052 Yaoge’s maximum profit

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 442    Accepted Submission(s): 122 Problem Description Yaoge likes to eat chicken chops late at night. Yaoge has eaten too

Hdu 5052 Yaoge’s maximum profit(树链剖分)

题目大意: 给出一棵树,每个点有商店,每个商店都有一个价格,Yaoge每次从x走到y都可以在一个倒卖商品,从中得取利益,当然,买一顶要在卖之前.但是没次走过一条路,这条路上的所有商品都会增加一个v. 输出每次的最大利益. 思路分析: 很容易想到树链剖分,可是关键在于如何维护这样一个变量,使得每次都要让买的再卖的前面. 维护变量 ltr 和 rtl ,表示从左去右和从右去左. 剖分熟练的时候,判断x 和 y的深度,一步一步往上爬. 然后维护区间最大值和最小值,爬的时候更新答案. ...4921ms

HDU 5052 Yaoge’s maximum profit 裸树链剖分 2014 ACM/ICPC Asia Regional Shanghai Online

题意: 给定n个点的带点权树. 下面n行给出每个点点权表示每个点买卖鸡腿的价格 下面n-1行给出树边 下面Q个操作 Q行 u, v, val 从u走到v,过程中可以买一个鸡腿,然后到后面卖掉,输出max(0, 最大的收益) 然后给[u,v]路径上点点权+=val 思路: 树链剖分裸题 屌丝题解:点击打开链接 #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include &

Maximum profit of stocks

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Large%20E-Commerce%20Company/E-Commerce%20Company%20-%20Interview%20Problems%20-%20SOLUTIONS/On-Site%20Question%201%20-%20SOLUTION.ipyn

[Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]

咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\(n\)的严格单调上升数组\(d_i\),求\(\max\limits_{1 \le l \le r \le n} (a\cdot(r-l+1)-\sum_{i=l}^{r}c_i-gap(l,r))\),其中\(gap(l, r) = \max\limits_{l \le i < r} (d_{i

HDU 5052 LCT

Yaoge's maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 516    Accepted Submission(s): 150 Problem Description Yaoge likes to eat chicken chops late at night. Yaoge has eaten to

hdu 5052 树链剖分

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 982    Accepted Submission(s): 274 Problem Description Yaoge likes to eat chicken chops late at night. Yaoge has eaten too

【HDU 5855】Less Time, More profit(网络流、最小割、最大权闭合子图)

Less Time, More profit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description The city planners plan to build N plants in the city which has M shops. Each shop needs products from some plants to make p