Link: Mayor's posters
Add posters to the wall in sequence. You can overlap and ask how many posters you can see at the end. (The covered posters are invisible)
Note:
Because the data is relatively large, it is not discretization, and the memory is definitely exploding.
There is also, not just discretization, but better boundary processing.
For example
4
2 10.
2 8
3 6
6 8
8 10
After discretization
2 3 6 8 10
1 2 3 4 5
Overwrite
Sections 1 5 and 1 4
Only 2 3, 3 4, 4 5
The answer is 3.
But the correct answer is 4.
Therefore, the border must be processed during discretization. If the spacing between adjacent numbers is greater than 1, add 1 to it.
AC code
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#define max(a,b) (a>b)?a:b#define min(a,b) (a>b)?b:aconst int maxn = 110000;using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define MAX INT_MAX#define MIN INT_MINbool vis[maxn];int zuo[maxn] , you[maxn];int dian[maxn<<2];int lazy[maxn<<4];int sum[maxn<<2];int lisan[maxn<<2];int ans = 0;int cmp(const void *a,const void *b){ return *(int *)a - *(int *)b;}void pushup(int rt){ sum[rt] = sum[rt<<1|1] = sum[rt<<1] ;}void pushdown(int rt){if (lazy[rt] != -1) {lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];lazy[rt] = -1;}}void update(int L,int R,int c,int l,int r,int rt){if (L <= l && r <= R) {lazy[rt] = c;return ;}pushdown(rt);int m = (l + r) >> 1;if (L <= m) update(L , R , c , lson);if (m < R) update(L , R , c , rson);pushup(rt);}void query(int l,int r,int rt){if (lazy[rt] != -1) {if (!vis[lazy[rt]]) ans++;vis[lazy[rt]] = 1;return ;}if (l == r) return ;int m = (l + r) >> 1;query(lson);query(rson);}int B_search(int key,int n){int low = 0 , high = n-1;while (low <=high) {int mid = (low + high)/2;if (lisan[mid] == key) return mid;if (lisan[mid] < key) low = mid + 1;else high = mid - 1;}return -1;}int main(){int T , n;scanf("%d",&T);while (T--) {scanf("%d",&n);int num = 0;for (int i = 0 ; i < n ; i ++){scanf("%d%d",&zuo[i] , &you[i]);dian[num++] = zuo[i];dian[num++] = you[i];}qsort(dian , num , sizeof(dian[0]),cmp);int cnt = 1;lisan[0] = dian[0];for (int i = 1 ; i < num; i ++){if (dian[i] == dian[i-1]) continue ; // printf("%d ",dian[i]); lisan[cnt++] = dian[i];}for (int i = cnt - 1 ; i > 0 ; i --){if (dian[i] > dian[i-1] + 1) { lisan[cnt++] = ++dian[i-1]; //printf("%d = %d ",i-1,dian[i-1]+1); }}//printf("\n");qsort(lisan , cnt , sizeof(lisan[0]),cmp);//for(int j = 0;j<m;j++)// printf("%d ",dian[j]);// printf("\n");memset(lazy , -1 , sizeof(lazy));//int *p,*q;for (int i = 0 ; i < n ; i ++) {int l = B_search(zuo[i], cnt);int r = B_search(you[i], cnt);update(l, r, i, 0, cnt - 1, 1);}ans = 0;memset(vis, false, sizeof(vis));query(0, cnt - 1, 1);printf("%d\n",ans);}return 0;}