题目来源:http://bailian.openjudge.cn/practice/2528?lang=en_US
题意:简化版题意:给你一个长为10000000(1e7)的线段(因为高度都一样 可以无视高度 只看成线段)。然后给定区间让你涂色,每次涂色颜色都不一样,同一位置后涂的颜色会覆盖掉之前涂的,涂完所有颜色,问最后能看到多少种颜色。
分析:区间操作,用线段树的区间修改。但分析数据量,线段总长度是1e7,但是给的染色次数没有那么大,所以要用到离散化。离散化不太明白的请移步:https://blog.youkuaiyun.com/weixin_43061009/article/details/82083983。看完离散化再回来。一定要回来啊!
离散化之后就是区间操作了,flag代表区间内颜色,所有颜色最先初始化为-1代表没颜色,若为0代表某段区间有多种颜色,若不为0代表区间内只有这一种颜色,修改(染色)的时候要用lazy标记(剪枝),查询颜色总数的时候,flag为正数时,对应的颜色就赋值1(需要一个con数组来记录该颜色是否存在)。最后遍历con数组,只要是1就sum++即可。
刚开始没离散化,T了,今天加上离散化就过了。
AC代码
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>//果然是原来的就写对了 只不过再加上离散化就对了~~~
#define maxn 20010//这里记得刚开始的答案不能赋值1 不能直接复制后面的题 颜色初始化为-1 且当有颜色的时候才能记录!
#define LL long long
using namespace std;//加个离散化可能就不T了!!!
int m;//数量 操作数 颜色数
int b[maxn<<1],con[maxn];//b记录所有的端点信息
struct node0{//记录区间
int l,r;
}a[maxn],lsh[maxn];//a是最初的区间值 lsh是离散化之后的区间值
struct node {
int l,r,flag,lazy;//flag标记是相同还是不同 相同标为颜色号 不同标为0
} t[maxn<<2];
void init() {
memset(con,0,sizeof(con));
memset(t,0,sizeof(t));
}
void pushup(int k) { //只有两个颜色相同且两个都不是0的时候 父节点也是那个颜色
if(t[k<<1].flag==t[k<<1|1].flag&&t[k<<1].flag!=0) {
t[k].flag=t[k<<1].flag;
} else {
t[k].flag=0;
}
}
void build(int k,int l,int r) {
t[k].l=l,t[k].r=r;
if(l==r) {
t[k].flag=-1;
} else {
int m=(l+r)>>1;
build(k<<1,l,m);
build(k<<1|1,m+1,r);
pushup(k);
}
}
int pushdown(int k) {
if(t[k].lazy) {
t[k<<1].flag=t[k].flag;
t[k<<1|1].flag=t[k].flag;
t[k<<1].lazy=t[k].lazy;
t[k<<1|1].lazy=t[k].lazy;
t[k].lazy=0;
}
}
void updata(int k,int v,int L,int R) {
if(L<=t[k].l&&t[k].r<=R) {
t[k].flag=v;//染色
t[k].lazy=v;
} else {
pushdown(k);
int m=(t[k].l+t[k].r)>>1;
if(L<=m) updata(k<<1,v,L,R);
if(m<R) updata(k<<1|1,v,L,R);
pushup(k);
}
}
int query(int k,int L,int R) {
if(L<=t[k].l&&t[k].r<=R) {
if(t[k].flag==0) { //说明下面不是同一种颜色
query(k<<1,L,R);
query(k<<1|1,L,R);
} else {
if(t[k].flag!=-1)
con[t[k].flag]=1;
}
} else {
pushdown(k);
int m=(t[k].l+t[k].r)>>1;
if(L<=m) query(k<<1,L,R);
if(m<R) query(k<<1|1,L,R);
pushup(k);
}
}
int Dis(){//离散化
}
int main() {
int T;
cin>>T;
while(T--) {
scanf("%d",&m);
init();
build(1,1,maxn);
int cnt=0;
for(int i=1; i<=m; i++) {
scanf("%d%d",&a[i].l,&a[i].r);//输入所有边的信息
b[++cnt]=a[i].l;
b[++cnt]=a[i].r;
}
sort(b+1,b+cnt+1);
int size=unique(b+1,b+cnt+1)-(b+1);
for(int i=1;i<=m;i++){
lsh[i].l=lower_bound(b+1,b+size+1,a[i].l)-b;
lsh[i].r=lower_bound(b+1,b+size+1,a[i].r)-b;
}
for(int i=1;i<=m;i++){
updata(1,i,lsh[i].l,lsh[i].r);
}
query(1,1,maxn);
int sum=0;
for(int i=1;i<=maxn;i++){
if(con[i]!=0){
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}
/*
1
5
1 100
2 99
3 98
4 97
5 96
*/
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:
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 l i 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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+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.
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4