题意:

给出数轴上的N个区间,M个询问"QUERY(a, b)", 意为[a, b]之间不相交的集合的最大数量是多少。

解法:

考虑 $O(n)$ 的贪心做法,预处理出对于每一个位置$i$,满足$i \leq L_j$ 的 $R_j$的最小值

这样暴力向后找即可。

用倍增优化这个过程 $O(nlogn)$

#include <bits/stdc++.h> 

#define N 100010

using namespace std;

struct node
{
int l,r;
}a[N],q[N]; int n,m,tot0;
int a0[N<<];
int minR[N<<][]; bool cmp(node a,node b)
{
return a.l<b.l;
} int ask(int l,int r)
{
int x = l,ans = ;
for(int i=;~i;i--)
if(minR[x][i] <= r)
x = minR[x][i], ans += (<<i);
return ans;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
a0[] = ;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a0[++a0[]] = a[i].l;
a0[++a0[]] = a[i].r;
}
for(int i=;i<=m;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
a0[++a0[]] = q[i].l;
a0[++a0[]] = q[i].r;
}
sort(a0+,a0+a0[]+);
tot0=;
for(int i=;i<=a0[];i++) if(a0[i]!=a0[i-]) a0[++tot0] = a0[i];
for(int i=;i<=n;i++)
{
a[i].l = lower_bound(a0+,a0+tot0+,a[i].l) - a0;
a[i].r = lower_bound(a0+,a0+tot0+,a[i].r) - a0;
}
sort(a+,a+n+,cmp);
int j=n,tmpR = tot0+;
for(int i=tot0;i>=;i--)
{
while(j> && a[j].l >= i)
{
tmpR = min(tmpR, a[j].r);
j--;
}
minR[i][] = tmpR;
}
for(int t=;t<=;t++)
for(int i=;i<=tot0;i++)
{
if(minR[i][t-]<=tot0)
minR[i][t] = minR[minR[i][t-]][t-];
else minR[i][t] = tot0+;
}
for(int i=;i<=m;i++)
{
q[i].l = lower_bound(a0+,a0+tot0+,q[i].l) - a0;
q[i].r = lower_bound(a0+,a0+tot0+,q[i].r) - a0;
printf("%d\n",ask(q[i].l,q[i].r));
}
}
return ;
}

Interval query的更多相关文章

  1. HDU 4343 D - Interval query 二分贪心

    D - Interval queryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  2. 刷题总结——Interval query(hdu4343倍增+贪心)

    题目: Problem Description This is a very simple question. There are N intervals in number axis, and M ...

  3. 【HDOJ】4343 Interval query

    最大不相交集合的数量.思路是dp[i][j]表示已经有i个不相交集合下一个不相交集合的最右边界.离散化后,通过贪心解. /* 4343 */ #include <iostream> #in ...

  4. 【HDU 4343】Interval query(倍增)

    BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...

  5. HDU 4343 Interval query(贪心 + 倍增)

    题目链接  2012多校5 Problem D 题意  给定$n$个区间,数字范围在$[0, 10^{9}]$之间,保证左端点严格大于右端点. 然后有$m$个询问,每个询问也为一个区间,数字范围在$[ ...

  6. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

  7. LA 3938 动态最大连续和 线段树

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  8. HDU 4343 贪心

    D - Interval queryTime Limit: 1.5 Sec Memory Limit: 256 MB Description This is a very simple questio ...

  9. GSS系列(1)——GSS1&&GSS3

    题意:询问一个区间内的最大连续子段和(GSS1),并且有单点修改的操作(GSS2). 思路:这个题目在老人家的大白鼠里出现过,不过那个是求两个下标,并且相同取更小值.——传的东西更多,判断也稍微繁琐一 ...

随机推荐

  1. svn 版本管理与自动部分发布(转)

    作为团队开发项目时,会遇到项目的版本管理,测试部署与发布部署,下面是摘至他人的关于版本管理和自动部署的方案. svn自动部署的实现: 使用svn的hook功能 1.在版本库的hooks目录下面,有一些 ...

  2. uboot 命令

    1.清除前一次的编译结果: make distclean 2.配置makefile:选择开发板 make  smdk6410_config 3.编译 make 注意::编译时,打开的文档文件,目录都要 ...

  3. 目标检测之显著区域检测---国外的一个图像显著区域检测代码及其效果图 saliency region detection

    先看几张效果图吧 效果图: 可以直接测试的代码: 头文件: // Saliency.h: interface for the Saliency class.////////////////////// ...

  4. RDLC报表 报表数据 栏 快捷键

    血淋淋的教训啊,这段时间用RDLC报表,创建报表后会在右边加载[服务器资源管理器][工具栏][报表数据]一些栏目 如图 [服务器资源管理器][工具栏]还好[报表数据]栏在开发的时候不小心点了X给关掉了 ...

  5. 九度OJ 1121:首字母大写 (字符串处理)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2865 解决:1007 题目描述: 对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母. 在字符串中,单词 ...

  6. In Git, there are two main ways to integrate changes from one branch into another: the merge and the rebase.

    https://git-scm.com/book/en/v2/Git-Branching-Rebasing

  7. Codeforces696 Round #362 (Div. 1)(vp) A~D题解

    很久没有打比赛了,内部模拟赛天天垫底,第一次vp之旅又是和**一样,这样下去GDOI之后直接退役算了 整场都在忘开LL A. Lorenzo Von Matterhorn 这个题一看我就想直接虚树+树 ...

  8. HDU5968 异或密码 —— 二分 + 边界的细节处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5968 异或密码 Time Limit: 2000/1000 MS (Java/Others)    M ...

  9. CSU 1554 SG Value —— 思维

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554 Description The SG value of a set (mult ...

  10. Codeforces Round #374 (Div. 2) C. Journey —— DP

    题目链接:http://codeforces.com/contest/721/problem/C C. Journey time limit per test 3 seconds memory lim ...