2018-2019 ICPC, NEERC, Southern Subregional Contest C - Cloud Computing [contest/1070]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z591826160/article/details/83268134

C - Cloud Computing [contest/1070]

题面

在这里插入图片描述

思路

考虑维护一个 c [ i ] c[i] 的权值线段树,并在树上维护 c [ i ] \sum c[i] , p [ i ] p[i] ,在到达区间左端点时,加入线段树,到达右端点后删除掉,在树内二分 c [ i ] \sum c[i] 即可

代码

(队友的代码 贴上来了)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
#define mp make_pair
#define se second
#define fi first
#define Son int mid=l+r>>1,lson=x<<1,rson=x<<1|1
const int maxn = 1e6+10;
const int mod = 1e9+7;
vector<pair<int,int> > ad[maxn],de[maxn];
struct nod {
    LL sum,num;
}tree[maxn<<2];
void up(int x,int l,int r,int pos,int v) {
    if (l==r) {
        tree[x].num+=v;
        tree[x].sum+=1ll*l*v;
        return ;
    }
    Son;
    if (pos<=mid) up (lson,l,mid,pos,v);
    else up (rson,mid+1,r,pos,v);
    tree[x].sum=tree[lson].sum+tree[rson].sum;
    tree[x].num=tree[lson].num+tree[rson].num;
}
ll query(int x,int l,int r,ll re) {
    if (tree[x].num<=re) {
        return tree[x].sum;
    }
    if (l==r) {
        return re*l;
    }
    Son;
    if (tree[lson].num>=re) return query(lson,l,mid,re);
    else return tree[lson].sum+query(rson,mid+1,r,re-tree[lson].num);
}
int main(){
    int n,k,m;
    scanf("%d%d%d",&n,&k,&m);
    int h=0;
    ll ans=0;
    for (int i=1;i<=m;i++) {
        int l,r,c,p;
        scanf("%d%d%d%d",&l,&r,&c,&p);
        if (p>h) h=p;
        ad[l].push_back(mp(c,p));
        de[r+1].push_back(mp(c,p));
    }
    for (int i=1;i<=n;i++) {
        for (int j=0;j<ad[i].size();j++) {
            up(1,1,h,ad[i][j].se,ad[i][j].fi);
        }
        for (int j=0;j<de[i].size();j++) {
            up(1,1,h,de[i][j].se,-de[i][j].fi);
        }
        ans+=query(1,1,h,k);
    }
    printf("%lld\n",ans);
}

猜你喜欢

转载自blog.csdn.net/z591826160/article/details/83268134
今日推荐