如三张海报为:1~10 1~4 6~10
离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10
第一张海报时:墙的1~4被染为1;
第二张海报时:墙的1~2被染为2,3~4仍为1;
第三张海报时:墙的3~4被染为3,1~2仍为2。
最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显不是这样,正确输出为3。
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 50053 | Accepted: 14536 |
Description
Input
Output

Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
代码:
/*=============================================================================
#
# Author: liangshu - cbam
#
# QQ : 756029571
#
# School : 哈尔滨理工大学
#
# Last modified: 2015-08-10 12:59
#
# Filename: B.cpp
#
# Description:
# The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF = 10010;
struct Tree
{
int l, r, c;
} tree[INF * 14];
struct Node
{
int val,num;
} node[INF<<2];
set<int>cnt;
int cmp(Node a, Node b)
{
return a.val < b.val;
}
void create(int t, int l, int r)
{
tree[t].l = l;
tree[t].r = r;
tree[t].c = 0;
if(l == r)
return ;
int a, b, mid = (l + r)>>1;
create(t<<1, l, mid);
create(t<<1|1, mid + 1, r);
}
void update(int t, int val, int l, int r)
{
if(tree[t].l >= l && tree[t].r <= r)
{
tree[t].c = val;
return ;
}
if(tree[t].c > 0)
{
tree[t<<1].c = tree[t].c;
tree[t<<1|1].c =tree[t].c;
tree[t].c = 0;
}
if(tree[t].l == tree[t].r)
return ;
int mid = (tree[t].l + tree[t].r) >>1;
// if(l <= mid)
// {
// update(t<<1, val, l, r);
// }
// if(mid < r)
// {
// update(t<<1|1, val, l, r);
// }
if( l > mid)update(t<<1 | 1, val, l, r);
else if(r <= mid)
update(t<<1, val, l, r);
else
{
update(t<<1,val, l, mid);
update(t<<1|1,val, mid + 1, r);
}
}
int flag[INF<<2];
int coun = 0;
void cal(int t)
{
if(tree[t].c > 0)
{
if(!flag[tree[t].c])
{
coun++;
flag[tree[t].c] = 1;
}
return ;
}
if(tree[t].l == tree[t].r)
return ;
cal(t<<1);
cal(t<<1|1);
}
int main()
{
int dict[INF][3];
int t;
cin>>t;
int n,tx;
while(t--)
{
memset(flag, 0, sizeof(flag));
memset(dict, 0 ,sizeof(dict));
coun = 0;
tx = 1;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &dict[i][0], &dict[i][1]);
node[2 * i - 1]. val = dict[i][0];
node[2 * i - 1].num = i;
node[2 * i].val = dict[i][1];
node[2 * i].num = -1 * i;
}
sort(node + 1, node + 2 * n + 1 , cmp);
if(n >= 2)
{
for(int i = 2; i <= 2 * n ; i += 1)
{
if(node[i].val - node[i-1].val > 1)
{
node[2 * n + tx].val = node[i].val - 1;
node[2 * n + 1 + tx].num = INF<<3;
tx++;
}
}
}
sort(node + 1, node + 2 * n + tx , cmp);
int x = 1;
dict[abs(node[1].num)][node[1].num > 0 ? 0 : 1] = x;
for(int i = 2; i <= 2 *n +tx -1 ; i++)
{
if(node[i].val != node[i-1].val)
{
if(node[i].num == INF<<3)
{
x++;
continue;
}
x++;
}
if(node[i].num > 0)
dict[node[i].num][0] = x;
else
dict[-1 * node[i].num][1] = x;
}
create(1, 1, x);
for(int i = 1; i<= n; i++)
{
update(1, i, dict[i][0], dict[i][1]);
}
cal(1);
printf("%d\n",coun );
}
return 0;
}
4
版权声明:本文为博主原创文章,未经博主允许不得转载。
poj 2528 Mayor's posters(线段树 离散化 区间更新 贴海报)
原文地址:http://blog.csdn.net/lsgqjh/article/details/47396949