[bzoj2843&&bzoj1180]极地旅行社 (lct)
双倍经验双倍的幸福。。。
所以另一道是300大洋的世界T_T。。。虽然题目是一样的,不过2843数据范围小了一点。。。
都是lct基本操作
#include<cstdio>
#include<iostream>
#include<math.h>
using namespace std;
const int maxn=;
struct zs{
int c[],fa,sum,val;
bool rev;
}tree[maxn];
int i,j,x,y,n,m;
int stack[maxn];
char id[];
inline bool isroot(int x){return tree[tree[x].fa].c[]!=x&&tree[tree[x].fa].c[]!=x;
}
inline void update(int x){tree[x].sum=tree[tree[x].c[]].sum+tree[x].val+tree[tree[x].c[]].sum;
}
inline void pushdown(int x){
if(!tree[x].rev)return;
int l=tree[x].c[],r=tree[x].c[];
if(l)tree[l].rev^=;if(r)tree[r].rev^=;tree[x].rev^=;
swap(tree[x].c[],tree[x].c[]);
}
inline void rotate(int x){
int fa=tree[x].fa,gfa=tree[fa].fa;
if(!isroot(fa))tree[gfa].c[tree[gfa].c[]==fa]=x;
int l=tree[fa].c[]==x,r=l^;
tree[fa].c[l]=tree[x].c[r];tree[x].c[r]=fa;
tree[fa].fa=x;tree[x].fa=gfa;tree[tree[fa].c[l]].fa=fa;
update(fa);update(x);
}
void splay(int x){
int top=,tmp=x;stack[++top]=x;
while(!isroot(tmp))stack[++top]=tree[tmp].fa,tmp=tree[tmp].fa;
while(top)pushdown(stack[top]),top--;
int fa,gfa;
while(!isroot(x)){
fa=tree[x].fa,gfa=tree[fa].fa;
if(!isroot(fa))
if((tree[gfa].c[]==fa)^(tree[fa].c[]==x))rotate(x);
else rotate(fa);
rotate(x);
}
}
inline void access(int x){
int son=;
while(x){
splay(x);tree[x].c[]=son;
update(x);son=x;x=tree[x].fa;
}
}
inline void makeroot(int x){
access(x);splay(x);tree[x].rev^=;
}
inline void cut(int x,int y){
makeroot(x);access(y);splay(y);tree[y].c[]=tree[x].fa=;
}
inline void link(int x,int y){
makeroot(x);tree[x].fa=y;
}
inline int getfa(int x){
access(x);splay(x);
while(tree[x].c[]){x=tree[x].c[];pushdown(x);}
splay(x);return x;
}
inline int query(int x,int y){
makeroot(x);access(y);splay(y);
return tree[y].sum;
}
inline void change(int x,int val){
makeroot(x);tree[x].val=val;update(x);
}
int main(){
scanf("%d",&n);for(i=;i<=n;i++)scanf("%d",&tree[i].val),tree[i].sum=tree[i].val;
scanf("%d",&m);
while(m--){
scanf("%s%d%d",id,&x,&y);
if(id[]=='e'){
if(getfa(x)!=getfa(y))printf("impossible\n");else printf("%d\n",query(x,y));
}else if(id[]=='b'){
if(getfa(x)==getfa(y))printf("no\n");
else printf("yes\n"),link(x,y);
}else if(id[]=='p')change(x,y);
}
return ;
}
16.1.8:重写了一发
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=;
int ch[maxn][],fa[maxn],sum[maxn],num[maxn],st[maxn];
int i,j,x,y,n,m;
bool rev[maxn]; int ra;char rx;
inline int read(){
rx=getchar(),ra=;
while(rx<''||rx>'')rx=getchar();
while(rx>=''&&rx<='')ra*=,ra+=rx-,rx=getchar();return ra;
} inline bool isrt(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
inline void upd(int x){sum[x]=sum[ch[x][]]+num[x]+sum[ch[x][]];}
inline void pushdown(int x){
if(!rev[x])return;
rev[x]=,swap(ch[x][],ch[x][]),rev[ch[x][]]^=,rev[ch[x][]]^=;
}
inline void rotate(int x){
int f=fa[x],gfa=fa[f],l=ch[f][]==x,r=l^;
if(!isrt(f))ch[gfa][ch[gfa][]==f]=x;
ch[f][l]=ch[x][r],ch[x][r]=f,fa[x]=gfa,fa[f]=x,fa[ch[f][l]]=f;
sum[x]=sum[f],upd(f);
}
void splay(int x){
int f,gfa;
for(st[st[]=]=f=x;!isrt(f);)st[++st[]]=(f=fa[f]);
while(st[])pushdown(st[st[]--]);
for(f=fa[x],gfa=fa[f];!isrt(x);rotate(x),f=fa[x],gfa=fa[f])
if(!isrt(f))rotate(((ch[f][]==x)^(ch[gfa][]==f))?x:f);
}
inline void access(int x){
for(int t=;x;t=x,x=fa[x])splay(x),ch[x][]=t,upd(x);
}
inline void makert(int x){
access(x),splay(x),rev[x]^=;
}
void link(int x,int y){
makert(x),fa[x]=y;if(!(x&))splay(x);
}
inline int getfa(int x){
for(access(x),splay(x);ch[x][];x=ch[x][]);
return x;
}
int main(){
n=read();for(i=;i<=n;i++)num[i]=read();
m=read();char id;
while(m--){
for(id=getchar();id<'a'||id>'z';id=getchar());
x=read(),y=read();
if(id=='b')if(getfa(x)!=getfa(y))link(x,y),puts("yes");else puts("no");
if(id=='e')if(getfa(x)==getfa(y))makert(x),access(y),splay(y),printf("%d\n",sum[y]);else puts("impossible");
if(id=='p')makert(x),num[x]=y,upd(x);
}
return ;
}
2843: 极地旅行社
Time Limit: 10 Sec Memory Limit: 256 MB
Description
Input
第一行一个正整数N,表示冰岛的数量。
第二行N个范围[0, 1000]的整数,为每座岛屿初始的帝企鹅数量。
第三行一个正整数M,表示命令的数量。
接下来M行即命令,为题目描述所示。
Output
对于每个bridge命令与excursion命令,输出一行,为题目描述所示。
Sample Input
4 2 4 5 6
10
excursion 1 1
excursion 1 2
bridge 1 2
excursion 1 2
bridge 3 4
bridge 3 5
excursion 4 5
bridge 1 3
excursion 2 4
excursion 2 5
Sample Output
impossible
yes
6
yes
yes
15
yes
15
16
HINT
1<=N<=30000,1<=M<=100000(bzoj2843)
1<=n<=30000, 1<=m<=300000 (bzoj1180)
[bzoj2843&&bzoj1180]极地旅行社 (lct)的更多相关文章
- 【bzoj2843】极地旅行社 LCT
题目描述 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mir ...
- 【BZOJ2843】极地旅行社(Link-Cut Tree)
[BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...
- 【BZOJ2843】极地旅行社 离线+树链剖分+树状数组
[BZOJ2843]极地旅行社 Description 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些 ...
- BZOJ 2843: 极地旅行社( LCT )
LCT.. ------------------------------------------------------------------------ #include<cstdio> ...
- BZOJ2843:极地旅行社(LCT入门题)
不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服 务.当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mirko的旅 ...
- BZOJ2843 极地旅行社 LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2843 题意概括 有n座岛 每座岛上的企鹅数量虽然会有所改变,但是始终在[0, 1000]之间.你的 ...
- 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT
竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...
- BZOJ 2843: 极地旅行社 lct splay
http://www.lydsy.com/JudgeOnline/problem.php?id=2843 https://blog.csdn.net/clove_unique/article/deta ...
- BZOJ1180 [CROATIAN2009]OTOCI LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1180 本题和BZOJ2843一样. BZOJ2843 极地旅行社 LCT 题意概括 有n座岛 每座 ...
随机推荐
- iOS APP内购
看到网上文章一大把,看了这个觉得挺不错的,谢谢 iOS大全 公众平台; 原文:http://mp.weixin.qq.com/s?__biz=MzAxMzE2Mjc2Ng==&mid=2652 ...
- jQuery 最外面的那层皮
这次学习 jQuery 源码,基于当前最新的版本,3.2.1. IIFE (function() { 'use strict'; // })(); 定义一个匿名函数并立即执行,得益于 javascri ...
- 阿里云 Angular 2 UI框架 NG-ZORRO介绍
说明: Angular2出来后,一直想找个基于Angular2的前端后台管理框架,但一直没有找到比较适合的.前段时间在Angular官网资源无意之间看到NG-ZORRO,NG-ZORRO由阿里计算平台 ...
- 栈详解及java实现
导读 栈和队列是有操作限制的线性表. 目录 1.栈的概念.特点.存储结构. 2.栈的java实现及运用. 概念 栈是一种只允许在一端进行插入或删除的线性表. 1.栈的操作端通常被称为栈顶,另一端被称为 ...
- ElasticSearch 学习记录之ES高亮搜索
高亮搜索 ES 通过在查询的时候可以在查询之后的字段数据加上html 标签字段,使文档在在web 界面上显示的时候是由颜色或者字体格式的 GET /product/_search { "si ...
- python内置函数与匿名函数
内置函数 Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() slice() any() d ...
- 用元类和__getattribute__改变类属性的读取方式
首先,需要知道两点: 类本身是type类的实例 __getattribute__ 可以改变类实例的属性的读取方式(http://www.cnblogs.com/blackmatrix/p/568148 ...
- Netty对Protocol Buffer的支持(七)
Netty对Protocol Buffer的支持(七) 一.简介 在上一篇博文中笔者已经介绍了google的Protocol Buffer的使用,那么本文笔者就开始介绍netty对Protocol B ...
- ubuntu16编译安装mysql5.7
安装环境: linux版本: ubuntu-16.04.3-desktop-amd64 mysql版本:mysql-boost-5.7.20.tar.gz 下载地址:https://dev.mysql ...
- HTML5本地存储应用sessionStorage和localStorage
在html5之前,浏览器要实现数据的存储,一般都是用cookie,但是cookie有域名和大小限定. html5流行之后,可以通过localStorage和sessionStorage实现浏览器端的数 ...