传送门


题目的操作大概是:求某个点到根的链的逆序对,然后对这条链做区间赋值

求某个点到根的链,就是LCT中的access操作,所以我们每一次把access过后的链打上标记,就可以做到区间赋值了。

计算答案只需要在access的过程中用树状数组求一下逆序对即可。

  1. #include<bits/stdc++.h>
  2. #define PII pair < int , int >
  3. //This code is written by Itst
  4. using namespace std;
  5. inline int read(){
  6. int a = 0;
  7. char c = getchar();
  8. bool f = 0;
  9. while(!isdigit(c) && c != EOF){
  10. if(c == '-')
  11. f = 1;
  12. c = getchar();
  13. }
  14. if(c == EOF)
  15. exit(0);
  16. while(isdigit(c)){
  17. a = a * 10 + c - 48;
  18. c = getchar();
  19. }
  20. return f ? -a : a;
  21. }
  22. const int MAXN = 1e5 + 7;
  23. struct node{
  24. int fa , ch[2] , sz;
  25. bool mark;
  26. }Tree[MAXN];
  27. int N , cnt , val[MAXN] , lsh[MAXN];
  28. long long ans;
  29. queue < PII > q;
  30. namespace BIT{
  31. #define lowbit(x) ((x) & -(x))
  32. int BIT[MAXN];
  33. inline void add(int x , int num){
  34. while(x <= cnt){
  35. BIT[x] += num;
  36. x += lowbit(x);
  37. }
  38. }
  39. inline int get(int x){
  40. int sum = 0;
  41. while(x){
  42. sum += BIT[x];
  43. x -= lowbit(x);
  44. }
  45. return sum;
  46. }
  47. }
  48. using namespace BIT;
  49. inline bool nroot(int x){
  50. return Tree[Tree[x].fa].ch[0] == x || Tree[Tree[x].fa].ch[1] == x;
  51. }
  52. inline bool son(int x){
  53. return Tree[Tree[x].fa].ch[1] == x;
  54. }
  55. inline void pushup(int x){
  56. Tree[x].sz = Tree[Tree[x].ch[0]].sz + Tree[Tree[x].ch[1]].sz + 1;
  57. }
  58. inline void rotate(int x){
  59. bool f = son(x);
  60. int y = Tree[x].fa , z = Tree[y].fa , w = Tree[x].ch[f ^ 1];
  61. Tree[x].fa = z;
  62. if(nroot(y))
  63. Tree[z].ch[son(y)] = x;
  64. Tree[y].fa = x;
  65. Tree[x].ch[f ^ 1] = y;
  66. Tree[y].ch[f] = w;
  67. if(w)
  68. Tree[w].fa = y;
  69. pushup(y);
  70. }
  71. inline void mark(int x){
  72. Tree[x].mark ^= 1;
  73. swap(Tree[x].ch[0] , Tree[x].ch[1]);
  74. }
  75. inline void pushdown(int x){
  76. if(Tree[x].mark){
  77. mark(Tree[x].ch[0]);
  78. mark(Tree[x].ch[1]);
  79. Tree[x].mark = 0;
  80. }
  81. }
  82. void pushdown_all(int x){
  83. if(nroot(x))
  84. pushdown_all(Tree[x].fa);
  85. pushdown(x);
  86. }
  87. inline void Splay(int x){
  88. pushdown_all(x);
  89. while(nroot(x)){
  90. if(nroot(Tree[x].fa))
  91. rotate(son(x) == son(Tree[x].fa) ? Tree[x].fa : x);
  92. rotate(x);
  93. }
  94. pushup(x);
  95. }
  96. inline void access(int x){
  97. int y = x;
  98. x = Tree[x].fa;
  99. for( ; x ; y = x , x = Tree[x].fa){
  100. Splay(x);
  101. int t = x;
  102. while(Tree[t].ch[1])
  103. pushdown(t = Tree[t].ch[1]);
  104. q.push(PII(val[t] , Tree[Tree[x].ch[0]].sz + 1));
  105. ans += 1ll * get(val[t] - 1) * (Tree[Tree[x].ch[0]].sz + 1);
  106. add(val[t] , Tree[Tree[x].ch[0]].sz + 1);
  107. Tree[x].ch[1] = y;
  108. pushup(x);
  109. }
  110. }
  111. inline void clear(){
  112. while(!q.empty()){
  113. PII t = q.front();
  114. q.pop();
  115. add(t.first , -t.second);
  116. }
  117. }
  118. int main(){
  119. #ifndef ONLINE_JUDGE
  120. freopen("in","r",stdin);
  121. freopen("out","w",stdout);
  122. #endif
  123. N = read();
  124. for(int i = 1 ; i <= N ; ++i)
  125. val[i] = lsh[i] = read();
  126. sort(lsh + 1 , lsh + N + 1);
  127. cnt = unique(lsh + 1 , lsh + N + 1) - lsh - 1;
  128. for(int i = 1 ; i <= N ; ++i)
  129. val[i] = lower_bound(lsh + 1 , lsh + cnt + 1 , val[i]) - lsh;
  130. for(int i = 1 ; i < N ; ++i){
  131. int a = read() , b = read();
  132. ans = 0;
  133. Tree[b].fa = a;
  134. access(b);
  135. Splay(b);
  136. cout << ans << '\n';
  137. clear();
  138. }
  139. return 0;
  140. }

