题目链接

Tree Ⅱ\(=\)【模板】LCT+【模板】线段树2。。

分别维护3个标记,乘的时候要把加法标记也乘上。

还有就是模数的平方刚好爆\(int\),所以开昂赛德\(int\)就可以了。

我把初始化放在连边的那个循环里了,而那个循环是\(1\)到\(n-1\)的,所以第\(n\)个没初始化到。。\(WA\)了好久。

#include <cstdio>
#include <cstring>
#define YCH 51061
#define R register unsigned int
#define I inline void
#define lc c[x][0]
#define rc c[x][1]
#define mul(x, y) x *= y; x %= YCH
#define add(x, y) x += y; x %= YCH
const int MAXN = 100010;
inline int read(){
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
unsigned int f[MAXN], c[MAXN][2], v[MAXN], s[MAXN], st[MAXN], rt[MAXN], mt[MAXN], at[MAXN], sz[MAXN];
inline int nroot(R x){
return c[f[x]][0] == x || c[f[x]][1] == x;
}
I pushup(R x){
s[x] = (s[lc] + s[rc] + v[x]) % YCH;
sz[x] = sz[lc] + sz[rc] + 1;
}
I pushr(R x){
lc ^= rc; rc = lc ^ rc; lc ^= rc; rt[x] ^= 1;
}
I pushm(R x, R p){
mul(s[x], p); mul(at[x], p);
mul(mt[x], p); mul(v[x], p);
}
I pusha(R x, R p){
add(s[x], p * sz[x]);
add(at[x], p); add(v[x], p);
}
I pushdown(R x){
if(mt[x] != 1){
pushm(lc, mt[x]); pushm(rc, mt[x]);
mt[x] = 1;
}
if(at[x]){
pusha(lc, at[x]); pusha(rc, at[x]);
at[x] = 0;
}
if(rt[x]){
pushr(lc); pushr(rc);
rt[x] = 0;
}
}
I rotate(R x){
R y = f[x], z = f[y], k = c[y][1] == x, w = c[x][!k];
if(nroot(y)) c[z][c[z][1] == y] = x;
c[x][!k] = y; c[y][k] = w; f[y] = x; f[x] = z;
if(w) f[w] = y;
pushup(y);
}
I splay(R x){
R y = x, z = 0;
st[++z] = y;
while(nroot(y)) st[++z] = y = f[y];
while(z) pushdown(st[z--]);
while(nroot(x)){
y = f[x]; z = f[y];
if(nroot(y)) (c[z][1] == y) ^ (c[y][1] == x) ? rotate(x) : rotate(y);
rotate(x);
}
pushup(x);
}
I access(R x){
for(R y = 0; x; x = f[y = x]){
splay(x); rc = y; pushup(x);
}
}
I makeroot(R x){
access(x); splay(x);
pushr(x);
}
I split(R x, R y){
makeroot(x); access(y); splay(y);
}
I link(R x, R y){
makeroot(x);
f[x] = y;
}
I cut(R x, R y){
split(x, y);
f[x] = c[y][0] = 0;
pushup(y);
}
int n, m, a, b, x, y;
char opt;
int main(){
n = read(); m = read();
for(R i = 1; i < n; ++i)
link(read(), read()), v[i] = mt[i] = 1;
v[n] = mt[n] = 1;
while(m--){
opt = getchar(); while(opt != '-' && opt != '+' && opt != '*' && opt != '/') opt = getchar();
a = read(); b = read();
switch(opt){
case '+' : x = read(); split(a, b); pusha(b, x); break;
case '-' : x = read(); y = read(); cut(a, b); link(x, y); break;
case '*' : x = read(); split(a, b); pushm(b, x); break;
case '/' : split(a, b); printf("%d\n", s[b]); break;
}
}
return 0;
}

最新文章

  1. VPN使用指南|稳定的VPN|
  2. 数据库连接池dbcp基本配置
  3. java 调用 r, Can&#39;t find dependent libraries
  4. 趣拍SDK接入问题Android
  5. Applescript 带参数调用某个App的方法
  6. ServiceStack
  7. moto xt800 刷机到2.2.2
  8. nopCommerce的配置以及汉化
  9. ssh(Struts2+hibernate+spring)简单分页
  10. 谷歌浏览器chrome假死、卡死、经常无反应,火狐firefox闪黑格子的解决办法(显卡/驱动兼容问题)
  11. [BZOJ 2165] 大楼 【DP + 倍增 + 二进制】
  12. [Angular 2] Create a simple search Pipe
  13. rm-rf 恢复过程中滥用
  14. 理解java值传递与引用传递
  15. 树链剖分——边权poj2763
  16. hadoop学习笔记--找到执行hadoop的入口
  17. 爬虫之抓取js生成的数据
  18. python下载安装requests库
  19. hihoCoder 1632 Secret Poems(ACM-ICPC北京赛区2017网络同步赛)
  20. 教你动手做一个 iOS 越狱 app

热门文章

  1. 【剑指offer】Java实现(持续更新中)
  2. 两个list比较相等元素
  3. Contest 1
  4. atcoder 2017Code festival C ——D题 Yet Another Palindrome Partitioning(思维+dp)
  5. 【BZOJ5333】荣誉称号(动态规划)
  6. BZOJ4033:[HAOI2015]树上染色——题解
  7. 洛谷 P2505 [HAOI2012]道路 解题报告
  8. 【loj2472】IIIDX
  9. python基础----列表生成式、生成器表达式
  10. Linux应用编程之串口操作20170901