POJ 2528 - Mayor's posters
其实就是区间用N个操作染色,问最后有多少种不同的颜色,注意由于范围较大需要map。
先拍了一个裸的,之所以称之为裸的是因为我觉得pushup是没必要存在的,先这样吧,改天再更新。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 #define MAXV 32768 7 8 int _v[MAXV << 1], *const v = &_v[-1]; 9 int _dirty[MAXV << 1], *const dirty = &_dirty[-1]; 10 11 #define recursive_def int l, int r, int i 12 #define lsi i<<1 13 #define rsi i<<1 | 1 14 #define lsn l, m, lsi 15 #define rsn m+1, r, rsi 16 #define pushdown if (dirty[i]) {\ 17 dirty[lsi] = dirty[rsi] = dirty[i]; \ 18 v[lsi] = v[rsi] = dirty[i]; \ 19 dirty[i] = 0;} 20 #define pushup (v[i] = v[lsi] != v[rsi]? -1 : v[lsi]); 21 22 void build(recursive_def) 23 { 24 v[i] = dirty[i] = 0; 25 if (l != r) { 26 int m = l+r >> 1; 27 build(lsn), build(rsn); 28 } 29 } 30 31 void update(int L, int R, int val, recursive_def) 32 { 33 if (L<=l && r<=R) v[i] = dirty[i] = val; 34 else { 35 pushdown 36 int m = l+r >> 1; 37 if (L<=m) update(L, R, val, lsn); 38 if (m < R) update(L, R, val, rsn); 39 pushup 40 } 41 } 42 43 char mark[10005]; 44 45 int query(recursive_def){ 46 if (v[i] >= 0) 47 if (!mark[v[i]]) { 48 mark[v[i]] = 1; 49 return 1; 50 } else return 0; 51 else { 52 int m = l+r >> 1; 53 return query(lsn) + query(rsn); 54 } 55 } 56 57 int poster_l[10000], poster_r[10000]; 58 int sorted[20000]; 59 60 int main(void) 61 { 62 // freopen("hdu2528.txt", "r", stdin); 63 int C; 64 scanf("%d", &C); 65 while(C) { 66 int N; 67 scanf("%d", &N); 68 int L = 0; 69 for(int i=0; i<N; ++i) { 70 scanf("%d%d", &poster_l[i], &poster_r[i]); 71 sorted[L] = poster_l[i], sorted[L+1] = poster_r[i]; 72 L += 2; 73 } 74 sort(sorted, sorted + L); 75 build(0, L-1, 1); 76 // map到 [0, L)上 77 for(int i=0; i<N; ++i) { 78 poster_l[i] = lower_bound(sorted, sorted + L, poster_l[i]) - sorted; 79 poster_r[i] = lower_bound(sorted, sorted + L, poster_r[i]) - sorted; 80 update(poster_l[i], poster_r[i], i+1, 0, L-1, 1); 81 } 82 memset(mark, 0, sizeof(mark)); 83 mark[0] = 1; 84 printf("%d\n", query(0, L-1, 1)); 85 --C; 86 } 87 return 0; 88 }
2528 | Accepted | 1036K | 79MS | G++ | 1867B | 2014-06-11 14:57:31 |
This article is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
本文采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。