离散化 + 树状数组。

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


  

  

题目描述:

  一维坐标中有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. 多个VSTO解决方案间传输变量

    参照微软官方文档,为需要公开参数的解决方案添加公开接口.不过稍显遗憾的是,不能传输List泛型 https://docs.microsoft.com/zh-cn/visualstudio/vsto/c ...

  2. Linux vim常用选项

    Vim命令 三种模式 Command Mode 命令模式 Insert Mode 插入模式 Last LineMode 底行模式 底行模式常用命令 :w 保存 :q 退出 :! + 命令 :! ls ...

  3. SAS笔记(4) FIRST.和LAST.临时变量

    FIRST.和LAST.临时变量是SAS很有特色的一点,我在R和Python中暂时没有发现类似的功能(也许它们也有这个功能,我不知道而已).考虑这样一种场景:我们有患者就诊的数据,每一条观测对应一个患 ...

  4. QQ音乐MP3下载

    QQ音乐MP3下载 没错本次写的内容的对象是我们熟知的QQ Music. 本篇文章涉及内容包括:Python,爬虫,json解析,request 库的使用 缘起 前几天刷B站无意中又刷到了一首神曲,“ ...

  5. 阿里maven镜像配置

    setting.xml<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven< ...

  6. A. The Fair Nut and Elevator(暴力)

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

  7. codeforces C. Vasya And The Mushrooms (思维+模拟)

    题意:给定一个2*n的矩形方格,每个格子有一个权值,从(0,0)开始出发,要求遍历完整个网格(不能重复走一个格子),求最大权值和,(权值和是按照step*w累加,step步数从0开始). 转载: 题解 ...

  8. NIO2

    Files工具包下包含创建临时文件, 文件copy, move等API Path tempFile = Files.createTempFile(null, ".tmp"); // ...

  9. Linux重新挂载磁盘

    Linux下磁盘和目录的概念与WIN不同:比如,分了一个系统分区默认挂载了根(/)目录,根下还有其它目录,比如/user /lib等.如果系统分区不够用,可以再分出分支,把根下其它目录分别挂载出来,例 ...

  10. windows下apache服务器的下载,安装,配置

    1.进行apache官网->Download->Files for Microsoft Windows->ApacheHaus,然后选择合适的版本下载 2.将下载下来的压缩包解压到合 ...