Mayor's posters线段树之区间更新+离散化
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 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.
![]()
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
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int N=1e5+10; int a[N],f[N<<2],lazy[N<<2]; int ans,le[N],ri[N]; bool vis[N<<2]; void build(int root,int left,int right){ if(left==right){ f[root]=0; return; } int mid=(left+right)>>1; int rt=root<<1; build(rt,left,mid); build(rt+1,mid+1,right); } void pushdown(int root,int left,int right){ int rt=root<<1; int mid=(left+right)>>1; // if(f[root]==0){ // return; // } f[rt]=f[root]; f[rt+1]=f[root]; f[root]=0; } void update(int root,int left,int right,int uleft,int uright,int val){ int rt=root<<1; int mid=(left+right)>>1; if(uleft>right||left>uright){ return ; } if(uleft<=left&&uright>=right){ f[root]=val; return; } if(f[root]!=0){ pushdown(root,left,right); } if(uleft<=mid){ update(rt,left,mid,uleft,uright,val); } if(uright>mid){ update(rt+1,mid+1,right,uleft,uright,val); } } void query(int root,int left,int right){ int rt=root<<1; int mid=(left+right)>>1; if(f[root]!=0){ if(!vis[f[root]]){ ++ans; //printf("$$\n"); vis[f[root]]=true; } return; } if(left==right){ return; } if(f[root]!=0){ pushdown(root,left,right); } query(rt,left,mid); query(rt+1,mid+1,right); } int main(){ int T, n; scanf("%d",&T); while(T--){ scanf("%d",&n); build(1,1,n); int len = 0; for(int i = 1; i <= n; ++i){ scanf("%d%d",&le[i],&ri[i]); lazy[len++] = le[i]; lazy[len++] = ri[i]; } // 离散化 sort(lazy, lazy+len); // for(int i=1;i<=len;i++){ // printf("%d ",lazy[i]); // } len = unique(lazy, lazy+len) - lazy; //unique是 c++标准模板库STL中十分实用的函数之一,使用此函数 //需要#include <algorithm>头文件 //该函数的作用是“去除”容器或者数组中相邻元素的重复出现的元素 //(1) 这里的去除并非真正意义的erase,而是将重复的元素放到容器的末尾,返回值是去重之后的尾地址。 int m = len; for(int i = 0; i < m-1; ++i){ if(lazy[i+1] - lazy[i] > 1) { lazy[len++] = lazy[i] + 1; } } sort(lazy, lazy+len); for(int i = 1; i <= n; ++i){ int a = lower_bound(lazy, lazy+len, le[i]) - lazy+1; int b = lower_bound(lazy, lazy+len, ri[i]) - lazy+1; update(1,1,len,a, b, i); } ans = 0; memset(vis, 0, sizeof(vis)); // printf("**\n"); query(1, 1, len); printf("%d\n", ans); } return 0; }
离散化:在不改变数据相对大小的条件下,对数据进行相应的缩小。当数据只与它们之间的相对大小有关,而与具体是多少无关时,可以进行离散化。
在本题中离散化的价值就体现在缩小len 的值,减少遍历的时间消耗。
int C[MAXN], L[MAXN]; // 在main函数中... memcpy(C, A, sizeof(A)); // 复制 sort(C, C + n); // 排序 int l = unique(C, C + n) - C; // 去重
for (int i = 0; i < n; ++i){ L[i] = lower_bound(C, C + l, A[i]) - C + 1; // 查找
}

浙公网安备 33010602011771号