hdu-5700 区间交(二分+树状数组)

news/2024/7/5 17:58:38

题目链接:

区间交

Problem Description
 

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

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

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

 

Input
 

多组测试数据

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

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

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

 

Output
 

一行表示答案

 

 

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

题意:


思路:

求相交区间和的最大值,首先是树状数组sum可以求log(n)求区间和,在找区间的时候枚举左端点,二分右端点,先把区间按左端点排序,然后一边更新一边询问,由于按左端点排序,所以左端点可以作为相交区间的左端点,二分右端点时询问这个点是否被大于等于k次覆盖,找到右端点最大的那个点,此时对应的区间就是这个左端点能得到的最大的区间,枚举完左端点就可以找到最大值了;
复杂度好像是mlog(n)log(n);


AC代码:

#include <bits/stdc++.h>
/*
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+4;
int n,k,m;
LL a[N];
LL sum[N];
int num[N];
struct node
{
    int l,r;
}po[N];
int cmp(node x,node y)
{
    if(x.l==y.l)x.r>y.r;
    return x.l<y.l;
}
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,LL y)
{
    while(x<=n)
    {
        sum[x]+=y;
        x+=lowbit(x);
    }
}
LL query(int pos)
{
    LL ans=0;
    while(pos>0)
    {
        ans+=sum[pos];
        pos-=lowbit(pos);
    }
    return ans;
}
void update1(int x,int flag)
{
    while(x<=n)
    {
        num[x]+=flag;
        x+=lowbit(x);
    }
}
int query1(int x)
{
    int ans=0;
    while(x>0)
    {
            ans+=num[x];
            x-=lowbit(x);
    }
    return ans;
}
int check(int x)
{
    if(query1(x)>=k)return 1;
    return 0;
}
int main()
{
        while(scanf("%d%d%d",&n,&k,&m)!=EOF)
        {
            LL ans=0;
            mst(num,0);
            mst(sum,0);
            Riep(n)scanf("%lld",&a[i]),update(i,a[i]);
            Riep(m)scanf("%d%d",&po[i].l,&po[i].r);
            sort(po+1,po+m+1,cmp);
            for(int i=1;i<=m;i++)
            {
                update1(po[i].l,1);
                update1(po[i].r+1,-1);
                int L=po[i].l,R=po[i].r;
                while(L<=R)
                {
                    int mid=(L+R)>>1;
                    if(check(mid))L=mid+1;
                    else R=mid-1;
                }
                LL fx=query(L-1),fy;
                if(po[i].l>1)fy=query(po[i].l-1);
                else fy=0;
                ans=max(ans,fx-fy);
            }
            printf("%lld\n",ans);
        }
    return 0;
}

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5526640.html


http://www.niftyadmin.cn/n/4482049.html

相关文章

csdn的资源使用

资源库&#xff1a; http://lib.csdn.net/转载于:https://www.cnblogs.com/Eddyer/p/5535161.html

【转】下载太慢?简单设置让iTunes提速十几倍

原文网址&#xff1a;http://www.startos.com/mac/ipad/tips/2010120713291.html 今年可以说是苹果欢笑的一年&#xff0c;ipad的发布&#xff0c;iphone4的成功&#xff0c;让用苹果设备的消费者越来越多&#xff0c;itouch也取得了不俗的销售成绩&#xff0c;不过&#xff0c…

课堂练习(卖书问题)

设计思想&#xff1a; 通过简单的枚举计算&#xff0c;发现只有在 买书数35*n&#xff08;n>0&#xff09;时才会有特殊情况产生&#xff0c;其他情况均一致&#xff0c;所以就利用switch语句对 买书数除5的余数进行判断&#xff0c;进行不同的分类计算&#xff0c;其中当…

Murano Weekly Meeting 2016.05.31

Meeting time&#xff1a;   2016.May.31 1:00~2:00 Chairperson&#xff1a;   Kirill Zaitsev, from Mirantis Meeting summary&#xff1a; 1.Action Item Review update the wiki to mention new CPLs. kzaitsev_mb ping stable reviewers to review the backports. 2…

Windows下获取和安装PEAR包管理器 Getting and installing the PEAR package manager

1. 安装PHP&#xff0c;确保能使用能在命令行使用php命令&#xff1b; C:\Users\YangLong>php -v PHP 5.6.4 (cli) (built: Dec 17 2014 13:20:35) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologieswith Xdebug v2.2.…

Java 设计模式——外观模式

概述 今天要说的外观模式是一个相对简单的设计模式&#xff0c;而且在日常的开发中&#xff0c;可能你也会时常使用它&#xff0c;只是你可能并未想过这是一个设计模式。本文会从一些实例着手&#xff0c;来对本文要说明的外观模式进行尽可能全面的讲解。希望于你有益。 引言 这…

MVC系列-3.数据显示

显示用户列表 1. 创建ViewModel 在ViewModels 文件下&#xff0c;创建新类并命名为AccountListViewModel 2.在Controller中修改Index方法&#xff0c;添加相关View, 显示所有用户 1&#xff09;将ViewModell作为参数传到view 2&#xff09;Views Account Index.cshtml 创建V…

夜深了 关于 异步Action的定义的截图

转载于:https://www.cnblogs.com/ganmk--jy/p/5565472.html