标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 46857 | Accepted: 13601 |
Description
Input
Output
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
Source
题意:n张海报,贴在一张墙上,现给出这n张海报的初始位置和终点位置,问在贴完这n张海报之后,能在墙上看到几张海报(因为存在全部覆盖的情况)。
因为数据给的太大,用普通的线段树一定会超时和超内存,所以需要用到离散化,离散化其实现在看来就是把需要用到的数据经过处理对应到线段树的每个区间去。这样可以减少很多的区间浪费。
代码:
<span style="font-size:18px;">#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include<stdlib.h> using namespace std; const int manx = 10100; int v[11000]; struct xian { int a,b; int key; } point[manx]; struct node { int l,r; int key; bool flag; } q[manx<<4]; int ans; int data1[manx<<1]; int data2[manx<<2]; void Push_down(int rt) { if(q[rt].flag) { q[rt<<1].flag = q[rt<<1|1].flag = true; q[rt<<1].key = q[rt<<1|1].key = q[rt].key; q[rt].flag = false; } } void build(int l, int r, int rt) { q[rt].l = data2[l]; q[rt].r = data2[r]; q[rt].key = -1; q[rt].flag = false; if(l == r) { return; } int mid = (l + r)>>1; build(l , mid , rt<<1); build(mid+1, r, rt<<1|1); } void query(int l, int r, int rt) { if(q[rt].l == q[rt].r) { if(v[q[rt].key] == 0 && q[rt].key != -1) { ans++; v[q[rt].key] = 1; } return; } Push_down(rt); int mid = (l + r)>>1; query(l , mid , rt<<1); query(mid+1, r, rt<<1|1); } void updata(int ll, int rr, int id, int l, int r, int rt) { if(rr < q[rt].l || ll > q[rt].r) return; if(ll<=q[rt].l && rr>=q[rt].r) { q[rt].key = id; q[rt].flag = true; return; } Push_down(rt); int mid = (l + r) >> 1; updata(ll, rr, id, l , mid , rt<<1); updata(ll, rr, id, mid+1, r, rt<<1|1); } int main() { int t; scanf("%d",&t); while(t--) { memset(v,0,sizeof(v)); int n; scanf("%d",&n); int tem = 0; for(int i=1; i<=n; i++) { scanf("%d%d",&point[i].a,&point[i].b); point[i].key = i; data1[++tem] = point[i].a; data1[++tem] = point[i].b; } sort(data1+1,data1+tem+1); int w = 0; data1[0] = -1; for(int i=1; i<=tem; i++) { if(data1[i] != data1[i-1]) { data1[++w] = data1[i]; } } tem = w; w = 0; data2[++w] = data1[1]; for(int i=2; i<=tem; i++) { if(data1[i] > data1[i-1] + 1) { data2[++w] = data1[i] - 1; data2[++w] = data1[i]; } else { data2[++w] = data1[i]; } } build(1,w,1); for(int i=1; i<=n; i++) { updata(point[i].a, point[i].b, point[i].key, 1, w, 1); } ans = 0; query(1, w ,1); printf("%d\n",ans); } return 0; } </span>
POJ 2528 Mayor's posters(线段树+离散化)
标签:
原文地址:http://blog.csdn.net/yeguxin/article/details/44938361