【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5700

【题目大意】

  给出一个长度为n的数列和m个区间,现在求k个区间,使得他们的区间交内的数列项和最大。

【题解】

  将区间按照右端点为第一关键字排序,
  那么在从后往前扫描的过程中,已经扫过的部分右端点一定大于当前
  所以我们可以枚举区间交的右端点,找出第k小的左端点,来更新答案
  因为右端点固定,因此左端点越小,答案一定越大,
  所以枚举右端点不会遗漏答案。

【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=200000,M=200000;
typedef long long LL;
int k,q,x,y,T[M<<2],n,m;
void up(int x){T[x]=T[x<<1]+T[x<<1|1];}
void update(int t,int num,int l,int r,int x){
if(l==r){T[x]+=num;return;}
int mid=(l+r)>>1;
if(t<=mid)update(t,num,l,mid,x<<1);
else update(t,num,mid+1,r,x<<1|1);
up(x);
}
int find(int k,int l,int r,int x){
int mid=(l+r)>>1,tmp=T[x<<1];
if(l==r)return l;
if (k<=tmp)return find(k,l,mid,x<<1);
else return find(k-tmp,mid+1,r,x<<1|1);
}
LL s[N];
struct data{int l,r;}p[N];
bool cmp(data a,data b){return a.r<b.r||a.r==b.r&&a.l<b.l;}
int main(){
while(~scanf("%d%d%d",&n,&k,&m)){
memset(T,0,sizeof(T));
for(int i=1;i<=n;i++){
scanf("%d",&x);
s[i]=s[i-1]+x;
}for(int i=1;i<=m;i++)scanf("%d%d",&p[i].l,&p[i].r);
sort(p+1,p+m+1,cmp);
for(int i=m;i>m-k+1;i--)update(p[i].l,1,1,n,1);
LL ans=0;
for(int i=m-k+1;i;i--){
update(p[i].l,1,1,n,1);
int t=find(k,1,n,1);
if(t<=p[i].r)ans=max(ans,s[p[i].r]-s[t-1]);
}printf("%lld\n",ans);
}return 0;
}

  

HDU 5700 区间交(线段树)的更多相关文章

  1. hdu 5700区间交(线段树)

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  2. HDU 5700 区间交 线段树暴力

    枚举左端点,然后在线段树内,更新所有左边界小于当前点的区间的右端点,然后查线段树二分查第k大就好 #include <cstdio> #include <cstring> #i ...

  3. HDU 5700 区间交 离线线段树

    区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...

  4. HDU 5700——区间交——————【线段树+枚举】

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  5. HDU 5700 区间交

    枚举起点 二分终点 树状数组check #include<iostream> #include<cstring> #include<cmath> #include& ...

  6. HDU 1540 区间合并线段树

    题目大意: 就是给定一堆位置,进行删除还原,最后找到 t 位置上的最大连续位置 #include <cstdio> #include <cstring> #include &l ...

  7. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  8. 【BZOJ4653】【NOI2016】区间(线段树)

    [BZOJ4653][NOI2016]区间(线段树) 题面 BZOJ 题解 \(NOI\)良心送分题?? 既然是最大长度减去最小长度 莫名想到那道反复减边求最小生成树 从而求出最小的比值 所以这题的套 ...

  9. BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针

    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...

随机推荐

  1. glusterFS的缓存测试

    众所周知,glusterFS在客户端有缓存,缓存目的在于提高读性能.那么多个客户端同时对文件进行读写,会不会存在client缓存与server文件不一致的情况?比如client A和client B读 ...

  2. GNU所有软件下载,其中最有意思的是octave

    http://ftp.gnu.org/gnu/ 最有意思的是octave:https://www.gnu.org/software/octave/http://ftp.gnu.org/gnu/octa ...

  3. 简单Demo的用户登录JSP界面IE、Firefox(chrome) Enter 键提交表单

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  4. 【LeetCode练习题】Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  5. 网易云课堂_C++开发入门到精通_章节3: 类、对象和封装

    课时12构造函数与析构函数-2 构造函数 构造函数可以有多个 构造函数可以重载 构造函数用于隐式类型转换 class Student { public: explicit Student(int ss ...

  6. #include <list>

    clear();删除向量中的所有对象 erase(iterator it);删除it所指向的容器对象 insert(iterator it,const T&);向it所指的向量位置前插入一个对 ...

  7. SQL自动创建表和自动插入字段实例

    USE [UMoney] GO /****** Object: StoredProcedure [dbo].[WCL_WorkerStatDailyWrite] Script Date: 10/08/ ...

  8. iOS开展UI一片—简单的浏览器观看节目

    iOS开发UI篇-简单的浏览器查看程序 一.程序实现要求 1.要求 2. 界面分析 (1) 须要读取或改动属性的控件须要设置属性 序号标签 图片 图片描写叙述 左边button 右边button (2 ...

  9. 国内外DNS服务器地址列表

    DNS(Domain Name System)是域名解析服务器的意思,它在互联网的作用是把域名转换成为网络可以识别的IP地址.目前国内电信运营商通过使用DNS劫持的方法,干扰用户正常上网,使得用户无法 ...

  10. Tsinghua dsa pa2

    第一题,列车调度(train) 在这个题目中,模拟压栈出栈,同时判断调度方案是否可行. #include <cstdio> #include <cstring> #define ...