https://ac.nowcoder.com/acm/problem/14136(牛客网 监视任务)

题目链接:
题解:本题我们不能一上来就用树状数组来统计每一位的贡献,我们需要先对区间进行一个排序,先按照区间的右端点由小到大排序,再按照区间的左端点从小到大排序,再按照区间的k值从大到小排序。坑点是再加入值的时候不能用连续的区间修改,因为我们要加入的一些位置可能离散而不连续

#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int maxn=1e6+10;
const LL mod=1e9+7;
const double pi=acos(-1.0);
inline LL read()
{
    LL X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}
LL n,m;
LL c[maxn];
LL vis[maxn];
struct node{
    LL l,r,k;
}p[maxn];
bool cmp(node x,node y)
{
    if(x.r!=y.r){
        return x.r<y.r;
    }
    if(x.l!=y.l){
        return x.l<y.l;
    }
    return x.k>y.k;
    //return x.r<y.r;
}
void add(LL x,LL d)
{
    while(x<=n){
        c[x]+=d;
        x+=(x&(-x));
    }
}
LL query(LL x)
{
    LL res=0;
    while(x>0){
        res+=c[x];
        x-=(x&(-x));
    }
    return res;
}

int main()
{
    scanf("%lld%lld",&n,&m);
    for(LL i=1;i<=m;i++){
        scanf("%lld%lld%lld",&p[i].l,&p[i].r,&p[i].k);
    }
    sort(p+1,p+1+m,cmp);
    memset(vis,0,sizeof(vis));
    LL res=0;
    for(LL i=1;i<=m;i++){
        LL l=p[i].l,r=p[i].r;
        LL pre=query(r)-query(l-1);
        if(pre<p[i].k){
            LL ko=p[i].k-pre;
            LL s=0;
            for(LL j=p[i].r;j>=p[i].l;j--){
                if(vis[j]==0){
                    add(j,1);
                    s++;
                    res++;
                    vis[j]=1;
                    if(s>=ko){
                        break;
                    }
                }
            }
        }
        else{
            continue;
        }
    }
    printf("%lld\n",res);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值