HDU 5700——区间交——————【线段树+枚举】
2016-05-27 08:45
330 查看
区间交
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 567 Accepted Submission(s): 279
[align=left]Problem Description[/align]
小A有一个含有n个非负整数的数列与m个区间。每个区间可以表示为li,ri。
它想选择其中k个区间, 使得这些区间的交的那些位置所对应的数的和最大。
例如样例中,选择[2,5]与[4,5]两个区间就可以啦。
[align=left]Input[/align]
多组测试数据
第一行三个数n,k,m(1≤n≤100000,1≤k≤m≤100000)。
接下来一行n个数ai,表示lyk的数列(0≤ai≤109)。
接下来m行,每行两个数li,ri,表示每个区间(1≤li≤ri≤n)。
[align=left]Output[/align]
一行表示答案
[align=left]Sample Input[/align]
5 2 3
1 2 3 4 6
4 5
2 5
1 4
[align=left]Sample Output[/align]
10
[align=left]Source[/align]
2016"百度之星" - 初赛(Astar Round2B)
解题思路:首先排序右端点从小到大(也可以排序左端点,也可以从大到小排序,看怎么处理了),然后枚举右端点(保证所枚举的那个端点最少有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 */
相关文章推荐
- 【CodeForces】675A - Infinite Sequence
- I2C SPI UART和CAN的区别
- 定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)。要求如下: (1)可以生成具有特定坐标的点对象。 (2)提供可以设置三个坐标的方法。 (3)提供可以计算该“点”距原点距离平方的方法。 (4)编写主类程序验证。
- Android绘图机制与处理技巧(二)——Android图像处理之色彩特效处理
- iOS 即时通讯 + 仿微信聊天框架 + 源码
- 8大排序算法图文讲解
- 【DRP分销】--- JSP的四个常用对象的作用域
- java使用正则抓取网页邮箱
- IT十八掌掌第十天课程总结
- android studio获取sha1值
- XHTML 相对路径与绝对路径
- (gnupg)gpg centos linux 虚拟机编译过程总结,,,,整个过程头痛
- XHTML 相对路径与绝对路径
- hge source explor 0x9 Power & Random & Ini
- excel学习--从基础开始3
- (4.1.11.5)RecyclerView:实现带header的grid
- HDU 1233 还是畅通工程
- 【English】English Week is coming
- 个人工作总结11
- What is difference between Vim's clipboard “unnamed” and “unnamedplus” settings?