题目大意:给一棵树,有四种操作:

  1. $+\;u\;v\;c:$将路径$u->v$区间加$c$
  2. $-\;u_1\;v_1\;u_2\;v_2:$将边$u_1-v_1$切断,改成边$u_2-v_2$,保证数据合法
  3. $*\;u\;v\;c:$将路径$u->v$区间乘$c$
  4. $/\;u\;v:$询问路径$u->v$区间和

题解:$LCT$乱搞

卡点:

C++ Code:

  1. #include <cstdio>
  2. #define maxn 100010
  3. #define lc(rt) son[rt][0]
  4. #define rc(rt) son[rt][1]
  5. const long long mod = 51061;
  6. int n, q;
  7. long long V[maxn], s[maxn], tg[maxn], M[maxn], A[maxn];
  8. int son[maxn][2], fa[maxn], sz[maxn];
  9. inline void swap(int &a, int &b) {a ^= b ^= a ^= b;}
  10. inline void swap(int x) {
  11. swap(lc(x), rc(x));
  12. tg[lc(x)] ^= 1, tg[rc(x)] ^= 1, tg[x] = 0;
  13. }
  14. inline void setmul(int x) {
  15. long long &tmp = M[x];
  16. M[lc(x)] = M[lc(x)] * tmp % mod, M[rc(x)] = M[rc(x)] * tmp % mod;
  17. A[lc(x)] = A[lc(x)] * tmp % mod, A[rc(x)] = A[rc(x)] * tmp % mod;
  18. V[lc(x)] = V[lc(x)] * tmp % mod, V[rc(x)] = V[rc(x)] * tmp % mod;
  19. s[lc(x)] = s[lc(x)] * tmp % mod, s[rc(x)] = s[rc(x)] * tmp % mod;
  20. tmp = 1;
  21. }
  22. inline void setadd(int x) {
  23. long long &tmp = A[x];
  24. A[lc(x)] = (A[lc(x)] + tmp) % mod, A[rc(x)] = (A[rc(x)] + tmp) % mod;
  25. V[lc(x)] = (V[lc(x)] + tmp) % mod, V[rc(x)] = (V[rc(x)] + tmp) % mod;
  26. s[lc(x)] = (s[lc(x)] + sz[lc(x)] * tmp) % mod, s[rc(x)] = (s[rc(x)] + sz[rc(x)] * tmp) % mod;
  27. tmp = 0;
  28. }
  29. inline void pushdown(int x) {
  30. if (tg[x]) swap(x);
  31. if (M[x] != 1) setmul(x);
  32. if (A[x]) setadd(x);
  33. }
  34. inline void update(int x) {
  35. s[x] = (s[lc(x)] + s[rc(x)] + V[x]) % mod;
  36. sz[x] = sz[lc(x)] + sz[rc(x)] + 1;
  37. }
  38. inline int get(int x) {return rc(fa[x]) == x;}
  39. inline bool isrt(int x) {return lc(fa[x]) != x && rc(fa[x]) != x;}
  40. inline void rotate(int x) {
  41. int y = fa[x], z = fa[y], b = get(x);
  42. if (!isrt(y)) son[z][get(y)] = x;
  43. fa[son[y][b] = son[x][!b]] = y; son[x][!b] = y;
  44. fa[y] = x, fa[x] = z;
  45. update(y), update(x);
  46. }
  47. int S[maxn], top;
  48. inline void splay(int x) {
  49. S[top = 1] = x;
  50. for (int y = x; !isrt(y); S[++top] = y = fa[y]);
  51. for (; top; top--) pushdown(S[top]);
  52. for (; !isrt(x); rotate(x)) if (!isrt(fa[x]))
  53. get(x) ^ get(fa[x]) ? rotate(x) : rotate(fa[x]);
  54. update(x);
  55. }
  56. inline void access(int x) {for (int t = 0; x; rc(x) = t, t = x, x = fa[x]) splay(x);}
  57. inline void mkrt(int x) {access(x), splay(x), tg[x] ^= 1;}
  58. inline void link(int x, int y) {mkrt(x), fa[x] = y;}
  59. inline void split(int x, int y) {mkrt(x), access(y), splay(y);}
  60. inline void cut(int x, int y) {split(x, y), lc(y) = fa[x] = 0;}
  61. inline void add(int x, int y, long long num) {
  62. split(x, y);
  63. A[y] = (A[y] + num) % mod;
  64. V[y] = (V[y] + num) % mod;
  65. s[y] = (s[y] + sz[y] * num) % mod;
  66. }
  67. inline void mul(int x, int y, long long num) {
  68. split(x, y);
  69. A[y] = A[y] * num % mod;
  70. V[y] = V[y] * num % mod;
  71. M[y] = M[y] * num % mod;
  72. s[y] = s[y] * num % mod;
  73. }
  74. inline long long query(int x, int y) {
  75. split(x, y);
  76. pushdown(y);
  77. return s[y];
  78. }
  79.  
  80. int main() {
  81. scanf("%d%d", &n, &q);
  82. for (int i = 1; i <= n; i++) V[i] = 1, M[i] = 1, s[i] = 1;
  83. for (int i = 1; i < n; i++) {
  84. int a, b;
  85. scanf("%d%d", &a, &b);
  86. link(a, b);
  87. }
  88. while (q --> 0) {
  89. int x, y;
  90. long long z;
  91. char op[10];
  92. scanf("%s%d%d", op, &x, &y);
  93. switch (*op) {
  94. case '+': scanf("%lld", &z), add(x, y, z); break;
  95. case '-': cut(x, y), scanf("%d%d", &x, &y), link(x, y); break;
  96. case '*': scanf("%lld", &z), mul(x, y, z); break;
  97. case '/': printf("%lld\n", query(x, y));
  98. }
  99. }
  100. return 0;
  101. }

