题目链接: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. string的不可变性

    1.不可变性 代码如下: static void Main(string[] args){string str1 = "a";string str2 = str1;str2 = & ...

  2. EasyUI - Messager消息框

    全局设定: JavaScript代码: //设置按钮中的文字,默认是-ok/cancel ,可以任意设置文字,比如现在的-确认/取消 $.messager.defaults = { ok: '确认', ...

  3. AssertValid函数学�

    转自http://tsitao.blog.163.com/blog/static/29795822006914105840496/ VC的调试中,AssertValid和Dump函数的应用 CObje ...

  4. 节点地址的函数list_entry()原理详解

    本节中,我们继续讲解,在linux2.4内核下,如果通过一些列函数从路径名找到目标节点. 3.3.1)接下来查看chached_lookup()的代码(namei.c) [path_walk()> ...

  5. JQuery - 点击,滚动回到顶部 / 底部刷新回到顶部

    if ($(document).scrollTop() != 0) { //刷新之后,回到顶部 $('body,html').animate({ scrollTop: 0 }, 500); }

  6. JSP的学习(1)——基本知识与底层原理

    通过之前的学习,我们已经对Servlet有所了解,现在我们先来学习JSP,当能使用JSP进行友好的页面显示之后,再回去学习Servlet的其他高级特性会将整个学习很好的融入进来. JSP,即Java ...

  7. 【翻译】ASP.NET Web API是什么?

    原文 [翻译]ASP.NET Web API是什么? 说明:随微软ASP.NET MVC 4一起发布的还有一个框架,叫做ASP.NET Web API.目前国内关注这项技术的人似乎还很少,这方面的文章 ...

  8. [Android学习笔记]Fragment使用

    一.android.app.Fragment 与 android.support.v4.app.Fragment 区别 support.v4.app.Fragment是为了给低版本Android使用的 ...

  9. oracle系统包——dbms_random用法及order by 小结(转)

    dbms_random是一个可以生成随机数值或者字符串的程序包. 这个包有initialize().seed().terminate().value().normal().random().strin ...

  10. 14.6.3 Grouping DML Operations with Transactions 组DML操作

    14.6.3 Grouping DML Operations with Transactions 组DML操作 默认情况下,连接到MySQL server 开始是以启动自动提交模式, 会自动提交每条S ...