poj 3237 Tree(树链剖分,点权,线段树)

题目链接
题意:一棵树3个操作
1.chang i,v 把第i条边权值修改为v
2.negative a,b 把a到b权值取反
3.query a,b 查询a到b的边上的最大值
树链剖分就不说了,线段树,由于取反操作,所以我们同时记录下最小值,这样取反操作,就是最大的变最小了,最小的变最大了,附加一个懒惰标记。偶次反转是没变的。

//#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define fastIO ios::sync_with_stdio(false);cin.tie(0);
#define ll  long long
#define pb push_back
#define gcd __gcd

const double EPS = 1e-8;
const int maxn = 1e4+100;
const int inf  = 1 << 28;

int n,m;
struct Edge{
    int to,next;
}es[maxn<<1];
int head[maxn],tot;
void addEdge(int u,int v){
    es[tot].to=v;es[tot].next=head[u];head[u]=tot++;
}


int num[maxn],p[maxn],fp[maxn],fa[maxn],son[maxn],top[maxn],deep[maxn],pos;
void init(){
    tot=0;
    pos=0;
    cl(head,-1);
    cl(son,-1);
}
void dfs(int u,int pre,int d){
    num[u]=1;
    deep[u]=d;
    fa[u]=pre;
    for(int i=head[u];~i;i=es[i].next){
        int v=es[i].to;
        if(v!=pre){
            dfs(v,u,d+1);
            num[u]+=num[v];
            if(son[u]==-1||num[son[u]]<num[v])son[u]=v;
        }
    }
}
void getpos(int u,int sp){
    top[u]=sp;
    p[u]=pos++;
    fp[p[u]]=u;
    if(son[u]==-1)return;
    getpos(son[u],sp);
    for(int i=head[u];~i;i=es[i].next){
        int v=es[i].to;
        if(v!=fa[u]&&v!=son[u])getpos(v,v);
    }
}
/
int a[maxn];
int Max[maxn<<2],Min[maxn<<2],tag[maxn<<2];
void Swap(int &x,int &y){
    int t=x;x=-y;y=-t;
}
void pushup(int rt){
    Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
    Min[rt]=min(Min[rt<<1],Min[rt<<1|1]);
}
void pushdown(int rt){
    if(tag[rt]){
        tag[rt<<1]+=tag[rt];
        tag[rt<<1|1]+=tag[rt];
        if(tag[rt]%2){
            Swap(Max[rt<<1],Min[rt<<1]);
            Swap(Max[rt<<1|1],Min[rt<<1|1]);
        }
        tag[rt]=0;
    }
}
void build(int rt,int l,int r){
    tag[rt]=0;
    if(l==r){
        Max[rt]=Min[rt]=a[fp[l]];
        return ;
    }
    int mid=l+r>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    pushup(rt);
}
void update(int rt,int l,int r,int x,int y){
    if(x<=l&&r<=y){
        tag[rt]++;
        Swap(Max[rt],Min[rt]);
        return ;
    }
    pushdown(rt);
    int mid=l+r>>1;
    if(x<=mid)update(rt<<1,l,mid,x,y);
    if(y>mid)update(rt<<1|1,mid+1,r,x,y);
    pushup(rt);
}
void update_(int rt,int l,int r,int k,int val){
    if(l==r){
        Max[rt]=Min[rt]=val;
        tag[rt]=0;
        return ;
    }
    pushdown(rt);
    int mid=l+r>>1;
    if(k<=mid)update_(rt<<1,l,mid,k,val);
    else update_(rt<<1|1,mid+1,r,k,val);
    pushup(rt);
}
int query(int rt,int l,int r,int x,int y){
    if(x<=l&&r<=y){
        return Max[rt];
    }
    pushdown(rt);
    int mid=l+r>>1;
    int ans=-(1<<28);
    if(x<=mid)ans=max(ans,query(rt<<1,l,mid,x,y));
    if(y>mid)ans=max(ans,query(rt<<1|1,mid+1,r,x,y));
    return ans;
}

int findMax(int u,int v){
    int ans=-(1<<28);
    while(top[u]!=top[v]){
        if(deep[top[u]]<deep[top[v]])swap(u,v);
        ans=max(ans,query(1,1,pos-1,p[top[u]],p[u]));
        u=fa[top[u]];
    }
    if(u==v)return ans;
    if(deep[u]>deep[v])swap(u,v);
    return max(ans,query(1,1,pos-1,p[son[u]],p[v]));
}
void modify(int u,int v){
    while(top[u]!=top[v]){
        if(deep[top[u]]<deep[top[v]])swap(u,v);
        update(1,1,pos-1,p[top[u]],p[u]);
        u=fa[top[u]];
    }
    if(u==v)return ;
    if(deep[u]>deep[v])swap(u,v);
    update(1,1,pos-1,p[son[u]],p[v]);
}

int e[maxn][3];

int main(){
    int T;scanf("%d",&T);
    while(T--){
        init();
        scanf("%d",&n);
        for(int i=1;i<n;i++){
            scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]);
            addEdge(e[i][0],e[i][1]);
            addEdge(e[i][1],e[i][0]);
        }

        dfs(1,0,0);
        getpos(1,1);
        for(int i=1;i<n;i++){
            if(deep[e[i][0]]<deep[e[i][1]])swap(e[i][0],e[i][1]);
            a[e[i][0]]=e[i][2];
        }
        build(1,1,pos-1);

        char op[44];
        int u,v;
        while(~scanf("%s",op)&&op[1]!='O'){
            scanf("%d%d",&u,&v);
            if(op[1]=='H'){
                update_(1,1,pos-1,p[e[u][0]],v);
            }
            else if(op[1]=='E'){
                modify(u,v);
            }
            else{
                printf("%d\n",findMax(u,v));
            }
        }
    }
    return 0;
}
/*


*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值