topic
Title Description
To Bytetown city mayor elections, all voters can speak freely to the speech of the candidates running for mayor. In order to unify management of the city council as voters prepared a poster of electoral wall.
Posted rules are as follows:
-
electoral walls are rectangular with a length of N units, each unit referred to as a lattice;
-
All posters must be posted highly consistent with the height of the electoral wall;
-
Each poster "AB" indicates that the poster from the A to the B-th lattice grid;
-
Poster posted after previously attached to may be covered partially or posters poster.
Now you judge, after you put all the posters, the electoral posters on the wall can also see how much.
Input Format
The first line: NM respectively represent the length and the number of wall posters electoral
The next M lines: Ai Bi represent the position of each poster
Output Format
After output had finished all posters, the number of electoral posters in the wall can be seen.
Sample input and output
100 5 1 4 2 6 8 10 3 4 7 10
4
Description / Tips
Note: Source: Luo Gu
【Restrictions】
1 0<= N <= 10000000 1<=M<=1000 1<= Ai <= Bi <=10000000
All data are integers. There is a space between data
analysis
A very wonderful reverse thinking
if positive thinking, eliminate coverage when coverage of brains, a lot of trouble
to see the last exposed, as long as not in front of the block, will be exposed to, for example:
Can be stored in the blank, backward operation, as long as the covering blank poster, will be exposed
but in discrete time to be wrong, for example:
8 3
3 6
1 3
6 7
Originally:
After 3 and 6 adjacent discrete, should answer 3, the output is 2, becomes:
Solution: When discretization is not erased intermediate, but becomes larger than the intermediate distance 1 1
Note array size
Code
. 1 / * ************************************************************ 2 the User: Mandy.HY . 3 Language: C ++ . 4 Problem: luogu3740 . 5 Algorithm: discrete segment tree + 6 ************************************************************ * / 7 8 // a very wonderful reverse thinking 9 // If positive thinking, when covered twist their brains to eliminate coverage, a lot of trouble 10 // look at last be revealed, not just in front of the block, will be exposed to 11 // can be stored in the blank, backward operation, as long as the covering blank poster, it will be exposed to 12 // but discretization error when, for example: 13 // . 8. 3 14 @ . 3. 6 15 // . 1. 3 16 @ 67 17 // discretization 3 and 6 adjacent to answer 3 should output 2 18 is // Solution: discretization, the intermediate is not erased, but becomes larger than the intermediate distance 1 1 . 19 // Note array size 20 is 21 is 22 is #include <bits / STDC ++ H.> 23 is #define Max (X, Y) ((X)> (Y) (X):? (Y)) 24 #define Min (X, Y) ( ? (X) <(Y) (X): (Y)) 25 #define LSON L, MID, K <<. 1 26 is #define rson MID +. 1, R & lt, K <<. 1 |. 1 27 28 the using namespace STD; 29 30 const int MAXM = 2005 ; 31 is int n-, m, ANS, Judge =0; 32 int tree[maxm << 4]; 33 34 struct Poster{ 35 int l,r; 36 }pos[maxm]; 37 38 struct Temp{ 39 int l,id; 40 bool operator < (const Temp &a)const { 41 return l < a.l; 42 } 43 }tmp[maxm << 1]; 44 45 template<class T>inline void read(T &x) { 46 x = 0;bool flag = 0;char ch = getchar(); 47 while(!isdigit(ch)) flag |= ch == '-',ch = getchar(); 48 while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar(); 49 if(flag) x = -x; 50 } 51 52 template<class T>void putch(const T x) { 53 if(x > 9) putch(x / 10); 54 putchar(x % 10 | 48); 55 } 56 57 template<class T>void put(const T x) { 58 if(x < 0) putchar('-'),putch(-x); 59 else putch(x); 60 } 61 62 void file() { 63 freopen("3740.in","r",stdin); 64 // freopen("2161.out","w",stdout); 65 } 66 67 void readdata() { 68 read(n);read(m); 69 } 70 71 void pushup(int k){ 72 tree[k] = tree[k<<1] + tree[k<<1|1]; 73 } 74 75 void pushdown(int k){ 76 if(!tree[k]){ 77 tree[k<<1] = 0; 78 tree[k<<1|1] = 0; 79 } 80 } 81 82 void buildtree(int l,int r,int k){ 83 if(l == r){ 84 tree[k] = 1; 85 return; 86 } 87 int mid = (l + r) >> 1; 88 buildtree(lson); 89 buildtree(rson); 90 pushup(k); 91 } 92 93 void modify(int l,int r,int k,int x,int y){ 94 if(x <= l && r <= y){ 95 if(tree[k]) judge = 1; 96 tree[k] = 0; 97 return; 98 } 99 pushdown(k); 100 int mid = (l + r) >> 1; 101 if(x <= mid) modify(lson,x,y); 102 if(y > mid) modify(rson,x,y); 103 pushup(k); 104 } 105 106 void work() { 107 int cnt = 0; 108 for(int i = 1;i <= m ; ++ i){ 109 read(tmp[++cnt].l);tmp[cnt].id = i; 110 read(tmp[++cnt].l);tmp[cnt].id = i; 111 } 112 113 sort(tmp + 1,tmp + cnt + 1); 114 int tot = 0;//去重 及 变为1 115 for(int i = 1;i <= cnt; ++ i){ 116 int id = tmp[i].id; 117 if(tmp[i].l != tmp[i - 1].l){ 118 if(tmp[i].l - tmp[i - 1].l > 1) ++tot; 119 if(pos[id].l) pos[id].r = ++tot; 120 else pos[id].l = ++tot; 121 } else { 122 if(pos[id].l) pos[id].r = tot; 123 else pos[id].l = tot; 124 } 125 126 } 127 128 buildtree(1,tot,1); 129 130 for(int i = m;i >= 1; -- i){ 131 judge = 0; 132 modify(1,tot,1,pos[i].l,pos[i].r); 133 if(judge) ans++; 134 } 135 put(ans); 136 } 137 138 int main() { 139 // file(); 140 readdata(); 141 work(); 142 return 0; 143 }