Input
Output
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
本身的数据范围是比较大的,但考虑到我们其实关心的只是区间的关系,每个区间的长度并不在意,故可以建立一个映射,使数据范围降下来。题目中是用一个字母i表示一个区间,改成[i,i+1)
会好很多,也方便进行排序。排序时使用set进行(这样用[i,i+1)排序不再需要像其他一些题解里所写的一样加数字),用map建立索引。之后就是线段树的更新操作。向下结点的更新在更改到
这个父结点时再进行。
1 #include <iostream> 2 //#include<bits/stdc++.h> 3 #include <stack> 4 #include <queue> 5 #include <map> 6 #include <set> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10 using namespace std; 11 typedef long long ll; 12 typedef unsigned long long ull; 13 const int MAX=1e4+5; 14 set <int> x; 15 int l[MAX],r[MAX]; 16 map<int,int> re; 17 struct node 18 { 19 int left,right; 20 int col; 21 }st[200*MAX]; 22 int an=0; 23 bool vi[MAX]; 24 void init(int l,int r,int k) 25 { 26 st[k].left=l; 27 st[k].right=r; 28 st[k].col=0; 29 if(l+1==r) 30 return ; 31 init(l,(l+r)/2,2*k); 32 init((l+r)/2,r,2*k+1); 33 } 34 void update(int l,int r,int k,int co) 35 { 36 if(st[k].left==l&&st[k].right==r) 37 { 38 st[k].col=co; 39 return; 40 } 41 if(st[k].col) 42 { 43 st[2*k].col=st[2*k+1].col=st[k].col; 44 st[k].col=0; 45 } 46 if(st[2*k].right<=l) 47 update(l,r,2*k+1,co); 48 else if(st[2*k].right>=r) 49 update(l,r,2*k,co); 50 else 51 { 52 update(l,st[2*k].right,2*k,co); 53 update(st[2*k].right,r,2*k+1,co); 54 } 55 } 56 void query(int k) 57 { 58 if(st[k].col) 59 { 60 if(!vi[st[k].col]) 61 { 62 an++; 63 vi[st[k].col]=true; 64 } 65 return; 66 } 67 if(st[k].left+1==st[k].right) 68 return; 69 query(2*k); 70 query(2*k+1); 71 return; 72 73 } 74 int main() 75 { 76 int c; 77 scanf("%d",&c); 78 while(c--) 79 { 80 int n; 81 scanf("%d",&n); 82 int i; 83 for(i=1;i<=n;i++) 84 { 85 scanf("%d %d",&l[i],&r[i]); 86 r[i]++; 87 x.insert(l[i]); 88 x.insert(r[i]); 89 } 90 i=1; 91 while(!x.empty()) 92 { 93 re[*x.begin()]=i; 94 x.erase(x.begin()); 95 i++; 96 } 97 init(1,i,1); 98 for(i=1;i<=n;i++) 99 update(re[l[i]],re[r[i]],1,i); 100 memset(vi,false,sizeof(vi)); 101 an=0; 102 query(1); 103 printf("%d\n",an); 104 105 } 106 }
 (线段树,离散化)poj2528-Mayor's posters
原文:http://www.cnblogs.com/quintessence/p/6418556.html