【NOIP Round #4】治病

· · 题解

\colorbox{orange}{\color{white}\texttt{题目传送门}}

抽象做法。

首先可以无脑暴力算出没有医生是庸医的答案,直接将每种药分开算,用区间覆盖,区间和的线段树即可,如果你要用珂朵莉树也没人拦着你。

注意到,如果第 i 天的原本要吃第 j 种药,但只有编号为 k 的医生让他吃,那么如果编号为 k 的医生是庸医,就可以少花费 c_j 元。

同样用线段树维护,要用区间加、整体清 0,区间查询 1 个数的线段树。

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,c[500005];
long long sum,ans[500005];
struct did{int l,r,id;};
vector<did>vec[500005];
int d=0;
namespace T1_X{
    struct node{
        long long s;
        int t;
    }t[4000005];
    void push_down(int p,int l,int r){
        int mid=(l+r>>1);
        if(t[p].t!=-1){
            t[p<<1].s=t[p].t*(mid-l+1);
            t[p<<1|1].s=t[p].t*(r-mid);
            t[p<<1].t=t[p<<1|1].t=t[p].t;
            t[p].t=-1;
        }
    }
    inline void push_up(int p){
        t[p].s=t[p<<1].s+t[p<<1|1].s;
    }
    void build(int l,int r,int p=1){
        t[p].t=-1;
        if(l==r){
            return;
        }
        build(l,l+r>>1,p<<1);
        build((l+r>>1)+1,r,p<<1|1);
        push_up(p);
    }
    long long getsum(int l,int r,int L,int R,int p=1){
        if(L>=l&&R<=r)return t[p].s;
        if(L>r||l>R)return 0;
        push_down(p,L,R);
        return getsum(l,r,L,L+R>>1,p<<1)+getsum(l,r,(L+R>>1)+1,R,p<<1|1);
    }
    void update(int l,int r,int L,int R,int c,int p=1){
        if(L>=l&&R<=r){
            t[p].s=(R-L+1)*c;
            t[p].t=c;
            return;
        }
        if(L>r||l>R)return;
        push_down(p,L,R);
        update(l,r,L,L+R>>1,c,p<<1);
        update(l,r,(L+R>>1)+1,R,c,p<<1|1);
        push_up(p);
    }
}
namespace T2_X{
    struct node{
        int c[2];
        bool f;
        int t;
    }t[4000005];
    void push_down(int p,int l,int r){
        int mid=(l+r>>1);
        if(t[p].f!=0){
            t[p<<1].c[0]=mid-l+1;
            t[p<<1|1].c[0]=r-mid;
            t[p<<1].c[1]=t[p<<1|1].c[1]=0;
            t[p<<1].f=t[p<<1|1].f=t[p].f;
            t[p<<1].t=t[p<<1|1].t=0;
            t[p].f=0;
        }
        if(t[p].t!=0){
            if(t[p].t==1){
                t[p<<1].c[1]=t[p<<1].c[0];
                t[p<<1].c[0]=0;
                t[p<<1|1].c[1]=t[p<<1|1].c[0];
                t[p<<1|1].c[0]=0;
            }else{
                t[p<<1].c[0]=t[p<<1|1].c[0]=t[p<<1].c[1]=t[p<<1|1].c[1]=0;
            }
            t[p<<1].t+=t[p].t;
            t[p<<1|1].t+=t[p].t;
            t[p].t=0;
        }
    }
    inline void push_up(int p){
        t[p].c[0]=t[p<<1].c[0]+t[p<<1|1].c[0];
        t[p].c[1]=t[p<<1].c[1]+t[p<<1|1].c[1];
    }
    void build(int l,int r,int p=1){
        t[p].t=0;
        t[p].f=0;
        if(l==r){
            t[p].c[0]=1;
            return;
        }
        build(l,l+r>>1,p<<1);
        build((l+r>>1)+1,r,p<<1|1);
        push_up(p);
    }
    int getsum(int l,int r,int L,int R,int p=1){
        if(L>=l&&R<=r)return t[p].c[1];
        if(L>r||l>R)return 0;
        push_down(p,L,R);
        return getsum(l,r,L,L+R>>1,p<<1)+getsum(l,r,(L+R>>1)+1,R,p<<1|1);
    }
    void update(int l,int r,int L,int R,int p=1){
        if(L>=l&&R<=r){
            t[p].c[1]=t[p].c[0];
            t[p].c[0]=0;
            if(t[p].t<100)++t[p].t;
            return;
        }
        if(L>r||l>R)return;
        push_down(p,L,R);
        update(l,r,L,L+R>>1,p<<1);
        update(l,r,(L+R>>1)+1,R,p<<1|1);
        push_up(p);
    }
    void clear(){
        t[1].f=1;
        t[1].t=0;
        t[1].c[0]=d;
        t[1].c[1]=0;
    }
}
signed main(){
    freopen("doctor.in","r",stdin);
    freopen("doctor.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>c[i];
    }
    for(int i=1;i<=n;i++){
        int l,r,k;
        cin>>l>>r>>k;
        while(k--){
            int x;
            cin>>x;
            vec[x].push_back({l,r,i});
        }
        if(r>d)d=r;
    }
    T1_X::build(1,d);
    for(int i=1;i<=m;i++){
        for(auto[l,r,x]:vec[i]){
            T1_X::update(l,r,1,d,1);
        }
        sum+=1ll*c[i]*T1_X::getsum(1,d,1,d);
        T1_X::update(1,d,1,d,0);
    }
    for(int i=1;i<=n;i++){
        ans[i]=sum;
    }
    T2_X::build(1,d);
    for(int i=1;i<=m;++i){
        for(auto[l,r,x]:vec[i]){
            T2_X::update(l,r,1,d);
        }
        for(auto[l,r,x]:vec[i]){
            ans[x]-=1ll*c[i]*T2_X::getsum(l,r,1,d);
        }
        T2_X::clear();
    }
    for(int j=1;j<=n;j++){
        cout<<ans[j]<<" ";
    } 
    return 0;
}//rp++
//很好的代码,使我旋转