【BZOJ 2157】旅游
再水一道模板题,明天就要出发去参加二轮省选了赶紧复习复习模板。
链剖模板题,可是写链剖太麻烦了,还是写lct吧。
但这个lct比较麻烦了,因为边权有正有负,要统计最大值和最小值,这样点权赋为什么值都会妨碍统计。
想了半天,后来发现自己脑抽了,统计的时候特判一下当前点是点还是边不就可以了吗?
裸的模板题调了好久啊,因为后来往原先错误的程序加上上述特判时总是漏这漏那,以后一定要认真思考每个细节确保无误后再开始写代码,这样脑子里有一个清晰的框架,写起来更快,而且不容易出错,大大节省了调试的时间。切忌边想边写!!!这样写到最后会有很多bug,而且对于程序的每个函数的作用会比较模糊。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 20003;
const int inf = 0x7fffffff;
void read(int &k) {
k = 0; int fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 1) + (k << 3) + c - '0';
k = k * fh;
} struct node *null;
struct node {
node *ch[2], *fa;
int d, mx, mn, sum;
bool edge;
short rev, change;
bool pl() {return fa->ch[1] == this;}
void setc(node *r, bool c) {ch[c] = r; r->fa = this;}
bool check() {return fa == null || (fa->ch[0] != this && fa->ch[1] != this);}
void upd() {if (this == null) return; sum = -sum; d = -d; swap(mx, mn); mx = -mx; mn = -mn;}
void push() {
if (rev) {rev = 0; swap(ch[0], ch[1]); ch[0]->rev ^= 1; ch[1]->rev ^= 1;}
if (change) {
change = 0;
ch[0]->change ^= 1; ch[1]->change ^= 1;
ch[0]->upd(); ch[1]->upd();
}
}
void count() {
sum = ch[0]->sum + ch[1]->sum;
mx = max(ch[0]->mx, ch[1]->mx); mn = min(ch[0]->mn, ch[1]->mn);
if (edge) mx = max(mx, d), mn = min(mn, d), sum += d;
}
} pool[N + N], *p[N], *E[N]; namespace LCT {
void rotate(node *r) {
node *f = r->fa; bool c = r->pl();
if (f->check()) r->fa = f->fa;
else f->fa->setc(r, f->pl());
f->setc(r->ch[!c], c);
r->setc(f, !c);
f->count();
}
void update(node *r) {if (!r->check()) update(r->fa); r->push();};
void splay(node *r) {
update(r);
for(; !r->check(); rotate(r))
if (!r->fa->check()) rotate(r->pl() == r->fa->pl() ? r->fa : r);
r->count();
}
node *access(node *r) {
node *y = null;
for(; r != null; y = r, r = r->fa)
splay(r), r->ch[1] = y;
return y;
}
void changert(node *r) {access(r)->rev ^= 1; splay(r);}
void link(node *r, node *t) {changert(r); r->fa = t;}
int tot = 0;
node *newnode(int num, bool flag) {
node *t = &pool[++tot];
t->ch[0] = t->ch[1] = t->fa = null;
t->d = t->sum = num;
t->rev = t->change = 0;
t->edge = flag;
if (flag) t->mx = t->mn = num;
else t->mx = -inf, t->mn = inf;
return t;
}
void Build(int n) {
null = new node;
null->ch[0] = null->ch[1] = null->fa = null;
null->mx = -inf; null->mn = inf;
null->rev = null->change = null->edge = null->d = null->sum = 0;
for(int i = 0; i < n; ++i) p[i] = newnode(0, 0);
}
} int n, m;
int main() {
read(n);
LCT::Build(n);
int u, v, e, x, y;
for(int i = 1; i < n; ++i) {
read(u); read(v); read(e);
E[i] = LCT::newnode(e, 1);
LCT::link(p[u], E[i]); LCT::link(p[v], E[i]);
}
read(m);
char c[3];
for(int i = 1; i <= m; ++i) {
scanf("%s", c); read(u); read(v);
switch (c[0]) {
case 'C':
LCT::splay(E[u]);
E[u]->d = v; E[u]->count();
break;
case 'N':
LCT::changert(p[u]); LCT::access(p[v]); LCT::splay(p[v]);
p[v]->upd(); p[v]->change ^= 1;
break;
case 'S':
LCT::changert(p[u]); LCT::access(p[v]); LCT::splay(p[v]);
printf("%d\n", p[v]->sum);
break;
case 'M':
LCT::changert(p[u]); LCT::access(p[v]); LCT::splay(p[v]);
if (c[1] == 'A') printf("%d\n", p[v]->mx);
else printf("%d\n", p[v]->mn);
break;
}
}
return 0;
}
SDOI 2016 Round2 Day-1 Bless All,祝CreationAugust,TA,morestep,Vampire,Sunshine,Lcomyn,Rivendell学长们和fye学姐都进队!
【BZOJ 2157】旅游的更多相关文章
- BZOJ 2157: 旅游( 树链剖分 )
树链剖分.. 样例太大了根本没法调...顺便把数据生成器放上来 -------------------------------------------------------------------- ...
- bzoj 2157: 旅游 (LCT 边权)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2157 题面; 2157: 旅游 Time Limit: 10 Sec Memory Lim ...
- BZOJ 2157: 旅游
2157: 旅游 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1347 Solved: 619[Submit][Status][Discuss] ...
- 【刷题】BZOJ 2157 旅游
Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间 ...
- BZOJ 2157 旅游(动态树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2157 [题目大意] 支持修改边,链上查询最大值最小值总和,以及链上求相反数 [题解] ...
- BZOJ 2157 旅游(树链剖分+线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2157 [题目大意] 支持修改边,链上查询最大值最小值总和,以及链上求相反数 [题解] ...
- 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 ...
- bzoj 2157: 旅游【树链剖分+线段树】
裸的树链剖分+线段树 但是要注意一个地方--我WA了好几次才发现取完相反数之后max值和min值是要交换的-- #include<iostream> #include<cstdio& ...
- BZOJ 2157: 旅游 (树链剖分+线段树)
树链剖分后线段树维护区间最大最小值与和. 支持单点修改与区间取反. 直接写个区间取反标记就行了.线段树板题.(200行6000B+ 1A警告) #include <cstdio> #inc ...
- BZOJ 2157: 旅游 (结构体存变量)
用结构体存变量好像确实能提高运行速度,以后就这么写数据结构了 Code: #include <cstdio> #include <algorithm> #include < ...
随机推荐
- POJ 3608 Bridge Across Islands --凸包间距离,旋转卡壳
题意: 给你两个凸包,求其最短距离. 解法: POJ 我真的是弄不懂了,也不说一声点就是按顺时针给出的,不用调整点顺序. 还是说数据水了,没出乱给点或给逆时针点的数据呢..我直接默认顺时针给的点居然A ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- Java注解的使用
概念:java提供了一种原程序中的元素关联任何信息和任何元数据的途径和方法. Java中的常见注解 JDK自带注解: @Override//覆盖父类的方法 @Deprecated//表示方法过时了 @ ...
- jmeter beanshell内容
byte [] sampledata = ctx.getPreviousResult().getResponseData(); String smapledatastring = new Strin ...
- Zygote进程【1】——Zygote的诞生
在Android中存在着C和Java两个完全不同的世界,前者直接建立在Linux的基础上,后者直接建立在JVM的基础上.zygote的中文名字为"受精卵",这个名字很好的诠释了zy ...
- SOAP-XML请求(iOS应用下集成携程api)
用携程机票为例: 携程联盟 飞机票.门票 联盟ID:278639 站点ID:739462 密钥KEY:BE57B925-E8CE-4AA2-AC8E-3EE4BBBB686F API_URL:open ...
- JS 中如何判断字符串类型的数字
function isNumberStr(str){ var n = Number(str); return !isNaN(n); } console.log(isNumberStr('37')); ...
- Linux收集
1.rsync快速删除文件 rsync --delete -avH /empty /rmdir 选项说明: –delete-before 接收者在传输之前进行删除操作 –progress 在传输时显示 ...
- 01Spring_基本jia包的导入andSpring的整体架构and怎么加入日志功能
1.什么是Spring : v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:u ...
- 清北学堂2017NOIP冬令营入学测试P4749 F’s problem(f)
时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 这个故事是关于小F的,它有一个怎么样的故事呢. 小F是一个田径爱好者,这天它们城市里正在 ...