LOJ2831 JOISC2018 道路建设 LCT、树状数组的更多相关文章

  1. [JOISC2018]道路建设 LCT

    [JOISC2018]道路建设 LOJ传送门 考的时候打的大暴力,其实想到了LCT,但是思路有点没转过来.就算想到了估计也不能切,我没有在考场写LCT的自信... 其实这题不是让你直接用LCT维护答案 ...

  2. bzoj 3779 重组病毒 —— LCT+树状数组(区间修改+区间查询)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 RELEASE操作可以对应LCT的 access,RECENTER则是 makeroo ...

  3. [Codeforces1137F]Matches Are Not a Child's Play——LCT+树状数组

    题目链接: [Codeforces1137F]Matches Are Not a Child's Play 题目大意: 我们定义一棵树的删除序列为:每一次将树中编号最小的叶子删掉,将该节点编号加入到当 ...

  4. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

  5. 【BZOJ2870】最长道路tree 点分治+树状数组

    [BZOJ2870]最长道路tree Description H城很大,有N个路口(从1到N编号),路口之间有N-1边,使得任意两个路口都能互相到达,这些道路的长度我们视作一样.每个路口都有很多车辆来 ...

  6. 【bzoj3779】重组病毒 LCT+树上倍增+DFS序+树状数组区间修改区间查询

    题目描述 给出一棵n个节点的树,每一个节点开始有一个互不相同的颜色,初始根节点为1. 定义一次感染为:将指定的一个节点到根的链上的所有节点染成一种新的颜色,代价为这条链上不同颜色的数目. 现有m次操作 ...

  7. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  8. 【BZOJ-1103】大都市meg 树状数组 + DFS序

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2009  Solved: 1056[Submit][Sta ...

  9. 【BZOJ-3648】寝室管理 环套树 + 树状数组 + 点分治

    3648: 寝室管理 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 239  Solved: 106[Submit][Status][Discuss] ...

随机推荐

  1. Java并发专题(二)线程安全

    前言 随着时代的发展,CPU核数的增加和计算速度的提升,串行化的任务执行显然是对资源的极大浪费,掌握多线程是每个程序员必须掌握的技巧.但是同时多线程也是一把双刃剑,带来了共享资源安全的隐患.在本节会介 ...

  2. c语言之gdb调试。

    1.此文档演示如何使用gdb调试c语言代码. 代码如下: #include <stdio.h> /*函数声明*/ void digui(int n); int main() { ; dig ...

  3. 全网Star最多(近20k)的Spring Boot开源教程 2019 年要继续更新了!

    从2016年1月开始写博客,默默地更新<Spring Boot系列教程>,从无人问津到千万访问,作为一个独立站点(http://blog.didispace.com),相信只有那些跟我一样 ...

  4. 杭电ACM2012--素数判定

    素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. vb.net 分割byte数组的方法SplitBytes

    以下代码随手写的 并没有大量测试 效率也有待提升 如果需要C#的请自行转换 Function SplitBytes(Data As Byte(), Delimiter As Byte()) As Li ...

  6. [PHP]命令执行函数的区别

    <?php $cmd="ps aux|grep php-fpm"; $res=exec($cmd,$o); var_dump($o);//数组形式返回,每行一个元素 var_ ...

  7. sql server去掉某个字段前后空格问题

    数据通过页面表单保存到数据库,由于有个选项是一个树形的下拉框,导致保存的这个字段的数据前面有空格,在sql server中可以使用 SELECT LTRIM(RTRIM(BelongPartyCode ...

  8. Python常见的问题

    1. new.init区别,如何实现单例模式,有什么优点 new是一个静态方法,init是一个实例方法new返回一个创建的实例,init什么都不返回new返回一个cls的实例时后面的init才能被调用 ...

  9. float与double

    对数值类型的细节了解在大学里就是一带而过,自己始终也没好好看过.这是在csdn上看到的一篇文章,挺好的,记录下来. https://blog.csdn.net/Demon__Hunter/articl ...

  10. IPD体系向敏捷开发模式转型实施成功的四个关键因素

    文/杨学明  集成产品开发(IPD).集成能力成熟度模型(CMMI).敏捷开发(Agile Development)是当前国内外企业产品研发管理的最常用的3种模式.随着创新环境的快速发展,许多企业都会 ...