Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals
题目连接:
http://codeforces.com/gym/100231/attachments
Description
给你n个区间,告诉你每个区间内都有ci个数
然后你需要找一个最小的点集,使得满足这n个区间的条件
Input
n
然后li,ri,ci
Output
输出最小点集大小
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Hint
题意
题解:
线段树+二分+贪心
首先我们贪心一发,按照右端点排序之后,我们点肯定是优先放在右边
那么我们怎么确认有多少个放右边呢?
二分+线段树就好了,我们二分有多少个点放在右边,然后在用线段树的区间和来check是否大于ci就好了
代码
#include<bits/stdc++.h>
using namespace std;
#include<bits/stdc++.h>
using namespace std;
typedef int SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum , lazy;
void updata(SgTreeDataType v)
{
sum = (R-L+1)*v;
lazy = v;
}
};
treenode tree[301500];
inline void push_down(int o)
{
SgTreeDataType lazyval = tree[o].lazy;
if(lazyval!=0){
tree[2*o].updata(lazyval) ; tree[2*o+1].updata(lazyval);
tree[o].lazy = 0;
}
}
inline void push_up(int o)
{
tree[o].sum = tree[2*o].sum + tree[2*o+1].sum;
}
inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = tree[o].lazy = 0;
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
}
}
inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
if(QL>QR)return;
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].updata(v);
else
{
push_down(o);
int mid = (L+R)>>1;
if (QL <= mid) updata(QL,QR,v,o*2);
if (QR > mid) updata(QL,QR,v,o*2+1);
push_up(o);
}
}
inline SgTreeDataType query(int QL,int QR,int o)
{
if(QL>QR)return 0;
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
push_down(o);
int mid = (L+R)>>1;
SgTreeDataType res = 0;
if (QL <= mid) res += query(QL,QR,2*o);
if (QR > mid) res += query(QL,QR,2*o+1);
push_up(o);
return res;
}
}
struct node
{
int l,r,c;
}p[52000];
bool cmp(node a,node b)
{
if(a.r==b.r&&a.l==b.l)
return a.c<b.c;
if(a.r==b.r)
return a.l>b.l;
return a.r<b.r;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&p[i].l,&p[i].r,&p[i].c);
p[i].l+=1,p[i].r+=1;
}
sort(p+1,p+1+n,cmp);
build_tree(1,p[n].r+100,1);
int ans = 0;
for(int i=1;i<=n;i++)
{
//int now = query(p[i].l,p[i].r,1);
//if(now<p[i].c)
//{
int L = p[i].l-1,R = p[i].r;
while(L<=R)
{
int mid = (L+R)/2;
if(query(p[i].l,mid,1)+(p[i].r-mid)>=p[i].c)
L=mid+1;
else
R=mid-1;
}
ans+=(p[i].r-L+1)-query(L,p[i].r,1);
//cout<<L<<" "<<p[i].r<<endl;
updata(L,p[i].r,1,1);
//}
}
//for(int i=1;i<=p[n].r;i++)
// cout<<query(i,i,1)<<" ";
//cout<<endl;
cout<<ans<<endl;
}
/*
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
*/
Codeforces Gym 100231B Intervals 线段树+二分+贪心的更多相关文章
- Codeforces 431E Chemistry Experiment 线段树 + 二分
Chemistry Experiment 维护一个权值线段树,然后二分答案. #include<bits/stdc++.h> #define LL long long #define LD ...
- Codeforces Gym100543B 计算几何 凸包 线段树 二分/三分 卡常
原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100543B.html 题目传送门 - CF-Gym100543B 题意 给定一个折线图,对于每一条 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)
题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n n个数,然后求有多少个区间[l,r] 满足 a[l]+a[r]=max([l, ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- hdu4614 线段树+二分 插花
Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...
- 【arc073e】Ball Coloring(线段树,贪心)
[arc073e]Ball Coloring(线段树,贪心) 题面 AtCoder 洛谷 题解 大型翻车现场,菊队完美压中男神的模拟题 首先钦定全局最小值为红色,剩下的袋子按照其中较大值排序. 枚举前 ...
- 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块
!!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...
随机推荐
- xtraTabControl 如何遍历每个选项卡 z
XtraTabHitInfo hi = tabPositionControl.CalcHitInfo(new Point(e.X, e.Y)); if (hi.HitTest == XtraTabHi ...
- Locker
题意: 有2个数字串,每次可以变化1-3位(每位+1或-1(0-9,9-0)可循环),求由1串变到2串的最小用的次数. 分析: dp[i][num]表示变到第i位时最后两位组成的数是num时最小次数( ...
- windows下ncl生成tiff图(案例)
一:安装软件和准备数据 1.需要安装Vapor(注意安装路径不要存在空格) 注:版本2.4.2及以后 2.安装NCL,方法见http://www.cnblogs.com/striver-zhu/p/4 ...
- Cocos2d-android (02) 添加一个精灵对象
什么是精灵: 1.精灵就是游戏当中的一个元素,通常用于代表画面当前中的一个事物,例如主人公,NPC和背景元素等: 2.一个精灵对象通常都与一张图片关联 3.精灵对象可以通过动作对象(CCAction) ...
- 转】MyEclipse10安装Log4E插件
原博文出自于:http://www.cnblogs.com/xdp-gacl/p/4231812.html 感谢! 一. Log4E插件下载 下载地址:http://log4e.jayefem.de/ ...
- webrtc 的回声抵消(aec、aecm)算法简介(转)
webrtc 的回声抵消(aec.aecm)算法简介 webrtc 的回声抵消(aec.aecm)算法主要包括以下几个重要模块:1.回声时延估计 2.NLMS(归一化最小均方自适应算法) ...
- C++11lambda表达式
[C++11lambda表达式] mutable 修饰符,用于修改[]中以值传递的变量,无mutable修饰符的话则不行. 使用示例: #include <vector> #include ...
- hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)
http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的 ...
- Python基础 数字、字符串、列表、元组、字典
Number(数字)---> int.float.complex(复数) class int 在Python3中,整形(int)就是常见的整数:1,100,500,1000...... 浮点型( ...
- Eclipse 安装对 Java 8 的支持
Java 8 正式版今天已经发布了(详情),但最常用的 Java 开发工具 Eclipse 还没有正式发布对 Java 8 的支持.不过目前可以通过更新 JDT 来支持 Java 8.步骤如下: 菜单 ...