hdu5700区间交(线段树)

题目链接:点这里!!!!!


题意:

小A有一个含有nn个非负整数的数列与mm个区间。每个区间可以表示为l_{i},r_{i}li,ri

它想选择其中kk个区间, 使得这些区间的交的那些位置所对应的数的和最大。

数据范围:1<=n<=1e5,1<=k<=m<=1e5


题解:

我们去枚举符合条件的区间的左端点i,然后把所有左端点小于等于i的区间的有端点加入到线段树里。我们要求k个区间交的区间和的最大值,说明我们对于左端点i要选尽可能大的右端点,我们取右端点最大的k个区间取交集,右端点对应的就是第k大的右端点,利用线段树求第K大就可以了!!


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
const LL  MOD = 1000000007;
const int N = 1e5+15;
const int maxn = 1e5+15;
const int letter = 130;
const LL INF = 1e7;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,ans[N<<2];
LL a[N];
struct node{
    int l,r;
    bool operator < (const node &p)const{
        return l<p.l;
    }
}que[N];
void pushup(int lr){
    ans[lr]=ans[lr<<1]+ans[lr<<1|1];
}
void build(int lr,int l,int r){
    ans[lr]=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
}
void update(int id,int lr,int l,int r){
    if(l==r){
        ans[lr]++;
        return;
    }
    int mid=(l+r)>>1;
    if(id<=mid) update(id,lson);
    else update(id,rson);
    pushup(lr);
}
int query(int k,int lr,int l,int r){
    if(l==r) return l;
    int mid=(l+r)>>1;
    if(ans[lr<<1]>=k) return query(k,lson);
    else return query(k-ans[lr<<1],rson);
}
int main(){
    while(~scanf("%d%d%d",&n,&k,&m)){
        clr(a,0);
        for(int i=1;i<=n;i++) scanf("%I64d",a+i);
        for(int i=1;i<=m;i++) scanf("%d%d",&que[i].l,&que[i].r);
        for(int i=1;i<=n;i++) a[i]+=a[i-1];
        sort(que+1,que+m+1);
        build(1,1,n);
        LL max1=0;
        int ps=1;
        for(int i=1;i<=n;i++){
            while(ps<=m&&que[ps].l<=i){
                update(que[ps++].r,1,1,n);
            }
            int vs=ps-k;
            if(vs<=0) continue;
            int r=query(vs,1,1,n);
            if(i<=r) max1=max(max1,a[r]-a[i-1]);
        }
        printf("%I64d\n",max1);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值