【poj3237】【Tree】

1583. [POJ3237]树的维护

★★★☆ 输入文件:maintaintree.in 输出文件:maintaintree.out 简单对比

时间限制:5 s 内存限制:128 MB

【题目描述】

给你由N个结点组成的树。树的节点被编号为1到N,边被编号为1到N-1。每一条边有一个权值。然后你要在树上执行一系列指令。指令可以是如下三种之一:

CHANGE i v:将第i条边的权值改成v。

NEGATE a b:将点a到点b路径上所有边的权值变成其相反数。

QUERY a b:找出点a到点b路径上各边的最大权值。

【输入格式】

输入文件的第一行有一个整数N(N<=10000)。

接下来N-1行每行有三个整数a,b,c,代表点a和点b之间有一条权值为c的边。这些边按照其编号从小到大给出。

接下来是若干条指令(不超过10^5条),都按照上面所说的格式。

输入文件的最后一行是”DONE”.

【输出格式】

对每个“QUERY”指令,输出一行,即路径上各边的最大权值。

【样例输入】

3

1 2 1

2 3 2

QUERY 1 2

CHANGE 1 3

QUERY 1 2

DONE

【样例输出】

1

3

思路:这道题只是比平常的多了一个置反操作,我们止血药打一个标记就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=10100;
int n,point[N],next[N*10],tot=1,siz[N]={0},fa[N][20]={0},son[N]={0};
int deep[N]={0},num=0,pos[N]={0},belong[N]={0},r[N]={0},map[N]={0},de[N*4]={0};
struct S{
    int st,en,va;
}aa[N*10];
struct C{
    int maxn,minn,lazy;
}tr[N*4];
bool use[N];
inline void add(int x,int y,int z)
{
    tot+=1;next[tot]=point[x];point[x]=tot;
    aa[tot].st=x;aa[tot].en=y;aa[tot].va=z;
    tot+=1;next[tot]=point[y];point[y]=tot;
    aa[tot].st=y;aa[tot].en=x;aa[tot].va=z;
}
inline void dfs_1(int x)
{
    int i;
    siz[x]=1;
    use[x]=false;
    for(i=1;i<=14;++i){
        if(deep[x]<(1<<i)) break;
        fa[x][i]=fa[fa[x][i-1]][i-1];
    }
    for(i=point[x];i;i=next[i])
      if(use[aa[i].en]){
        fa[aa[i].en][0]=x;
        deep[aa[i].en]=deep[x]+1;
        dfs_1(aa[i].en);
        siz[x]+=siz[aa[i].en];
      }
}
inline void dfs_2(int x,int y)
{
    int i,k=0;
    num+=1;
    belong[x]=y;
    for(i=point[x];i;i=next[i]){
        if(deep[aa[i].st]>deep[aa[i].en]) map[aa[i].st]=i>>1;
        else map[aa[i].en]=i>>1;
    }
    for(i=point[x];i;i=next[i])
      if(deep[x]<deep[aa[i].en]&&siz[k]<siz[aa[i].en])
        k=aa[i].en,son[x]=i>>1,pos[i>>1]=num;
    if(k==0) {num-=1; return ;}
    dfs_2(k,y);
    for(i=point[x];i;i=next[i])
      if(deep[x]<deep[aa[i].en]&&k!=aa[i].en)
        num+=1,pos[i>>1]=num,dfs_2(aa[i].en,aa[i].en);
}
inline int LCA(int x,int y)
{
    int i;
    if(deep[x]<deep[y])swap(x,y);
    int t=deep[x]-deep[y];
    for(i=0;i<=14;++i)
        if(t&(1<<i))x=fa[x][i];
    for(i=14;i>=0;--i)
        if(fa[x][i]!=fa[y][i])
        {x=fa[x][i];y=fa[y][i];}
    if(x==y)return x;
    else return fa[x][0];
}
#define mid (l+r)/2
#define L k<<1,l,mid
#define R k<<1|1,mid+1,r
inline void solve(int k)
{
    swap(tr[k].maxn,tr[k].minn);
    tr[k].maxn=-tr[k].maxn;
    tr[k].minn=-tr[k].minn;
}
inline void pushdown(int k)
{
    if(de[k]!=0){
        if((k<<1)<N*4){
            solve(k<<1); de[k<<1]=!de[k<<1];
            solve(k<<1|1); de[k<<1|1]=!de[k<<1|1];
        }
        de[k]=0;
    }
}
inline void insert(int k,int l,int r,int x,int y)
{
    pushdown(k);
    if(l==r&&l==x){
        tr[k].maxn=tr[k].minn=y;
        return ;
    }
    if(x<=mid) insert(L,x,y);
    else insert(R,x,y);
    tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
    tr[k].minn=min(tr[k<<1].minn,tr[k<<1|1].minn);
}
inline int qurry(int k,int l,int r,int x,int y)
{
    int maxn=-210000000;
    pushdown(k);
    if(x<=l&&y>=r) return tr[k].maxn;
    if(x<=mid) maxn=max(maxn,qurry(L,x,y));
    if(y>mid) maxn=max(maxn,qurry(R,x,y));
    return maxn;
}
inline int ask(int x,int y)
{
    int maxn=-210000000,z;
    if(map[x]==0) return maxn;
    if(belong[x]==x&&x!=y){
        maxn=max(maxn,qurry(1,1,n-1,pos[map[x]],pos[map[x]]));
        x=fa[x][0];
    }
    if(map[x]==0) return maxn;
    while(belong[x]!=belong[y]){
        maxn=max(maxn,qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]]));
        z=fa[belong[x]][0];
        maxn=max(maxn,qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]]));
        x=z;
    }
    if(map[x]==0) return maxn;
    maxn=max(maxn,qurry(1,1,n-1,pos[son[y]],pos[map[x]]));
    return maxn;
}
inline void change(int k,int l,int r,int x,int y)
{
    pushdown(k);
    if(x<=l&&y>=r){
        solve(k); de[k]=1;
        return ;
    }
    if(x<=mid) change(L,x,y);
    if(y>mid) change(R,x,y);
    tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
    tr[k].minn=min(tr[k<<1].minn,tr[k<<1|1].minn);
}
inline void negate_(int x,int y)
{
    int z;
    if(map[x]==0) return ;
    if(belong[x]==x&&x!=y){
        change(1,1,n-1,pos[map[x]],pos[map[x]]);
        x=fa[x][0];
    }
    if(map[x]==0) return ;
    while(belong[x]!=belong[y]){
        change(1,1,n-1,pos[son[belong[x]]],pos[map[x]]);
        z=fa[belong[x]][0];
        change(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]]);
        x=z;
    }
    if(map[x]==0) return ;
    change(1,1,n-1,pos[son[y]],pos[map[x]]);
    return ;
}
int main()
{
    int i,j,x,y,z;
    char ch[10];
    memset(use,1,sizeof(use));
    scanf("%d",&n);
    for(i=1;i<n;++i){
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);r[i]=z;
    }
    dfs_1(1);
    dfs_2(1,1);
    for(i=1;i<n;++i) insert(1,1,n-1,pos[i],r[i]);
    while(scanf("%*c%s",&ch)){
        if(ch[0]==‘D‘) break;
        scanf("%d%d",&x,&y);
        if(ch[0]==‘C‘) insert(1,1,n-1,pos[x],y);
        if(ch[0]==‘Q‘){
            int lca=LCA(x,y);
            printf("%d\n",max(ask(x,lca),ask(y,lca)));
        }
        if(ch[0]==‘N‘){
            int lca=LCA(x,y);
            negate_(x,lca);negate_(y,lca);
        }
    }
}
时间: 2024-08-13 00:12:23

