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

BZOJ 2157: 旅游的更多相关文章

  1. BZOJ 2157: 旅游( 树链剖分 )

    树链剖分.. 样例太大了根本没法调...顺便把数据生成器放上来 -------------------------------------------------------------------- ...

  2. bzoj 2157: 旅游 (LCT 边权)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2157 题面; 2157: 旅游 Time Limit: 10 Sec  Memory Lim ...

  3. 【刷题】BZOJ 2157 旅游

    Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间 ...

  4. BZOJ 2157 旅游(动态树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2157 [题目大意] 支持修改边,链上查询最大值最小值总和,以及链上求相反数 [题解] ...

  5. BZOJ 2157 旅游(树链剖分+线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2157 [题目大意] 支持修改边,链上查询最大值最小值总和,以及链上求相反数 [题解] ...

  6. BZOJ 2157: 旅游 (2017.7.21 6:30-2017.7.21 15:38 今日第一题。。)

    Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1754  Solved: 765 Description Ray 乐忠于旅游,这次他来到了T 城.T ...

  7. bzoj 2157: 旅游【树链剖分+线段树】

    裸的树链剖分+线段树 但是要注意一个地方--我WA了好几次才发现取完相反数之后max值和min值是要交换的-- #include<iostream> #include<cstdio& ...

  8. BZOJ 2157: 旅游 (树链剖分+线段树)

    树链剖分后线段树维护区间最大最小值与和. 支持单点修改与区间取反. 直接写个区间取反标记就行了.线段树板题.(200行6000B+ 1A警告) #include <cstdio> #inc ...

  9. BZOJ 2157: 旅游 (结构体存变量)

    用结构体存变量好像确实能提高运行速度,以后就这么写数据结构了 Code: #include <cstdio> #include <algorithm> #include < ...

随机推荐

  1. C#平均值计算器具体实现

    1. 题目及要求 2. Avg.cs 在直接编写窗口程序之前,我们需要创建一个Avg类,我们可以在类库中编辑,也可以像java一样直接在项目中新建类. 有关类库的创建与连接方法,我们在上一次的< ...

  2. SURF算法的一篇翻译与论证

    原文地址:http://www.sohu.com/a/157742015_715754 SURF: Speeded Up Robust Features 摘要 本文提出了一种新型的具有尺度和旋转不变特 ...

  3. Aria2 Linux 完整安装及使用教程

    Aria2 嘛,主要是用来离线下载,功能强大,支持 http/https 直链.ftp.电驴.磁力链接等等,且可以跨平台使用,配合网页端操作,简直是一代下载神器. 安装 Debian/Ubuntu: ...

  4. OLAP和OLTP的区别

    OLAP(On-Line Analytical Processing)联机分析处理,也称为面向交易的处理过程,其基本特征是前台接收的用户数据可以立即传送到计算中心进行处理,并在很短的时间内给出处理结果 ...

  5. [文章存档]Kudu 的 Debug Console 窗口如何查看更多文件

    链接:https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-service-web-h ...

  6. DevOps on AWS之Elastic BeanStalk

    Elastic BeanStalk相关概念 童话世界中存在着一种魔力beanstalk(豆荚),种在花盆里可以无限的向上生长,越长越高直达云端.AWS Elastic Beanstalk也采用类似概念 ...

  7. Shell重新学习(忘光了)

    成功和失败都放同一个文件 调试shell

  8. js 基础拓展

    1.关于 try catch 的用法 <body> <div>请输出一个 5 到 10 之间的数字:</div> <input id="demo&q ...

  9. C++ 类的定义与实现

    摘自这篇博客https://blog.csdn.net/xulingxin/article/details/81335030   一."类" 的介绍     在C++中, 用 &q ...

  10. PSP Daily新增功能说明书

    1.选择输入类别时可以记录原来的输入,支持用户选择记录清空功能 2.添加“恢复最近”button,点击这个按钮可以跳出一个页面显示最近的excel记录,用户可以通过勾选相应的excel文件名,恢复选中 ...