离散化 + 树状数组。

  这些东西自己都是刚接触不久的,所以需要多写点题练练手。


  

  

题目描述:

  一维坐标中有N条线段,其中有一个点上面覆盖的线段数是最多的,求该点上面的线段数目。

  这道题和HDU1556特别相似,不过这道题数据的值比较大,所以要离散化预处理一下数据。

  个人常用的离散化方法:先预存一下数据,然后用数组tmp[]存一下数据,对tmp[]数组排序,然后二分查找原数据在tmp[]数组中的下标,并且把下标作为离散化的数据。有一点比较方便的是,不需要去重。

  然后就是树状数组这部分,一维树状数组支持两种操作: 1. 单点更新,区间求和 ; 2 . 区间更新,单点求值。这两种操作的更新和求和这部分是反过来的,前者是对上更新,对下求值,后者是对下更新,对上求值。所以说树状数组比较好实现也容易推广到多维,但是功能不如线段树。

算法:

  用树状数组来存每个点的覆盖次数,覆盖一次即视为该点的数值+1;由于更新的时候是区间更新,所以对[a , b]这个区间覆盖的话,先把[1 , a-1]区间更新-1,然后把[1,b]区间更新+1,所以求最后所有点的最大值即可。

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
using namespace std;
const int maxn = 1 + ;
int c[maxn] , n , k;
int L[maxn][] , tmp[maxn];
int lowbit(int x)
{
return x & (-x);
}
void update(int x , int num)
{
while(x > ) {
c[x] += num;
x -= lowbit(x);
}
}
int getsum(int i)
{
int res = ;
while(i <= k) {
res += c[i];
i += lowbit(i);
}
return res;
}
int binary_Search(int a[] , int l , int r , int x)
{
int m = (l + r) >> ;
while(l <= r) {
if(a[m] == x)
return m;
if(a[m] < x)
l = m + ;
if(a[m] > x)
r = m;
m = (l + r) >> ;
}
return -;
}
int main()
{
int T , i , j;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(c , , sizeof(c));
for(i = , k = ; i <= n ; i++) {
scanf("%d %d" , &L[i][] , &L[i][]);
tmp[++k] = L[i][];
tmp[++k] = L[i][];
}
sort(tmp + , tmp + k +);
for(i = ; i <= n ; i++) {
for(j = ; j <= ; j++) {
int pos = binary_Search(tmp , , k , L[i][j]);
L[i][j] = pos;
}
}
for(i = ; i <= n ; i++) {
update(L[i][] - , -);
update(L[i][] , );
}
int max = ;
for(i = ; i <= k ; i++) {
if(getsum(max) < getsum(i))
max = i;
}
printf("%d\n",getsum(max));
}
return ;
}

  

  

HDU5124 lines的更多相关文章

  1. HDU5124:lines(线段树+离散化)或(离散化思想)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...

  2. extracting lines bases a list using awk

    extracting lines bases a list using awk awk 'NR==FNR{a[$1]=$0; next}($1 in a){print a[$1]"\n&qu ...

  3. Enum:Game of Lines(POJ 3668)

    画直线 题目大意:给定一些点集,要你找两点之间的连线不平行的有多少条 数据量比较少,直接暴力枚举,然后放到set查找即可 #include <iostream> #include < ...

  4. 我的常用mixin 之 lines

    /** * 最多显示 $lineCount 行 * lines * * example: * @include lines; * @include lines(3); */ @mixin lines( ...

  5. uva 1471 defence lines——yhx

    After the last war devastated your country, you - as the king of the land of Ardenia - decided it wa ...

  6. POJ 1269 Intersecting Lines --计算几何

    题意: 二维平面,给两条线段,判断形成的直线是否重合,或是相交于一点,或是不相交. 解法: 简单几何. 重合: 叉积为0,且一条线段的一个端点到另一条直线的距离为0 不相交: 不满足重合的情况下叉积为 ...

  7. POJ 1269 Intersecting Lines【判断直线相交】

    题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0) ...

  8. [CareerCup] 13.1 Print Last K Lines 打印最后K行

    13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最 ...

  9. Codeforces 593B Anton and Lines

    LINK time limit per test 1 second memory limit per test 256 megabytes input standard input output st ...

随机推荐

  1. Spring基本原理模拟(IoC部分)

    package ioc; import java.io.File; import java.lang.reflect.Method; import java.util.Collections; imp ...

  2. eclipse导出带有图片、音效、其他二进制文件的jar文件的经历

    先说下简单流程吧: 1.选中“项目”,右键->export->java的“jar file”->勾选“export generated clas files and resource ...

  3. 暴风魔镜SDK:MojingSDK For Unity V1.3.5112 (R).zip

    去年买了个暴风魔镜4,如今一直放在家里吃灰,这些天对Unity3D开发VR兴趣正浓,刚好公司项目不忙,花了几天玩玩暴风魔镜SDK,因为网上的资料不算多,暴风提供的文档也不太适合像我这样的Unity小白 ...

  4. Mysql实例参数优化15个主要参数讲解(原创)

    1.innodb_buffer_pool_size 设置物理内存的60%-80%,反应IO吞吐的最大上限2.innodb_thread_concurrency 线程并发,设置为CPU核心数,如果等于0 ...

  5. 牛客 PUBG

    题目链接:点击打开链接 题目大意:跑毒,跑到安全区,每个地方有敌人,输出路线经过的最少敌人的数量:-1是起点. -2是安全区 输入 5 6 6 0 -2 3 4 2 1 2 1 2 2 8 9 7 8 ...

  6. DHCP原理和配置

    在大型网络中,会有大量的主机和设备需要获取ip地址和网络参数,为了解决手动配置的工作量大.ip冲突问题,因此需要使用DHCP(dynamic host configuration protocol). ...

  7. 一些有关PyCharm使用总结

    目前在这里,你能看见 license server Python版本配置 添加另外版本的Python 设置字体大小 关于编码 关于模版 安装好之后,第一个问题就是 license server 问题, ...

  8. ProtocBuffer安装

    学习protocolhttp://www.jianshu.com/p/fa126a8535a0 mac安装protocbuff: 下边总结一下安装流程: 查看官方文档源码在 https://githu ...

  9. postgresql删除还有活动连接的数据库

    select pg_terminate_backend(pid) from pg_stat_activity where datname='testdb' and pid<>pg_back ...

  10. Tree--lecture08

    1.二叉树 完全二叉树(complete binary tree):除了最下面一层都是满的,最下面一层也是优先排列在左边.这样的话父亲节点和孩子节点就在序号上面有关系: 父亲节点为n,那么子节点的编号 ...