CSU 1607: Do You Have The Template?(树链剖分)边权

本文介绍了一道关于树链剖分与线段树的应用题,通过树链剖分将边权转化为点权,并利用线段树维护边权,解决包括路径查询在内的多种操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1607: Do You Have The Template?

Time Limit: 7 Sec   Memory Limit: 128 MB
Submit: 112   Solved: 8
[ Submit][ Status][ Web Board]

Description

There is a tree with N vertices, each edges have a positive length.
And here comes 4 operations,
0 a b, means query the maximum length and the sum of length on the path between vertex a and vertex b.
1 a b, means change the a-th edge's length to b.
2 a b 0 c, means change all edges' length on the path between vertex a and vertex b to c.
2 a b 1 c, means add all edges' length on the path between vertex a and vertex b by c.

Input

There're several cases.
The first line contains a positive integer N (2 <= N <= 10000), meaning there're N vertices, marked 1 to N. Then N – 1 lines follow with the i-th line describing the i-th edge. Each line contains three integers a b c, meaning there is an edge between vertex a and vertex b with the length of c.
Then one integer M (M <= 10000), means there're M queries. And followed by M lines, each line will contain one operation described as above.

Output

For each 0 a b query, you should output the maximum length and the sum.
The data insure no number will be bigger than 2^31.

Sample Input

4
1 2 1
2 3 2
2 4 3
10
0 1 3
0 1 4
0 3 4
1 2 3
0 1 3
2 4 3 0 2
0 1 4
2 4 3 1 2
0 1 3
0 1 4

Sample Output

2 3
3 4
3 5
3 4
2 3
4 5
4 5
题意:给出一棵树有n个节点,每个边有权值,现在有M个操作:
0 a b 表示输出从a点到b点的路径的边权最大值和边树总和。
1 a b 表示第a条边的权值变为b
2 a b 0 c 表示从a点到b点的路径的每条边权都变成c
2 a b 1 c 表示从a点到b点的路径的每条边权都加上c
坑点:在CSU的这道题用vector建图就WA。标程代码有漏洞,就是用root[k].toc去判断是否需要更新线段树中k的左右子节点。
解题:用树链剖分,边权变点权。用线段树去维护边权。
#include<stdio.h>
#include<string.h>
#include<vector>
const int N=10015;
using namespace std;
 
int head[N], to[N << 1], next1[N << 1], tot;
int top[N];    //top[v]=u表示点v,u在一个链中,且u是这个链深度最小的点(即顶端)
int fath[N];   //记录父节点
int deep[N];   //每个点在树上的深度
int num[N];    //每棵子树的节点个数
int son[N];    //选的重边子节点
int p[N];      //树上每个点在线段树中所对应的点
int pos;
 
void addEdge(const int& u, const int& v) {
  to[tot] = v, next1[tot] = head[u], head[u] = tot++;
}
 
void addUndirEdge(const int& u, const int& v) {
  addEdge(u, v), addEdge(v, u);
}
 
void init(int n)
{
    pos=0; tot=0;
    memset(son,-1,sizeof(son));
    memset(head,-1,sizeof(head));
}
void dfs1(int u,int pre,int d)
{
    deep[u]=d; fath[u]=pre; num[u]=1;
    for (int i = head[u]; i != -1; i = next1[i]) {
    int v = to[i];
        if(v==pre)continue;
        dfs1(v,u,d+1);
        num[u]+=num[v];
        if(son[u]==-1||num[v]>num[son[u]])
            son[u]=v;
    }
}
void getpos(int u,int root)
{
    top[u]=root;
    p[u]=pos++;
    if(son[u]==-1)
        return ;
    getpos(son[u],root);
    for (int i = head[u]; i != -1; i = next1[i]) {
    int v = to[i];
        if(son[u]!=v&&v!=fath[u])
            getpos(v,v);
    }
}
 
//线段树
struct tree
{
    int sum,maxv,toc,addc;
 }root[N*4];