【poj3237】【Tree】的相关文章

【Recover Binary Search Tree】cpp

题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}&

【Gradient Boosted Decision Tree】林轩田机器学习技术

GBDT之前实习的时候就听说应用很广,现在终于有机会系统的了解一下. 首先对比上节课讲的Random Forest模型,引出AdaBoost-DTree(D) AdaBoost-DTree可以类比AdaBoost-Stump模型,就可以直观理解了 1)每轮都给调整sample的权重 2)获得gt(D,ut) 3)计算gt的投票力度alphat 最后返回一系列gt的线性组合. weighted error这个比较难搞,有没有不用动原来的模型,通过输入数据上做文章就可以达到同样的目的呢? 回想bag

【Validate Binary Search Tree】cpp

题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with

【POJ3237】Tree 树链剖分+线段树

[POJ3237]Tree Description You are given a tree with N nodes. The tree's nodes are numbered 1 through N and its edges are numbered 1 through N ? 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. Th

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example

【HDOJ 5379】 Mahjong tree

[HDOJ 5379] Mahjong tree 往一颗树上标号 要求同一父亲节点的节点们标号连续 同一子树的节点们标号连续 问一共有几种标法 画了一画 发现标号有二叉树的感觉 初始标号1~n 根结点1可以标1或n 否则其他情况无法让下面的子树满足各自连续并且该根的儿子节点都要连续 根结点下的节点平分其他标号 画一画可以发现 每个根下最多有两颗子树 否则无法满足条件 并且两颗子树占据剩余标号的左右两边 中间夹的必须是叶子 这样才能满足该根下的儿子节点标号连续 若根下只有一颗子树 同样可以选择占剩

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

【POJ 3321】 Apple Tree (dfs重标号设区间+树状数组求和)

[POJ 3321] Apple Tree (dfs重标号设区间+树状数组求和) Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21966   Accepted: 6654 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. K

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html 只是子树的前序和中序遍历序列分别更新为: //左子树: left_prestart = prestart+1 lef