[NOI2015]软件包管理器 树链剖分

本文介绍了解决NOI2015软件包管理器问题的方法,通过树链剖分和线段树区间覆盖实现高效更新与查询,详细解析了pushup和pushdown函数,并提供了完整的代码实现。

[NOI2015]软件包管理器 树链剖分

在水一道
在这里插入图片描述
题意 如上

思路
1.版子
2.线段树区间覆盖
pushup

void pushup(int o){
    t[o].sum=t[o<<1].sum+t[o<<1|1].sum;
}

pushdown

void pushdown(int o){
    if(t[o].lazy!=-1){
        t[o<<1].lazy=t[o<<1|1].lazy=t[o].lazy;
        t[o<<1].sum=(t[o<<1].r-t[o<<1].l+1)*t[o<<1].lazy;
        t[o<<1|1].sum=(t[o<<1|1].r-t[o<<1|1].l+1)*t[o<<1|1].lazy;
        t[o].lazy=-1;
    }
}

3.更新前t[1].sum 与更新后的t[1].sum差的绝度值即为答案

完整代码
还是很简单的,一遍过

#pragma GCC optimize(3,"Ofast","inline")  	//G++
#include<bits/stdc++.h>
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fcout cout<<setprecision(4)<<fixed
using namespace std;
typedef long long ll;
//======================================
namespace FastIO{
char print_f[105];void read() {}void print() {putchar('\n');}
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth){x = 0;char ch = getchar();ll f = 1;while (!isdigit(ch)){if (ch == '-')f *= -1;ch = getchar();}while (isdigit(ch)){x = x * 10 + ch - 48;ch = getchar();}x *= f;read(oth...);}
template <typename T, typename... T2>
inline void print(T x, T2... oth){ll p3=-1;if(x<0) putchar('-'),x=-x;do{print_f[++p3] = x%10 + 48;}while(x/=10);while(p3>=0) putchar(print_f[p3--]);putchar(' ');print(oth...);}} // namespace FastIO
using FastIO::print;
using FastIO::read;
//======================================
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int maxn = 1e6+5;
struct node{
    int l,r,sum,lazy;
}t[maxn<<2];
void pushup(int o){
    t[o].sum=t[o<<1].sum+t[o<<1|1].sum;
}
void pushdown(int o){
    if(t[o].lazy!=-1){
        t[o<<1].lazy=t[o<<1|1].lazy=t[o].lazy;
        t[o<<1].sum=(t[o<<1].r-t[o<<1].l+1)*t[o<<1].lazy;
        t[o<<1|1].sum=(t[o<<1|1].r-t[o<<1|1].l+1)*t[o<<1|1].lazy;
        t[o].lazy=-1;
    }
}
vector<int>edge[maxn];
void add(int x,int y){
    edge[x].push_back(y);
    edge[y].push_back(x);
}
int a[maxn];
int siz[maxn],deep[maxn],fa[maxn],son[maxn],top[maxn],dfn[maxn];
void dfs1(int u,int fr){
    deep[u]=deep[fr]+1,siz[u]=1,fa[u]=fr;
    for(auto v:edge[u]){
        if(v==fr) continue;
        dfs1(v,u);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]]) son[u]=v;
    }
}
void dfs2(int u,int fr){
    top[u]=fr;
    static int cnt=0;
    dfn[u]=++cnt;
    if(son[u]) dfs2(son[u],fr);
    for(auto v:edge[u]){
        if(v==son[u]||v==fa[u]) continue;
        dfs2(v,v);
    }
}
void build(int l,int r,int o=1){
    t[o].l=l,t[o].r=r,t[o].sum=0,t[o].lazy=-1;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(l,mid,o<<1);
    build(mid+1,r,o<<1|1);
}
void update(int l,int r,int val,int o=1){
    if(t[o].l==l&&t[o].r==r){
        t[o].sum=(t[o].r-t[o].l+1)*val;
        t[o].lazy=val;
        return ;
    }
    pushdown(o);
    int mid=(t[o].l+t[o].r)>>1;
    if(r<=mid) update(l,r,val,o<<1);
    else if(l>mid) update(l,r,val,o<<1|1);
    else update(l,mid,val,o<<1),update(mid+1,r,val,o<<1|1);
    pushup(o);
}
void update2(int x,int y,int val){
    while(top[x]!=top[y]){
        if(deep[top[x]]<deep[top[y]]) swap(x,y);
        update(dfn[top[x]],dfn[x],val);
        x=fa[top[x]];
    }
    if(deep[x]>deep[y]) swap(x,y);
    update(dfn[x],dfn[y],val);
}
char s[105];
int main() {
#ifndef ONLINE_JUDGE
    freopen("H:\\code\\in.in", "r", stdin);
    freopen("H:\\code\\out.out", "w", stdout);
    clock_t c1 = clock();
#endif
//**************************************
    ios;
    int n;
    cin>>n;
    for(int i=2;i<=n;i++){
        int x;
        cin>>x;
        x++;
        add(i,x);
    }
    dfs1(1,0);
    dfs2(1,1);
    build(1,n);
    int _;
    cin>>_;
    while(_--){
        cin>>(s+1);
        int u;
        cin>>u;
        u++;
        int t1=t[1].sum;
        if(s[1]=='i'){
            update2(u,1,1);
            int t2=t[1].sum;
            cout<<abs(t1-t2)<<"\n";
        }
        else{
            update(dfn[u],dfn[u]+siz[u]-1,0);
            int t2=t[1].sum;
            cout<<abs(t1-t2)<<"\n";
        }
    }
//**************************************
    
#ifndef ONLINE_JUDGE
    cerr << "Time:" << clock() - c1 << "ms" << endl;
#endif
    return 0;
}
根据原作 https://pan.quark.cn/s/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标签,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值