hdu 1556 线段树
这个题其实有O(n)的算法,不过还是用线段树做了一遍,还写了个自认为不错的pushalldown函数,哈哈。 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 100001; 7 int ans[N]; 8 9 struct Node 10 { 11 int l, r, add; 12 } node[N << 2]; 13 14 void build( int i, int l, int r ) 15 { 16 node[i].l = l, node[i].r = r, node[i].add = 0; 17 if ( l == r ) return ; 18 int lc = i << 1, rc = lc | 1, mid = ( l + r ) >> 1; 19 build( lc, l, mid ); 20 build( rc, mid + 1, r ); 21 } 22 23 void pushdown( int i ) 24 { 25 if ( node[i].add ) 26 { 27 int lc = i << 1, rc = lc | 1; 28 node[lc].add += node[i].add; 29