【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)
http://www.lydsy.com/JudgeOnline/problem.php?id=1180
今天状态怎么这么不好。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
又是调了好久。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
我竟然忘记更改值那里要先splay后再更改,而且还要pushup先!!!!!
QAQ
太弱了。。
记住!!!更改信息一定要先变成了根再修改,而且还要pushup!
(蒟蒻问了下神犇,总算弄懂了:“原树里面的父亲 在splay中可以是儿子”,所以在更新了父亲的点时,由于在splay中是儿子,所以没更新到在原树中是儿子的点。因此就错了。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } struct node* null;
struct node {
node *f, *c[2];
int w, s; bool rev;
node(int _k=0) { w=s=_k; f=c[0]=c[1]=null; rev=0; }
void setc(node *x, bool d) { c[d]=x; x->f=this; }
bool check() { return f==null || (f->c[0]!=this && f->c[1]!=this); }
bool d() { return f->c[1]==this; }
void pushup() { s=w+c[0]->s+c[1]->s; }
void upd() { if(this==null) return; rev=!rev; swap(c[0], c[1]); }
void pushdown() {
if(rev) {
rev=0;
c[0]->upd();
c[1]->upd();
}
}
};
void rot(node *x) {
node *f=x->f;
f->pushdown(); x->pushdown(); bool d=x->d();
if(f->check()) x->f=f->f;
else f->f->setc(x, f->d());
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;
}
node* findroot(node *x) {
access(x); splay(x);
while(x->c[0]!=null) x=x->c[0];
return x;
}
void mkroot(node *x) { access(x)->upd(); splay(x); }
void link(node *x, node *y) { mkroot(x); x->f=y; } const int N=30005;
node *root[N];
int n;
int main() {
null=new node; null->f=null->c[0]=null->c[1]=null;
read(n);
for1(i, 1, n) root[i]=new node(getint());
int m=getint();
while(m--) {
char c=getchar(); while(c<'a'||c>'z') c=getchar();
int a=getint(), b=getint();
if(c=='b') {
if(findroot(root[a])==findroot(root[b])) puts("no");
else { puts("yes"); link(root[a], root[b]); };
}
else if(c=='p') { mkroot(root[a]); root[a]->w=b; root[a]->pushup(); }
else {
if(findroot(root[a])!=findroot(root[b])) { puts("impossible"); continue; }
mkroot(root[a]); access(root[b]); splay(root[b]);
printf("%d\n", root[b]->s);
}
}
return 0;
}
Description
给出n个结点以及每个点初始时对应的权值wi。起始时点与点之间没有连边。有3类操作: 1、bridge A B:询问结点A与结点B是否连通。如果是则输出“no”。否则输出“yes”,并且在结点A和结点B之间连一条无向边。 2、penguins A X:将结点A对应的权值wA修改为X。 3、excursion A B:如果结点A和结点B不连通,则输出“impossible”。否则输出结点A到结点B的路径上的点对应的权值的和。给出q个操作,要求在线处理所有操作。数据范围:1<=n<=30000, 1<=q<=300000, 0<=wi<=1000。
Input
第一行包含一个整数n(1<=n<=30000),表示节点的数目。第二行包含n个整数,第i个整数表示第i个节点初始时对应的权值。第三行包含一个整数q(1<=n<=300000),表示操作的数目。以下q行,每行包含一个操作,操作的类别见题目描述。任意时刻每个节点对应的权值都是1到1000的整数。
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
Source
【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)的更多相关文章
- 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT
竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...
- BZOJ 2843: 极地旅行社( LCT )
LCT.. ------------------------------------------------------------------------ #include<cstdio> ...
- BZOJ 1180: [CROATIAN2009]OTOCI [LCT]
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 961 Solved: 594[Submit][S ...
- BZOJ 1180: [CROATIAN2009]OTOCI
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 989 Solved: 611[Submit][S ...
- BZOJ 2843: 极地旅行社 lct splay
http://www.lydsy.com/JudgeOnline/problem.php?id=2843 https://blog.csdn.net/clove_unique/article/deta ...
- bzoj 1180: [CROATIAN2009]OTOCI【LCT】
一道几乎是板子的LCT,但是沉迷数学很久时候突然1A了这道题还是挺开心的 #include<iostream> #include<cstdio> using namespace ...
- 【刷题】BZOJ 1180 [CROATIAN2009]OTOCI
Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出"no&quo ...
- 1180: [CROATIAN2009]OTOCI(LCT)
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 1200 Solved: 747[Submit][ ...
- [bzoj2843&&bzoj1180]极地旅行社 (lct)
双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...
随机推荐
- CPU供电维修
- TCP同步与异步及阻塞模式,多线程+阻塞模式,非阻塞模式简单介绍
首先我简单介绍一下同步TCP编程 与异步TCP编程. 在服务端我们通常用一个TcpListener来监听一个IP和端口.客户端来一个请求的连接,在服务端可以用同步的方式来接收,也可以用异步的方式去接收 ...
- JavaScript常用事件
一般事件 事件 浏览器支持 描述 onClick HTML: 2 | 3 | 3.2 | 4 Browser: IE3 | N2 | O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 on ...
- Mobile first! Wijmo 5 + Ionic Framework之:Hello World!
本教程中,我们用Wijmo 5 和 Ionic Framework实现一个Mobile的工程:Hello World. Ionic是什么? Ionic是一个HTML5框架.免费.开源,用于帮助生成hy ...
- Shallow Size 和 Retained Size
所有包含Heap Profling功能的工具(MAT, Yourkit, JProfiler, TPTP等)都会使用到两个名词,一个是Shallow Size,另一个是 Retained Size. ...
- jQuery工具函数
要点:1.字符串操作2.数组和对象操作3.测试操作4.URL 操作5.浏览器检测6.其他操作 工具函数是指直接依附于 jQuery 对象,针对 jQuery 对象本身定义的方法,即全局性的函数.它的作 ...
- Android之数据库操作
安卓数据库帮助类 /** * 数据库帮助类,用于管理数据库 * @author Administrator * */ public class PersonSQLiteOpenHelper exten ...
- Linux命令--文件管理
1.ls ls -a 列出目录下是所有文件 ls -l 列出文件的详细信息 2.cd cd /root 进入更目录下的root文件夹 cd file 进入当前目录的file文件夹 cd .. 进入 ...
- php 简单操作数据库
<?php header("content-type:text/html;charset=utf-8"); /*//造一个连接 $connect = @mysql_conne ...
- setup 桌面化设置网卡
# setup