hdoj 5124 lines 【离散化 + 线段树】
2015-11-08 12:40
387 查看
linesTime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1354 Accepted Submission(s): 560 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 integer T(1≤T≤100)(the data for N>100 less than 11 cases),indicating the number of test cases. Each test case begins with an integer N(1≤N≤105),indicating the number of lines. Next N lines contains two integers Xi and Yi(1≤Xi≤Yi≤109),describing a line. Output For each case, output an integer means how many lines cover A. Sample Input 2 5 1 2 2 2 2 4 3 4 5 1000 5 1 1 2 2 3 3 4 4 5 5 Sample Output 3 1 |
先把坐标离散化,然后就是线段树区间更新维护区间最大值了。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <vector> #define INF 0x3f3f3f3f #define eps 1e-8 #define MAXN (100000+10) #define MAXM (50000000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; struct Tree { int l, r; int Max; int lazy; }; Tree tree[MAXN<<2]; int x[MAXN], y[MAXN]; void PushUp(int o){ tree[o].Max = max(tree[ll].Max, tree[rr].Max); } void PushDown(int o) { if(tree[o].lazy) { tree[ll].lazy += tree[o].lazy; tree[rr].lazy += tree[o].lazy; tree[ll].Max += tree[o].lazy; tree[rr].Max += tree[o].lazy; tree[o].lazy = 0; } } void build(int o, int l, int r) { tree[o].Max = tree[o].lazy = 0; tree[o].l = l, tree[o].r = r; if(l == r) return ; int mid = (l + r) >> 1; build(lson); build(rson); } int rec[MAXN*2]; int Find(int val, int l, int r) { while(r >= l) { int mid = (l + r) >> 1; if(rec[mid] == val) return mid; else if(rec[mid] > val) r = mid-1; else l = mid+1; } } void update(int o, int L, int R, int v) { if(L <= tree[o].l && R >= tree[o].r) { tree[o].lazy += v; tree[o].Max += v; return ; } PushDown(o); int mid = (tree[o].l + tree[o].r) >> 1; if(R <= mid) update(ll, L, R, v); else if(L > mid) update(rr, L, R, v); else { update(ll, L, mid, v); update(rr, mid+1, R, v); } PushUp(o); } int main() { int t; Ri(t); W(t) { int n; Ri(n); int len = 1; for(int i = 0; i < n; i++) { Ri(x[i]); Ri(y[i]); rec[len++] = x[i]; rec[len++] = y[i]; } sort(rec+1, rec+len); int R = 2; for(int i = 2; i < len; i++) if(rec[i] != rec[i-1]) rec[R++] = rec[i]; sort(rec+1, rec+R); build(1, 1, R-1); for(int i = 0; i < n; i++) { if(x[i] > y[i]) swap(x[i], y[i]); int l = Find(x[i], 1, R-1); int r = Find(y[i], 1, R-1); update(1, l, r, 1); } printf("%d\n", tree[1].Max); } return 0; }
相关文章推荐
- Codeforces 592D Super M 【求解包含若干个点的最小树 + 树的直径】
- Codeforces 593B Anton and Lines 【思维】
- Codeforces 593A 2Char 【思维】
- Dom4j介绍与接口说明
- IMP-00038:无法转换为环境字符集句柄
- lightoj 1410 - Consistent Verdicts 【思维】
- lightoj 1369 - Answering Queries 【思维】
- lightoj 1354 - IP Checking 【进制水题】
- lightoj 1297 - Largest Box 【函数最值】
- lightoj 1294 - Positive Negative Sign 【基础计数】
- lightoj 1349 - Aladdin and the Optimal Invitation 【中位数的运用】
- lightoj 1311 - Unlucky Bird 【物理基础】
- HDU 5514 Frogs (2015沈阳F题&&容斥+剪枝)
- Linux_NetworkManager_RHEL7
- Oracle dmp文件导入(还原)到不同的表空间和不同的用户下
- 【DirectX 9.0c入门教程】之一 开发环境搭建:安装vs2008 sp1和DirectX SDK
- lightoj 1319 - Monkey Tradition 【CRT】
- mysql学习笔记(3)
- lightoj 1179 - Josephus Problem 【约瑟夫环】
- 驱动开发(4)内核中的内存分配和错误码