先给出一个经典的区间处理方法 对每个区间 我们对其起点用绿色标识  终点用蓝色标识 然后把所有的点离散在一个坐标轴上 如下图
 
这样做有什么意义呢。由于我们的区间可以离散的放在一条轴上面那么我们在枚举区间的时候 0(n)的复杂度就可以了 具体的操作 我们看两道题目

http://codeforces.com/contest/822/problem/C

C. Hacker, pack your bags!
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.

So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly xdays. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers liricosti — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.

At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.

Help Leha to choose the necessary vouchers!

Input

The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.

Each of the next n lines contains three integers liri and costi(1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.

Output

Print a single integer — a minimal amount of money that Leha will spend, or print  - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.

Examples
input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
output
5
input
3 2
4 6 3
2 4 1
3 5 4
output
-1
Note

In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.

In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.

题目的意思是取两段不相交的区间 它们的长度等于x 求花费最小的取法 。

借助上面区间离散的思想 我们把区间放在一条轴上,然后开始枚举 。怎么枚举呢?当我们遇到是一个区间的起点的时候,我们算出选取当前区间的最小花费,这里又有一个技巧,由于我们取第二个区间是之前遍历过且已经结束(遍历到终点)区间(题目要求不相交),我们可以用一个mark数组来记录遍历过的状态,然后在0(1)的复杂就可以算出当前情况的最小值(这里最好结合代码理解);当遇到的点是区间的终点的时候,我们更新一下mark数组;

有一个trick要注意 对于断电重合的区间 在离散化到轴上的时候 由于题意要求不想交 我们在处理的时候 把起点放在终点之前。

上代码

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const int inf=2e9 + ;//注意最大值。。
struct node //还是用结构体吧
{
int pos;
int flag;
int days;
int cost;
node(int a,int b,int c,int d)
{
pos=a;
flag=b;
days=c;
cost=d;
}
};
int cmp(node a,node b)
{
if(a.pos==b.pos)
{
return a.flag < b.flag;
}
return a.pos < b.pos;
}
int main()
{
int n,k;
cin>>n>>k;
vector< node >vec;
vec.clear();
for(int i=;i<=n;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
int temp_days=b-a+;
int temp_cost=c;
vec.push_back(node(a,,temp_days,temp_cost));
vec.push_back(node(b,,temp_days,temp_cost));
}
int mark[];
memset(mark,,sizeof(mark));
sort(vec.begin(),vec.end(),cmp);
int minn=inf;
for(int i=;i<vec.size();i++)
{
node temp=vec[i];
if(temp.flag==)// 起点的情况
{
int key=k-temp.days;
if(key<=) continue;
if(mark[key]==) continue;
minn=min(minn,mark[key]+temp.cost);
}
else// 终点的情况
{
int key=temp.days;
if(mark[key]== || mark[key] > temp.cost) mark[key]=temp.cost;// 更新mark数组
}
}
if(minn==inf) cout<<-<<endl;
else cout<<minn<<endl;
return ;
}

http://hihocoder.com/problemset/problem/1305

接下来是这道区间求差 我们要求区间是在A集合而不在B集合中 怎么处理呢?

我们还是把所有的点离散的放在一条坐标轴上 然后用一个conta表示A contb标识B 每当遇到一个区间的起点的时候cont++ 遇到终点的时候cont-- 所以对于一个区间 当cont>0的时候 说明这个区间被覆盖

那么对一段区间 当conta>0 &&coutb==0 就可以表示区间属于A而部署于B

上代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int cmp(pair<int,int>a,pair<int,int>b)
{
if(a.second==b.second) return a.first<b.first;
return a.second < b.second;
}
int main()
{
int n,m;
int a[],b[];
memset(a,,sizeof(a));
memset(b,,sizeof(b));
cin>>n>>m;
for(int i=;i<=*n;i++) scanf("%d",&a[i]);
for(int i=;i<=*m;i++) scanf("%d",&b[i]);
vector<pair <int,int> > v;
for(int i=;i<=*n;i+=)
{
v.push_back(make_pair(,a[i]));
v.push_back(make_pair(,a[i+]));
}
for(int i=;i<=*m;i+=)
{
v.push_back(make_pair(,b[i]));
v.push_back(make_pair(,b[i+]));
}
sort(v.begin(),v.end(),cmp);
int conta,contb,sum;
conta=contb=sum=;
for(int i=;i<v.size();i++)
{
pair<int,int>temp=v[i];
switch(temp.first)
{
case :conta++;break;
case :conta--;break;
case :contb++;break;
case :contb--;break;
}
if(conta>&&contb==) sum+=v[i+].second-temp.second;
}
cout<<sum<<endl;
return ;
}

区间问题 codeforces 422c+hiho区间求差问的更多相关文章

  1. hiho #1305 区间求差

    #1305 : 区间求差 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个区间集合 A 和 B,其中集合 A 包含 N 个区间[ A1, A2 ], [ A3,  ...

  2. hihocoder 1305 - 区间求差 - [hiho一下152周][区间问题]

    题目链接:https://hihocoder.com/problemset/problem/1305 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个区间集合 A ...

  3. hihoCoder 1305 区间求差

    #1305 : 区间求差 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个区间集合 A 和 B,其中集合 A 包含 N 个区间[ A1, A2 ], [ A3,  ...

  4. hdu 5289 Assignment(给一个数组,求有多少个区间,满足区间内的最大值和最小值之差小于k)

    1.区间是一段的,不是断开的哟 2.代码是看着标程写的 3.枚举左端点,二分右端点流程: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L ...

  5. 区间合并 --- Codeforces 558D : Gess Your Way Out ! II

    D. Guess Your Way Out! II Problem's Link: http://codeforces.com/problemset/problem/558/D Mean: 一棵满二叉 ...

  6. hdu6003 Problem Buyer 贪心 给定n个区间,以及m个数,求从n个区间中任意选k个区间,满足m个数都能在k个区间中找到一个包含它的区间,如果一个区间包含了x,那么 该区间不能再去包含另一个数,即k>=m。求最小的k。如果不存在这样的k,输出“IMPOSSIBLE!”。

    /** 题目:hdu6003 Problem Buyer 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6003 题意:给定n个区间,以及m个数,求从n个区 ...

  7. hdu-3333 Turing Tree 离线区间+树状数组(区间不同数的和)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 题目大意: 给出一数组,以及m个查询区间,每次查询该区间不同数字的和.相同数字只加一次. 解题 ...

  8. linux_coom _ Linux文件比较,文本文件的交集、差集与求差

    交集和差集操作在集合论相关的数学课上经常用到,不过,在Linux下 对文本进行类似的操作在某些情况下也很有用. comm命令 comm命令可以用于两个文件之间的 比较,它有一些选项可以用来调整输出,以 ...

  9. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

随机推荐

  1. Java写入的常用技巧(二)

    在一般从流接收数据写入介质的场景中,大部分存在每批次数据较小,导致小文件较多的问题. 一般考虑设置一个缓冲池,将多个批次的数据先缓冲进去,达到一定大小,再一次性批量写入 //公共缓冲池和缓冲池大小,如 ...

  2. leetcode题目142.环形链表Ⅱ(中等)

    题目描述: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 p ...

  3. 提交本地文件至gitlab已有的项目中(更新gitlab)

    gitlab代码更新 gitlab官网 1.安装git git官网 官网下载安装,安装过程一直next即可(路径自己选) 2.clone至本机 格式:git clone url(可转到指定目录克隆) ...

  4. ci框架总结(一)

    在进行数据库操作前一定要先初始化数据库类:$this->load->database(); 在model类中: class Myiapp_model extends CI_Model{ p ...

  5. 记录一个微信网页授权中不小心踩到的坑(Curl请求返回false)

    原文章地址在这里 这个问题是file_get_contents不能获取https的内容引起的.这样的情况下,我们一般会采用curl拓展来模拟请求. 代码demo(当然这是错误的示范): functio ...

  6. HTTP状态码分类及异常状态码处理

    1xx:表示临时响应100:(继续)请求者应当继续提出请求.服务器返回此代码表示已收到请求的第一部分,正在等待其余部分101:(切换协议)请求者已要求服务器切换协议,服务器已确认并准备切换 2xx:表 ...

  7. FastDFS 学习总结

    1.1   什么是FastDFS FastDFS是用c语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用 ...

  8. 一百二十六:CMS系统之轮播图管理页面布局和添加轮播图的模态对话框制作

    视图 @bp.route('/banners/')@login_required@permission_required(CMSPersmission.POSTER)def banners(): re ...

  9. js如何控制select展开

    找了一圈也没找到靠谱的方案,后来通过动态的控制select的size属性实现了. 这也算是一种方法吧. 先判断option的数量n,然后把select的size调整到n,当用户选择后,再把size设置 ...

  10. Qt在控制台输出中文的解决办法(转载)

    转载网址:https://blog.csdn.net/qq_22512533/article/details/75408984 按下快捷键Win+R,输入regedit打开注册变编辑器,依次找到 HK ...