hdu 1556 树状数组
2014-07-16 18:15
323 查看
这题用树状数组做的,貌似线段树也可以吧
怎么用树状数组呢?
对于a,b,那么首先用1更新a,然后用-1更新b+1
这样所有包含a到b的都+1了
AC代码如下:
怎么用树状数组呢?
对于a,b,那么首先用1更新a,然后用-1更新b+1
这样所有包含a到b的都+1了
AC代码如下:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int MAX_N = 1e5; int sum[MAX_N+5]; int N; inline int lowbit( int x ){ return x & ( -x ); } void updata( int x, int val ){ while( x <= N ){ sum[x] += val; x += lowbit( x ); } } int getsum( int x ){ int ans = 0; while( x > 0 ){ ans += sum[x]; x -= lowbit( x ); } return ans; } int main(){ while( scanf( "%d", &N ) && N ){ memset( sum, 0, sizeof( sum ) ); for( int i = 1; i <= N; i++ ){ int temp1, temp2; scanf( "%d%d", &temp1, &temp2 ); updata( temp1, 1 ); updata( temp2 + 1, -1 ); } printf( "%d", getsum( 1 ) ); for( int i = 2; i <= N; i++ ){ printf( " %d", getsum( i ) ); } printf( "\n" ); } return 0; }
相关文章推荐
- 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 很典型的更新区间查找点的题(线段树树状数组两种解法)
- hdu 1556 Color the ball (树状数组)
- hdu 1556 树状数组 成段更新 单点求值
- hdu 1556 Color the ball(树状数组)
- HDU 1556(树状数组-区间修改,查询点)
- Color the ball - HDU 1556 简单树状数组
- HDU 1556树状数组求解
- HDU 1556 Color the ball(树状数组,区间修改,单点统计)
- HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)