题解:

就是按照常规的合并

期望有一点麻烦

首先计算全部的和

再减去有多少种

具体看看http://blog.csdn.net/PoPoQQQ/article/category/2542261这个博客吧

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define pa t[x].fa
  4. #define lc t[x].ch[0]
  5. #define rc t[x].ch[1]
  6. const int N=5e4+;
  7. typedef long long ll;
  8. int read()
  9. {
  10. char c=getchar();int x=,f=;
  11. while(c<''||c>''){if (c=='-')f=-;c=getchar();}
  12. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  13. return x*f;
  14. }
  15. struct node
  16. {
  17. int ch[],fa,rev;
  18. ll add,lsum,rsum,sum,exp,w,size;
  19. }t[N];
  20. int wh(int x){return t[pa].ch[]==x;}
  21. int isRoot(int x){return t[pa].ch[]!=x&&t[pa].ch[]!=x;}
  22. void update(int x)
  23. {
  24. t[x].size=t[lc].size+t[rc].size+;
  25. t[x].sum=t[lc].sum+t[rc].sum+t[x].w;
  26. t[x].lsum=t[lc].lsum+t[x].w*(t[lc].size+)+t[rc].lsum+t[rc].sum*(t[lc].size+);
  27. t[x].rsum=t[rc].rsum+t[x].w*(t[rc].size+)+t[lc].rsum+t[lc].sum*(t[rc].size+);
  28. t[x].exp=t[lc].exp+t[rc].exp
  29. +t[lc].lsum*(t[rc].size+)+t[rc].rsum*(t[lc].size+)
  30. +t[x].w*(t[lc].size+)*(t[rc].size+);
  31. }
  32. ll cal1(ll x){return x*(x+)/;}
  33. ll cal2(ll x){return x*(x+)*(x+)/;}
  34. void paint(int x,ll d)
  35. {
  36. t[x].w+=d;
  37. t[x].add+=d;
  38. t[x].sum+=d*t[x].size;
  39. t[x].lsum+=d*cal1(t[x].size);
  40. t[x].rsum+=d*cal1(t[x].size);
  41. t[x].exp+=d*cal2(t[x].size);
  42. }
  43. void rever(int x)
  44. {
  45. swap(lc,rc);
  46. swap(t[x].lsum,t[x].rsum);
  47. t[x].rev^=;
  48. }
  49. void pushDown(int x)
  50. {
  51. if (t[x].rev)
  52. {
  53. rever(lc);
  54. rever(rc);
  55. t[x].rev=;
  56. }
  57. if (t[x].add)
  58. {
  59. paint(lc,t[x].add);
  60. paint(rc,t[x].add);
  61. t[x].add=;
  62. }
  63. }
  64. void rotate(int x)
  65. {
  66. int f=t[x].fa,g=t[f].fa,c=wh(x);
  67. if (!isRoot(f)) t[g].ch[wh(f)]=x;t[x].fa=g;
  68. t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
  69. t[x].ch[c^]=f;t[f].fa=x;
  70. update(f);update(x);
  71. }
  72. int st[N],top;
  73. void splay(int x)
  74. {
  75. top=;st[++top]=x;
  76. for (int i=x;!isRoot(i);i=t[i].fa) st[++top]=t[i].fa;
  77. for (int i=top;i>=;i--) pushDown(st[i]);
  78. for (;!isRoot(x);rotate(x))
  79. if (!isRoot(pa)) rotate(wh(x)==wh(pa)?pa:x);
  80. }
  81. void Access(int x)
  82. {
  83. for (int y=;x;y=x,x=pa)
  84. {
  85. splay(x);
  86. rc=y;
  87. update(x);
  88. }
  89. }
  90. void MakeR(int x){Access(x);splay(x);rever(x);}
  91. int FindR(int x){Access(x);splay(x);while(lc) x=lc;return x;}
  92. void Link(int x,int y){MakeR(x);t[x].fa=y;}
  93. void Cut(int x,int y)
  94. {
  95. MakeR(x);Access(y);splay(y);
  96. t[y].ch[]=t[x].fa=;
  97. update(y);
  98. }
  99. void Add(int x,int y,int d)
  100. {
  101. if (FindR(x)!=FindR(y)) return;
  102. MakeR(x);Access(y);splay(y);
  103. paint(y,d);
  104. }
  105. ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);}
  106. void Que(int x,int y)
  107. {
  108. if (FindR(x)!=FindR(y)){puts("-1");return;}
  109. MakeR(x);Access(y);splay(y);
  110. ll a=t[y].exp,b=t[y].size*(t[y].size+)/;
  111. ll g=gcd(a,b);
  112. printf("%lld/%lld\n",a/g,b/g);
  113. }
  114. int n,Q,a,op,x,y,d;
  115. int main()
  116. {
  117. n=read();Q=read();
  118. for (int i=;i<=n;i++)
  119. {
  120. a=read();
  121. t[i].size=;
  122. t[i].w=t[i].lsum=t[i].rsum=t[i].sum=t[i].exp=a;
  123. }
  124. for (int i=;i<=n-;i++) x=read(),y=read(),Link(x,y);
  125. while(Q--)
  126. {
  127. op=read();x=read();y=read();
  128. if (op==) if (FindR(x)==FindR(y)) Cut(x,y);
  129. if (op==) if (FindR(x)!=FindR(y)) Link(x,y);
  130. if (op==) d=read(),Add(x,y,d);
  131. if (op==) Que(x,y);
  132. }
  133. }