int val[N];
int MAX(int a,int b)
{
    return a>b?a:b;
}
void build(int l,int r,int k)
{
    int mid=(l+r)/2;
    root[k].addc=0;
    root[k].toc=0;
    if(l==r){
        root[k].sum=root[k].maxv=val[l]; return ;
    }
    build(l,mid,k<<1);
    build(mid+1,r,k<<1|1);
    root[k].sum=root[k<<1].sum+root[k<<1|1].sum;
    root[k].maxv=MAX(root[k<<1].maxv,root[k<<1|1].maxv);
}
void upson(int k,int l,int r)
{
    int mid=(l+r)/2;
    if(root[k].toc)
    {
        root[k<<1].sum=(mid-l+1)*root[k].toc;
        root[k<<1].maxv=root[k].toc;
        root[k<<1].toc=root[k].toc;
        root[k<<1].addc=0;
 
        root[k<<1|1].sum=(r-mid)*root[k].toc;
        root[k<<1|1].maxv=root[k].toc;
        root[k<<1|1].toc=root[k].toc;
        root[k<<1|1].addc=0;
        root[k].toc=0;
    }
 
    if(root[k].addc)
    {
        root[k<<1].sum+=(mid-l+1)*root[k].addc;
        root[k<<1].maxv+=root[k].addc;
        root[k<<1].addc+=root[k].addc;
 
        root[k<<1|1].sum+=(r-mid)*root[k].addc;
        root[k<<1|1].maxv+=root[k].addc;
        root[k<<1|1].addc+=root[k].addc;
        root[k].addc=0;
    }
}
void updata1(int l,int r,int k,const int L,const int R,int c)
{
    if(L<=l&&r<=R)
    {
        root[k].sum=(r-l+1)*c; root[k].maxv=c;
        root[k].toc=c; root[k].addc=0;
        return ;
    }
    int mid=(l+r)/2;
    upson(k,l,r);
 
    if(L<=mid)
        updata1(l,mid,k<<1,L,R,c);
    if(mid<R)
        updata1(mid+1,r,k<<1|1,L,R,c);
    root[k].sum=root[k<<1].sum+root[k<<1|1].sum;
    root[k].maxv=MAX(root[k<<1].maxv,root[k<<1|1].maxv);
}
void updata2(int l,int r,int k, int L, int R,int c)
{
    if(L<=l&&r<=R)
    {
        root[k].sum+=(r-l+1)*c; root[k].maxv+=c;
        root[k].addc+=c;
        return ;
    }
    int mid=(l+r)/2;
    upson(k,l,r);
 
    if(L<=mid)
        updata2(l,mid,k<<1,L,R,c);
    if(mid<R)
        updata2(mid+1,r,k<<1|1,L,R,c);
    root[k].sum=root[k<<1].sum+root[k<<1|1].sum;
    root[k].maxv=MAX(root[k<<1].maxv,root[k<<1|1].maxv);
}
int sum,maxv;
void query(int l,int r,int k,int L,int R)
{
    if(L<=l&&r<=R)
    {
        sum+=root[k].sum;
        maxv=MAX(maxv,root[k].maxv);
        return ;
    }
    int mid=(l+r)/2;
    upson(k,l,r);
 
    if(L<=mid)
        query(l,mid,k<<1,L,R);
    if(mid<R)
        query(mid+1,r,k<<1|1,L,R);
}
void swp(int &a,int &b)
{
    int tt;
    tt=a; a=b; b=tt;
}
void Operat0(int u,int v)
{
    int f1=top[u], f2=top[v];
    sum=0; maxv=0;
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swp(f1,f2); swp(u,v);
        }
        query(1,pos,1,p[f1],p[u]);
        u=fath[f1]; f1=top[u];
    }
    if(u==v) return ;
    if(deep[u]>deep[v])swp(u,v);
    query(1,pos,1,p[son[u]],p[v]);
}
void Operat1(int u,int v,int c)
{
    int f1=top[u], f2=top[v];
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swp(f1,f2); swp(u,v);
        }
        updata1(1,pos,1,p[f1],p[u],c);
        u=fath[f1]; f1=top[u];
    }
    if(u==v) return ;
    if(deep[u]>deep[v])swp(u,v);
    updata1(1,pos,1,p[son[u]],p[v],c);
}
void Operat2(int u,int v,int c)
{
    int f1=top[u], f2=top[v];
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swp(f1,f2); swp(u,v);
        }
        updata2(1,pos,1,p[f1],p[u],c);
        u=fath[f1]; f1=top[u];
    }
    if(u==v) return ;
    if(deep[u]>deep[v])swp(u,v);
    updata2(1,pos,1,p[son[u]],p[v],c);
}
 
