题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124

题意:有n条线段,求被覆盖到次数最多的点的次数

分析:

1.可以转化成求前缀和最大的问题:将区间改成左闭右开(即右端点加1),排序,从左往右遍历,若为左端点则加一,右端点则减一。

2.树状数组,离散化一下,然后区间更新,单点查询。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define inf 1<<30
#define mod 1000000007
using namespace std;
int a[],n,m;
int c[];
struct node
{
int l,r;
}s[];
int bin(int key,int n,int a[])
{
int l=,r=n-;
while(l<=r)
{
int m=(l+r)>>;
if(a[m]==key)return m;
if(a[m]<key)l=m+;
else r=m-;
}
return -;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int cnt=,l,r;
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
{
scanf("%d%d",&l,&r);
s[i].l=l;s[i].r=r;
a[cnt++]=l;a[cnt++]=r;
}
sort(a,a+cnt);
m=;
//离散化好模板
for(int i=;i<cnt;i++)
if(a[i]!=a[i-])a[m++]=a[i];
for(int i=m-;i>=;i--)
if(a[i]!=a[i-]+)a[m++]=a[i-]+;
sort(a,a+m);
for(int i=;i<=n;i++)
{
l=bin(s[i].l,m,a);
r=bin(s[i].r,m,a);
c[l]++;c[r+]--;
}
int ans=,sum=;
for(int i=;i<m;i++)
{
sum+=c[i];
ans=max(ans,sum);
}
printf("%d\n",ans);
}
}
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define inf 1<<30
#define mod 1000000007
using namespace std;
int a[],n,m;
int c[];
struct node
{
int l,r;
}s[];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int d)
{
while(x)
{
c[x]+=d;
x-=lowbit(x);
}
}
int sum(int x)
{
int res=;
while(x<=m+)
{
res+=c[x];
x+=lowbit(x);
}
return res;
}
int bin(int key,int n,int a[])
{
int l=,r=n-;
while(l<=r)
{
int m=(l+r)>>;
if(a[m]==key)return m;
if(a[m]<key)l=m+;
else r=m-;
}
return -;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int cnt=,l,r;
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
{
scanf("%d%d",&l,&r);
s[i].l=l;s[i].r=r;
a[cnt++]=l;a[cnt++]=r;
}
sort(a,a+cnt);
m=;
//离散化好模板
for(int i=;i<cnt;i++)
if(a[i]!=a[i-])a[m++]=a[i];
for(int i=m-;i>=;i--)
if(a[i]!=a[i-]+)a[m++]=a[i-]+;
sort(a,a+m);
for(int i=;i<=n;i++)
{
l=bin(s[i].l,m,a);
r=bin(s[i].r,m,a);
//树状数组习惯把区间右移了1,防止x+=lowbit(x)陷于死循环,不过这里加不是这个原因
l+=;r+=;
update(l-,-);update(r,);
}
int ans=;
for(int i=;i<=m+;i++)ans=max(ans,sum(i));
printf("%d\n",ans);
}
}

当然不一定非得把坐标映射到x轴上,vextor<pair<int,int> >v;v[i].first用来把坐标排序,起到映射的作用。然后数组的下标就起到了压缩的作用。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define inf 1<<30
#define mod 1000000007
using namespace std; vector <pair<int,int> > v; int main()
{
int T;
scanf("%d",&T);
while (T--)
{
v.clear();
int n;
scanf("%d",&n);
for (int i = ; i < n; i++)
{
int x;
scanf("%d",&x);
v.push_back(make_pair(x,));
scanf("%d",&x);
v.push_back(make_pair(x + ,-));
}
sort(v.begin(), v.end());
int ans = ;
int sum = ;
for (int i = ; i < v.size(); i++)
{
sum += v[i].second;
ans = max(sum,ans);
}
printf("%d\n",ans);
}
}

hdu5124(树状数组+离散化)的更多相关文章

  1. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  3. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  4. BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学

    1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...

  5. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  6. [HDOJ4325]Flowers(树状数组 离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...

  7. Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6321  Solved: 2628[Su ...

  8. CF 61E 树状数组+离散化 求逆序数加强版 三个数逆序

    http://codeforces.com/problemset/problem/61/E 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 会 ...

  9. Ultra-QuickSort(树状数组+离散化)

    Ultra-QuickSort  POJ 2299 Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50495   Accep ...

随机推荐

  1. MYSQL 语法大全自己总结的

    mysql语法大全 --------数据链接---------------------数据库服务启动net start mysql --关闭服务net stop mysql --登录 -u,-p后面不 ...

  2. 基于visual Studio2013解决C语言竞赛题之1076放鞭炮

        题目 解决代码及点评 /************************************************************************/ /* ...

  3. 论文阅读笔记 - YARN : Architecture of Next Generation Apache Hadoop MapReduceFramework

    作者:刘旭晖 Raymond 转载请注明出处 Email:colorant at 163.com BLOG:http://blog.csdn.net/colorant/ 更多论文阅读笔记 http:/ ...

  4. CppCMS URL使用

    Artyom觉得URL分为三个组成部分: Script_Name / Path_Info ? Query_String 比方以下的: /foo/bar.php/test?x=10 Script_Nam ...

  5. MySQL 暂时文件夹

    MySQL数据文件夹/data/mysql所在的上层文件夹/data磁盘空间不足导致MySQL启动失败,所以清理了/data文件夹下除了mysql子文件夹外的其它无用文件夹.重新启动发现还是失败.检查 ...

  6. DELPHI XE7 新的并行库

    DELPHI XE7 的新功能列表里面增加了并行库System.Threading, System.SyncObjs. 为什么要增加新的并行库? 还是为了跨平台.以前要并行编程只能从TThread类继 ...

  7. C#多线程实现方法——Task/Task.Factary

    原文:C#多线程实现方法--Task/Task.Factary Task 使用 Task以及Task.Factory都是在.Net 4引用的.Task跟Thread很类似,通过下面例子可以看到. st ...

  8. Android菜鸟的成长笔记(7)——什么是Activity

    原文:[置顶] Android菜鸟的成长笔记(7)——什么是Activity 前面我们做了一个小例子,在分析代码的时候我们提到了Activity,那么什么是Activity呢? Activity是An ...

  9. jsp:include怎么设置才能正确显示包含的页面呢

    1.项目的所有jsp都放在WEB-INF文件夹之下,使用的是SpirngMVC进行了过滤,jsp:include只能引入WEB-INF外部的jsp文件,对于改变后缀显示为htm的jsp的WEB-INF ...

  10. c vs c++ in strcut and class

    c vs c++ in strcut and class 总习惯用c的用法,现在学习C++,老爱拿来比较.声明我用的是g++4.2.1 SUSE Linux.看例子吧 #include <ios ...