hdu 1556 Color the ball
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=1556
Color the ball
Description
$N$个气球排成一排,从左到右依次编号为$1,2,3....N.$每次给定2个整数$a\ b(a \leq b)$,lele便为骑上他的“小飞鸽"牌电动车从气球$a$开始到气球$$b依次给每个气球涂一次颜色。但是$N$次以后lele已经忘记了第$I$个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
Input
每个测试实例第一行为一个整数$N,(N \leq 100000)$.接下来的N行,每行包括2个整数$a\ b(1 \leq a \leq b \leq N)$。
当$N = 0$,输入结束。
Output
每个测试实例输出一行,包括$N$个整数,第$I$个数代表第$I$个气球总共被涂色的次数。
SampleInput
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
SampleOutput
1 1 1
3 2 1
线段树。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<map> 8 using std::cin; 9 using std::cout; 10 using std::endl; 11 using std::find; 12 using std::map; 13 using std::pair; 14 using std::vector; 15 #define all(c) (c).begin(), (c).end() 16 #define cls(arr,val) memset(arr,val,sizeof(arr)) 17 #define iter(c) decltype((c).begin()) 18 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 19 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 20 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 21 #define pb(e) push_back(e) 22 #define mp(a, b) make_pair(a, b) 23 const int Max_N = 100010; 24 typedef unsigned long long ull; 25 #define mid ((l+r)>>1) 26 #define lc (root<<1) 27 #define rc (root<<1|1) 28 struct Node { int val; }; 29 int arr[Max_N]; 30 struct SegTree { 31 Node seg[Max_N << 2]; 32 inline void init() { cls(seg, 0); } 33 inline void push_down(int root) { 34 if (seg[root].val != 0) { 35 int &t = seg[root].val; 36 seg[lc].val += t; 37 seg[rc].val += t; 38 t = 0; 39 } 40 } 41 inline void update(int root, int l, int r, int x, int y){ 42 if (x > r || y < l) return; 43 if (x <= l && y >= r) { seg[root].val += 1; return; } 44 push_down(root); 45 update(lc, l, mid, x, y); 46 update(rc, mid + 1, r, x, y); 47 } 48 inline int query(int root, int l, int r, int p) { 49 if (p > r || p < l) return 0; 50 if (p <= l && p >= r) return seg[root].val; 51 push_down(root); 52 int ret = 0; 53 ret += query(lc, l, mid, p); 54 ret += query(rc, mid + 1, r, p); 55 return ret; 56 } 57 }seg; 58 int main() { 59 #ifdef LOCAL 60 freopen("in.txt", "r", stdin); 61 freopen("out.txt", "w+", stdout); 62 #endif 63 int n, a, b; 64 while (~scanf("%d", &n) && n) { 65 seg.init(); 66 rep(i, n) scanf("%d %d", &a, &b), seg.update(1, 1, n, a, b); 67 rep(i, n) arr[i] = seg.query(1, 1, n, i + 1); 68 rep(i, n) printf("%d%c", arr[i], i < n - 1 ? ' ' : '\n'); 69 } 70 return 0; 71 }
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明