题目

传送门:QWQ

分析

  看起来就是一个支持link的东西。

  但有环,考虑缩点......

  但疯狂Tle。大概是常数卡不过去。

  行走的大常数noble_

代码

#include <bits/stdc++.h>
#define lc son[x][0]
#define rc son[x][1]
using namespace std;
const int maxn=;
int son[maxn][],s[maxn],v[maxn],lazy[maxn],fa[maxn],st[maxn],dig[];
int pa[maxn], belong[maxn], a[maxn];
inline int findset(int x){if(belong[x]==x) return x;else return belong[x]=findset(belong[x]);}
inline int find(int x){if(pa[x]==x) return x;else return pa[x]=find(pa[x]);}
inline int ws(int x){return son[findset(fa[x])][]==x;}
inline int isroot(int x){int f=findset(fa[x]);return !(son[f][]==x||son[f][]==x);}
inline int rev(int x){lazy[x]^=; swap(lc,rc);}
inline void pushdown(int x){
if(!lazy[x]) return;
if(lc) rev(lc); if(rc) rev(rc); lazy[x]=;
}
inline void update(int x){ s[x]=s[lc]+s[rc]+v[x]; }
inline void rot(int x){
int f=fa[x], ff=fa[f],w1=ws(x),w2=ws(fa[x]),xx=son[x][!w1];
if(!isroot(f))son[ff][w2]=x;son[x][!w1]=f;son[f][w1]=xx;
if(xx)fa[xx]=f;fa[f]=x;fa[x]=ff;
update(f);update(x);
}
inline void splay(int x){
int y=x,z=; st[++z]=y; pushdown(x);
while(!isroot(y)) st[++z]=y=findset(fa[y]);
while(z) pushdown(st[z--]),fa[st[z]]=findset(fa[st[z]]);
for(;!isroot(x);rot(x))
if(ws(x)==ws(fa[x])&&!isroot(fa[x])) rot(fa[x]);
update(x);
}
inline void access(int x){
for(int y=;x;x=findset(fa[y=x])){
splay(x); rc=y; if(y) fa[y]=x; update(x);
}
}
inline void makeroot(int x){
access(x); splay(x); rev(x);
}
inline int findroot(int x){
access(x); splay(x);
while(lc) pushdown(x),x=lc;
return x;
}
inline void link(int x,int y){
makeroot(x);
if(findroot(y)==x) return;
fa[x]=y;
}
inline void merge(int x,int y){
if(!x) return;
belong[findset(x)]=findset(y); pushdown(x);
if(x!=y) v[y]+=v[x];
if(lc) merge(lc,y); if(rc) merge(rc,y);
// puts("**********************");
lc=rc=;
}
inline int in(){
char c=getchar();
for (;c>''||c<'';c=getchar());
int num=;
for (;c>=''&&c<='';c=getchar())
num=num*+c-'';
return num;
}
inline void write(int x)
{
if (!x)
{
putchar('');
putchar('\n');
return;
}
dig[]=;
while (x)
{
dig[++dig[]]=x%;
x/=;
}
for (int i=dig[];i>=;--i) putchar(dig[i]+'');
putchar('\n');
}
int main(){
int n,m; n=in();m=in();
for(register int i= ;i<=n;++i) v[i]=in(),pa[i]=belong[i]=i,a[i]=v[i];
int p,x,y;
while(m--){
p=in();x=in();y=in();
if(p==){
x=findset(x);y=findset(y);
if(x==y) continue;
int r1=find(x), r2=find(y);
if(r1!=r2){
pa[r2]=r1;
link(x,y);
}
else{
makeroot(x); access(y); splay(y);
merge(y,y); update(y);
}
}
if(p==){
int xx=x;
x=findset(x);
access(x); splay(x); v[x]+=y-a[xx]; a[xx]=y;
update(x);
}
if(p==){
x=findset(x); y=findset(y);
if(find(x)!=find(y) ){
write(-); continue;
}
makeroot(x); access(y); splay(y);
write(s[y]);
}
}
return ;
}

