题目链接: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. ASP.NET - 使用MqSql数据库

    1. 首先需要安装mysql, 脚本之家下载地址: http://www.jb51.net/softs/2193.html 或者去mysql.com官网都可以,一路next,安装好后,有个简单配置,提 ...

  2. js 常用方法记事本

    1.获取被选中行的名称<tab选项卡中为iframe> /* S 获取首页被选中的选项卡名称 */ var currTab = $("#layout_center_tabs&qu ...

  3. JDBC操作数据库的学习(1)

    单单对数据库的操作,比如说MySQL,我们可以在命令行窗口中执行,但是一般是应用程序要操作数据库,因此我们应该在程序中的代码上体现对数据库的操作,那么使用程序应用如何操作数据库呢?那就要使用到数据库的 ...

  4. nodejs+socket.io即时聊天实例

    在这之前你应该先安装好 Node.js,安装过程不再讲解 首先在你的电脑上创建一个新目录,姑且命名为 chat,然后在该目录创建两个文件,分别是 app.js 和 index.html. app.js ...

  5. Cookie不能保存中文解决方式

     在用cookie保存username的时候,发现cookie值不能存中文,报例如以下错: Control character in cookie value, consider BASE64 e ...

  6. CTreeCtrl结点拖动实现(与后台联动)

    原帖及讨论:http://bbs.bccn.net/thread-211413-1-1.html 效果描述:鼠标点击并拖动某一结点可以把它以动到其他结点下.原理:把一个结点机器下面的所有结点在需要释放 ...

  7. 【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)

    原文:[ASP.NET Web API教程]3.3 通过WPF应用程序调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...

  8. 两道二分coming~

    第一道:poj 1905Expanding Rods 题意:两道墙(距离L)之间架一根棒子,棒子受热会变长,弯曲,长度变化满足公式( s=(1+n*C)*L),求的是弯曲的高度h. 首先来看这个图: ...

  9. MySQL 执行计划里的rows

    <pre name="code" class="html">SQL> alter session set statistics_level=a ...

  10. c++ __declspec关键字详细用法

    c++ __declspec关键字详细用法 __declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和C++语言的 ...