【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 < ...
随机推荐
- 翻译《Writing Idiomatic Python》(五):类、上下文管理器、生成器
原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...
- NOI 2002 营业额统计 (splay or fhq treap)
Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...
- 协议的分用以及wireshark对协议的识别
在TCP/IP详解一书中谈到了协议的分用,书中的图1-8如上.图1-8可以很好地解释在互联网的分层结构中,底层的协议头是如何承载上层的不同的协议的.对于链路层而言,以太网首部中有不同帧类型用于表示以太 ...
- ubuntu下nginx+php5的部署
ubuntu下nginx+php5环境的部署和centos系统下的部署稍有不同,废话不多说,以下为操作记录:1)nginx安装root@ubuntutest01-KVM:~# sudo apt-get ...
- Delphi项目的构成
Hello.cfg 項目配置文件 Hello.dof 項目選項文件 Hello.dpr 項目文件 Hello.exe 應用程序 Hello.res 資源文件 HelloWorld.dcu 窗口編譯文件 ...
- 03Spring_bean的创建和作用域以及生命周期
bean的三种创建方式: 方式一: 使用类构造器实例化对象 <!-- 方式一 使用构造器(无参数)实例化对象 --> <bean id="bean1" cla ...
- translateZ 带来的Z-index 问题
今天遇到了一个问题,当一个3D变换元素translateZ这个属性的值为负值的时候,这个元素的Z-index就不会其作用,解决方法就是translateZ的值必须大于等于0才能让Z-index 起作用 ...
- matlab jet color mapping C / C++ / VC 实现
在matlab中调用imagesc()将一幅灰阶图像以彩色显示时,默认使用的color mapping是Jet,其color bar 为: Jet的color mapping图为: Color map ...
- Android -- Apk安装简诉
安装涉及到如下几个目录 system/app 系统自带的应用程序,无法删除 data/app 用户程序安装的目录,有删除权限. 安装时把apk文件复制到此目录 data/data 存放 ...
- JavaEE 获取路径全攻略
本篇博客是 JavaWeb 应用服务器端在不同环境下获取文件路径的全面总结. 获取文件路径后主要应用的场景,读取 JavaWeb 自定义配置文件.在特定路径下生成各种类型的文件提供下载...... 想 ...