BZOJ 4129 Haruna’s Breakfast

树分块与莫队优化
本文介绍了一种结合树分块与带修改莫队算法的优化方案来解决类似于糖果公园的问题。通过预处理和数据结构操作实现了高效的路径查询与更新。

同糖果公园,直接树分块套带修莫队即可。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const int maxn = 505000;
vector<int> f[maxn],g[maxn];
int val[maxn],deep[maxn],fa[maxn],son[maxn],top[maxn],sz[maxn],dfn[maxn],q[maxn],pre[maxn];
int pos[maxn],res[maxn],num[maxn],belong[maxn],n,m,x,y,blo,blonum,dfs_clock,Top,ans;
struct oper{
    int x,y,id,op,t;
}T[maxn];
struct change{
    int x,y,t,pre,id;
}F[maxn];
bool vis[maxn];
bool operator < (oper a,oper b){
    if(belong[a.x]==belong[b.x]&&belong[a.y]==belong[b.y]) return a.t<b.t;
    else if(belong[a.x]==belong[b.x]) return belong[a.y]<belong[b.y];
    else return belong[a.x]<belong[b.x];
}
int dfs1(int x,int fath,int dep){
    int size=0;
    dfn[x]=++dfs_clock;deep[x]=dep;
    sz[x]=1;son[x]=0;fa[x]=fath;
    for(int i=0;i<f[x].size();i++){
        int v=f[x][i];
        if(v==fath) continue;
        size+=dfs1(v,x,dep+1);
        sz[x]+=sz[v];
        if(size>=blo){
            blonum++;
            for(int k=1;k<=size;k++)
                belong[q[Top--]]=blonum;
            size=0;
        }
        if(sz[son[x]]<sz[v]) son[x]=v;
    }
    q[++Top]=x;
    return size+1;
}
void dfs2(int x,int tp){
    top[x]=tp;
    if(son[x]) dfs2(son[x],tp);
    for(int i=0;i<f[x].size();i++){
        int v=f[x][i];
        if(v==fa[x]||v==son[x]) continue;
        dfs2(v,v);
    }
}
int lca(int a,int b){
    while(top[a]!=top[b]){
        if(deep[top[a]]<deep[top[b]]) swap(a,b);
        a=fa[top[a]];
    }
    return deep[a]>deep[b] ? b : a;
}
void reverse(int x){
    if(vis[x]){
        if(val[x]>maxn) return;
        num[val[x]]--;if(!num[val[x]]){
            if(ans>val[x]) ans=val[x];
        }
    } 
    else{
        if(val[x]>maxn) return;
        num[val[x]]++;if(ans==val[x]){
            while(num[ans]) ans++;
        }
    } 
    vis[x]^=1;
}
void change(int x,int y){
    if(vis[x]){
        reverse(x);val[x]=y;reverse(x);
    }else val[x]=y;
}
void solve(int x,int y){
    while(x!=y){
        if(deep[x]>deep[y]) reverse(x),x=fa[x];
        else reverse(y),y=fa[y];
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    blo=pow(n,2.0/3)*0.5;
    for(int i=1;i<=n;i++) scanf("%d",&val[i]),pre[i]=val[i];
    for(int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        f[x].push_back(y);
        f[y].push_back(x);  
    } 
    dfs1(1,1,0);
    dfs2(1,1);
    while(Top) belong [q[Top--]]=blonum;
    int c1=0,c2=0,typ;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&typ,&x,&y);
        if(!typ){
            c1++;
            F[c1].x=x;F[c1].y=y;F[c1].pre=pre[x];pre[x]=y;
        }else{
            c2++;
            if(dfn[x]>dfn[y]) swap(x,y);
            T[c2].x=x;T[c2].y=y;T[c2].id=c2;T[c2].t=c1;
        }
    }
    sort(T+1,T+1+c2);
    for(int i=1;i<=T[1].t;i++) change(F[i].x,F[i].y);
    int t=lca(T[1].x,T[1].y);
    solve(T[1].x,T[1].y);
    reverse(t);res[T[1].id]=ans;reverse(t);
//  cout<<T[1].id<<endl;
//  for(int j=1;j<=10;j++) cout<<vis[j]<<" ";
//  cout<<endl;
    for(int i=2;i<=c2;i++)
    {
//      cout<<T[i].id<<" "<<i<<" "<<T[i].x<<" "<<T[i].y<<" "<<T[i].t<<endl;
        for(int j=T[i-1].t+1;j<=T[i].t;j++)
            change(F[j].x,F[j].y);
        for(int j=T[i-1].t;j>T[i].t;j--)
            change(F[j].x,F[j].pre);
        solve(T[i-1].x,T[i].x);solve(T[i-1].y,T[i].y);
//      cout<<T[i-1].y<<" "<<T[i].y<<" "<<deep[T[i-1].y]<<" "<<deep[T[i].y]<<endl;
//      for(int j=1;j<=10;j++) cout<<vis[j]<<" ";
//      cout<<endl;   
         
        int t=lca(T[i].x,T[i].y);
        reverse(t);res[T[i].id]=ans;reverse(t); 
         
    }
    for(int i=1;i<=c2;i++)
        printf("%d\n",res[i]);
    return 0;
}
【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)内容概要:本文介绍了基于蒙特卡洛和拉格朗日方法的电动汽车充电站有序充电调度优化方案,重点在于采用分散式优化策略应对分时电价机制下的充电需求管理。通过构建数学模型,结合不确定性因素如用户充电行为和电网负荷波动,利用蒙特卡洛模拟生成大量场景,并运用拉格朗日松弛法对复杂问题进行分解求解,从而实现全局最优或近似最优的充电调度计划。该方法有效降低了电网峰值负荷压力,提升了充电站运营效率与经济效益,同时兼顾用户充电便利性。 适合人群:具备一定电力系统、优化算法和Matlab编程基础的高校研究生、科研人员及从事智能电网、电动汽车相关领域的工程技术人员。 使用场景及目标:①应用于电动汽车充电站的日常运营管理,优化充电负荷分布;②服务于城市智能交通系统规划,提升电网与交通系统的协同水平;③作为学术研究案例,用于验证分散式优化算法在复杂能源系统中的有效性。 阅读建议:建议读者结合Matlab代码实现部分,深入理解蒙特卡洛模拟与拉格朗日松弛法的具体实施步骤,重点关注场景生成、约束处理与迭代收敛过程,以便在实际项目中灵活应用与改进。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值