BZOJ 4003 [JLOI2015]城池攻占 可并堆

题意: 链接

方法: 可并堆

解析:

今年的省选题,当时我做的时候一顿蒙B。

好像当时搞了个树形DP?不记得了,反正没拿到分。

这题首先->骑士之间互不影响

->到一个城池后所有到达的骑士都要改变攻击力

->并且每次到一个城池把小于该城池防御力的骑士杀死。

这一想..维护一个小根堆….最后一直传到根节点,然后再把根节点连一个防御力正无穷的城池不就好了么。

每次到一个节点后把这个堆里小于他防御力的骑士干死。

然后合并一个节点所有子节点时涉及到合并操作。

所以考虑到可并堆。

然后我们从顶向下递归搞是不行的,要爆栈,所以保险起见我们从下向上搜。

但是直接搜不行,因为在处理一个节点前要处理它的所有子节点。

所以在拓扑图上搞就可以了。

代码:

#include <queue> 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 300010
#define INF (1ll<<63)-1
using namespace std;
typedef long long ll;
int n,m,cnt,cnt_l,top;
ll h[N],v[N],val[N];
int a[N],head[N],head_l[N],fa[N],du[N];
int he[N],ch[N][2],att[N],col[N],kill[N];
ll siz[N];
ll add[N],mul[N];
int stk[N],fron[N];
char getc()
{
    static const int LEN=1<<15;
    static char buf[LEN],*S=buf,*T=buf;
    if(S==T)
    {
        T=(S=buf)+fread(buf,1,LEN,stdin);
        if(S==T)    return EOF;
    }
    return *S++;
}
int read()
{
    static char ch;
    static int D;
    while(!isdigit(ch=getc()));
    for(D=ch-'0';isdigit(ch=getc());)
        D=(D<<3)+(D<<1)+(ch-'0');
    return D;
}
ll readll()
{
    static char ch;
    static ll D;
    ll f=1;
    while(!isdigit(ch=getc())){if(ch=='-')f=-1;}
    for(D=ch-'0';isdigit(ch=getc());)
        D=(D<<3)+(D<<1)+(ch-'0');
    return D*f;
}
struct node
{
    int from,to,next;
}edge[N];
struct link
{
    int from,to,next;
}l[N];
int find(int x)
{
    while(fa[x])x=fa[x];
    return x;
}
void init()
{
    he[0]=-1;
    memset(head,-1,sizeof(head));
    memset(head_l,-1,sizeof(head_l));
    for(int i=1;i<=m;i++)mul[i]=1;
    cnt=1,cnt_l=1;
}
void edgeadd(int from,int to)
{
    edge[cnt].from=from,edge[cnt].to=to,edge[cnt].next=head[from];
    head[from]=cnt++;
}
void linkadd(int from,int to)
{
    l[cnt_l].from=from,l[cnt_l].to=to,l[cnt_l].next=head_l[from];
    head_l[from]=cnt_l++;
}
void pushup(int x)
{
    siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
}
void pushdown(int x)
{
    if(add[x]!=0||mul[x]!=1)
    {
        if(ch[x][0]!=0)
        {
            val[ch[x][0]]*=mul[x];
            val[ch[x][0]]+=add[x];
            add[ch[x][0]]*=mul[x];
            add[ch[x][0]]+=add[x];
            mul[ch[x][0]]*=mul[x];
        }
        if(ch[x][1]!=0)
        {
            val[ch[x][1]]*=mul[x];
            val[ch[x][1]]+=add[x];
            add[ch[x][1]]*=mul[x];
            add[ch[x][1]]+=add[x];
            mul[ch[x][1]]*=mul[x];
        }
        add[x]=0,mul[x]=1;
    }
}
void pushdown_att(int x)
{
    if(col[x])
    {
        if(ch[x][0]!=0)
            col[ch[x][0]]+=col[x],att[ch[x][0]]+=col[x];
        if(ch[x][1]!=0)
            col[ch[x][1]]+=col[x],att[ch[x][1]]+=col[x];
        col[x]=0;
    }
}
int merge(int x,int y)
{
    if((!x)||(!y))return x+y;
    if(val[y]<val[x])swap(x,y);
    pushdown(x),pushdown_att(x);
    ch[x][1]=merge(ch[x][1],y);
    fa[ch[x][1]]=x;
    if(he[ch[x][1]]>he[ch[x][0]])swap(ch[x][0],ch[x][1]);
    he[x]=he[ch[x][1]]+1;
    pushup(x);
    return x;
}
struct ele
{
    int x,pre; 
};
void bfs()
{
    for(int i=1;i<=n;i++)
    {
        if(du[i]==0)
        {
            stk[++top]=i;
        }
    }
    while(top)
    {
        int u=stk[top--];
        int pre=0;
        for(int i=head_l[u];i!=-1;i=l[i].next)
        {
            int to=l[i].to;
            int t=merge(pre,to);
            pre=t;
        }
        int t=merge(pre,fron[u]);
        fron[u]=t;
        while(val[fron[u]]<h[u]&&fron[u])
        {
            pushdown(fron[u]),pushdown_att(fron[u]);
            int t=merge(ch[fron[u]][0],ch[fron[u]][1]);
            fa[t]=0;
            fa[fron[u]]=ch[fron[u]][0]=ch[fron[u]][1]=0;
            kill[u]++;
            fron[u]=t;
        }
        col[fron[u]]++,att[fron[u]]++;
        if(!a[u])
        {
            val[fron[u]]+=v[u];
            add[fron[u]]+=v[u];
        }else 
        {
            val[fron[u]]*=v[u];
            mul[fron[u]]*=v[u];
            add[fron[u]]*=v[u];
        }
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int to=edge[i].to;
            fron[to]=merge(fron[to],fron[u]);
            du[to]--;
            if(!du[to])stk[++top]=to;
        }
    }
}
int main()
{
//  freopen("fall.in","r",stdin);
//  freopen("fall.out","w",stdout); 
    n=read(),m=read();
    init();
    for(int i=1;i<=n;i++)h[i]=readll();
    h[n+1]=INF,edgeadd(1,1+n);
    du[1+n]=1;
    for(int i=2;i<=n;i++)
    {
        int pre;
        pre=read(),a[i]=read(),v[i]=readll(); 
        edgeadd(i,pre);
        du[pre]++; 
    }
    for(int i=1;i<=m;i++)
    {
        int fir;
        val[i]=readll(),fir=read(); 
        linkadd(fir,i);
    }
    bfs();
    for(int i=1;i<=n;i++)printf("%d\n",kill[i]);
    for(int i=1;i<=m;i++)printf("%d\n",att[i]);
}
### NOIP2015 运输计划 BZOJ4326 题解分析 #### 问题背景 该问题是经典的图论优化问题之一,主要考察树结构上的路径操作以及高效的数据处理能力。题目要求在一个由 $n$ 个节点组成的无向连通树中找到最优的一条边将其改造为虫洞(通过此边不需要耗费时间),从而使得给定的 $m$ 条运输路径中的最长耗时最小化。 --- #### 解决方案概述 解决这一问题的核心在于利用 **二分答案** 和 **树上差分技术** 的组合来实现高效的计算过程。以下是具体的技术细节: 1. **二分答案**: 设当前目标是最小化的最大路径长度为 $T_{\text{max}}$。我们可以通过二分的方式逐步逼近最终的结果。每次尝试验证是否存在一种方式将某条边改为虫洞后使所有路径的最大值不超过当前设定的目标值 $mid$[^1]。 2. **路径标记与统计**: 使用树上差分的思想对每一条路径进行标记快速统计受影响的情况。假设两点之间的最近公共祖先 (Lowest Common Ancestor, LCA) 是 $r = \text{lca}(u_i, v_i)$,则可以在三个位置分别施加影响:增加 $(u_i + 1), (v_i + 1)$ 同时减少 $(r - 2)$。这种操作能够有效覆盖整条路径的影响范围,便于后续统一查询和判断[^1]。 3. **数据结构支持**: 结合线段树或者 BIT (Binary Indexed Tree),可以进一步加速区间修改和单点查询的操作效率。这些工具帮助我们在复杂度范围内完成大量路径的同时更新和检索需求[^2]。 4. **实际编码技巧**: 实现过程中需要注意一些边界条件和技术要点: - 正确维护 DFS 序列以便映射原树节点到连续编号序列; - 准备好辅助函数用于快速定位 LCA 节点及其对应关系; - 编码阶段应特别留意变量初始化顺序及循环终止逻辑以防潜在错误发生。 下面给出一段基于上述原理的具体 Python 实现代码作为参考: ```python from collections import defaultdict, deque class Solution: def __init__(self, n, edges): self.n = n self.graph = defaultdict(list) for u, v, w in edges: self.graph[u].append((v, w)) self.graph[v].append((u, w)) def preprocess(self): """Preprocess the tree to get dfs order and lca.""" pass def binary_search_answer(self, paths): low, high = 0, int(1e9) best_possible_time = high while low <= high: mid = (low + high) // 2 if self.check(mid, paths): # Check feasibility with current 'mid' best_possible_time = min(best_possible_time, mid) high = mid - 1 else: low = mid + 1 return best_possible_time def check(self, limit, paths): diff_array = [0]*(self.n+1) for path_start, path_end in paths: r = self.lca(path_start, path_end) # Apply difference on nodes based on their relationship. diff_array[path_start] += 1 diff_array[path_end] += 1 diff_array[r] -= 2 suffix_sum = [sum(diff_array[:i]) for i in range(len(diff_array)+1)] # Verify whether any edge can be modified within given constraints. possible_to_reduce_max = False for node in range(1, self.n+1): parent_node = self.parent[node] if suffix_sum[node]-suffix_sum[parent_node]>limit: continue elif not possible_to_reduce_max: possible_to_reduce_max=True return possible_to_reduce_max # Example usage of class methods would follow here... ``` --- #### 总结说明 综上所述,本题的关键突破点在于如何巧妙运用二分策略缩小搜索空间,再辅以恰当的树形结构遍历技术和差分手段提升整体性能表现。这种方法不仅适用于此类特定场景下的最优化求解任务,在更广泛的动态规划领域也有着广泛的应用前景[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值