POJ 3277 - City Horizon
我是用的线段树做的,时间常数好大,感觉拍的不好。本题要注意统计的方式,而且不要省略pushup操作。
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 #define MAXN 40000 6 7 int _dirty[MAXN * 4 * 4], *const dirty = &_dirty[-1]; 8 9 #define recursive_def int l, int r,int i 10 #define lsi i<<1 11 #define rsi lsi | 1 12 #define lsn l, m, lsi 13 #define rsn m+1, r, rsi 14 #define pushdown if (dirty[i] >= 0) {\ 15 dirty[lsi] = dirty[rsi] = dirty[i];\ 16 dirty[i] = -1; \ 17 } 18 #define pushup if (dirty[i] < 0 && dirty[lsi] == dirty[rsi]) dirty[i] = dirty[lsi]; 19 20 void update(int L, int R, int h, recursive_def) 21 { 22 if (L<=l && r<=R && dirty[i] >= 0) { 23 if (dirty[i] < h) dirty[i] = h; 24 return; 25 } 26 int m = (l+r) >> 1; 27 pushdown 28 if (L <= m) update(L, R, h, lsn); 29 if (R > m) update(L, R, h, rsn); 30 pushup 31 } 32 33 int sorted[MAXN*4], sorted_total; 34 35 int stt_l, stt_r, stt_h; 36 long long rslt; 37 38 void query0(recursive_def) 39 { 40 if (dirty[i] >= 0) { 41 // cout << l << ' '<< r << ' ' << dirty[i] << endl; 42 if (dirty[i] == stt_h) stt_r = r; 43 else { 44 rslt += (long long)stt_h * (sorted[stt_r] - sorted[stt_l] + 1); 45 stt_l = l, stt_r = r, stt_h = dirty[i]; 46 } 47 return; 48 } 49 int m = (l+r) >> 1; 50 query0(lsn); 51 if (m<r) query0(rsn); 52 } 53 54 long long query(recursive_def) 55 { 56 rslt = stt_h = 0; 57 query0(l, r, i); 58 rslt += (long long)stt_h *(sorted[stt_r] - sorted[stt_l] + 1); 59 return rslt; 60 } 61 62 int N; 63 int A[MAXN], B[MAXN], H[MAXN]; 64 65 int main(void) 66 { 67 // freopen("poj3277.txt", "r", stdin); 68 scanf("%d", &N); 69 sorted_total = 0; 70 for(int i=0; i<N; ++i) { 71 scanf("%d%d%d", &A[i], &B[i], &H[i]); 72 sorted[sorted_total++] = A[i] - 1; 73 sorted[sorted_total++] = A[i]; 74 sorted[sorted_total++] = B[i]; 75 sorted[sorted_total++] = --B[i]; 76 } 77 sort(sorted, sorted + sorted_total); 78 int i, t; 79 i = 0; 80 for(t=1; t<sorted_total; ++t) 81 if (sorted[t-1] != sorted[t]) 82 sorted[i++] = sorted[t-1]; 83 sorted[i] = sorted[t-1], sorted_total = i+1; 84 85 dirty[1] = 0; 86 for(int i=0; i<N; ++i) { 87 A[i] = lower_bound(sorted, sorted + sorted_total, A[i]) - sorted; 88 B[i] = lower_bound(sorted, sorted + sorted_total, B[i]) - sorted; 89 update(A[i], B[i], H[i], 0, sorted_total-1, 1); 90 } 91 printf("%lld", query(0, sorted_total-1, 1)); 92 return 0; 93 }
3277 | Accepted | 3524K | 1766MS | G++ | 2238B | 2014-07-22 13:01:18 |
This article is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
本文采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。