COJ983 WZJ的数据结构(负十七)
显然是动态树裸题:O(mlogn)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
inline int read()
{
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
int sumv[maxn],val[maxn],pre[maxn],fa[maxn],ch[maxn][],flip[maxn];
void maintain(int x) {sumv[x]=sumv[ch[x][]]+sumv[ch[x][]]+val[x];}
void pushdown(int x)
{
if(!flip[x]) return;
flip[ch[x][]]^=;flip[ch[x][]]^=;
swap(ch[x][],ch[x][]);flip[x]=;
}
void rotate(int x,int d)
{
pushdown(x);int y=pre[x],z=pre[y];
pre[ch[x][d]]=y;ch[y][d^]=ch[x][d];
pre[x]=z;ch[z][ch[z][]==y]=x;
pre[y]=x;ch[x][d]=y;maintain(y);
}
void splay(int x)
{
int rt=x;while(pre[rt]) rt=pre[rt];
if(rt!=x)
{
fa[x]=fa[rt];fa[rt]=;
while(pre[x]) pushdown(pre[x]),rotate(x,ch[pre[x]][]==x);
maintain(x);
}
else pushdown(x);
}
void access(int x)
{
for(int y=;x;x=fa[x])
{
splay(x);pre[ch[x][]]=;fa[ch[x][]]=x;
ch[x][]=y;pre[y]=x;fa[y]=;
maintain(x);y=x;
}
}
int findrt(int x)
{
access(x);splay(x);
while(ch[x][]) x=ch[x][];return x;
}
void makeroot(int x) {access(x);splay(x);flip[x]^=;}
void link(int x,int y) {makeroot(x);fa[x]=y;}
void query(int x,int y) {makeroot(x);access(y);splay(y);printf("%d\n",sumv[y]);}
int main()
{
int n=read();
for(int i=;i<=n;i++) val[i]=read();
int m=read();
while(m--)
{
char cmd=getchar();while(!isalpha(cmd)) cmd=getchar();
int x=read(),y=read();
if(cmd=='e')
{
if(findrt(x)==findrt(y)) query(x,y);
else puts("impossible");
}
else
{
if(findrt(x)==findrt(y)) puts("no");
else link(x,y),puts("yes");
}
}
return ;
}
可以写个启发式合并O(mlog^2n)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
inline int read()
{
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
int first[maxn],next[maxn*],to[maxn*],e;
void AddEdge(int u,int v)
{
to[++e]=v;next[e]=first[u];first[u]=e;
to[++e]=u;next[e]=first[v];first[v]=e;
}
int val[maxn],pa[maxn],s[maxn];
inline int findset(int x) {return pa[x]==x?x:pa[x]=findset(pa[x]);}
int fa[maxn][],sum[maxn],dep[maxn];
void dfs(int x)
{
sum[x]=sum[fa[x][]]+val[x];dep[x]=dep[fa[x][]]+;
for(int i=;i<;i++) fa[x][i]=fa[fa[x][i-]][i-];
for(int i=first[x];i;i=next[i]) if(to[i]!=fa[x][]) fa[to[i]][]=x,dfs(to[i]);
}
void query(int x,int y)
{
int ret=sum[x]+sum[y],lca;
if(dep[x]<dep[y]) swap(x,y);
for(int i=;i>=;i--) if((<<i)<=dep[x]-dep[y]) x=fa[x][i];
for(int i=;i>=;i--) if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
lca=(x==y)?x:fa[x][];
printf("%d\n",ret-sum[lca]-sum[fa[lca][]]);
}
int main()
{
int n=read();
for(int i=;i<=n;i++) sum[i]=val[i]=read(),pa[i]=i,s[i]=;
int m=read();
while(m--)
{
char cmd=getchar();while(!isalpha(cmd)) cmd=getchar();
int x=read(),y=read();
if(cmd=='e')
{
int p1=findset(x),p2=findset(y);
if(x==y) printf("%d\n",val[x]);
else if(p1==p2) query(x,y);
else puts("impossible");
}
else if(cmd=='b')
{
int p1=findset(x),p2=findset(y);
if(p1==p2) {puts("no");continue;}puts("yes");
if(s[p1]>s[p2]) swap(p1,p2),swap(x,y);
pa[p1]=p2;s[p2]+=s[p1];fa[x][]=y;AddEdge(x,y);
dfs(x);
}
}
return ;
}
COJ983 WZJ的数据结构(负十七)的更多相关文章
- [COJ0985]WZJ的数据结构(负十五)
[COJ0985]WZJ的数据结构(负十五) 试题描述 CHX有一个问题想问问大家.给你一个长度为N的数列A,请你找到两个位置L,R,使得A[L].A[L+1].…….A[R]中没有重复的数,输出R- ...
- [COJ0988]WZJ的数据结构(负十二)
[COJ0988]WZJ的数据结构(负十二) 试题描述 输入 见题目,注意本题不能用文件输入输出 输出 见题目,注意本题不能用文件输入输出 输入示例 输出示例 数据规模及约定 1≤N≤1500,M≤N ...
- [COJ0989]WZJ的数据结构(负十一)
[COJ0989]WZJ的数据结构(负十一) 试题描述 给出以下定义: 1.若子序列[L,R]的极差(最大值-最小值)<=M,则子序列[L,R]为一个均匀序列. 2.均匀序列[L,R]的权值为S ...
- COJ966 WZJ的数据结构(负三十四)
WZJ的数据结构(负三十四) 难度级别:C: 运行时间限制:20000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给一棵n个节点的树,请对于形如"u ...
- COJ967 WZJ的数据结构(负三十三)
WZJ的数据结构(负三十三) 难度级别:C: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...
- COJ970 WZJ的数据结构(负三十)
WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...
- COJ968 WZJ的数据结构(负三十二)
WZJ的数据结构(负三十二) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,边上均有权值,每个点上有 ...
- COJ969 WZJ的数据结构(负三十一)
WZJ的数据结构(负三十一) 难度级别:D: 运行时间限制:3000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 A国有两个主基站,供给全国的资源.定义一个主基站 ...
- COJ986 WZJ的数据结构(负十四)
WZJ的数据结构(负十四) 难度级别:D: 运行时间限制:6000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小 ...
随机推荐
- MySql 插入数据中文乱码
在数据库连接URL后加上characterEncoding=UTF-8 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ssm ...
- Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- centos 编译安装Apache 2.4
2013年12月29日 16:40:20 ./configure --prefix=/usr/local/web/apache --enable-so --enable-rewrite --enabl ...
- poj 3080 Blue Jeans 解题报告
题目链接:http://poj.org/problem?id=3080 该题属于字符串处理中的串模式匹配问题.题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...
- Linux下RPM、tar.gz、DEB格式软件包的区别
初接解Linux的朋友一定对软件的安装特别头疼,同样都是for Linux,但RPM.tar.gz.DEB包还是有很大区别的,这种区别很有可能使你的安装过程进行不下去.那我们应该下载什么格式的包呢 ...
- VS添加lib库
#pragma comment(lib,"opengl32.lib")
- Controller与View之间的数据传递
1)Controller向View传递数据ViewData["message"] = "Hello";//使用ViewData传递数据ViewBag.Time ...
- Solr学习笔记(一)
最近准备为一个产品做一个站内的搜索引擎,是一个java产品.由于原来做过Lucene.net,所以自然而然的就想到了使用Lucene.在复习Lucene的过程中发现了Solr这个和Lucene绑定在一 ...
- InnoDB引擎的索引和存储结构
在Oracle 和SQL Server等数据库中只有一种存储引擎,所有数据存储管理机制都是一样的.而MySql数据库提供了多种存储引擎.用户可以根据不同的需求为数据表选择不同的存储引擎,用户也可以根据 ...
- 二叉树学习笔记之二叉查找树(BSTree)
二叉查找树即搜索二叉树,或者二叉排序树(BSTree),学习回顾一下有关的知识. >>关于二叉查找树 二叉查找树(Binary Search Tree)是指一棵空树或者具有下列性质的二叉树 ...