bzoj3901
题解:
就是按照常规的合并
期望有一点麻烦
首先计算全部的和
再减去有多少种
具体看看http://blog.csdn.net/PoPoQQQ/article/category/2542261这个博客吧
代码:
#include<bits/stdc++.h>
using namespace std;
#define pa t[x].fa
#define lc t[x].ch[0]
#define rc t[x].ch[1]
const int N=5e4+;
typedef long long ll;
int read()
{
char c=getchar();int x=,f=;
while(c<''||c>''){if (c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
struct node
{
int ch[],fa,rev;
ll add,lsum,rsum,sum,exp,w,size;
}t[N];
int wh(int x){return t[pa].ch[]==x;}
int isRoot(int x){return t[pa].ch[]!=x&&t[pa].ch[]!=x;}
void update(int x)
{
t[x].size=t[lc].size+t[rc].size+;
t[x].sum=t[lc].sum+t[rc].sum+t[x].w;
t[x].lsum=t[lc].lsum+t[x].w*(t[lc].size+)+t[rc].lsum+t[rc].sum*(t[lc].size+);
t[x].rsum=t[rc].rsum+t[x].w*(t[rc].size+)+t[lc].rsum+t[lc].sum*(t[rc].size+);
t[x].exp=t[lc].exp+t[rc].exp
+t[lc].lsum*(t[rc].size+)+t[rc].rsum*(t[lc].size+)
+t[x].w*(t[lc].size+)*(t[rc].size+);
}
ll cal1(ll x){return x*(x+)/;}
ll cal2(ll x){return x*(x+)*(x+)/;}
void paint(int x,ll d)
{
t[x].w+=d;
t[x].add+=d;
t[x].sum+=d*t[x].size;
t[x].lsum+=d*cal1(t[x].size);
t[x].rsum+=d*cal1(t[x].size);
t[x].exp+=d*cal2(t[x].size);
}
void rever(int x)
{
swap(lc,rc);
swap(t[x].lsum,t[x].rsum);
t[x].rev^=;
}
void pushDown(int x)
{
if (t[x].rev)
{
rever(lc);
rever(rc);
t[x].rev=;
}
if (t[x].add)
{
paint(lc,t[x].add);
paint(rc,t[x].add);
t[x].add=;
}
}
void rotate(int x)
{
int f=t[x].fa,g=t[f].fa,c=wh(x);
if (!isRoot(f)) t[g].ch[wh(f)]=x;t[x].fa=g;
t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f;t[f].fa=x;
update(f);update(x);
}
int st[N],top;
void splay(int x)
{
top=;st[++top]=x;
for (int i=x;!isRoot(i);i=t[i].fa) st[++top]=t[i].fa;
for (int i=top;i>=;i--) pushDown(st[i]);
for (;!isRoot(x);rotate(x))
if (!isRoot(pa)) rotate(wh(x)==wh(pa)?pa:x);
}
void Access(int x)
{
for (int y=;x;y=x,x=pa)
{
splay(x);
rc=y;
update(x);
}
}
void MakeR(int x){Access(x);splay(x);rever(x);}
int FindR(int x){Access(x);splay(x);while(lc) x=lc;return x;}
void Link(int x,int y){MakeR(x);t[x].fa=y;}
void Cut(int x,int y)
{
MakeR(x);Access(y);splay(y);
t[y].ch[]=t[x].fa=;
update(y);
}
void Add(int x,int y,int d)
{
if (FindR(x)!=FindR(y)) return;
MakeR(x);Access(y);splay(y);
paint(y,d);
}
ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);}
void Que(int x,int y)
{
if (FindR(x)!=FindR(y)){puts("-1");return;}
MakeR(x);Access(y);splay(y);
ll a=t[y].exp,b=t[y].size*(t[y].size+)/;
ll g=gcd(a,b);
printf("%lld/%lld\n",a/g,b/g);
}
int n,Q,a,op,x,y,d;
int main()
{
n=read();Q=read();
for (int i=;i<=n;i++)
{
a=read();
t[i].size=;
t[i].w=t[i].lsum=t[i].rsum=t[i].sum=t[i].exp=a;
}
for (int i=;i<=n-;i++) x=read(),y=read(),Link(x,y);
while(Q--)
{
op=read();x=read();y=read();
if (op==) if (FindR(x)==FindR(y)) Cut(x,y);
if (op==) if (FindR(x)!=FindR(y)) Link(x,y);
if (op==) d=read(),Add(x,y,d);
if (op==) Que(x,y);
}
}
bzoj3901的更多相关文章
- 题解-bzoj3901 棋盘游戏
2019年第一篇文章 (。・∀・)ノ゙ Problem bzoj无良权限题,拿学长的号交的 题目概要:给定一个\(n\times n\)的矩阵.令\(x=\frac {n+1}2\).可以进行任意次以 ...
随机推荐
- 【我的Android进阶之旅】解决AndroidStudio编译时报错:Timeout waiting to lock artifact cache .
1. 错误描述 今天在Android Studio中,使用gradle命令的时候,出现了如下所示的错误: D:\GitLab Source\XTCLint>gradlew clean uploa ...
- Python isdigit() isalnum()
Python isdigit() 方法检测字符串是否只由数字组成. 返回值 如果字符串只包含数字则返回 True 否则返回 False. >>> choice = input(&qu ...
- Java调用Python脚本并获取返回值
在Java程序中有时需要调用Python的程序,这时可以使用一般的PyFunction来调用python的函数并获得返回值,但是采用这种方法有可能出现一些莫名其妙的错误,比如ImportError.在 ...
- Flask系列(四)Flask实现简单页面登陆
from flask import Flask,render_template,request,redirect,session app = Flask(__name__,template_folde ...
- Linux系统——搭建FTP方式的本地定制化Yum仓库
(1)搭建公网源yum仓库 安装wget aliyun源 # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epe ...
- 应用连接mysql8.0注意事项
mysql8.0在安装过程中会让你选择一个Authentication Method,如果你选择上面的更强的授权机制,那么就会出现下面的情况 图片来源:https://blog.csdn.net/qq ...
- ubuntu 中启动SDK manager
Android SDK安装后,目录结构如下: root@localhost:/home/ranxf/Android/Sdk/android-sdk-linux# ll 总用量 drwxrwxr-x r ...
- OpenCV图像的轮廓的匹配
http://blog.sina.com.cn/s/blog_67a7426a0101cxl0.html 一个跟轮廓相关的最常用到的功能是匹配两个轮廓.如果有两个轮廓,如何比较它们;或者如何比较一个轮 ...
- bzoj2456 / P2397 yyy loves Maths VI (mode)
P2397 yyy loves Maths VI (mode) 神奇的摩尔投票法(大雾) 保证众数个数大于一半. 两两相消,剩下的那个必定是众数. 我们只要开2个变量,一个存个数,一个存值即可. (l ...
- Example解析
本文总结自:https://github.com/abel533/Mapper/wiki/6.example,旨在提供一些基本概念上的解释 Example类用于条件查询,以代替冗长的"sel ...