hdu 5700 区间交(线段树)

好久没做这种题都不太会了。想法是先排序然后就大概有个方向了。

#include <stdio.h>
#include <string.h>
#include <ctime>
#include <stack>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) (x&-x)

const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
LL sum[maxn];
int tree[maxn<<2];

struct Node
{
    int l,r;
    bool operator<(const Node& cmp) const
    {
        if(r==cmp.r)
            return l<cmp.l;
        return r>cmp.r;
    }
}a[maxn];

void update(int o,int l,int r,int rt)
{
    if(l==r)
    {
        tree[rt] += 1;
        return ;
    }
    int m = (l+r)>>1;
    if(o<=m)
        update(o,lson);
    else
        update(o,rson);
    tree[rt] = tree[rt<<1] + tree[rt<<1|1];
}

int query(int k,int l,int r,int rt)
{
    if(l==r)
        return l;
    int m = (l+r)>>1;
    if(k<=tree[rt<<1]) return query(k,lson);
    else return query(k-tree[rt<<1],rson);
}

int main()
{
    int n,m,k;
    while(scanf("%d%d%d",&n,&k,&m)!=EOF)
    {
        memset(tree,0,sizeof(tree));
        for(int i=1; i<=n; i++)
        {
            int u;
            scanf("%d",&u);
            sum[i] = sum[i-1]+u;
        }
        for(int i=0; i<m; i++)
            scanf("%d%d",&a[i].l,&a[i].r);
        sort(a,a+m);
        for(int i=0; i<k-1; i++)
            update(a[i].l,1,n,1);
        LL ans = 0;
        for(int i=k-1; i<m; i++)
        {
            update(a[i].l,1,n,1);
            int l = query(k,1,n,1);
            if(l<=a[i].r)
                ans = max(ans,sum[a[i].r]-sum[l-1]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

区间交

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1091    Accepted Submission(s): 431


Problem Description
小A有一个含有 n 个非负整数的数列与 m 个区间。每个区间可以表示为 li,ri

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

例如样例中,选择 [2,5] [4,5] 两个区间就可以啦。
 

Input
多组测试数据

第一行三个数 n,k,m(1n100000,1km100000)

接下来一行 n 个数 ai ,表示lyk的数列 (0ai109)

接下来 m 行,每行两个数 li,ri ,表示每个区间 (1lirin)

 

Output
一行表示答案
 

Sample Input
  
  
5 2 3 1 2 3 4 6 4 5 2 5 1 4
 

Sample Output
  
  
10
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值