[COGS 1799][国家集训队2012]tree(伍一鸣)

Description

一棵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的余数。

Input

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

Output

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

Sample Input

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

Sample Output

4

Hint

10%的数据保证,1<=n,q<=2000
另外15%的数据保证,1<=n,q<=5*10^4,没有-操作,并且初始树为一条链
另外35%的数据保证,1<=n,q<=5*10^4,没有-操作
100%的数据保证,1<=n,q<=10^5,0<=c<=10^4

题解

比较简单,用来练习 $lct$ 上的 $lazy$ 操作。

 //It is made by Awson on 2018.1.16
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
using namespace std;
const int MOD = ;
const int N = 1e5;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
}
void write(int x) {
if (x > ) write(x/);
putchar(x%+);
} char ch[];
int n, q, u, v, c;
struct Link_Cut_Tree {
int ch[N+][], pre[N+], rev[N+], sum[N+], prod[N+], val[N+], tol[N+], isrt[N+], size[N+];
Link_Cut_Tree() {for (int i = ; i <= N; i++) val[i] = tol[i] = isrt[i] = prod[i] = size[i] = ; }
void pushup(int o) {tol[o] = (tol[ch[o][]]+tol[ch[o][]]+val[o])%MOD, size[o] = (size[ch[o][]]+size[ch[o][]]+)%MOD; }
void pushdown(int o) {
int ls = ch[o][], rs = ch[o][];
if (rev[o]) {
Swap(ch[ls][], ch[ls][]), Swap(ch[rs][], ch[rs][]);
rev[ls] ^= , rev[rs] ^= , rev[o] = ;
}
if (prod[o] != ) {
prod[ls] = (LL)prod[ls]*prod[o]%MOD, prod[rs] = (LL)prod[rs]*prod[o]%MOD;
sum[ls] = (LL)sum[ls]*prod[o]%MOD, sum[rs] = (LL)sum[rs]*prod[o]%MOD;
val[ls] = (LL)val[ls]*prod[o]%MOD, val[rs] = (LL)val[rs]*prod[o]%MOD;
tol[ls] = (LL)tol[ls]*prod[o]%MOD, tol[rs] = (LL)tol[rs]*prod[o]%MOD;
prod[o] = ;
}
if (sum[o]) {
sum[ls] = (sum[ls]+sum[o])%MOD, sum[rs] = (sum[rs]+sum[o])%MOD;
val[ls] = (val[ls]+sum[o])%MOD, val[rs] = (val[rs]+sum[o])%MOD;
tol[ls] = (tol[ls]+(LL)sum[o]*size[ls]%MOD)%MOD, tol[rs] = (tol[rs]+(LL)sum[o]*size[rs]%MOD)%MOD;
sum[o] = ;
}
}
void push(int o) {
if (!isrt[o]) push(pre[o]);
pushdown(o);
}
void rotate(int o, int kind) {
int p = pre[o];
ch[p][!kind] = ch[o][kind], pre[ch[o][kind]] = p;
if (isrt[p]) isrt[o] = , isrt[p] = ;
else ch[pre[p]][ch[pre[p]][] == p] = o;
pre[o] = pre[p];
ch[o][kind] = p, pre[p] = o;
pushup(p), pushup(o);
}
void splay(int o) {
push(o);
while (!isrt[o]) {
if (isrt[pre[o]]) rotate(o, ch[pre[o]][] == o);
else {
int p = pre[o], kind = ch[pre[p]][] == p;
if (ch[p][kind] == o) rotate(o, !kind), rotate(o, kind);
else rotate(p, kind), rotate(o, kind);
}
}
}
void access(int o) {
int y = ;
while (o) {
splay(o);
isrt[ch[o][]] = , isrt[ch[o][] = y] = ;
pushup(o); o = pre[y = o];
}
}
void makeroot(int o) {access(o), splay(o); rev[o] ^= , Swap(ch[o][], ch[o][]); }
void link(int x, int y) {makeroot(x); pre[x] = y; }
void cut(int x, int y) {makeroot(x), access(y), splay(y); ch[y][] = pre[x] = , isrt[x] = ; pushup(y); }
void split(int x, int y) {makeroot(x), access(y), splay(y); }
void add(int x, int y, int c) {split(x, y); sum[y] = (sum[y]+c)%MOD, val[y] = (val[y]+c)%MOD, tol[y] = (tol[y]+(LL)c*size[y]%MOD)%MOD; }
void plus(int x, int y, int c) {split(x, y); prod[y] = (LL)prod[y]*c%MOD, sum[y] = (LL)sum[y]*c%MOD, val[y] = (LL)val[y]*c%MOD, tol[y] = (LL)tol[y]*c%MOD; }
int query(int x, int y) {split(x, y); return tol[y]; }
}T; void work() {
read(n), read(q);
for (int i = ; i < n; i++) {
read(u), read(v); T.link(u, v);
}
while (q--) {
scanf("%s", ch);
if (ch[] == '+') {read(u), read(v), read(c); T.add(u, v, c); }
else if (ch[] == '-') {
read(u), read(v); T.cut(u, v);
read(u), read(v); T.link(u, v);
}
else if (ch[] == '*') {read(u), read(v), read(c); T.plus(u, v, c); }
else if (ch[] == '/') {read(u), read(v); write(T.query(u, v)), putchar('\n'); }
}
}
int main() {
work();
return ;
}
上一篇:在ASP.NET中,IE与Firefox下载文件带汉字名时乱码的解决方法


下一篇:Android建立gprs连接源码