题目描述

一棵n个点的树,每个点的初始权值为1。对于这棵树有q个操作,每个操作为以下四种操作之一:
+ u v c:将u到v的路径上的点的权值都加上自然数c;
- u1 v1 u2 v2:将树中原有的边(u1,v1)删除,加入一条新边(u2,v2),保证操作完之后仍然是一棵树;
* u v c:将u到v的路径上的点的权值都乘上自然数c;
/ u v:询问u到v的路径上的点的权值和,求出答案对于51061的余数。

输入

第一行两个整数n,q
接下来n-1行每行两个正整数u,v,描述这棵树
接下来q行,每行描述一个操作

输出

对于每个/对应的答案输出一行

样例输入

3 2
1 2
2 3
* 1 3 4
/ 1 1

样例输出

4


题解

带点权的LCT

需要注意的是3标记的处理:先乘后加,同时乘标记,与翻转互不影响。

这题的坑点在于int会WA,long long会TLE,必须用unsigned int。

#include <cstdio>
#include <algorithm>
#define N 100010
#define MOD 51061
#define lson c[0][x]
#define rson c[1][x]
using namespace std;
int fa[N] , c[2][N] , si[N] , rev[N];
unsigned w[N] , sum[N] , add[N] , mul[N];
char str[5];
inline int read()
{
int ret = 0; char ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9') ret = (ret << 3) + (ret << 1) + ch - '0' , ch = getchar();
return ret;
}
void pushup(int x)
{
si[x] = si[lson] + si[rson] + 1;
sum[x] = (sum[lson] + sum[rson] + w[x]) % MOD;
}
void cal(int x , unsigned a , unsigned m , int r)
{
sum[x] = (sum[x] * m + si[x] * a) % MOD;
w[x] = (w[x] * m + a) % MOD;
mul[x] = (mul[x] * m) % MOD;
add[x] = (add[x] * m + a) % MOD;
if(r) swap(lson , rson) , rev[x] ^= 1;
}
void pushdown(int x)
{
cal(lson , add[x] , mul[x] , rev[x]);
cal(rson , add[x] , mul[x] , rev[x]);
add[x] = rev[x] = 0 , mul[x] = 1;
}
bool isroot(int x)
{
return c[0][fa[x]] != x && c[1][fa[x]] != x;
}
void update(int x)
{
if(!isroot(x)) update(fa[x]);
pushdown(x);
}
void rotate(int x)
{
int y = fa[x] , z = fa[y] , l = (c[1][y] == x) , r = l ^ 1;
if(!isroot(y)) c[c[1][z] == y][z] = x;
fa[x] = z , fa[y] = x , fa[c[r][x]] = y , c[l][y] = c[r][x] , c[r][x] = y;
pushup(y) , pushup(x);
}
void splay(int x)
{
update(x);
while(!isroot(x))
{
int y = fa[x] , z = fa[y];
if(!isroot(y))
{
if((c[0][y] == x) ^ (c[0][z] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int t = 0;
while(x) splay(x) , rson = t , pushup(x) , t = x , x = fa[x];
}
void makeroot(int x)
{
access(x) , splay(x);
swap(lson , rson) , rev[x] ^= 1;
}
void link(int x , int y)
{
makeroot(x) , fa[x] = y;
}
void cut(int x , int y)
{
makeroot(x) , access(y) , splay(y) , c[0][y] = fa[x] = 0 , pushup(y);
}
void split(int x , int y)
{
makeroot(y) , access(x) , splay(x);
}
int main()
{
int n , m , i , x , y;
unsigned z;
n = read() , m = read();
for(i = 1 ; i <= n ; i ++ ) si[i] = w[i] = sum[i] = mul[i] = 1;
for(i = 1 ; i < n ; i ++ ) x = read() , y = read() , link(x , y);
while(m -- )
{
scanf("%s" , str) , x = read() , y = read();
switch(str[0])
{
case '+': z = (unsigned)read() , split(x , y) , cal(x , z , 1 , 0); break;
case '-': cut(x , y) , x = read() , y = read() , link(x , y); break;
case '*': z = (unsigned)read() , split(x , y) , cal(x , 0 , z , 0); break;
default: split(x , y) , printf("%u\n" , sum[x]);
}
}
return 0;
}

最新文章

  1. Apache执行Python脚本
  2. 字典NSDictionary以及NSMutableDictionary的用法总结
  3. MVVM架构~Knockoutjs系列之验证机制的引入
  4. 让CKEditor支持FLV视频播放
  5. 局域网IP段
  6. HDU-1255 覆盖的面积 (扫描线)
  7. CentOS 搭建 FastDFS-5.0.5集群
  8. 查找EBS中各种文件版本(Finding File Versions in the Oracle Applications EBusiness Suite - Checking the $HEADER)
  9. Mac Please try running this command again as root/Administrator.
  10. linux下观看b站视频,解决字体乱码
  11. Java微信开发_02_本地服务器映射外网
  12. 洛谷 [P1341]无序字母对
  13. is not eligible for getting processed by all BeanPostProcessors
  14. Java 开发常用工具
  15. C# LINQ语法详解
  16. jsp标签库选择框示例
  17. javascript之Map
  18. Coding and Paper Letter(五十八)
  19. JavaScript(JS)之Javascript对象DOM之增删改查(四)
  20. Java 遍历类中的属性

热门文章

  1. sublime text 3 配置terminal 启动npm
  2. java中的基本算法
  3. phpspider 的简单使用
  4. JNI模板
  5. Excel学习路径总结
  6. 深入了解jQuery Mobile-3装载器
  7. C语言 字符数组与字符指针比较
  8. 【转】手把手教你:Ubuntu14+apache2+django1.7+python2.7下网页/网站部署
  9. 20160120使用myeclipse一年开始转IntelliJ IDEA 15做以下总结
  10. 台湾ML笔记--1.2 formalize the learning probelm