[洛谷P1501][国家集训队]Tree II的更多相关文章

  1. 洛谷 P1501 [国家集训队]Tree II 解题报告

    P1501 [国家集训队]Tree II 题目描述 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\( ...

  2. 洛谷P1501 [国家集训队]Tree II(LCT,Splay)

    洛谷题目传送门 关于LCT的其它问题可以参考一下我的LCT总结 一道LCT很好的练习放懒标记技巧的题目. 一开始看到又做加法又做乘法的时候我是有点mengbi的. 然后我想起了模板线段树2...... ...

  3. 【刷题】洛谷 P1501 [国家集训队]Tree II

    题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...

  4. 洛谷P1501 [国家集训队]Tree II(LCT)

    题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...

  5. 洛谷P1501 [国家集训队]Tree II(打标记lct)

    题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...

  6. 洛谷 P1501 [国家集训队]Tree II

    看来这个LCT板子并没有什么问题 #include<cstdio> #include<algorithm> using namespace std; typedef long ...

  7. 洛谷 P1501 [国家集训队]Tree II Link-Cut-Tree

    Code: #include <cstdio> #include <algorithm> #include <cstring> #include <strin ...

  8. [洛谷P1501] [国家集训队]Tree II(LCT模板)

    传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...

  9. 洛谷.1501.[国家集训队]Tree II(LCT)

    题目链接 日常zz被define里没取模坑 //标记下放同线段树 注意51061^2 > 2147483647,要开unsigned int //*sz[]别忘了.. #include < ...

随机推荐

  1. HTTP:地址栏输入url到显示页面的步骤

    在浏览器地址栏输入URL 浏览器查看缓存,如果请求资源在缓存中并且新鲜,跳转到转码步骤 如果资源未缓存,发起新请求 如果已缓存,检验是否足够新鲜,足够新鲜直接提供给客户端,否则与服务器进行验证. 检验 ...

  2. lnmp安装后,phpmyadmin空白

    使用lnmp 一键安装后,运行phpinfo是没有问题的,说明php没有问题,但是运行phpmyadmin确实一片空白,网上说的解决方案有几种: 1.config.inc.php增加一个配置$cfg[ ...

  3. 1189: [HNOI2007]紧急疏散evacuate

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3831  Solved: 1119[Submit][Status][Discuss] Descript ...

  4. 给网站添加icon图标

    只需制成ico结尾的图片即可

  5. 再次写给VC++ Windows开发者

    距离我的上一篇文章--写给VC++ Windows开发的初学者已经4年多时间过去了,感慨于时光如梭之余,更感慨于这么多年来(从1998年我初学VC 算起吧)到如今其实我仍然还只是个初学者而已.看看之前 ...

  6. 【笔记】objdump命令的使用

    ---恢复内容开始--- objdump命令是Linux下的反汇编目标文件或者可执行文件的命令,它还有其他作用,下面以ELF格式可执行文件test为例详细介绍: objdump -f test 显示t ...

  7. 尺取法 poj 2566

    尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...

  8. 关于SSM框架项目中jsp页面EL表达式使用的一些疑问(一)

    问题 ssm框架整合中,jsp页面中EL表达式所引用的对象“page”可以在controller中使用mav.addObject(“page”,pag )进行添加,如果省略mav.addObject( ...

  9. 菜鸟学Linux - Tarball安装的一般步骤

    所谓的Tarball软件,实际上指的是从网络上下载到的源码包.通常是以.tar.gz和tar.bz2结尾.至于gz和bz2的区别在于压缩算法的不同(bz2的压缩效果好像好一些).源码包下载完成后,需要 ...

  10. 9 与python2交互

    1.创建外键 # 创建room表 mysql> create table rooms(id )); Query OK, rows affected (0.01 sec) #创建学生表 mysql ...