bzoj3901的更多相关文章

  1. 题解-bzoj3901 棋盘游戏

    2019年第一篇文章 (。・∀・)ノ゙ Problem bzoj无良权限题,拿学长的号交的 题目概要:给定一个\(n\times n\)的矩阵.令\(x=\frac {n+1}2\).可以进行任意次以 ...

随机推荐

  1. CF593C Beautiful Function 构造

    正解:构造 解题报告: 传送门! 我知道我咕了好几篇博客似乎,,,但我不听!我就是要发新博客QAQ!(理不直气也壮 这题,想明白了还是比较简单的QwQ实现起来似乎也没有很复杂QAQ 首先思考一下,显然 ...

  2. loadNibNamed:(NSString *)name owner:(nullable id)owner options:(nullable NSDictionary *)options用法

    1.name xib的名字 owner当前类对象 options初始参数 实际应用: NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@&quo ...

  3. SSH secure shell 权威指南(转载)

    本书是一本介绍通信安全的书籍,如果你想保障你的通信安全,本书能给你一个很好的解决方案.本书从ssh协议介绍起,到具体的开源实现和商业实现.但本书同时介绍开源实现和商业实现,给人感觉比较乱.注意:由于o ...

  4. mac终端显示日历信息命令

    cal 命令: 用法: usage: cal [-jy] [[month] year] cal [-j] [-m month] [year] ncal [-Jjpwy] [-s country_cod ...

  5. hadoop中map和reduce的数量设置

    hadoop中map和reduce的数量设置,有以下几种方式来设置 一.mapred-default.xml 这个文件包含主要的你的站点定制的Hadoop.尽管文件名以mapred开头,通过它可以控制 ...

  6. cocos代码研究(8)持续动作子类学习笔记

    理论部分 时间间隔动作(ActionInterval)是一个在一段时间内执行的动作. 它有一个开始时间和完成时间.完成时间等于起始时间加上持续时间. ActionInterval的子类与位置有关的动作 ...

  7. ng-深度学习-课程笔记-14: 人脸识别和风格迁移(Week4)

    1 什么是人脸识别( what is face recognition ) 在相关文献中经常会提到人脸验证(verification)和人脸识别(recognition). verification就 ...

  8. Android 自定义View-字母索引表(一)

    在有些Android应用中,为了方便快速定位,经常会看到屏幕右侧有一个字母索引表,今天尝试使用自定义View的方式实现了索引表的基本布局. 字母索引表的样式如下面的示意图所示, 此时我们至少需要知道以 ...

  9. nginx重启 failed (98: Address already in use)

    启动nginx服务,无法正常启动,一查log日志,发现如题错误信息. 问题描述:地址已被使用.可能nginx服务卡死了,导致端口占用,出现此错误. 查看端口 netstat -ntpl 杀掉进程   ...

  10. 用通俗的语言解释restful

    实现了REST规范的Web API就叫RESTful API. 简单来说:就是用url定位资源,用http描述来操作资源. web是什么:分布式信息系统为超文本文件和其他对象(资源)提供访问入口. 资 ...