Bzoj2631 Tree

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Elemmir/article/details/51789046

Link-Cut-Tree模板题。

/*
簡単でしょ?笑えるよね。
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int mod = 51061;
#define LL long long
#define uint unsigned int
struct node {
    int siz;
    uint sum,key;
    uint add,mul;
    bool rev;
    node *ch[2],*fa;
} tree[maxn];
int n,m;
#define siz(x) ( x == NULL ? 0 : x->siz )
#define sum(x) ( x == NULL ? 0 : x->sum )
void update(node *x,uint a,uint m) {
    if ( x == NULL ) return;
    x->key = ( (LL) x->key * m ) % mod + a % mod;
    x->sum = (( (LL)x->sum * m ) % mod + (LL) a * x->siz ) % mod;
    x->add = ( (LL) x->add * m ) % mod + a % mod;
    x->mul = (LL) x->mul * m % mod;
}
bool isroot(node *x) {
    return x->fa == NULL || ( x->fa->ch[0] != x && x->fa->ch[1] != x);
}
void pushup(node *x) {
    x->sum = ((LL)x->key + sum(x->ch[0]) + sum(x->ch[1]) ) % mod;
    x->siz = siz(x->ch[0]) + siz(x->ch[1]) + 1;
}
void pushdown(node *x) {
    if ( x->rev ) {
        x->rev = 0;
        if ( x->ch[0] ) x->ch[0]->rev ^= 1;
        if ( x->ch[1] ) x->ch[1]->rev ^= 1;
        swap(x->ch[0],x->ch[1]);
    }
    if ( x->add == 0 && x->mul == 1) return;
    if ( x->ch[0] ) update(x->ch[0],x->add,x->mul);
    if ( x->ch[1] ) update(x->ch[1],x->add,x->mul);
    x->add = 0; x->mul = 1;
}
void rotate(node *x,bool d) {
    node *y = x->fa;
    y->ch[!d] = x->ch[d]; if ( x->ch[d] ) x->ch[d]->fa = y;
    x->fa = y->fa;
    if ( y->fa && y == y->fa->ch[0] ) 
        y->fa->ch[0] = x;
    else if ( y->fa && y == y->fa->ch[1] ) 
        y->fa->ch[1] = x;
    x->ch[d] = y; y->fa = x;
    pushup(y); pushup(x);
}
node *sta[maxn]; int top = 0;
void splay(node *x) {
    top = 0;
    sta[++top] = x;
    for (node *y = x; !isroot(y); y = y->fa) sta[++top] = y->fa;
    while(top) pushdown(sta[top--]);
    while (!isroot(x)) {
        node *y = x->fa;
        if ( x == y->ch[0] ) {
            if ( !isroot(y) && y == y->fa->ch[0])
                rotate(y,1);
            rotate(x,1);
        } else {
            if ( !isroot(y) && y == y->fa->ch[1])
                rotate(y,0);
            rotate(x,0);
        }
    }
}
void access(node *x) {
    for (node *y = NULL; x; x = x->fa) {
        splay(x); x->ch[1] = y; pushup(y = x);
    }
}
void makeroot(node *x) {
    access(x); splay(x); x->rev ^= 1;
}
void link(node *x,node *y) {
    makeroot(x); x->fa = y;
}
void cut(node *x,node *y) {
    makeroot(x); access(y); splay(y);
    y->ch[0] = x->fa = NULL;
}
void modify(node *x,node *y,uint a,uint m) {
    makeroot(x); access(y); splay(y);
    update(y,a,m);
}
uint query(node *x,node *y) {
    makeroot(x); access(y); splay(y);
    return y->sum;
}
int main() {
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++)
        tree[i].key = tree[i].siz = tree[i].sum = tree[i].mul = 1;
    for (int i = 1; i <= n-1; i++) {
        int x,y;
        scanf("%d%d",&x,&y);
        link(&tree[x],&tree[y]);
    }
    char str[3]; int x,y,z,w;
    while (m--) {
        scanf("%s",str);
        if ( str[0] == '+' ) {
            scanf("%d%d%d",&x,&y,&z);
            modify(&tree[x],&tree[y],z,1);
        }
        if ( str[0] == '-' ) {
            scanf("%d%d%d%d",&x,&y,&z,&w);
            cut(&tree[x],&tree[y]);
            link(&tree[z],&tree[w]);
        }
        if ( str[0] == '*' ) {
            scanf("%d%d%d",&x,&y,&z);
            modify(&tree[x],&tree[y],0,z);
        }
        if ( str[0] == '/' ) {
            scanf("%d%d",&x,&y);
            printf("%d\n",query(&tree[x],&tree[y]));
        }
    }
}
阅读更多
换一批

Tree Tree Tree

11-05

Web应用程序中用拖放的方式产生的treeview加上节点为什么在.aspx.cs中看不到代码?rn如何才能手写代码产生树即节点?rn如果先用拖入的方式生成了树和节点,还能用代码加节点吗?rnrnrnusing System;rnusing System.Collections;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Drawing;rnusing System.Web;rnusing System.Web.SessionState;rnusing System.Web.UI;rnusing System.Web.UI.WebControls;rnusing System.Web.UI.HtmlControls;rnrnnamespace WebApp1rnrn /// rn /// WebForm3 的摘要说明。rn /// rn public class treetest : System.Web.UI.Pagern rn protected Microsoft.Web.UI.WebControls.TreeView TreeView1;//这里是拖放后自动生成的;rn protected System.Web.UI.WebControls.Label Label1;rn TreeNode root = new TreeNode();rn//我手工代码加根节点就不能编译rnD:\Inetpub\wwwroot\WebApp1\treetest.aspx.cs(21): 找不到类型或命名空间名称“TreeNode”(是否缺少 using 指令或程序集引用?)rnrnrn rn private void Page_Load(object sender, System.EventArgs e)rn rn rn rn rnrn #region Web Form Designer generated codern override protected void OnInit(EventArgs e)rn rn //rn // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。rn //rn InitializeComponent();rn base.OnInit(e);rn rn rn /// rn /// 设计器支持所需的方法 - 不要使用代码编辑器修改rn /// 此方法的内容。rn /// rn private void InitializeComponent()rn rn this.Load += new System.EventHandler(this.Page_Load);rnrn rn #endregionrn rnrn

没有更多推荐了,返回首页