【BZOJ】2959: 长跑(lct+缩点)(暂时弃坑)的更多相关文章

  1. BZOJ 2959: 长跑 [lct 双连通分量 并查集]

    2959: 长跑 题意:字词加入边,修改点权,询问两点间走一条路径的最大点权和.不一定是树 不是树

  2. BZOJ 2959 长跑 (LCT、并查集)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2959 题解 真是被这题搞得心态大崩--调了7个小时--然而并查集都能写成\(O(n^2) ...

  3. BZOJ 2959 长跑 (LCT+并查集)

    题面:BZOJ传送门 当成有向边做的发现过不去样例,改成无向边就忘了原来的思路.. 因为成环的点一定都能取到,我们把它们压成一个新点,权值为环上所有点的权值和 这样保证了图是一颗森林 每次询问转化为, ...

  4. BZOJ 2959: 长跑 lct 双联通分量 并查集 splay

    http://www.lydsy.com/JudgeOnline/problem.php?id=2959 用两个并查集维护双联通分量的编号和合并. #include<iostream> # ...

  5. (暂时弃坑)(半成品)ACM数论之旅18---反演定理 第二回 Mobius反演(莫比乌斯反演)((づ ̄3 ̄)づ天才第一步,雀。。。。)

    莫比乌斯反演也是反演定理的一种 既然我们已经学了二项式反演定理 那莫比乌斯反演定理与二项式反演定理一样,不求甚解,只求会用 莫比乌斯反演长下面这个样子(=・ω・=) d|n,表示n能够整除d,也就是d ...

  6. (暂时弃坑)ACM数论之旅15---置换群与Polya定理(我把标题看成poi了,poipoipoi(*≧▽≦)ツ)

    (挖坑...) ////////////////////////////////////////////////// 暂时弃坑 开学了,有空再写....

  7. BZOJ 2959: 长跑 解题报告

    2959: 长跑 Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能"为祖国健康工作五十年",同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑 ...

  8. bzoj 2959 长跑(LCT+BCC+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2959 [题意] n个点,提供操作:连边,修改点权,查询自定义边的方向后起点a终点b能经 ...

  9. bzoj 2959: 长跑【LCT+并查集】

    如果没有环的话直接LCT 考虑有环怎么办,如果是静态的话就tarjan了,但是这里要动态的缩环 具体是link操作的时候看一下是否成环(两点已联通),成环的话就用并查集把这条链缩到一个点,把权值加给祖 ...

随机推荐

  1. Windows环境下redis 配置文件中设置的密码无效

    当我们安装了redis服务后,发现在其配置文件redis.windows.conf(或redis.conf)设置了密码:requirepass ****** 但是打开redis-cli.exe后输入命 ...

  2. Linux:history命令详解

      Linux下History命令 主要用于显示历史指令记录内容, 下达历史纪录中的指令 . 语法 history [n] history [-c] history [-raw] histfiles ...

  3. ajax请求-IE缓存处理

    IE浏览器下使用GET发送请求时,如果两次请求的地址和参数相同,在不刷新页面的情况下,浏览器会缓存第一次的请求的内容,服务端更新后浏览器仍然显示第一次的内容 如在当前页面用户登录,在未登录的情况下,服 ...

  4. JMter请求参数中文显示乱码

    如上图所示,上传的参数为中文的时候,显示不出来,解决方法如下: 1.进入Jmter安装文件bin文件夹,找到文件jmeter.properties 2.在该文件找到jsyntaxtextarea.fo ...

  5. SSH服务及其扩展(sshpass和expect)

    SSH服务及其扩展(sshpass和expect) Linux SSH服务一共包含三个工具:ssh.scp.sftp [远程连接及执行命令] 语法:ssh -p端口 账号@IP 命令 参数说明:-o ...

  6. VS 工具箱 Dev控件显示

    工具箱修复Dev控件显示 用VS2010新打开一个项目,居然发现工具箱里边没了Dev控件. 命令提示符定位到Dev控件的安装目录下的\Components\Tools文件夹下,执行如下命令 添加:To ...

  7. Windows2012启动自动帐户登陆

    Win+R 启动 输入regedit  运行注册表 找到[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogo ...

  8. [译]贵宾犬咬伤了TLS

    原文链接:https://community.qualys.com/blogs/securitylabs/2014/12/08/poodle-bites-tls 原文发表时间:2014.12.8 今天 ...

  9. php simple_html_dom

    这个真的很好用,如果用正则,就太麻烦了. 首先,下载simple_html_dom,用include_once就可以使用了. 可以直接定位,可以像个对象一样操作,很方便. $ret=file_get_ ...

  10. 使用python处理selenium中的鼠标悬停问题

    # 导入selenium中的actionchains的方法 from selenium.webdriver.common.action_chains import ActionChains #识别需要 ...