欢迎光临
我们一直在努力

PLCT检查是什么luogu P4719 【模板】动态 DP (LCT版)

// luogu-judger-enable-o2
//Dynamic DP with LCT 
#include<bits/stdc++.h>
#define ll long long 
#define setIO(s) freopen(s".in","r",stdin)  
#define maxn 100002 
#define inf 100000000 
using namespace std; 
//Link cut tree
void de()
{
    printf("ok
"); 
}
namespace LCT
{     

    struct Matrix
    {
        ll a[2][2];
        ll*operator[](int x){ return a[x];}    
    }t[maxn],tmp[maxn];  
    Matrix operator*(Matrix a,Matrix b)
    {
    	Matrix c;
    	c[0][0]=max(a[0][0]+b[0][0],a[0][1]+b[1][0]); 
    	c[0][1]=max(a[0][0]+b[0][1],a[0][1]+b[1][1]); 
    	c[1][0]=max(a[1][0]+b[0][0],a[1][1]+b[1][0]); 
    	c[1][1]=max(a[1][0]+b[0][1],a[1][1]+b[1][1]); 
    	return c; 
    }

    //tmp :: 虚儿子信息 
    //t :: 树剖实际转移矩阵 
    #define lson ch[x][0]
    #define rson ch[x][1] 
    int ch[maxn][2],f[maxn]; 
    int isRoot(int x) 
    {	
        return !(ch[f[x]][0]==x || ch[f[x]][1]==x);  
    }
    int get(int x) 
    { 
        return ch[f[x]][1]==x; 
    } 
    void pushup(int x)

    void rotate(int x)

    void splay(int x) 

    void Access(int x)

            if(y) 
            { 
                tmp[x][0][0]-=max(t[y][0][0],t[y][1][0]); 
                tmp[x][1][0]-=t[y][0][0];  
            }
            tmp[x][0][1]=tmp[x][0][0];  
            rson=y,pushup(x); 
        }
    } 
};     

//variables 
int DP[maxn][2];   
int V[maxn],hd[maxn],to[maxn<<1],nex[maxn<<1]; 
int n,Q,edges; 
void add(int u,int v){ nex[++edges]=hd[u],hd[u]=edges,to[edges]=v;  } 

//build graph 
void dfs(int u,int ff)

    LCT::tmp[u]=(LCT::Matrix){ DP[u][0], DP[u][0], DP[u][1], -inf};
    LCT::t[u]=LCT::tmp[u];   
}

//主程序~ 
int main()

    dfs(1,0); 
    while(Q--)
    {
        int x,y; 
        scanf("%d%d",&x,&y);            
        LCT::Access(x);
        LCT::splay(x);                
        LCT::tmp[x][1][0]+=(ll)y-V[x];
        V[x]=y;        
        LCT::pushup(x);    
        LCT::splay(1); 
        printf("%lld
",max(LCT::t[1][0][0], LCT::t[1][1][0]));       
    }
    return 0; 
}
赞(0)
未经允许不得转载:上海聚慕医疗器械有限公司 » PLCT检查是什么luogu P4719 【模板】动态 DP (LCT版)

登录

找回密码

注册