线段树 fzu2187 回家种地
来源:互联网 发布:sql union all 效率 编辑:程序博客网 时间:2024/05/01 13:58
一道很经典的面积并问题。
和普通的面积并相比,这个的面积并求的是只覆盖了一次的答案。
相对于普通的来说,就是求cnt==1的长度,而不是求cnt>=1的长度而已
我的想法是用S1维护cnt>=1的长度,S2维护cnt==1的长度
其他地方和普通的面积并一模一样,只是在push_up的时候稍微多了对S2的维护而已
#include<map>#include<set>#include<cmath>#include<queue>#include<cstdio>#include<string>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#include<functional>using namespace std;typedef long long LL;typedef pair<int, int> PII;const int MX = 2e5 + 5;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define root 1,rear,1int rear, cnt[MX << 2];int S1[MX << 2], S2[MX << 2], A[MX];struct Que { int sign; int top, L, R; bool operator<(const Que &b)const { return top < b.top; } Que() {} Que(int _top, int _L, int _R, int _sign) { top = _top; L = _L; R = _R; sign = _sign; }} Q[MX];int BS(int x) { int l = 1, r = rear, m; while(l <= r) { m = (l + r) >> 1; if(A[m] == x) return m; if(A[m] < x) l = m + 1; else r = m - 1; } return -1;}void push_up(int l, int r, int rt) { if(cnt[rt]) { S1[rt] = A[r + 1] - A[l]; if(cnt[rt] == 1) S2[rt] = S1[rt] - S1[rt << 1] - S1[rt << 1 | 1]; else S2[rt] = 0; } else if(l == r) S1[rt] = S2[rt] = 0; else { S1[rt] = S1[rt << 1] + S1[rt << 1 | 1]; S2[rt] = S2[rt << 1] + S2[rt << 1 | 1]; }}void update(int L, int R, int d, int l, int r, int rt) { if(L <= l && r <= R) { cnt[rt] += d; push_up(l, r, rt); return; } int m = (l + r) >> 1; if(L <= m) update(L, R, d, lson); if(R > m) update(L, R, d, rson); push_up(l, r, rt);}int main() { int n, T, ansk = 0; //freopen("input.txt", "r", stdin); scanf("%d", &T); while(T--) { rear = 0; memset(cnt, 0, sizeof(cnt)); memset(S1, 0, sizeof(S1)); memset(S2, 0, sizeof(S2)); scanf("%d", &n); for(int i = 1; i <= n; i++) { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); A[++rear] = x1, A[++rear] = x2; Q[i] = Que(y1, x1, x2, 1); Q[i + n] = Que(y2, x1, x2, -1); } sort(Q + 1, Q + 1 + 2 * n); sort(A + 1, A + 1 + rear); rear = unique(A + 1, A + 1 + rear) - A - 1; LL ans = 0; int last = 0; for(int i = 1; i <= 2 * n; i++) { ans += (LL)(Q[i].top - last) * S2[1]; update(BS(Q[i].L), BS(Q[i].R) - 1, Q[i].sign, root); last = Q[i].top; } printf("Case %d: %I64d\n", ++ansk, ans); } return 0;}
0 0
- 线段树 fzu2187 回家种地
- 英语不懂这几句,不如回家去种地!
- FZU 2187 回家种地(矩形面积并)
- 回家
- 回家
- 回家
- 回家
- 回家
- 回家
- 回家
- 回家
- 回家
- 回家!
- 回家
- 回家
- 回家
- 回家
- 回家
- 音乐播放器:添加了广播和后台服务、通知
- python2/3差异之——字符串差异
- 【POJ 1129】Channel Allocation
- C++对象的静态类型与动态类型
- initImageAppleJPEG:1512: falling back to libJPEG
- 线段树 fzu2187 回家种地
- 朴素贝叶斯公式形象化描述
- Linux裸设备管理详解
- Matlab Codes and Datasets for Feature Learning(转)
- 二维几何基础模板(一)
- 数据结构(6)二叉树
- 数据结构(7)平衡查找树之2-3树
- Android studio 使用心得(八)----测试程序单元测试
- iOS项目代码行数统计