Mayor's posters
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 46093 | Accepted: 13350 |
Description
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.
- Every candidate can place exactly one poster on the wall.
- All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
- The wall is divided into segments and the width of each segment is one byte.
- Each poster must completely cover a contiguous number of wall segments.
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.
Input
The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.
Output
For each input data set print the number of visible posters after all the posters are placed.
The picture below illustrates the case of the sample input.
The picture below illustrates the case of the sample input.
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4
Source
线段树离散化:
假设输入为[1 1000] [1000 9999999] 其中2 到999和1001 到 9999998用不到,需要离散化,因为本题到10000000,
按照线段树的开内存规则,需要开到40000000,这样必定MLE,所以,要离散化,
离散化前:1 1000 99999999
离散化后:1 2 3
注意:一开始我都是开的10010的数组,后来错了好几遍,然后看到别人开了20010的数组,然后修改后姐AC了。。。。。
#include <stdio.h>
#include <string.h>
#include <algorithm>
int d[20010 << 2];//线段树的数组
int sum[20010 << 2];//lazy数组
bool hs[20010 << 2];//哈希数组,统计最后有多少不同
int lsh[20000010];//离散化数组
int qc[20010 << 2];//输入并且去重数组
struct node
{
int a;
int b;
}ls[20010];//输入数据接收数组
int cnt;//统计最后有多少不同颜色
void upd(int s,int t,int us,int ut,int unum,int step)
{
if(s > ut || t < us)
return ;
if(s >= t)
{
d[step] = unum;
return ;
}
if(us <= s && t <= ut)
{
d[step] = unum;
sum[step] = unum;
return ;
}
if(sum[step])
{
sum[step << 1] = sum[step];
sum[step << 1 | 1] = sum[step];
d[step << 1] = sum[step];
d[step << 1 | 1] = sum[step];
sum[step] = 0;
}
int mid = (s + t) >> 1;
if(us <= mid)
upd(s,mid,us,ut,unum,step << 1);
if(ut > mid)
upd(mid + 1,t,us,ut,unum,step << 1 | 1);
if(d[step << 1] && d[step << 1] == d[step << 1 | 1])
d[step] = d[step << 1];
else
d[step] = 0;
}
void que(int s,int t,int qs,int qt,int step)
{
if(s > qt || t < qs)
return ;
if(s >= t)
{
if(!hs[d[step]])
{
hs[d[step]] = true;
cnt++;
}
return ;
}
if(qs <= s && t <= qt && d[step])
{
if(!hs[d[step]])
{
hs[d[step]] = true;
cnt++;
}
return ;
}
if(sum[step])
{
sum[step << 1] = sum[step];
sum[step << 1 | 1] = sum[step];
d[step << 1] = sum[step];
d[step << 1 | 1] = sum[step];
sum[step] = 0;
}
int mid = (s + t) >> 1;
if(qs <= mid)
que(s,mid,qs,qt,step << 1);
if(qt > mid)
que(mid + 1,t,qs,qt,step << 1 | 1);
}
int main()
{
int t,n,mn,mx,num;
while(~scanf("%d",&t))
{
while(t--)
{
num = 0;
mx = -1;
mn = 10000010;
memset(d,0,sizeof(d));
memset(hs,0,sizeof(hs));
memset(sum,0,sizeof(sum));
scanf("%d",&n);
for(int i = 0;i < n;i++)
{
scanf("%d%d",&ls[i].a,&ls[i].b);
qc[num++] = ls[i].a;
qc[num++] = ls[i].b;
if(mn > ls[i].a)
mn = ls[i].a;
if(mx < ls[i].b)
mx = ls[i].b;
}
std::sort(qc,qc + num);
lsh[qc[0]] = 1;
for(int i = 1;i < num;i++)
{
if(qc[i] > qc[i - 1])
lsh[qc[i]] = lsh[qc[i - 1]] + 1;
}
for(int i = 0;i < n;i++)
{
upd(lsh[mn],lsh[mx],lsh[ls[i].a],lsh[ls[i].b],i + 1,1);
}
cnt = 0;
que(lsh[mn],lsh[mx],lsh[mn],lsh[mx],1);
printf("%d\n",cnt);
}
}
return 0;
}