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\).可以进行任意次以 ...
随机推荐
- CF593C Beautiful Function 构造
正解:构造 解题报告: 传送门! 我知道我咕了好几篇博客似乎,,,但我不听!我就是要发新博客QAQ!(理不直气也壮 这题,想明白了还是比较简单的QwQ实现起来似乎也没有很复杂QAQ 首先思考一下,显然 ...
- loadNibNamed:(NSString *)name owner:(nullable id)owner options:(nullable NSDictionary *)options用法
1.name xib的名字 owner当前类对象 options初始参数 实际应用: NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@&quo ...
- SSH secure shell 权威指南(转载)
本书是一本介绍通信安全的书籍,如果你想保障你的通信安全,本书能给你一个很好的解决方案.本书从ssh协议介绍起,到具体的开源实现和商业实现.但本书同时介绍开源实现和商业实现,给人感觉比较乱.注意:由于o ...
- mac终端显示日历信息命令
cal 命令: 用法: usage: cal [-jy] [[month] year] cal [-j] [-m month] [year] ncal [-Jjpwy] [-s country_cod ...
- hadoop中map和reduce的数量设置
hadoop中map和reduce的数量设置,有以下几种方式来设置 一.mapred-default.xml 这个文件包含主要的你的站点定制的Hadoop.尽管文件名以mapred开头,通过它可以控制 ...
- cocos代码研究(8)持续动作子类学习笔记
理论部分 时间间隔动作(ActionInterval)是一个在一段时间内执行的动作. 它有一个开始时间和完成时间.完成时间等于起始时间加上持续时间. ActionInterval的子类与位置有关的动作 ...
- ng-深度学习-课程笔记-14: 人脸识别和风格迁移(Week4)
1 什么是人脸识别( what is face recognition ) 在相关文献中经常会提到人脸验证(verification)和人脸识别(recognition). verification就 ...
- Android 自定义View-字母索引表(一)
在有些Android应用中,为了方便快速定位,经常会看到屏幕右侧有一个字母索引表,今天尝试使用自定义View的方式实现了索引表的基本布局. 字母索引表的样式如下面的示意图所示, 此时我们至少需要知道以 ...
- nginx重启 failed (98: Address already in use)
启动nginx服务,无法正常启动,一查log日志,发现如题错误信息. 问题描述:地址已被使用.可能nginx服务卡死了,导致端口占用,出现此错误. 查看端口 netstat -ntpl 杀掉进程 ...
- 用通俗的语言解释restful
实现了REST规范的Web API就叫RESTful API. 简单来说:就是用url定位资源,用http描述来操作资源. web是什么:分布式信息系统为超文本文件和其他对象(资源)提供访问入口. 资 ...