• HDU 5002 Tree LCT 区间更新


    Tree

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93850#problem/F

    Description

    You are given a tree with N nodes which are numbered by integers 1..N. Each node is associated with an integer as the weight.

    Your task is to deal with M operations of 4 types:

    1.Delete an edge (x, y) from the tree, and then add a new edge (a, b). We ensure that it still constitutes a tree after adding the new edge.

    2.Given two nodes a and b in the tree, change the weights of all the nodes on the path connecting node a and b (including node a and b) to a particular value x.

    3.Given two nodes a and b in the tree, increase the weights of all the nodes on the path connecting node a and b (including node a and b) by a particular value d.

    4.Given two nodes a and b in the tree, compute the second largest weight on the path connecting node a and b (including node a and b), and the number of times this weight occurs on the path. Note that here we need the strict second largest weight. For instance, the strict second largest weight of {3, 5, 2, 5, 3} is 3.

    Input

     The first line contains an integer T (T<=3), which means there are T test cases in the input.

    For each test case, the first line contains two integers N and M (N, M<=10^5). The second line contains N integers, and the i-th integer is the weight of the i-th node in the tree (their absolute values are not larger than 10^4).

    In next N-1 lines, there are two integers a and b (1<=a, b<=N), which means there exists an edge connecting node a and b.

    The next M lines describe the operations you have to deal with. In each line the first integer is c (1<=c<=4), which indicates the type of operation.

    If c = 1, there are four integers x, y, a, b (1<= x, y, a, b <=N) after c.
    If c = 2, there are three integers a, b, x (1<= a, b<=N, |x|<=10^4) after c.
    If c = 3, there are three integers a, b, d (1<= a, b<=N, |d|<=10^4) after c.
    If c = 4 (it is a query operation), there are two integers a, b (1<= a, b<=N) after c.

    All these parameters have the same meaning as described in problem description.

    Output

    For each test case, first output "Case #x:"" (x means case ID) in a separate line.

    For each query operation, output two values: the second largest weight and the number of times it occurs. If the weights of nodes on that path are all the same, just output "ALL SAME" (without quotes).
     

    Sample Input

     2 3 2 1 1 2 1 2 1 3 4 1 2 4 2 3 7 7 5 3 2 1 7 3 6 1 2 1 3 3 4 3 5 4 6 4 7 4 2 6 3 4 5 -1 4 5 7 1 3 4 2 4 4 3 6 2 3 6 5 4 3 6

    Sample Output

     Case #1: ALL SAME 1 2 Case #2: 3 2 1 1 3 2 ALL SAME

    HINT

    题意

    树上,维护连边,删除边,区间加,区间定值

    询问第二大的数是什么,以及有多少个的操作

    题解:

    LCT模板题

    主要是修改pushdown和pushup这两个地方(比较麻烦……

    代码:

    #include <cstdio>
    #include <iostream>
    using namespace std;
    #define N 300010
    const int inf = 1 << 25;
    typedef pair<int,int>dl;
    struct node
    {
        node *p,*ch[2];
        int max,rev,val,addv,min;
        int max2,ti2,ti1;
        int setv;
        int size;
    }nodes[N],*cur,*null;
    int n,m,u,v,w;
    node *newnode(int key)
    {
        cur->p=cur->ch[0]=cur->ch[1]=null;
        cur->max=cur->min=cur->val=key;
        cur->max2=cur->min;
        cur->ti2=0;
        cur->ti1=1;
        cur->rev=0;
        cur->size=0;
        cur->setv=-inf;
        return cur++;
    }
    void init()
    {
        null=nodes;
        null->p=null->ch[0]=null->ch[1]=null;
        null->max=null->val=null->max2=-inf;
        null->min=inf;
        null->ti2=0;
        null->ti1=1;
        null->addv=0;
        null->rev=0;
        null->setv=-inf;
        null->size=0;
        cur=nodes+1;
    }
    struct dynaminctree
    {
        bool isroot(node *x)//判根
        {
            return x==null || x->p->ch[0]!=x && x->p->ch[1]!=x;
        }
        void pushup(node *x)
        {
            x->max = x->val , x->max2 = -inf;x->ti1=1;x->ti2 = 0;
            x->size=1;
    
            if(x->ch[0] != null)
            {
                x->size += x->ch[0]->size;
                int mv = x->ch[0]->max;
                if(mv > x->max)
                {
                    x->max2 = x->max;
                    x->ti2 = x->ti1;
                    x->max = mv;
                    x->ti1 = x->ch[0]->ti1;
                }
                else if(mv == x->max)
                {
                    x->ti1 += x->ch[0]->ti1;
                }
                else if(mv > x->max2)
                {
                    x->max2 = mv;
                    x->ti2 = x->ch[0]->ti1;
                }
                else if(mv == x->max2)
                {
                    x->ti2 += x->ch[0]->ti1;
                }
    
                mv = x->ch[0]->max2;
                if(mv != -inf)
                {
                    if(mv > x->max2)
                    {
                        x->max2 = mv;
                        x->ti2 = x->ch[0]->ti2;
                    }
                    else if(mv == x->max2)
                    {
                        x->ti2 += x->ch[0]->ti2;
                    }
                }
            }
    
    
            //*****************//
            if(x->ch[1] != null)
            {
                x->size += x->ch[1]->size;
                int mv = x->ch[1]->max;
                if(mv > x->max)
                {
                    x->max2 = x->max;
                    x->ti2 = x->ti1;
                    x->max = mv;
                    x->ti1 = x->ch[1]->ti1;
                }
                else if(mv == x->max)
                {
                    x->ti1 += x->ch[1]->ti1;
                }
                else if(mv > x->max2)
                {
                    x->max2 = mv;
                    x->ti2 = x->ch[1]->ti1;
                }
                else if(mv == x->max2)
                {
                    x->ti2 += x->ch[1]->ti1;
                }
    
                mv = x->ch[1]->max2;
                if(mv != -inf)
                {
                    if(mv > x->max2)
                    {
                        x->max2 = mv;
                        x->ti2 = x->ch[1]->ti2;
                    }
                    else if(mv == x->max2)
                    {
                        x->ti2 += x->ch[1]->ti2;
                    }
                }
            }
    
        }
        void pushdown(node *x)
        {
            if(x==null) return;
            if(x->rev)
            {
                x->rev=0;
                if(x->ch[0]!=null) x->ch[0]->rev^=1;
                if(x->ch[1]!=null) x->ch[1]->rev^=1;
                swap(x->ch[0],x->ch[1]);
            }
            if(x->setv != -inf)
            {
                if(x->ch[0] != null)
                {
                    x->ch[0]->max = x->setv , x->ch[0]->ti1 = x->ch[0]->size;
                    x->ch[0]->max2 = -inf , x->ch[0]->ti2 = 0;
                    x->ch[0]->setv = x->setv;
                    x->ch[0]->addv = 0;
                    x->ch[0]->val = x->setv;
                }
    
                if(x->ch[1] != null)
                {
                    x->ch[1]->max = x->setv , x->ch[1]->ti1 = x->ch[1]->size;
                    x->ch[1]->max2 = -inf , x->ch[1]->ti2 = 0;
                    x->ch[1]->setv = x->setv;
                    x->ch[1]->addv = 0;
                    x->ch[1]->val = x->setv;
                }
                x->setv = -inf;
            }
    
            if(x->addv)
            {
                if(x->ch[0] != null)
                {
                    x->ch[0]->max += x->addv;
                    if(x->ch[0]->max2 != -inf) x->ch[0]->max2 += x->addv;
                    x->ch[0]->addv += x->addv;
                    x->ch[0]->val += x->addv;
                }
    
                if(x->ch[1] != null)
                {
                    x->ch[1]->max += x->addv;
                    if(x->ch[1]->max2 != -inf) x->ch[1]->max2 += x->addv;
                    x->ch[1]->addv += x->addv;
                    x->ch[1]->val += x->addv;
                }
    
                x->addv = 0;
            }
        }
        void rotate(node *x,int f)
        {
            if(isroot(x)) return;
            node *y=x->p;
            y->ch[!f]=x->ch[f];
            x->p=y->p;
            if(x->ch[f]!=null) x->ch[f]->p=y;
            if(y!=null)
            {
                if(y==y->p->ch[1]) y->p->ch[1]=x;
                else if(y==y->p->ch[0]) y->p->ch[0]=x;
            }
            x->ch[f]=y;
            y->p=x;
            pushup(y);
        }
        void splay(node *x)
        {
            static node *sta[N];
            int top=1;
            sta[0]=x;
            for(node *y=x;!isroot(y);y=y->p)
                sta[top++]=y->p;
            while (top) pushdown(sta[--top]);
            while (!isroot(x))
            {
                node *y=x->p;
                if(isroot(y)) rotate(x,x==y->ch[0]);
                else
                {
                    int f=y->p->ch[0]==y;
                    if(y->ch[f]==x) rotate(x,!f);
                    else rotate(y,f);
                    rotate(x,f);
                }
            }
            pushup(x);
        }
        node *access(node *u)
        {
            node *v=null;
            while (u!=null)
            {
                splay(u);
                v->p=u;
                u->ch[1]=v;
                pushup(u);
                v=u;
                u=u->p;
            }
            return v;
        }
        node *link(node *u,node *v)//合并
        {
            access(u);
            splay(u);
            u->rev=1;
            u->p=v;
        }
        node *cut(node *u)//分离
        {
            access(u);
            splay(u);
            u->ch[0]=u->ch[0]->p=null;
            pushup(u);
        }
        void changeroot(node *u)//换根
        {
            access(u)->rev^=1;
        }
        node *getroot(node *u)//找根
        {
            access(u);
            splay(u);
            while (u->p!=null)
            {
                u=u->p;
            }
            splay(u);
            return u;
        }
        bool queryuv(node *u,node *v)//判断是否在同一子树
        {
            while (u->p!=null) u=u->p;
            while (v->p!=null) v=v->p;
            return u==v;
        }
    }splay;
    int eu[N],ev[N];
    int main ()
    {
        //freopen("in.txt","r",stdin);
        int t;scanf("%d",&t);
        for(int cas=1;cas<=t;cas++)
        {
            int q;
            scanf("%d%d",&n,&q);
            init();
            for(int i=1;i<=n;i++)
            {
                int a;
                scanf("%d",&a);
                newnode(a);
            }
            for(int i=1;i<n;i++)
                scanf("%d%d",&eu[i],&ev[i]);
            for(int i=1;i<n;i++)
                splay.link(nodes+eu[i],nodes+ev[i]);
            printf("Case #%d:
    ",cas);
            for(int i=1;i<=q;i++)
            {
                scanf("%d",&u);
                if(u==1)
                {
                    int x,y;
                    scanf("%d%d",&x,&y);
                    scanf("%d%d",&u,&v);
                    splay.changeroot(nodes+x);
                    splay.cut(nodes+y);
                    splay.link(nodes+u,nodes+v);
                }
                else if(u==2)
                {
                    scanf("%d%d%d",&u,&v,&w);
                    splay.changeroot(nodes+u);
                    splay.access(nodes+v);
                    node *q=splay.getroot(nodes+v);
                    q->max=w;
                    q->val=w;
                    if(q->max2!=-inf)q->max2=w;
                    q->setv=w;
                }
                else if(u==3)
                {
                    scanf("%d%d%d",&u,&v,&w);
                    splay.changeroot(nodes+u);
                    splay.access(nodes+v);
                    node *q=splay.getroot(nodes+v);
                    q->addv+=w;
                    q->max+=w;
                    if(q->max2!=-inf)q->max2+=w;
                    q->val+=w;
                }
                else
                {
                    scanf("%d%d",&u,&v);
                    splay.changeroot(nodes+u);
                    splay.access(nodes+v);
                    node *q = splay.getroot(nodes+v);
                    if(q->max2 == -inf) printf("ALL SAME
    ");
                    else printf("%d %d
    ",q->max2,q->ti2);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    ArcEngine做栅格数据拉伸
    http://webhelp.esri.com/arcgisexplorer/2500/zhCN/index.html#add_raster_data.htm
    ArcEngine 9.3 学习笔记(六):图层符号化(COlorRamp,MarkerSymbol,LineSymbol,FillSymbol,TextSymbol,3DChartSymbol,使用ServerStyle符号库,FeatureRender,RasterRender)
    代码社区
    有关文件夹与文件的查找,删除等功能 在 os 模块中实现
    sar
    【深度长文】国内外雷达发展简况
    符号化Symbol(符号)体系
    ArcGIS Engine DEM拉伸渲染
    IIS 服务器应用程序不可用 解决办法
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4856035.html
Copyright © 2020-2023  润新知