【题目分析】

又一道LCT的题目,LCT只能维护链上的信息。

【代码】

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>

using namespace std;

#define maxn 1000005
#define inf (0x3f3f3f3f)
#define mod 51061

unsigned int read()
{
    unsigned int x=0,f=1; char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

unsigned int ch[maxn][2],fa[maxn],add[maxn],mul[maxn],num[maxn],sum[maxn];
unsigned int n,m,siz[maxn],rev[maxn],sta[maxn],top=0;

void update(unsigned int x)
{
    siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
    sum[x]=(sum[ch[x][0]]+sum[ch[x][1]]+num[x])%mod;
}

void solve(unsigned int x,unsigned int ad,unsigned int ml)
{
    if (!x) return ;
    add[x]=(add[x]*ml+ad)%mod; mul[x]=(mul[x]*ml)%mod;
    sum[x]=(sum[x]*ml+siz[x]*ad)%mod; num[x]=(num[x]*ml+ad)%mod;
}

void pushdown(unsigned int x)
{
    if (rev[x])
    {
        rev[x]^=1;
        rev[ch[x][0]]^=1;
        rev[ch[x][1]]^=1;
        swap(ch[x][0],ch[x][1]);
    }
    solve(ch[x][0],add[x],mul[x]);
    solve(ch[x][1],add[x],mul[x]);
    mul[x]=1; add[x]=0;
}

bool isroot(unsigned int x)
{
    return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;
}

void rot(unsigned int x)
{
    unsigned int y=fa[x],z=fa[y],l,r;
    if (ch[y][0]==x) l=0; else l=1;
    r=l^1;
    if (!isroot(y))
    {
        if (ch[z][0]==y) ch[z][0]=x;
        else ch[z][1]=x;
    }
    fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
    ch[y][l]=ch[x][r]; ch[x][r]=y;
    update(y); update(x);
}

void splay(unsigned int x)
{
    unsigned int top=0; sta[top++]=x;
    for (int i=x;!isroot(i);i=fa[i]) sta[top++]=fa[i];
    while (top--) pushdown(sta[top]);

    while (!isroot(x))
    {
        unsigned int y=fa[x],z=fa[y];
        if (!isroot(y))
        {
            if (ch[z][0]==y^ch[y][0]==x) rot(x);
            else rot(y);
        }
        rot(x);
    }
}

void access(unsigned int x)
{
    for (int t=0;x;t=x,x=fa[x])
        splay(x),ch[x][1]=t,update(x);
}

void makeroot(unsigned int x)
{
    access(x);
    splay(x);
    rev[x]^=1;
}

unsigned int find(unsigned int x)
{
    access(x);
    splay(x);
    while (ch[x][0]) x=ch[x][0];
    return x;
}

void link(unsigned int x,unsigned int y)
{
    makeroot(x);
    fa[x]=y;
}

void cut(unsigned int x,unsigned int y)
{
    makeroot(x);
    access(y);
    splay(y);
    ch[y][0]=fa[x]=0;
}

int main()
{
    n=read();m=read();
    for (unsigned int i=1;i<=n;++i) num[i]=1,update(i);
    char opt[10];
    for (unsigned int i=1;i<n;++i) link(read(),read());
    while (m--)
    {
        scanf("%s",opt);
        unsigned int x,y,z;
        switch (opt[0])
        {
            case '+':
                x=read(); y=read(); z=read();
                makeroot(x);
                access(y);
                splay(y);
                solve(y,z,1);
                break;
            case '-':
                cut(read(),read()); link(read(),read());
                break;
            case '*':
                x=read();y=read();z=read();
                makeroot(x);
                access(y);
                splay(y);
                solve(y,0,z);
                break;
            case '/':
                x=read();y=read();
                makeroot(x);
                access(y);
                splay(y);
                printf("%u\n",sum[y]);
                break;
        }
    }
}

  

BZOJ 2631 Tree ——Link-Cut Tree的更多相关文章

  1. bzoj 3282: Tree (Link Cut Tree)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282 题面: 3282: Tree Time Limit: 30 Sec  Memory L ...

  2. 【BZOJ 3282】Tree Link Cut Tree模板题

    知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,, 现在才知道这些,,,真心弱,,, #include<cstdio ...

  3. [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)

    [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...

  4. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  5. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  6. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  7. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  8. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  9. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  10. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

随机推荐

  1. 无限轮播的N+2 策略

    N张照片把contentsSize设置为N+2个图片的宽度,例子如下,两端填充如图,当处于一端时,且即将进入循环状态的时候,如第二张图,从状态1滑动到状态2,在滑动结束的时候,将当前的位置直接转到状态 ...

  2. Hibernate 所有缓存机制详解

    hibernate提供的一级缓存 hibernate是一个线程对应一个session,一个线程可以看成一个用户.也就是说session级缓存(一级缓存)只能给一个线程用,别的线程用不了,一级缓存就是和 ...

  3. DX使用texconv工具批处理dds格式图片

    texconv D:\png\*.* -o E:\dds -m 5 -f dxt3 -ft dds 上述命令的意思是把D:\png目录下的全部文件(当然可以指定特定格式例如*.png)转换成dds格式 ...

  4. October 3rd 2016 Week 41st Monday

    Better to light one candle than to curse the darkness. 与其诅咒黑暗,不如燃起蜡烛. Sitting in the darkness and wa ...

  5. springMVC创建基础变量

    在springMVC中有一些变量是基础变量,可以在全局多个地方使用,在修改规则的时候,这样只用修改一个地方就好了,而且可以避免很多不必要的bug出现下面就来总结一下在我的项目中如何去创建一个全局基础变 ...

  6. Web前端开发工程师的就业前景

    Web前端开发工程师的就业前景 Web前端开发工程师是一个全新的职业,在IT行业真正受到重视的时间不超过5年.因此,大家越来越关心web前端工程师前景怎么样?web前端工程师就业如何?Web前端开发是 ...

  7. Oracle读写分离架构

    读写分离是架构分布式系统的一个重要思想.不少系统整体处理能力并不能同业务的增长保持同步,因此势必会带来瓶颈,单纯的升级硬件并不能一劳永逸.针对业务类型特点,需要从架构模式上进行一系列的调整,比如业务模 ...

  8. MVC - 18.缓存

    1.使用输出缓存 (不灵活,问题比较多,不建议使用) /// <summary> /// datagrid列表 /// </summary> /// <returns&g ...

  9. 【JAVA集合框架之Map】

    一.概述.1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...

  10. Power BI for Office 365介绍

    微软在七月份发布了一个新产品,它建立在微软的云的第一个数据平台- Power BI for Office 365.Satya Nadella,服务器和工具业务总裁,在当天的上午在微软的年度全球合作伙伴 ...