PKU 2528 WA
来源:互联网 发布:淘宝卖家回复语大全 编辑:程序博客网 时间:2024/05/17 03:23
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报
思路:这题数据范围很大,直接搞超时+超内存,需要离散化:
离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了
所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多
而这题的难点在于每个数字其实表示的是一个单位长度(并非一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱)
给出下面两个简单的例子应该能体现普通离散化的缺陷:
例子一:1-10 1-4 5-10
例子二:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4]
线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?
例子一是完全被覆盖掉了,而例子二没有被覆盖
思路:这题数据范围很大,直接搞超时+超内存,需要离散化:
离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了
所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多
而这题的难点在于每个数字其实表示的是一个单位长度(并非一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱)
给出下面两个简单的例子应该能体现普通离散化的缺陷:
例子一:1-10 1-4 5-10
例子二:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4]
线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?
例子一是完全被覆盖掉了,而例子二没有被覆盖
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.
线段树功能:update:成段替换 query:简单hash
WA代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1 const int maxn = 11111;bool hs[maxn];int li[maxn] , ri[maxn];int X[maxn*3];int col[maxn<<4];int cnt; void PushDown(int rt) {if (col[rt] != -1) {col[rt<<1] = col[rt<<1|1] = col[rt];col[rt] = -1;}}void update(int L,int R,int c,int l,int r,int rt) {if (L <= l && r <= R) {col[rt] = c;return ;}PushDown(rt);int m = (l + r) >> 1;if (L <= m) update(L , R , c , lson);if (m < R) update(L , R , c , rson);}void query(int l,int r,int rt) {if (col[rt] != -1) {if (!hs[col[rt]]) cnt ++;hs[ col[rt] ] = true;return ;}if (l == r) return ;int m = (l + r) >> 1;query(lson);query(rson);}int Bin(int key,int n,int X[]) {int l = 0 , r = n - 1;while (l <= r) {int m = (l + r) >> 1;if (X[m] == key) return m;if (X[m] < key) l = m + 1;else r = m - 1;}return -1;}int main() {int T , n;scanf("%d",&T);while (T --) {scanf("%d",&n);int nn = 0;for (int i = 0 ; i < n ; i ++) {scanf("%d%d",&li[i] , &ri[i]);X[nn++] = li[i];X[nn++] = ri[i];}sort(X , X + nn);int m = 1;for (int i = 1 ; i < nn; i ++) {if (X[i] != X[i-1]) X[m ++] = X[i];}for (int i = m - 1 ; i > 0 ; i --) {if (X[i] != X[i-1] + 1) X[m ++] = X[i-1] + 1;}sort(X , X + m);memset(col , -1 , sizeof(col));for (int i = 0 ; i < n ; i ++) {int l = Bin(li[i] , m , X);int r = Bin(ri[i] , m , X);update(l , r , i , 0 , m , 1);}cnt = 0;memset(hs , false , sizeof(hs));query(0 , m , 1);printf("%d\n",cnt);}return 0;}
搜索的代码:通过
#include<iostream>#include<algorithm>using namespace std;const int Max = 25000; struct{ int l, r; bool vis;}node[3*Max];struct data{ int pos, num;}post[Max];bool flag; bool cmp1(data &a, data &b){ if(a.pos < b.pos) return true; return false;} bool cmp2(data &a, data &b){ if(a.num > b.num) return true; if(a.num == b.num && a.pos < b.pos) return true; return false;} void BuildTree(int left, int right, int u){ // 建树。 node[u].l = left; node[u].r = right; node[u].vis = false; if(left == right) return; int mid = (left+right)>>1; BuildTree(left, mid, u<<1); BuildTree(mid+1, right, (u<<1)+1);} void query(int left, int right, int u){ // 查询。 if(node[u].vis == true) return; if(node[u].l == left && node[u].r == right){ node[u].vis = true; // 修改。 flag = true; // 对于新帖的海报,有区间可以让其露出来。 return; } if(right <= node[u<<1].r) query(left, right, u<<1); else if(left >= node[(u<<1)+1].l) query(left, right, (u<<1)+1); else{ query(left, node[u<<1].r, u<<1); query(node[(u<<1)+1].l, right, (u<<1)+1); } // *递归回来的时候,由于左右子结点性质的改变,必须对父结点信息进行相应的更改。 node[u].vis = node[u<<1].vis & node[(u<<1)+1].vis;} int main(){ int t, n, i; scanf("%d", &t); while(t --){ scanf("%d", &n); for(i = 0; i < n; i ++){ scanf("%d%d", &post[2*i].pos, &post[2*i+1].pos); post[2*i].num = post[2*i+1].num = i; } sort(post, post + 2*n, cmp1); int k = 0, pre = 0; // 离散化。 for(i = 0; i < 2*n; i ++){ if(post[i].pos != pre){ pre = post[i].pos; post[i].pos = ++ k; } else post[i].pos = k; } BuildTree(1, k, 1); sort(post, post + 2*n, cmp2); // 按贴的顺序从后往前排。 int ans = 0; for(i = 0; i < 2*n; i += 2){ int left = post[i].pos; int right = post[i+1].pos; flag = false; query(left, right, 1); if(flag) ans ++; } printf("%d\n", ans); } return 0;}
- PKU 2528 WA
- pku 2938 (wa wa wa wa......)
- wa
- WA~~~~~~~
- WA
- wa
- WA
- WA
- pku 2528
- pku 2528
- blas -- wa wa wa
- hdu1050 pku 1083 一组测试数据 我也wa n次了!
- PKU 2528 线段树
- PKU 2528 Mayor's posters
- wa jboss
- uva10004Bicoloring-WA
- Cup---wa
- 深搜 WA
- 百度地图API详解之公交导航
- 杭电ACM 2034 人见人爱A-B
- 最优二叉树&&哈夫曼编码
- URI encode与URL encode对空格的不同处理
- ZooKeeper编程
- PKU 2528 WA
- Shell 学习10(join命令)
- 2013开始了
- kinetics DAC数模转换模块
- Win2003 Computer Browser 服务没有自动启动,网络连接打不开。
- Toad 11.6 全版本下载
- 孙多洋的EQ3解说
- java tcp点对点聊天程序(单人/多人)
- 框架学习:ibatis框架和hibernate框架区别分析