典型的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. WPF中多窗口共享静态属性

    由于我的DoubanFm在重新考虑之后,需要设置一个全局的CurrentSong,这个字段要让所有的VM都知道,而我同时又想把它作为我所有VM的共有属性.而且我想尽量减少代码的复制,提高重用.所以我做 ...

  2. 浅谈Objective—C中的面向对象特性

    Objective-C世界中的面向对象程序设计 面向对象称程序设计可能是现在最常用的程序设计模式.如何开发实际的程序是存在两个派系的-- 面向对象语言--在过去的几十年中,很多的面向对象语言被发明出来 ...

  3. 帮朋友 解决一道 LeetCode QJ上问题

    引言 对于刷题,自己是没能力的. 最经一个朋友同事考我一道数组题 . 也许能当面试分享吧. 娱乐娱乐. 事情的开始是这样的. 前言 题目 截图 大概意思 是 在一个 数组中,找出其中两个不重复出现的元 ...

  4. C语言--通用类型栈

    #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h&g ...

  5. SAP B1 ADDON 开发

    承接各类SAP B1 ADDON 开发. 有需要,请联系.

  6. iOS之push present 动画

    直接源码: - (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIVi ...

  7. How to Notify Command to evaluate in mvvmlight

    How to Raize Command to evalituate in mvvm In mvvmlight, we bind our control to the relaycommand obj ...

  8. jquery介绍

    1.jQuery (1)jQuery简介 是一个js框架(.js文件),它的最大特点是,使用选择器( 借鉴了css选择器的语法)查找要操作的节点,并且将这些 节点封装成一个jQuery对象,通过调用j ...

  9. 用MSBuild和Jenkins搭建持续集成环境(1)

     http://www.infoq.com/cn/articles/MSBuild-1 你或其他人刚刚写完了一段代码,提交到项目的版本仓库里面.但等一下,如果新提交的代码把构建搞坏了怎么办?万一出现编 ...

  10. C++输出四则运算设计题的思路

    一,(1)题目避免重复:使用srand(seed)函数进行随机化,随seed的不同,可以产生不同的随机数二,(1)控制数量:输入变量n控制三,(1)控制是否有乘除:(chengchu=0,没有乘除:c ...