区间交

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

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

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

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

 
Input
多组测试数据

第一行三个数n,k,m(1≤n≤100000,1≤k≤m≤100000)。

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

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

 
Output
一行表示答案
 
Sample Input
5 2 3
1 2 3 4 6
4 5
2 5
1 4
 
Sample Output
10
 
Source
 
 
解题思路:首先排序右端点从小到大(也可以排序左端点,也可以从大到小排序,看怎么处理了),然后枚举右端点(保证所枚举的那个端点最少有k个区间可以覆盖)作为所求的交区间的右端点,这时候需要求出交区间的左端点,我们可以知道,右端点确定下,如果左端点越靠左,这个区间的范围约大。为了保证所交区间有k个,我们需要找到第k小的左端点,为了保证我枚举的右端点肯定是交区间的右端点,我们必须边枚举,边单点更新左端点。
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
#pragma comment(linker, "/STACK:102400000,102400000")
const int maxn = 1e5+300;
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ULL;
LL presum[maxn];
struct Interval{
int l, r;
}intervals[maxn];
struct Seg{
int cover;
}segs[maxn*4];
bool cmp(Interval a, Interval b){
return a.r < b.r; //
}
void PushUp(int rt){
segs[rt].cover = segs[rt*2].cover + segs[rt*2+1].cover;
}
void buildtree(int rt,int L,int R){
if(L == R){
segs[rt].cover = 0;
return;
}
buildtree(lson);
buildtree(rson);
PushUp(rt);
}
void Update(int rt,int L,int R,int id){
if(L == R){
segs[rt].cover++;
return ;
}
if(id <= mid){
Update(lson,id);
}else{
Update(rson,id);
}
PushUp(rt);
}
int query(int rt,int L, int R,int k){
if(L == R){
return L;
}
if(k <= segs[rt*2].cover){
return query(lson,k);
}else{
return query(rson,k-segs[rt*2].cover);
}
}
int main(){
int n, k, m;
while(scanf("%d%d%d",&n,&k,&m)!=EOF){
buildtree(1,1,n);
LL a;
for(int i = 1; i <= n; i++){
scanf("%lld",&a);
presum[i] = presum[i-1] + a;
}
int l, r;
for(int i = 1; i <= m; i++){
scanf("%d%d",&l,&r);
intervals[i].l = l;
intervals[i].r = r;
}
sort(intervals+1,intervals+1+m,cmp);
for(int i = m-k+1; i <= m; i++){
Update(1,1,n,intervals[i].l);
}
LL ans = 0;
for(int i = m-k+1; i >= 1; i--){
int l = query(1,1,n,k);
if(l <= intervals[i].r){
ans = max(ans, presum[intervals[i].r] - presum[l-1]);
}
Update(1,1,n,intervals[i-1].l);
}
printf("%lld\n",ans);
}
return 0;
} /*
5 1 1
1 2 3 4 6
4 5 3 4 */

  

 

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 区间交(线段树)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5700 [题目大意] 给出一个长度为n的数列和m个区间,现在求k个区间,使得他们的区间交内的数列项和 ...

  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. 【题解】P1712 [NOI2016]区间(贪心+线段树)

    [题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ...

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

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

随机推荐

  1. Backup--批量备份和还原

    -----------------------------批量备份数据------------------------------------------- Use master GO /*===== ...

  2. Web.config配置文件详解(新手必看) (转载)

    原文地址:http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html <?xmlversion="1.0&quo ...

  3. 慎用uniapp开发商业级应用

    官方的社区反馈问题只给解决简单的前端问题,涉及到IDE的问题长期没人回复没人认领 官方公布的各渠道联系方式都得不到回复,先后出现了两个无法解决的问题 第一个问题(现在你都可以去他们社区搜索,没人回复没 ...

  4. NPOI CellStyle 设置

    public class ExcelNPOIUnit { public static void SetCell(IWorkbook workbook, ISheet sheet, IRow row, ...

  5. [BJ2006] 狼抓兔子

    题目链接:戳我 按理说以dinic\(O(M*N^2)\)的时间复杂度应该是过不去的(呃我也知道这个上界很松).但是最小割确实可以水过去?? 但是本着写正解的精神,我还是学了学平面图和对偶图,跑最短路 ...

  6. ERWin7.2在Windows 8.1中生成数据库遇到的问题!

    我在Windows 8.1中使用ERWin7.2,想象以前在windows 7 或者XP里那样先预览一下要生成的SQL语句,结果我发现居然没有“preview”按钮了,自然也就无法看到要生成的SQL语 ...

  7. 30.Rotate Image(矩阵旋转)

    Level:   Medium 题目描述: You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...

  8. 526. Beautiful Arrangement

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

  9. 案例2-tomcat自启动脚本

    适用于ubuntu,centos 涉及知识点 1. 函数 2. case语句 #!/bin/bash #chkconfig: #description:Tomcat service #pidfile: ...

  10. oracle 创建临时表空间/表空间,用户及授权

    1:创建临时表空间 create temporary tablespace user_temp tempfile 'Q:\oracle\product\10.2.0\oradata\Test\xyrj ...