HDU5124 lines
离散化 + 树状数组。
这些东西自己都是刚接触不久的,所以需要多写点题练练手。
题目描述:
一维坐标中有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的更多相关文章
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
- 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 ...
- Enum:Game of Lines(POJ 3668)
画直线 题目大意:给定一些点集,要你找两点之间的连线不平行的有多少条 数据量比较少,直接暴力枚举,然后放到set查找即可 #include <iostream> #include < ...
- 我的常用mixin 之 lines
/** * 最多显示 $lineCount 行 * lines * * example: * @include lines; * @include lines(3); */ @mixin lines( ...
- uva 1471 defence lines——yhx
After the last war devastated your country, you - as the king of the land of Ardenia - decided it wa ...
- POJ 1269 Intersecting Lines --计算几何
题意: 二维平面,给两条线段,判断形成的直线是否重合,或是相交于一点,或是不相交. 解法: 简单几何. 重合: 叉积为0,且一条线段的一个端点到另一条直线的距离为0 不相交: 不满足重合的情况下叉积为 ...
- POJ 1269 Intersecting Lines【判断直线相交】
题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0) ...
- [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行,最 ...
- Codeforces 593B Anton and Lines
LINK time limit per test 1 second memory limit per test 256 megabytes input standard input output st ...
随机推荐
- “Enterprise Architect”和数据库的不解之缘
前言 在这个大数据盛行的时代,和数据打交道变的必不可少了,所有如果有工具来规范我们的数据库会更加方便我们的生活.这次机房,我们利用EA(Enterprise Architect)自动生成SQL语句来达 ...
- yum 缓存包到本地
yum install –downloadonly –downloaddir=/root/mypackages/ vim 说明: --downloadonly 只下载 --downloaddir 下载 ...
- Windows安装IIS后,启动网站报错:不能在此路径中使用此配置节……
在IIS里启动设置好的网站(ASP.net网站),浏览器报如下错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的(overrideModeDefault= ...
- P1984 [SDOI2008]烧水问题(具体证明)
传送门 我见过的第二恶心的题,第一是糖果传递... 以下是一堆具体的证明,自己想的,可能考虑不周,不想看也可以直接看结论 首先有一个很显然的贪心,烧开的水要尽量把热量传递出去 所以有一个比较显然的方法 ...
- (转)TComboBox patch for Delphi 7
unit D7ComboBoxStringsGetPatch; // The patch fixes TCustomComboBoxStrings.Get method . interface {$I ...
- 025 Reverse Nodes in k-Group 每k个一组翻转链表
给出一个链表,一次翻转 k 个指针节点,并返回修改后的链表.k 是一个正整数,并且小于等于链表的长度.如果指针节点的数量不是 k 的整数倍,那么最后剩余的节点应当保持原来的样子.你不应该改变节点的值, ...
- win10的一些设置
win10进入安全模式: 左下角->设置->更新和安全->恢复->立即重启 (重启后进入的界面可以进行一些设置来进入安全模式) win10切换账户: 任务管理器->账户
- Unity 切换场景的时候让某个游戏对象不消失
DontDestroyOnLoad(要操作的GanmeObject); 放在Start方法里就行
- Java多线程与并发——生产者与消费者应用案例
多线程的开发中有一个最经典的操作案例,就是生产者-消费者,生产者不断生产产品,消费者不断取走产品. package com.vince; /** * 生产者与消费者案例 * @author Admin ...
- 存储过程 jdbc
package com.itheima.procedure; import java.sql.CallableStatement; import java.sql.Connection; import ...