【BZOJ】2157: 旅游
http://www.lydsy.com/JudgeOnline/problem.php?id=2157
题解:裸lct不解释..
#include <bits/stdc++.h>
using namespace std;
struct node *null;
struct node {
node *c[2], *f;
bool flag, rev, tag; int k, sum, mx, mn;
bool d() { return f->c[1]==this; }
void setc(node *x, bool d) { c[d]=x; x->f=this; }
bool check() { return f->c[0]==this || f->c[1]==this; }
void upd() {
if(this==null) return;
tag=!tag; sum=-sum; k=-k;
swap(mx, mn); mx=-mx; mn=-mn;
}
void upd1() {
if(this==null) return;
rev=!rev; swap(c[0], c[1]);
}
void pushup() {
sum=c[0]->sum+c[1]->sum+k*flag;
mx=max(c[0]->mx, c[1]->mx); if(flag) mx=max(mx, k);
mn=min(c[0]->mn, c[1]->mn); if(flag) mn=min(mn, k);
}
void pushdown() {
if(tag) c[0]->upd(), c[1]->upd(), tag=0;
if(rev) c[0]->upd1(), c[1]->upd1(), rev=0;
}
}T[2000005], *it=T;
node *newnode(int k, bool flag) {
node *x=it++;
x->c[0]=x->c[1]=x->f=null; x->k=x->sum=k;
x->tag=x->rev=0; x->flag=flag;
if(flag) x->mx=x->mn=k;
else x->mx=-1005, x->mn=1005;
return x;
}
void rot(node *x) {
node *f=x->f;
f->pushdown(); x->pushdown(); bool d=x->d();
if(f->check()) f->f->setc(x, f->d());
else x->f=f->f;
f->setc(x->c[!d], d);
x->setc(f, !d);
f->pushup();
}
void fix(node *x) { if(x->check()) fix(x->f); x->pushdown(); }
void splay(node *x) {
fix(x);
while(x->check())
if(!x->f->check()) rot(x);
else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
x->pushup();
}
node *access(node *x) {
node *y=null;
for(; x!=null; y=x, x=x->f) splay(x), x->c[1]=y;
return y;
}
void mkroot(node *x) { access(x)->upd1(); splay(x); }
void split(node *x, node *y) { mkroot(x); access(y); splay(y); }
void link(node *x, node *y) { mkroot(x); x->f=y; } void init() {
null=it++;
null->c[0]=null->c[1]=null->f=null;
null->k=null->sum=0;
null->rev=null->flag=null->tag=0;
null->mx=-1005; null->mn=1005;
} void change(node *x, int k) {
splay(x);
x->k=k;
x->pushup();
}
void update(node *x, node *y) {
split(x, y);
y->upd();
}
void getsum(node *x, node *y) {
split(x, y); printf("%d\n", y->sum);
}
void getmx(node *x, node *y) {
split(x, y); printf("%d\n", y->mx);
}
void getmn(node *x, node *y) {
split(x, y); printf("%d\n", y->mn);
}
node *nd[200005], *ed[200005];
int main() {
init();
int n, q;
scanf("%d", &n);
for(int i=0; i<n; ++i) nd[i]=newnode(0, 0);
for(int i=1; i<n; ++i) {
int x, y, k; scanf("%d%d%d", &x, &y, &k);
ed[i]=newnode(k, 1);
link(nd[x], ed[i]); link(ed[i], nd[y]);
}
scanf("%d", &q); char s[5];
while(q--) {
int x, y;
scanf("%s%d%d", s, &x, &y);
if(s[0]=='C') change(ed[x], y);
else if(s[0]=='N') update(nd[x], nd[y]);
else if(s[0]=='S') getsum(nd[x], nd[y]);
else {
if(s[1]=='A') getmx(nd[x], nd[y]);
else getmn(nd[x], nd[y]);
}
}
return 0;
}
lct倒是半小时1次码好无错误 = =可是坑爹的题 :编号从0~n-1。。。坑了我好久啊= =
当练手速= =
【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 < ...
随机推荐
- DedeCMS Error: (PHP 5.3 and above) Please set request_order
部分使用PHP 5.3的主机可能会有下面的提示: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and ...
- ckplayer视频播放插件使用
研究ckplayer插件播放视频,播放视频需要配置信息修改如下: 1.设置ckplayer.js中的logo: 'null' 可以隐藏视频播放头部的图标: 2.设置ckplayer.js中的ckcpt ...
- 攻城狮在路上(壹) Hibernate(二)--- 第一个hibernate程序
1.直接通过JDBC API持久化实体域对象: A.java.sql常用接口和类: DriverManager:驱动程序管理器,负责创建数据库连接. Connection:代表数据库连接. State ...
- ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 350人阅读 评论(0) 收藏
问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...
- gnuplot安装问题(set terminal "unknown")
今天在系统同上要装个gnuplot,原来用的都是拷好的虚拟机.这也是第一次装.本来以为分分钟的事,却不料遇到不少麻烦.记录一下,供大家参考 一,快速开始安装 ubuntu下那自然是: sudo apt ...
- hdu 4063 福州赛区网络赛 圆 ****
画几个图后,知道路径点集一定是起点终点加上圆与圆之间的交点,枚举每两个点之间是否能走,能走则连上线,然后求一遍最短路即可 #include<cstdio> #include<cstd ...
- [Linux] yum和apt-get用法及区别
一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包 ...
- 在Salesforce中处理Email的发送
在Salesforce中可以用自带的 Messaging 的 sendEmail 方法去处理Email的发送 请看如下一段简单代码: public boolean TextFormat {get;se ...
- Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
E. George and Cards George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...
- 【项目总结】之——JS分割字符串
背景: 在我们做那个招标项目的时候,由于是刚刚接触到这个BS东西,我基本上是什么也不会.可是当时组长浩哥给过我一个任务,就是叫我将数据里面以字符串形式存在的信息切割开,然后显示到前台上去.当时对于浩哥 ...