Poj--3277(线段树,离散化,线段更新、求和)
2014-10-01 16:53:27
思路:比较裸的线段树,考的是离散化和线段更新,即:叶子节点表示线段。
1 /************************************************************************* 2 > File Name: 3277.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 01 Oct 2014 03:46:19 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <queue> 14 #include <iostream> 15 #include <algorithm> 16 using namespace std; 17 #define lp (p << 1) 18 #define rp (p << 1|1) 19 #define getmid(l,r) (l + (r - l) / 2) 20 typedef long long ll; 21 const int INF = 1 << 29; 22 const int maxn = 80010; 23 24 int N; 25 int val[maxn << 1]; 26 27 struct Building{ 28 int a,b,h; 29 friend bool operator < (Building x,Building y){ 30 return x.h < y.h; 31 } 32 }B[maxn]; 33 34 struct node{ 35 int l,r; 36 int tl,tr,cover; 37 ll sum; 38 }t[maxn << 2]; 39 40 void Build_tree(int p,int l,int r){ 41 t[p].l = l; 42 t[p].r = r; 43 t[p].tl = val[l]; 44 t[p].tr = val[r]; 45 t[p].cover = 0; 46 t[p].sum = 0; 47 if(l + 1 == r) 48 return; 49 int mid = getmid(l,r); 50 Build_tree(lp,l,mid); 51 Build_tree(rp,mid,r); 52 } 53 54 void Push_down(int p){ 55 int c = t[p].cover; 56 if(c){ 57 t[lp].cover = t[rp].cover = c; 58 t[lp].sum = (ll)c * (t[lp].tr - t[lp].tl); 59 t[rp].sum = (ll)c * (t[rp].tr - t[rp].tl); 60 t[p].cover = 0; 61 } 62 } 63 64 void Update(int a,int b,int c,int p,int l,int r){ 65 if(a <= t[p].tl && t[p].tr <= b){ 66 t[p].cover = c; 67 t[p].sum = (ll)c * (t[p].tr - t[p].tl); 68 return; 69 } 70 if(l + 1 == r) return; 71 Push_down(p); 72 int mid = getmid(l,r); 73 if(a <= t[lp].tr) Update(a,b,c,lp,l,mid); 74 if(b >= t[rp].tl) Update(a,b,c,rp,mid,r); 75 t[p].sum = t[lp].sum + t[rp].sum; 76 } 77 78 int main(){ 79 scanf("%d",&N); 80 int tot = 0; 81 for(int i = 1; i <= N; ++i){ 82 scanf("%d%d%d",&B[i].a,&B[i].b,&B[i].h); 83 val[++tot] = B[i].a; 84 val[++tot] = B[i].b; 85 } 86 sort(val + 1,val + tot + 1); 87 Build_tree(1,1,2 * N); 88 sort(B + 1,B + N + 1); 89 for(int i = 1; i <= N; ++i){ 90 Update(B[i].a,B[i].b,B[i].h,1,1,2 * N); 91 } 92 printf("%I64d\n",t[1].sum); 93 return 0; 94 }

浙公网安备 33010602011771号