hdu 1556 Color the ball(线段树的另一种应用)
2012-08-22 22:08
260 查看
/* 线段树 这道题做完以后,收获很大,学到了线段树的另一种应用:如果需要对某个区间内所有元素进行同样操作,则只需要对线段树上对应区间进行操作即可。 统计每个气球被涂次数,只需要匹配到线段树的对应的区间即可,不需要统计到叶子节点。否则会超时,在最后统计结果的时候则需要搜索到叶子节点。 即:①处的作用 */ #include <iostream> using namespace std; const int nMax = 100010; //int A[nMax]; struct Node { int l, r; int num; Node(){} Node(int l,int r,int num) : l(l), r(r), num(num){} }node[nMax * 4]; int ans[nMax]; int N; void build(int rt, int l, int r) { if(l < r) { int mid = (l + r) / 2; build(rt * 2, l, mid); build(rt * 2 + 1, mid + 1, r); node[rt] = Node(l, r, 0); } else if(l == r) node[rt] = Node(l, r, 0); } void update(int rt, int l, int r) { if(node[rt].l ==l && node[rt].r == r)//① node[rt].num ++; else { int mid = (node[rt].l + node[rt].r) / 2; if(mid >= r) update(rt * 2, l, r); else if(mid + 1 <= l) update(rt * 2 + 1, l, r); else { update(rt * 2, l, mid); update(rt * 2 + 1, mid + 1, r); } } } void solve(int rt) { int i; if(node[rt].num > 0) { for(i = node[rt].l; i <= node[rt].r; ++ i) ans[i] += node[rt].num; } if(node[rt].l < node[rt].r) { solve(rt * 2); solve(rt * 2 + 1); } } int main() { //freopen("e://data.in", "r", stdin); while(scanf("%d", &N) != EOF && N) { int a, b; int i; build(1, 1, N); for(i = 1; i <= N; ++ i) { scanf("%d%d", &a, &b); update(1, a, b); } memset(ans, 0, sizeof(ans)); solve(1); for(i = 1; i <= N; ++ i) { if(i != 1) printf(" "); printf("%d", ans[i]); } printf("\n"); } return 0; }
相关文章推荐
- HDU 1556 Color the Ball 线段树 题解
- HDU 1556 Color the ball(线段树)
- HDU 1556 Color the ball【线段树】
- 【线段树II:区间修改+点查询】hdu 1556 Color the ball
- HDU 1556 Color the ball 线段树 区间更新
- HDU 1556 Color the ball【线段树及lazy思想】【树状数组】
- hdu 1556:Color the ball(线段树,区间更新,经典题)
- HDU 1556 Color the ball (线段树)
- hdu 1556 Color the ball(线段树区间更新单点查询+懒惰标记)
- hdu 1556 Color the ball(线段树)
- hdu 1556 Color the ball(线段树)
- HDU 1556 Color the ball(线段树区间更新区间查询模板)
- HDU--杭电--1556--Color the ball--线段树
- HDU 1556 Color the Ball 线段树 题解
- hdu 1556 Color the ball(线段树 区间更新单点查询)
- hdu - 1556 (Color the ball 线段树)
- HDU 1556 Color the ball(线段树:区间更新)
- hdu 1556 color the ball 线段树区间更新 加lazy标记
- hdu 1556 Color the ball (树状数组||线段树成段更新)
- HDU - 1556 Color the ball(树状数组 or 线段树)