2157: 旅游

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 1347  Solved: 619
[Submit][Status][Discuss]

Description

Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。

Input

输入的第一行包含一个整数N,表示T 城中的景点个数。景点编号为 0...N − 1。接下来N − 1 行,每行三个整数u、v 和w,表示有一条u 到v,使 Ray 愉悦度增加w 的桥。桥的编号为1...N − 1。|w| <= 1000。输入的第N + 1 行包含一个整数M,表示Ray 的操作数目。接下来有M 行,每行描述了一个操作,操作有如下五种形式: C i w,表示Ray 对于经过第i 座桥的愉悦度变成了w。 N u v,表示Ray 对于经过景点u 到v 的路径上的每一座桥的愉悦度都变成原来的相反数。 SUM u v,表示询问从景点u 到v 所获得的总愉悦度。 MAX u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最大愉悦度。 MIN u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最小愉悦度。测试数据保证,任意时刻,Ray 对于经过每一座桥的愉悦度的绝对值小于等于1000。

Output

对于每一个询问(操作S、MAX 和MIN),输出答案。

Sample Input

3
0 1 1
1 2 2
8
SUM 0 2
MAX 0 2
N 0 1
SUM 0 2
MIN 0 2
C 1 3
SUM 0 2
MAX 0 2

Sample Output

3
2
1
-1
5
3

HINT

一共有10 个数据,对于第i (1 <= i <= 10) 个数据, N = M = i * 2000。

Source

 

[Submit][Status][Discuss]

Link-Cut-Tree 模板题

维护子树(路径)最值、权值和

 #include <cstdio>

 template <class T>
inline T swap(T &a, T &b)
{
T c;
c = a;
a = b;
b = c;
} template <class T>
inline T max(const T &a, const T &b)
{
return a > b ? a : b;
} template <class T>
inline T min(const T &a, const T &b)
{
return a < b ? a : b;
} const int mxn = ;
const int inf = 1e9 + ; int n, m, val[mxn]; struct node
{
int sum;
int maxi;
int mini;
bool neg;
bool rev;
node *son[];
node *father;
}tree[mxn]; inline bool isRoot(node *t)
{
node *&f = t->father; if (f == NULL)return true; if (f->son[] == t)return false;
if (f->son[] == t)return false; return true;
} inline void addNeg(node *t)
{
t->neg ^= true; swap(t->maxi, t->mini); t->sum = -t->sum;
t->maxi = -t->maxi;
t->mini = -t->mini; if (t - tree >= n)
{ // edge
int id = t - tree - n; val[id] = -val[id];
}
} inline void pushNeg(node *t)
{
if (t->neg)
{
t->neg = false; if (t->son[] != NULL)addNeg(t->son[]);
if (t->son[] != NULL)addNeg(t->son[]);
}
} inline void addRev(node *t)
{
t->rev ^= true; swap(t->son[], t->son[]);
} inline void pushRev(node *t)
{
if (t->rev)
{
t->rev = false; if (t->son[] != NULL)addRev(t->son[]);
if (t->son[] != NULL)addRev(t->son[]);
}
} inline void update(node *t)
{
if (t - tree < n)
{ // point
t->sum = ;
t->maxi = -inf;
t->mini = +inf;
}
else
{ // edge
int id = t - tree - n; t->sum = val[id];
t->maxi = val[id];
t->mini = val[id];
} if (t->son[] != NULL)
{
t->sum += t->son[]->sum;
t->maxi = max(t->maxi, t->son[]->maxi);
t->mini = min(t->mini, t->son[]->mini);
}
if (t->son[] != NULL)
{
t->sum += t->son[]->sum;
t->maxi = max(t->maxi, t->son[]->maxi);
t->mini = min(t->mini, t->son[]->mini);
}
} inline void connect(node *t, node *f, bool k)
{
if (t != NULL)t->father = f;
if (f != NULL)f->son[k] = t;
} inline void rotate(node *t)
{
node *f = t->father;
node *g = f->father; bool s = f->son[] == t; connect(t->son[!s], f, s);
connect(f, t, !s); t->father = g;
if (g && g->son[] == f)g->son[] = t;
if (g && g->son[] == f)g->son[] = t; update(f);
update(t);
} inline void pushdown(node *t)
{
pushNeg(t);
pushRev(t);
} inline void pushDown(node *t)
{
static node *stk[mxn]; int top = ; stk[++top] = t; while (!isRoot(t))
stk[++top] = t = t->father; while (top)pushdown(stk[top--]);
} inline void splay(node *t)
{
pushDown(t); while (!isRoot(t))
{
node *f = t->father;
node *g = f->father; if (isRoot(f))
rotate(t);
else
{
bool a = f && f->son[] == t;
bool b = g && g->son[] == f; if (a == b)
rotate(f), rotate(t);
else
rotate(t), rotate(t);
}
}
} inline void access(node *t)
{
node *q = t;
node *p = NULL; while (t != NULL)
{
splay(t);
t->son[] = p, update(t);
p = t, t = t->father;
} splay(q);
} inline void makeRoot(node *t)
{
access(t), addRev(t);
} inline void link(node *t, node *f)
{
makeRoot(t), t->father = f;
} signed main(void)
{
scanf("%d", &n); for (int i = ; i <= n; ++i)
update(tree + i);
for (int i = , x, y, w; i < n; ++i)
{
scanf("%d%d%d", &x, &y, &w);
val[i] = w, update(tree + i + n);
link(tree + x, tree + i + n);
link(tree + y, tree + i + n);
} scanf("%d", &m); while (m--)
{
static int x, y;
static char s[]; scanf("%s%d%d", s, &x, &y); if (s[] == 'C')
access(tree + x + n), val[x] = y, update(tree + x + n);
else if (s[] == 'N')
makeRoot(tree + x), access(tree + y), addNeg(tree + y);
else if (s[] == 'S')
makeRoot(tree + x), access(tree + y), printf("%d\n", tree[y].sum);
else if (s[] == 'A')
makeRoot(tree + x), access(tree + y), printf("%d\n", tree[y].maxi);
else
makeRoot(tree + x), access(tree + y), printf("%d\n", tree[y].mini);
}
}

