P1501 [国家集训队]Tree II
因为本题树形结构会改变,所以这里需要使用LCT来代替树链剖分来解决问题,所以就要涉及到关于LCT的一条链上的lazy标记的下放了。
很明显的一件事就是,我们可以在pushdown()操作中进行下放懒标记,因为本题与是否翻转没有直接关联,所以无须考虑r[]的翻转标记的作用。
void pushdown(int x)
{
clear(0);
if(mul[x] ^ 1)
{
if(c[x][0])
{
mul[c[x][0]] = mul[c[x][0]] * mul[x] % mod;
add[c[x][0]] = add[c[x][0]] * mul[x] % mod;
s[c[x][0]] = s[c[x][0]] * mul[x] % mod;
v[c[x][0]] = v[c[x][0]] * mul[x] % mod;
}
if(c[x][1])
{
mul[c[x][1]] = mul[c[x][1]] * mul[x] % mod;
add[c[x][1]] = add[c[x][1]] * mul[x] % mod;
s[c[x][1]] = s[c[x][1]] * mul[x] % mod;
v[c[x][1]] = v[c[x][1]] * mul[x] % mod;
}
mul[x] = 1;
}
if(add[x])
{
if(c[x][0])
{
add[c[x][0]] = (add[c[x][0]] + add[x]) % mod;
s[c[x][0]] = (s[c[x][0]] + siz[c[x][0]] * add[x]) % mod;
v[c[x][0]] = (v[c[x][0]] + add[x]) % mod;
}
if(c[x][1])
{
add[c[x][1]] = (add[c[x][1]] + add[x]) % mod;
s[c[x][1]] = (s[c[x][1]] + siz[c[x][1]] * add[x]) % mod;
v[c[x][1]] = (v[c[x][1]] + add[x]) % mod;
}
add[x] = 0;
}
if(r[x])
{
if(c[x][0]) pushr(c[x][0]);
if(c[x][1]) pushr(c[x][1]);
r[x] = 0;
}
}
所以,这里的懒标记的下推可以这么来写。
那么,什么时候需要pushdown呢?就是当节点在各自的Splay树中的位置改变的时候,那么也就是需要翻转了,实际上Rotate一个节点的时候,我们要下推他的子树的自身的根的到它的路径上的lazy,因为要改变树形顺序结构了,所以在Splay的时候,我们用栈来记录需要pushdown的节点个数。
int Stap[maxN];
void Splay(int x)
{
int y = x, z = 0;
Stap[++z] = y;
while(!isroot(y)) Stap[++z] = y = fa[y];
while(z) pushdown(Stap[z--]);
while(!isroot(x))
{
y = fa[x]; z = fa[y];
if(!isroot(y)) (c[z][0] == y) ^ (c[y][0] == x) ? Rotate(x) : Rotate(y);
Rotate(x);
}
}
因为,不需要维护子树信息,所以在Access操作以及链接边和断开边的操作上就不需要有其他的改变了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
const ll mod = 51061;
int N, Q;
namespace LCT
{
int fa[maxN], c[maxN][2];
ll s[maxN], v[maxN], mul[maxN], add[maxN];
int r[maxN], siz[maxN];
void clear(int x) { s[x] = v[x] = add[x] = r[x] = siz[x] = c[x][0] = c[x][1] = fa[x] = 0; mul[x] = 1; }
bool isroot(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
void pushup(int x)
{
clear(0);
siz[x] = siz[c[x][0]] + siz[c[x][1]] + 1;
s[x] = (s[c[x][0]] + s[c[x][1]] + v[x]) % mod;
}
void pushr(int x) { swap(c[x][0], c[x][1]); r[x] ^= 1; }
void pushdown(int x)
{
clear(0);
if(mul[x] ^ 1)
{
if(c[x][0])
{
mul[c[x][0]] = mul[c[x][0]] * mul[x] % mod;
add[c[x][0]] = add[c[x][0]] * mul[x] % mod;
s[c[x][0]] = s[c[x][0]] * mul[x] % mod;
v[c[x][0]] = v[c[x][0]] * mul[x] % mod;
}
if(c[x][1])
{
mul[c[x][1]] = mul[c[x][1]] * mul[x] % mod;
add[c[x][1]] = add[c[x][1]] * mul[x] % mod;
s[c[x][1]] = s[c[x][1]] * mul[x] % mod;
v[c[x][1]] = v[c[x][1]] * mul[x] % mod;
}
mul[x] = 1;
}
if(add[x])
{
if(c[x][0])
{
add[c[x][0]] = (add[c[x][0]] + add[x]) % mod;
s[c[x][0]] = (s[c[x][0]] + siz[c[x][0]] * add[x]) % mod;
v[c[x][0]] = (v[c[x][0]] + add[x]) % mod;
}
if(c[x][1])
{
add[c[x][1]] = (add[c[x][1]] + add[x]) % mod;
s[c[x][1]] = (s[c[x][1]] + siz[c[x][1]] * add[x]) % mod;
v[c[x][1]] = (v[c[x][1]] + add[x]) % mod;
}
add[x] = 0;
}
if(r[x])
{
if(c[x][0]) pushr(c[x][0]);
if(c[x][1]) pushr(c[x][1]);
r[x] = 0;
}
}
void Rotate(int x)
{
int y = fa[x], z = fa[y], k = c[y][1] == x;
if(!isroot(y)) c[z][c[z][1] == y] = x;
fa[x] = z;
c[y][k] = c[x][k ^ 1];
fa[c[x][k ^ 1]] = y;
c[x][k ^ 1] = y;
fa[y] = x;
pushup(y);
pushup(x);
if(z) pushup(z);
}
int Stap[maxN];
void Splay(int x)
{
int y = x, z = 0;
Stap[++z] = y;
while(!isroot(y)) Stap[++z] = y = fa[y];
while(z) pushdown(Stap[z--]);
while(!isroot(x))
{
y = fa[x]; z = fa[y];
if(!isroot(y)) (c[z][0] == y) ^ (c[y][0] == x) ? Rotate(x) : Rotate(y);
Rotate(x);
}
}
void Access(int x)
{
int y = 0;
while(x)
{
Splay(x);
c[x][1] = y;
pushup(x);
y = x;
x = fa[x];
}
}
void makeroot(int x)
{
Access(x);
Splay(x);
pushr(x);
}
int findroot(int x)
{
Access(x);
Splay(x);
while(c[x][0])
{
pushdown(x);
x = c[x][0];
}
Splay(x);
return x;
}
void Split(int x, int y)
{
makeroot(x);
Access(y);
Splay(y);
}
void link(int x, int y)
{
makeroot(x);
if(findroot(y) != x)
{
fa[x] = y;
}
}
void cut(int x, int y)
{
makeroot(x);
if(findroot(y) != x || fa[y] != x || c[y][0]) return;
fa[y] = c[x][1] = 0;
pushup(x);
}
};
using namespace LCT;
int main()
{
scanf("%d%d", &N, &Q);
for(int i=1; i<=N; i++) clear(i);
for(int i=1; i<=N; i++) v[i] = 1;
for(int i=1, u, v; i<N; i++)
{
scanf("%d%d", &u, &v);
link(u, v);
}
char op[3];
for(int i=1; i<=Q; i++)
{
scanf("%s", op);
switch (op[0])
{
case '+':
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
Split(x, y);
add[y] = (add[y] + z) % mod;
v[y] = (v[y] + z) % mod;
s[y] = (s[y] + siz[y] * z) % mod;
break;
}
case '-':
{
int u1, v1, u2, v2;
scanf("%d%d%d%d", &u1, &v1, &u2, &v2);
cut(u1, v1);
link(u2, v2);
break;
}
case '*':
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
Split(x, y);
mul[y] = mul[y] * z % mod;
add[y] = add[y] * z % mod;
v[y] = v[y] * z % mod;
s[y] = s[y] * z % mod;
break;
}
default:
{
int x, y;
scanf("%d%d", &x, &y);
Split(x, y);
printf("%lld\n", s[y]);
break;
}
}
}
return 0;
}