struct EDG
{
    int u,v,c;
}edg[N];
int main()
{
    int n,q,op,a,b;
    while(scanf("%d",&n)!=EOF)
    {
        init(n);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&edg[i].u,&edg[i].v,&edg[i].c);
            addUndirEdge(edg[i].u, edg[i].v);
        }
 
        dfs1(1,1,1);
        getpos(1,1);
        pos=n;
        for(int i=1;i<n;i++)
        {
            if(deep[edg[i].u]>deep[edg[i].v])
                edg[i].v=edg[i].u;
            val[p[edg[i].v]]=edg[i].c;
        }
        build(1,pos,1);
 
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d%d",&op,&a,&b);
            if(op==0)
            {
                Operat0(a,b);
                printf("%d %d\n",maxv,sum);
            }
            else if(op==1)
                updata1(1,pos,1,p[edg[a].v],p[edg[a].v],b);
            else
            {
                int tt,c;
                scanf("%d%d",&tt,&c);
                if(tt==0)
                    Operat1(a,b,c);
                else
                    Operat2(a,b,c);
            }
        }
    }
}
 
/**************************************************************
    Problem: 1607
    User: aking2015
    Language: C++
    Result: Accepted
    Time:912 ms
    Memory:2180 kb
****************************************************************/


内容概要:本文档详细介绍了一个基于MATLAB实现的电力负荷预测项目,该项目运用遗传算法(GA)优化支持向量回归(SVR)和支持向量机(SVM)模型的超参数及特征选择。项目旨在解决电力系统调度、发电计划、需求侧响应等多个应用场景中的关键问题,特别是在应对高比例可再生能源接入带来的非线性、非平稳负荷预测挑战。文中涵盖了从数据接入、特征工程、模型训练到部署上线的全流程,包括详细的代码示例和GUI设计,确保方案的可复现性和实用性。 适用人群:具备一定编程基础,尤其是熟悉MATLAB语言和机器学习算法的研发人员;从事电力系统调度、电力市场交易、新能源消纳等相关领域的工程师和技术专家。 使用场景及目标:①通过构建面向小时级别的滚动预测,输出高分辨率负荷轨迹,为日内与日前滚动调度提供际成本最小化的依据;②在负荷高峰和供给紧张时,通过价格信号或直接负荷控制实施需求侧响应,提升削峰效率并抑制反弹;③为灵活性资源(调峰机组、储能、可中断负荷)提供更清晰的出清路径,降低弃风弃光率,提升系统整体清洁度;④帮助市场主体更准确地评估际出清价格变化,提高报价成功率与收益稳定性,同时降低由预测偏差带来的风险敞口;⑤在运维与审计场景中,对预测产生的原因进行说明,保障业务侧与监管侧的可追溯性。 阅读建议:此资源不仅提供了完整的代码实现和GUI设计,更注重于理解GA优化过程中涉及到的数据处理、特征构造、模型选择及评估等核心步骤。因此,在学习过程中,建议结合实际案例进行实践,并深入研究每个阶段的具体实现细节,特别是适应度函数的设计、超参数空间的定义以及多样性维护机制的应用。此外,关注项目中关于数据对齐、缺失值处理、特征标准化等方面的最佳实践,有助于提高模型的鲁棒性和泛化能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值