【CUGBACM15级BC第20场 B】hdu 5124 lines
来源:互联网 发布:nodejs json解析 编辑:程序博客网 时间:2024/06/06 09:18
lines
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1837 Accepted Submission(s): 782
Total Submission(s): 1837 Accepted Submission(s): 782
Problem Description
John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A.
Input
The first line contains a single integerT(1≤T≤100) (the data for N>100 less than 11 cases),indicating the number of test cases.
Each test case begins with an integerN(1≤N≤105) ,indicating the number of lines.
Next N lines contains two integersXi and Yi(1≤Xi≤Yi≤109) ,describing a line.
Each test case begins with an integer
Next N lines contains two integers
Output
For each case, output an integer means how many lines cover A.
Sample Input
251 2 2 22 43 45 100051 12 23 34 45 5
Sample Output
31
题意:x轴上很多段区间,求覆盖最多的一个点的覆盖次数
思路:首先很容易想到这个点一定可以是某一段区间的端点,然后这里给了区间,要求一个点的出现次数,想到什么?
没错,区间更新,单点求和。 当然用线段树更加直观,只是本人确实不是很喜欢线段树...于是就用树状数组实现了
与单点求和,区间求和的区别在于更新区间的时候add(l,1)和add(r+1,-1) 单点求和就是直接sum(i)就可以了
记得要离散化处理
#include <iostream>#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <bitset>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <algorithm>#include <functional>#define PI acos(-1)#define eps 1e-8#define inf 0x3f3f3f3f#define debug(x) cout<<"---"<<x<<"---"<<endltypedef long long ll;using namespace std;const int N = 1e5 + 10;int a[N], b[N], p[2 * N], maxn;int lowbit(int x){ return x & (-x);}///区间修改+单点查询int arr[N * 2];inline int sum(int x){ int res = 0; while (x) { res += arr[x], x -= lowbit(x); } return res;}inline void add(int x, int n){ while (x < maxn) { arr[x] += n, x += lowbit(x); }}inline void update(int x, int y, int n)///区间修改{ add(x, n); add(y + 1, -n);}int main(){ std::ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; cin >> n; memset(arr, 0, sizeof(arr)); maxn = 2 * n; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; p[i] = a[i]; p[i + n] = b[i]; } sort(p + 1, p + 1 + 2 * n); for (int i = 1; i <= n; i++) { int l = lower_bound(p + 1, p + 1 + 2 * n, a[i]) - p; int r = lower_bound(p + 1, p + 1 + 2 * n, b[i]) - p; update(l, r, 1); } int ans = 1; for (int i = 1; i <= 2 * n; i++) { int s = sum(i); ans = max(ans, s); } cout << ans << endl; } return 0;}
阅读全文
0 0
- 【CUGBACM15级BC第20场 B】hdu 5124 lines
- 【CUGBACM15级BC第15场 B】hdu 5082 Instruction
- 【CUGBACM15级BC第17场 B】hdu 5101 Select
- 【CUGBACM15级BC第26场 B】hdu 5159 Card
- 【CUGBACM15级BC第28场 B】hdu 5167 Fibonacci
- 【CUGBACM15级BC第36场 B】hdu 5199 Gunner
- 【CUGBACM15级BC第12场 B】hdu 5059 Help him
- 【CUGBACM15级BC第11场 B】hdu 5055 Bob and math problem
- 【CUGBACM15级BC第13场 B】hdu 5063 Operation the Sequence
- 【CUGBACM15级BC第18场 B】hdu 5105 Math Problem
- 【CUGBACM15级BC第22场 B】hdu 5143 NPY and arithmetic progression
- 【CUGBACM15级BC第23场 B】hdu 5147 Sequence II
- 【CUGBACM15级BC第25场 B】hdu 5155 Harry And Magic Box
- 【CUGBACM15级BC第27场 B】hdu 5163 Taking Bus
- 【CUGBACM15级BC第10场 B】hdu 5019 Revenge of GCD
- 【CUGBACM15级BC第8场 B】hdu 4990 Reading comprehension
- 【CUGBACM15级BC第7场 B】hdu 4986 Little Pony and Alohomora Part I
- 【CUGBACM15级BC第29场 B】hdu 5171 GTY's birthday gift
- 算法系列——开篇及目录
- COGS 1473 O(N*logN) 高精乘 FFT
- 数据结构
- 逻辑代数的基本运算
- [第七季]6.实现DIV的显示和隐藏
- 【CUGBACM15级BC第20场 B】hdu 5124 lines
- 【笔记篇】C#笔记1
- 1.14 C# 集合(2)
- COM组件非常好的分析文章
- 高并发网站技术架构
- ARP欺骗获取同网段信息
- 随笔小感,或许就叫一见误终身
- activity学习2--安装插件
- JS中最经典的全局变量和局部变量问题