再水一道模板题,明天就要出发去参加二轮省选了赶紧复习复习模板。

链剖模板题,可是写链剖太麻烦了,还是写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】旅游的更多相关文章

  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: 旅游

    2157: 旅游 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1347  Solved: 619[Submit][Status][Discuss] ...

  4. 【刷题】BZOJ 2157 旅游

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

  5. BZOJ 2157 旅游(动态树)

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

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

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

  7. 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 ...

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

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

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

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

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

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

随机推荐

  1. 第9章 用内核对象进行线程同步(3)_信号量(semaphore)、互斥对象(mutex)

    9.5 信号量内核对象(Semaphore) (1)信号量的组成 ①计数器:该内核对象被使用的次数 ②最大资源数量:标识信号量可以控制的最大资源数量(带符号的32位) ③当前资源数量:标识当前可用资源 ...

  2. centos7下彻底卸载LibreOffice方法【转载】

    http://linux.it.net.cn/CentOS/course/2014/0720/3211.html你可以尝试 yum erase libreoffice\* 或者 yum remove ...

  3. 创建Visual studio项目模板 vstemplate关键点纪要

    from:http://www.cnblogs.com/stickman/p/3454719.html 经过多次的实验,终于完美生成一个.VSIX的项目模板安装包,其中遇到不少问题与挫折,久经goog ...

  4. Dubbo架构设计详解

    from:http://shiyanjun.cn/archives/325.html Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解 ...

  5. Debian 8(Jessie) 安装自带Mysql

    执行命令 sudo apt-get install mysql-server 这会把mysql-client也装上, 版本都是5.5. 安装过程中会提示你输入两遍root口令. 用ps aux|gre ...

  6. 4815 江哥的dp题a

    4815 江哥的dp题a  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 给出一个长度为N的序列A(A1,A ...

  7. Java集合系列:-----------03ArrayList源码分析

    上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解ArrayLi ...

  8. 在 C# App 中嵌入 Chrome 浏览器使用 CefSharp

    介绍 以前曾试过在app中整合一个可靠又快速的web浏览器吗? 在本文中,你会学到如何轻松地将奇妙的CefSharp网页浏览器组件(基于Chromium)集成到你的C# app中. 然后,你可以使用此 ...

  9. Linux 退格键不回显

    在程序使用system("stty erase ^H");可以实现在输入状态下,按退格键删除字符,不回显. 调用tcsetattr修改linux基本输入的控制字符定义 //Linu ...

  10. 清北学堂2017NOIP冬令营入学测试P4749 F’s problem(f)

    时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 这个故事是关于小F的,它有一个怎么样的故事呢. 小F是一个田径爱好者,这天它们城市里正在 ...