当前位置: 代码迷 >> 综合 >> G - Mayor‘s posters
  详细解决方案

G - Mayor‘s posters

热度:87   发布时间:2023-12-04 14:24:53.0

原题

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 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.

样例

Sample

Inputcopy Outputcopy
1
5
1 4
2 6
8 10
3 4
7 10
4

思路

线段树模板套一套,改一改,主要是需要离散化一下海报的长度,不然容易炸线段树数组,另外要注意离散化的时候容易出问题,比如一般离散

1 10     ,1 3, 6  10

离散化后对应的分别为 1 对1,3对2 , 6对 3,10对4,这样算出来结果为只有两幅海报,但其实 第一份海报的3 到 6 之间露了出来,但离散化的时候去掉了,所以离散化的时候要对边边扩大一下下。

 

吐槽:我一开始一看题,感觉就是着色问题,然后又刚刚做了这道题  F - Count Color ,直接copy了过来 ,修改了一下,然后一直WA , 一直想不明白,还造了好多数据,等到洗澡的时候才想明白这个的颜色种类和 F - Count Color 这题不一样,F - Count Color这题只有30个,可以用二进制方式存储,这道题有一万种颜色,二进制明显存不下,要更换修改方式、

代码

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;const int N=1e5+5;
int v[8*N];
int lazy[8*N];
int vis[N];void build(int l, int r, int x) {if(l == r) {v[x]=0;return ;}int mid = (l + r) >> 1;build(l, mid, x << 1) ;build(mid + 1, r, x << 1 | 1) ;
}void push_down(int x) {v[x << 1] = v[x << 1 | 1]=v[x];v[x]=0;
}void modify(int l, int r, int pl, int pr, int p, int color) {if( pl == l && pr == r ) {v[p]= color ;return ;}if(v[p])push_down(p);int mid = l + r >> 1;if (pl <= mid) modify(l, mid, pl, min(mid, pr), p << 1, color);if (pr > mid) modify(mid+1, r, max(mid+1, pl), pr, p << 1 | 1, color);
}void query(int l, int r, int ql, int qr, int k) {if( l == r || v[k] != 0) {vis[ v[k] ] =1;return ;}int mid = l + r >> 1;if(ql <= mid )  query(l, mid, ql, min(mid, qr), k << 1);if(qr > mid)   query(mid+1, r, max(mid+1, ql), qr, k << 1 | 1);return ;
}int Find[4*N];
int li[N],ri[N];
int m,n;
int c[4*N];int main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> m;while(m--) {cin >> n;int q=0; for(int i = 1; i <= n; i++ ) {cin >> li[i] >> ri[i];Find[++q] = li[i];Find[++q] = ri[i];}sort(Find+1, Find+1+q );int len = unique(Find+1, Find+1+q)-1-Find;int llen = len;memset(c,0,sizeof(c));for(int i = 1; i<= llen; i++) {if(i > 1 && Find[i] - Find[i - 1] > 1) {c[i] = c[i - 1] + 2;len++;}else c[i] = c[i - 1] +1;	}build(1, len, 1);for(int i = 1; i <= n; i++) {int l = lower_bound(Find+1, Find+1+llen, li[i])-Find;int r = lower_bound(Find+1, Find+1+llen, ri[i])-Find;l = c[l];r = c[r];modify(1, len, l, r, 1, i);}memset(vis,0,sizeof(vis));query(1, len, 1, len, 1);int cnt=0;for(int i=1; i <= n; i++) {if(vis[i] == 1)	cnt++;}cout<<cnt<<endl;}return 0;
}