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] 线段树+二分答案/分块
!!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...
随机推荐
- JAVA多线程二
Thread.Join() join()函数表示等待当前线程结束,然后返回. public final synchronized void join(long millis) throws Inter ...
- 凸包模板 POJ1873
// 凸包模板 POJ1873 // n=15所以可以按位枚举求凸包,再记录数据 #include <iostream> #include <cstdio> #include ...
- Oracle 表空间修改字段大小
1.修改字段大小 当表中已经存在数据,就不能直接修改某字段大小,需要新建一个字段来过渡 ALTER TABLE TABLE RENAME COLUMN GRP TO FUND_GRP_1; ); ...
- Java基础 —— 概述
Java语言: JDK(Java Development Kit)开发工具包,提供Java的开发环境和运行环境 --> 适合于开发 JRE(Java Runtime Environment)Ja ...
- 《LINUX程序设计 第四版》 阅读笔记:(一)
1. 头文件 使用-I标志来包含头文件. gcc -I/usr/openwin/include fred.c 2. 库文件 通过给出 完整的库文件路径名 或 用-l标志 来告诉编译器要搜索的库文件. ...
- carthage 简单使用步骤
brew install carthage切至项目目录:cd xxx创建Cartfile文件vi Cartfile填写依赖git "https://xxxxx" "mas ...
- C++builder XE10 终于支持类内变量初始化了
Win32终于支持类内变量初始化了,C++11 用bcc32C编译器 llvm CLang.还支持Unicode 中文汉字 变量名. 用经典的bcc32编译还是不支持! class TPerson ...
- Apache Spark 架构
1.Driver:运行 Application 的 main() 函数并且创建 SparkContext. 2.Client:用户提交作业的客户端. 3.Worker:集群中任何可以运行 Applic ...
- VMare中安装“功能增强工具”,实现CentOS5.5与win7host共享文件夹的创建
读者如要转载,请标明出处和作者名,谢谢. 地址01:http://space.itpub.net/25851087 地址02:http://www.cnblogs.com/zjrodger/ 地址03 ...
- 使用google map v3 api 开发地图服务
Google Map V3 API 学习地址: http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/article ...