【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 ...
随机推荐
- css+div绝对定位
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...
- Delphi经验总结(1)
先人的DELPHI基础开发技巧 ◇[DELPHI]网络邻居复制文件 uses shellapi; copyfile(pchar('newfile.txt'),pchar('//computername ...
- poj_2674 弹性碰撞
题目大意 给定一条直线,长度为L 表示区间[0, L].在直线上开始放置N个人,每个人有一个初始位置pos(用到直线上端点0的距离表示)和初始方向dir('p' 或 'P' 表示向端点L行走, 'n' ...
- int *p()与int (*p)()的区别
int *p()是返回指针的函数 int (*p)()是指向函数的指针 返回指针的函数: int *a(int x,int y); 有若干个学生的成绩(每个学生有4门课程),要求在用户输入学生序号 ...
- Android主题换肤实现
本系列文章主要是对一个Material Design的APP的深度解析,主要包括以下内容 基于Material Design Support Library作为项目整体框架.对应博文:Android ...
- markdown下编辑latex数学公式
在利用为知笔记编写笔记的时候,有时需要用的markdown,只要把文件名加上后缀.md,就可以使用markdown语法,以下介绍在markdown下编辑latex数学公式. 使用LaTeX写公式的基本 ...
- DRF如何序列化外键的字段
我觉得在有些应用场景下,这个操作是有用的,因为可以减少一个AJAX的请求,以增加性能. 当然,是二次请求,还是一次传输.这即要考虑用户体验,还要兼顾服务器性能. 一切是有条件的平衡吧.就算是一次传输, ...
- linux tricks 之 BUILD_BUG_ON_ZERO.
------------------------------------------- 本文系作者原创, 欢迎大家转载! 转载请注明出处:netwalker.blog.chinaunix.net -- ...
- linux tricks 之数据对齐。
转载:http://blog.chinaunix.net/uid-20608849-id-3027953.html 内核为了保持最大的兼容性和代码灵活性,不可能直接对某个数据类型定义它的大小范围. ...
- poj 2392 多重背包
题意:有几个砖,给出高度,能放的最大高度和数目,求这些砖能垒成的最大高度 依据lim排个序,按一层一层进行背包 #include<cstdio> #include<iostream& ...