1. 程式人生 > >BZOJ5168: [HAOI2014]貼海報 線段樹

BZOJ5168: [HAOI2014]貼海報 線段樹

Description

Bytetown城市要進行市長競選,所有的選民可以暢所欲言地對競選市長的候選人發表言論。為了統一管理,城市委 員 會為選民準備了一個張貼海報的electoral牆。張貼規則如下: 1.electoral牆是一個長度為N個單位的長方形,每個單位記為一個格子; 2.所有張貼的海報的高度必須與electoral牆的高度一致的; 3.每張海報以“A B”表示,即從第A個格子到第B個格子張貼海報; 4.後貼的海報可以覆蓋前面已貼的海報或部分海報。 現在請你判斷,張貼完所有海報後,在electoral牆上還可以看見多少張海報。

 

Input

第一行: N M 分別表示electoral牆的長度和海報個數 接下來M行: Ai Bi 表示每張海報張貼的位置

 

Output

輸出貼完所有海報後,在electoral牆上還可以看見的海報數。 1 0<= N <= 10000000 1<=M<=1000 1<= Ai <= Bi <=10000000 所有的資料都是整數。資料之間有一個空格

 

Sample Input

100 5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

Solution

倒序處理...沒想到這個就一直寫不出來啊...

用線段樹維護區間資訊。如果這個海報的區間被覆蓋了,那麼顯然就是被後面的海報覆蓋過了,那麼最後肯定就看不到這張海報了,這張海報就不用統計了

更新區間資訊的時候&一下左右兒子就可以了

#include <bits/stdc++.h>

#define ll long long
#define inf 0x3f3f3f3f 
#define il inline 

namespace io {

    #define in(a) a=read()
    #define out(a) write(a)
    #define
outn(a) out(a),putchar('\n') #define I_int int inline I_int read() { I_int x = 0 , f = 1 ; char c = getchar() ; while( c < '0' || c > '9' ) { if( c == '-' ) f = -1 ; c = getchar() ; } while( c >= '0' && c <= '9' ) { x = x * 10 + c - '0' ; c = getchar() ; } return x * f ; } char F[ 200 ] ; inline void write( I_int x ) { I_int tmp = x > 0 ? x : -x ; if( x < 0 ) putchar( '-' ) ; int cnt = 0 ; while( tmp > 0 ) { F[ cnt ++ ] = tmp % 10 + '0' ; tmp /= 10 ; } while( cnt > 0 ) putchar( F[ -- cnt ] ) ; } #undef I_int } using namespace io ; using namespace std ; #define N 100010 const int M = 1e7 + 10 ; #define lc (rt<<1) #define rc (rt<<1|1) int n = read() , m = read() ; int a[ N ] , b[ N ] ; int cover[ M << 2 ] ; int bc = 0 ; void upd( int L , int R , int l , int r , int rt ) { if( cover[ rt ] ) return ; if( L <= l && r <= R && !cover[ rt ] ) { cover[ rt ] = 1 ; bc = 0 ; return ; } int mid = ( l + r ) >> 1 ; if( L <= mid ) upd( L , R , l , mid , lc ) ; if( R > mid ) upd( L , R , mid + 1 , r , rc ) ; cover[ rt ] = cover[ lc ] & cover[ rc ] ; } int main() { int ans = 0 ; for( int i = 1 ; i <= m ; i ++ ) a[ i ] = read() , b[ i ] = read() ; for( int i = m ; i ; i -- ) { bc = 1 ; upd( a[ i ] , b[ i ] , 1 , n , 1 ) ; if( !bc ) ans ++ ; } outn( ans ) ; }