Lightoj1120线段树扫描线【模板】
来源:互联网 发布:海贼王 居鲁士 知乎 编辑:程序博客网 时间:2024/06/04 19:26
算是最基础的线段树扫描线了把。
注意横坐标区间如果是[1, 3]实际长度是2,维护是两个点而不是3个。
#include<stdio.h>#include<string.h>#include<math.h>#include<queue>#include<vector>#include<map>#include<set>#include<algorithm>#include<list>using namespace std;typedef pair<int,int> PII;typedef long long LL;#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1const int INF = 0x3f3f3f3f;const int Maxn = 33333 + 10;int x[Maxn<<1], n, sum[Maxn<<3], cnt[Maxn<<3], x_num, line_num;LL ans;struct Line{ int l, r, h, s; Line(){} Line(int ll, int rr, int hh ,int ss):l(ll), r(rr), h(hh), s(ss){} bool operator < (const Line &l) const{ return h < l.h; }}L[Maxn<<1];void build(){ memset(sum, 0, sizeof(sum)); memset(cnt, 0, sizeof(cnt)); x_num = line_num = 0; ans = 0;}void pushUp(int rt, int l, int r){ if(cnt[rt]) sum[rt] = x[r+1] - x[l]; else if(l == r) sum[rt] = 0; else sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void update(int L, int R, int s, int l, int r, int rt){ if(L <= l && R >= r){ cnt[rt] += s; pushUp(rt, l, r); return; } int m = (l+r) >> 1; if(m >= R) update(L, R, s, lson); else if(m < L) update(L, R, s, rson); else{ update(L, m, s, lson); update(m+1, R, s, rson); } pushUp(rt, l, r);}int search(int k){ int l = 1, r = x_num, m; while(l<=r){ m = (l + r) >> 1; if(x[m] == k) return m; else if(k < x[m]) r = m - 1; else l = m + 1; } return -1;}int main(){ int T, cas=1; scanf("%d", &T); while(T--){ scanf("%d", &n); build(); for(int i=0;i<n;i++){ int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); x[++x_num] = a; x[++x_num] = c; L[line_num++] = Line(a, c, b, 1); L[line_num++] = Line(a, c, d, -1); } sort(x+1, x+x_num+1); sort(L, L+line_num); int temp = 1; for(int i=2;i<=x_num;i++){ if(x[i] != x[i-1]) x[++temp] = x[i]; } ans = 0; x_num = temp; for(int i=0;i<line_num;i++){ int l = search(L[i].l); int r = search(L[i].r)-1; if(!i) update(l, r, L[i].s, 1, x_num, 1); else{ ans += (LL)sum[1]*(L[i].h - L[i-1].h); update(l, r, L[i].s, 1, x_num, 1); } } printf("Case %d: %lld\n", cas++, ans); } return 0;}
阅读全文
0 0
- Lightoj1120线段树扫描线【模板】
- 线段树扫描线模板
- POJ 1151 线段树 扫描线 模板题
- POJ 1177 线段树 扫描线 模板题
- 线段树[扫描线]
- 线段树+扫描线
- 线段树 扫描线
- 线段树扫描线
- 线段树-扫描线
- hdu4007 线段树扫描线
- poj2482 线段树扫描线
- poj2464 线段树扫描线
- ural1707 线段树扫描线
- hdu4419 线段树扫描线
- hdu1828线段树+扫描线
- POJ1177+线段树+扫描线
- POJ1151+线段树+扫描线
- HDU3255(线段树+扫描线)
- 数据结构 线性表的顺序结构
- wireshark抓包分析tcp
- 有用的插件
- 皮尔逊相关系数 和 R^2
- Android自动化测试(二)
- Lightoj1120线段树扫描线【模板】
- Python :assert断言使用
- 【嵌入式学习历程6】 指针(1)
- 二叉树的构造与遍历
- Java集合(五)- HashTable详解
- ATOM 安装+配置炫酷的activate-power-mode..
- FFMPEG视音频编解码资料收集
- 算法分析设计与分析-蛮力法(3)--顺序查找
- python基础-生成包、导入模块、包下_init_模块(__all__方法)