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模板。
对于u到v的路径操作就是makeroot(v),access(u)后对u的节点进行操作。
注意先下传乘标记再下传加标记。开unsigned int或long long(慢很多)。
#include<cstdio>
#include<iostream>
#include<algorithm>
#define nd(x) (nil + (x))
#define ll unsigned int
using namespace std;
inline int read(){
int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
const int N = 100000 + 10, mod = 51061;
int n, m;
struct Node{
bool rev;
ll dt, sum, add, mul, siz;
Node *c[2], *f;
Node();
int d() { return f->c[1] == this; }
void sc(Node *x, int d) { (c[d] = x) -> f = this; }
bool rt() { return f->c[0] != this && f->c[1] != this; }
void pup(){
sum = (c[0]->sum + c[1]->sum + dt) % mod;
siz = (c[0]->siz + c[1]->siz + 1) % mod;
}
void cal(int m, int a){
dt = (dt * m + a) % mod;
sum = (sum * m + a * siz) % mod;
add = (add * m + a) % mod;
mul = (mul * m) % mod;
}
void pdw();
}nil[N];
Node::Node() {f = c[0] = c[1] = nil;}
void Node::pdw(){
if(rev){
c[0]->rev ^= 1;
c[1]->rev ^= 1;
swap(c[0], c[1]);
rev = 0;
}
if(add != 0 || mul != 1){
if(c[0] != nil)c[0]->cal(mul, add);
if(c[1] != nil)c[1]->cal(mul, add);
mul = 1; add = 0;
}
}
void rotate(Node *x){
int d = x->d();
Node *p = x->f;
p->sc(x->c[!d], d);
if(p->rt()) x->f = p->f;
else p->f->sc(x, p->d());
x->sc(p, !d);
p->pup(); x->pup();
}
void splay(Node *x){
for(Node *y; !x->rt();){
y = x->f;
if(!y->rt()) y->f->pdw();
y->pdw(); x->pdw();
if(!y->rt()) x->d() ^ y->d() ? rotate(x) : rotate(y);
rotate(x);
}
}
void access(Node *x){
Node *t = nil, *y = x;
while(x != nil){
splay(x); x->pdw();
x->sc(t, 1); x->pup();
t = x; x = x->f;
}
splay(y);
}
void makeroot(Node *x){
access(x); x->rev ^= 1;
}
void link(Node *x, Node *y){
makeroot(x); x->f = y; splay(x);
}
void cut(Node *x, Node *y){
makeroot(x); access(y);
x->f = y->c[0] = nil;
}
void split(Node *x, Node *y){
makeroot(y); access(x);
}
void init(){
n = read(), m = read();
for(int i = 1; i <= n; i++)
nil[i].siz = nil[i].dt = nil[i].sum = nil[i].mul = 1;
int u, v;
for(int i = 1; i < n; i++){
u = read(); v = read();
link(nd(u), nd(v));
}
}
void work(){
int u, v, c;
char s[10];
for(int i = 1; i <= m; i++){
scanf("%s", s);
u = read(); v = read();
switch(s[0]){
case '+': c = read(); split(nd(u), nd(v)); nd(u)->cal(1, c); break;
case '-': cut(nd(u), nd(v)); u = read(); v = read(); link(nd(u), nd(v)); break;
case '*': c = read(); split(nd(u), nd(v)); nd(u)->cal(c, 0); break;
case '/': split(nd(u), nd(v)); printf("%d\n", nd(u)->sum); break;
}
}
}
int main(){
init();
work();
return 0;
}