典型的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;
}

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

  1. Yaoge’s maximum profit HDU - 5052

    Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

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

    题目大意: 给出一棵树.每一个点有商店.每一个商店都有一个价格,Yaoge每次从x走到y都能够在一个倒卖商品,从中得取利益.当然,买一顶要在卖之前.可是没次走过一条路,这条路上的全部商品都会添加一个v ...

  3. HDU 5052 Yaoge’s maximum profit 光秃秃的树链拆分 2014 ACM/ICPC Asia Regional Shanghai Online

    意甲冠军: 特定n小点的树权. 以下n每一行给出了正确的一点点来表达一个销售点每只鸡价格的格 以下n-1行给出了树的侧 以下Q操作 Q行 u, v, val 从u走v,程中能够买一个鸡腿,然后到后面卖 ...

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

    咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...

  5. Maximum profit of stocks

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  6. Maximum Profit

    Maximum Profit You can obtain profits from foreign exchange margin transactions. For example, if you ...

  7. Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈

    Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...

  8. 【leetcode】1235. Maximum Profit in Job Scheduling

    题目如下: We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtai ...

  9. codeforces1107G Vasya and Maximum Profit 【模拟】

    题目分析: 前缀和啥的模拟一下就行了. 代码: #include<bits/stdc++.h> using namespace std; ; int n,x,d[maxn],sta[max ...

随机推荐

  1. C# WPF使用ZXing生成二维码ImageSource

    介绍: 如果需要实在WPF窗体程序中现类似如下的二维码图片生成功能,可以通过本文的方法实现 添加步骤: 1.在http://zxingnet.codeplex.com/站点上下载ZXing .Net的 ...

  2. Helloworld模块之内核makefile详解

    Hello World 模块以及对应的内核makefile详解 hello.c: #include <linux/module.h> //所有模块都需要的头文件 #include < ...

  3. hdu 4593 Robot

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4593 Robot Description A robot is a mechanical or vir ...

  4. core java 5~6(OOP & 高级语言特征)

    MODULE 5 OOP 面向对象程序设计--------------------------------------------------------Object Oriented Program ...

  5. Android UI 组件 » GifView

    GifView 是一个为了解决android中现在没有直接显示gif的view,只能通过mediaplay来显示这个问题的项目,其用法和 ImageView一样,支持gif图片 使用方法: 1-把Gi ...

  6. golang面向对象初识

    struct是变量的集合 interface是方法的集合 struct与interface都支持匿名字段, 换言之, 支持组合实现继承. golang的struct与C++的class一样, 只能声明 ...

  7. Go defer延迟执行

    defer用于延迟执行,可以类比于java或c++中的析构函数. 查看一段示例代码: func Contents(filename string) (string, error) { //打开文件 f ...

  8. 基于jquery的页面代码的优化

    高亮显示,选中的文字链接 显示效果如下 默认选择“通知公告”效果 通知公告 学院动态 行业动态       选择“学院动态”效果 通知公告 学院动态 行业动态       选择“行业动态”效果 通知公 ...

  9. [转]windows 软链接的建立及删除

    [转]windows 软链接的建立及删除 http://blog.chinaunix.net/uid-74941-id-3764093.html 1.建立举例 ##建立d:develop链接目录,指向 ...

  10. C#泛型集合之Dictionary<k, v>使用技巧

    1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value) ...