题目链接: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. POJ 2250(最长公共子序列 变形)

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  2. Linux下SSH Session复制

    羡慕Windows下secureCRT的Session Copy功能,一直在寻找Linux下类似的软件,殊不知SSH本身就支持此功能. 特别感谢阿干同学的邮件分享. 详细方法 ? 1 2 3 4 Li ...

  3. java学习笔记-继承中super关键字

    背景: 在java继承的概念中我们得知,被声明为私有的类成员对所属的类来说仍然是私有的.类之外的任何代码都不能访问,包括子类. super关键字的两种用法: 1.用于调用超类的构造函数: 2.用于访问 ...

  4. Linux 安装ibus极点五笔输入法备忘录

    Linux 安装 ibus 五笔输入法备忘录 useful?: https://github.com/definite/ibus-table-chinese 一. yum install ibus* ...

  5. 基于visual Studio2013解决面试题之0601二叉树深度

     题目

  6. HDU 4070 Phage War

    贪心,t 大的放到前面...因为感染所有cell需要的phage的总数是一定的,所以产生phage需要的时间是一定的,只需要考虑用来感染的时间,这样考虑的话,把 t 小的放后面的话,可以发现总时间的最 ...

  7. Unity3D NGUI,uGUI总结

    跪求官方UI系统(2014年11月底已出,用原生的比用NGUI放心) uGUI注意点 1.要防止多个canvas叠加点击穿透,canvas里面的graphics raycaster调整到恰当选项 2. ...

  8. 国际化之DateFormat、NumberFormat

    之所以在国际化中介绍DateFormat和NumberFormat这两个类,一是因为本身这两个类是地区敏感类,即可用传入Locale对象:二是因为这两个类具有不同的输出模式,而这些模式能在国际化的动态 ...

  9. Linux Shell常用技巧(一) RE

    一.    特殊文件: /dev/null和/dev/tty Linux系统提供了两个对Shell编程非常有用的特殊文件,/dev/null和/dev/tty.其中/dev/null将会丢掉所有写入它 ...

  10. windows系统port监听

    通常情况下.假设想发现全部已经使用的和正在监听的port,我们能够使用netstat命令. netstat并不是一个port扫描工具.假设你想扫描计算机开放了哪些port的话.建议使用本文介绍的方法. ...