@Author: YouSiki

最新文章

  1. RecyclerView 的介绍以及多布局的实例
  2. 使用Proj库进行大地坐标转空间坐标、投影坐标的一个示例
  3. ffplay代码播放pcm数据
  4. matlab中patch函数的用法
  5. Making the Newsfeed web part available outside of My Sites in SharePoint 2013 分类: Sharepoint 2015-07-07 19:29 4人阅读 评论(0) 收藏
  6. enter键实现自动登录
  7. java web多线程
  8. sql常识-top
  9. 92. Reverse Linked List II
  10. JPEG图像密写研究(一) JPEG图像文件结构
  11. QUIC简单介绍
  12. LigerUI权限系统之角色管理
  13. python多进程拷贝数据
  14. Spring Security(二十三):6.5 The Default AccessDecisionManager(默认接入策略管理)
  15. 洛谷P1196 银河英雄传说
  16. 简单封装kafka相关的api
  17. C++反转单链表
  18. Wifi 开放系统认证和共享密钥身份认证
  19. curl 超时设置&lt;转&gt;
  20. Unity3D - 资源管理

热门文章

  1. Java字符串分割
  2. Spring入门学习笔记(1)
  3. 分享一篇IBN(Intent-based networking)调研报告
  4. passwd命令详解
  5. java内存结构JVM——java内存模型JMM——java对象模型JOM
  6. podSpec文件相关知识整理
  7. 读取classpath配置文件的方法
  8. JS进阶系列之闭包
  9. TeamWork#3,Week5,Introduction to the &quot;take-away&quot; Sale Selection Project
  10. Daily Srum 10.21