题解:

lct

和上一题差不多

这一题还要判断是否有链接

其实直接并查集判断就可以了

代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
const int N=;
using namespace std;
int read()
{
int x=;char ch=getchar();
for (;ch<''||ch>'';ch=getchar());
for (;ch>=''&&ch<='';ch=getchar())x=x*+ch-'';
return x;
}
int n,m,top,cnt,c[N][],fa[N],size[N],q[N],rev[N],f[N],sum[N],val[N],at[N],mt[N];
int isroot(int x){return c[fa[x]][]!=x&&c[fa[x]][]!=x;}
void update(int x)
{
int l=c[x][],r=c[x][];
sum[x]=sum[l]+sum[r]+val[x];
size[x]=size[l]+size[r]+;
}
void pushdown(int x)
{
int l=c[x][],r=c[x][];
if (rev[x])
{
rev[x]^=;rev[l]^=;rev[r]^=;
swap(c[x][],c[x][]);
}
}
void rotate(int x)
{
int y=fa[x],z=fa[y],l,r;
l=(c[y][]==x);r=l^;
if (!isroot(y))c[z][c[z][]==y]=x;
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
update(y);update(x);
}
void down(int x){if (!isroot(x))down(fa[x]);pushdown(x);}
void splay(int x)
{
down(x);
for (int y=fa[x];!isroot(x);rotate(x),y=fa[x])
if (!isroot(y))rotate((c[y][]==x)==(c[fa[y]][]==y)?y:x);
}
void access(int x)
{
for(int t=;x;t=x,x=fa[x])
{
splay(x);
c[x][]=t;
update(x);
}
}
void makeroot(int x){access(x);splay(x);rev[x]^=;}
void split(int x,int y){makeroot(y);access(x);splay(x);}
void link(int x,int y){makeroot(x);fa[x]=y;}
int find(int x){if (x==f[x])return f[x];return f[x]=find(f[x]);}
int main()
{
n=read();
for (int i=;i<=n;i++)val[i]=sum[i]=read(),f[i]=i,size[i]=;
m=read();char ch[];
while(m--)
{
scanf("%s",ch);
int u=read(),v=read();
if (ch[]=='b')
{
if (f[find(u)]==f[find(v)])puts("no");
else puts("yes"),link(u,v),f[find(u)]=f[find(v)];
}
if (ch[]=='p')
{
val[u]=v;
makeroot(u);
}
if (ch[]=='e')
{
if (f[find(u)]!=f[find(v)])
{
puts("impossible");
continue;
}
split(u,v);
printf("%d\n",sum[u]);
}
}
return ;
}

bzoj2843&&1180的更多相关文章

  1. 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree

    2843: 极地旅行社 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 323  Solved: 218[Submit][Status][Discuss ...

  2. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  3. Flex 1046: 找不到类型,或者它不是编译时常数;1180: 调用的方法 CompPropInfo 可能未定义

    导入项目之后一直报这个错误, 1046: 找不到类型,或者它不是编译时常数: 1180: 调用的方法 CompPropInfo 可能未定义 想这应该是没有把当前这个类编译进项目当中,找了半天也没有找到 ...

  4. timus 1180. Stone Game 解题报告

    1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...

  5. Vijos 1180 (树形DP+背包)

    题目链接: https://vijos.org/p/1180 题目大意:选课.只有根课选了才能选子课,给定选课数m, 问最大学分多少. 解题思路: 树形背包.cost=1. 且有个虚根0,取这个虚根也 ...

  6. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  7. 【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1180 今天状态怎么这么不好..................................... ...

  8. hdu - 1180 诡异的楼梯 (bfs+优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...

  9. ural 1180 Stone Game

    http://acm.timus.ru/problem.aspx?space=1&num=1180 #include <cstdio> #include <cstring&g ...

随机推荐

  1. 快递API接口

    快递100        

  2. Highway Project---zoj3946(最短路SPFA)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 题意: 有n个点 m(n,m<=10^5)条路,现在要建 ...

  3. Python开发【模块】:邮件

    邮件 1.简单发送 settings.py配置: import os import sys,string from bin.start import BASE_DIR # 日志存放地址 RUN_LOG ...

  4. Python性能鸡汤(转)

    英文原文:http://blog.monitis.com/index.php/2012/02/13/python-performance-tips-part-1/ 英文原文:http://blog.m ...

  5. android 获取经纬度

    android 定位的两种方式:GPS_PROVIDER and NETWORK_PROVIDER 定位的可以借助LocationManager来实现 MainActivity代码 static fi ...

  6. [WorldWind学习]19.WebDownload

    using System; using System.Diagnostics; using System.Globalization; using System.Net; using System.I ...

  7. Deep Learning(5)

    五.应用实例 1.计算机视觉. ImageNet Classification with Deep Convolutional Neural Networks, Alex Krizhevsky, Il ...

  8. mysql++使用

    Mysql++是官方发布的.一个为MySQL设计的C++语言的API.Mysql++为Mysql的C-Api的再次封装,它用STL(Standard Template Language)开发并编写,并 ...

  9. python16_day40【数据结构】

    一.链表 #!/usr/bin/env python # -*-coding:utf8-*- __author__ = "willian" # 一.简单链表 class Node( ...

  10. ubuntu16.04(64位)安装 Drcom

    一 : 下载DrClient(DrcomAndPPOE) 解压   下载地址 https://www.baidu.com/s?wd=DrClient(DrcomAndPPOE)&rsv_spt ...