hdu Just a Hook(線段樹區間修改)

2021-06-25 16:36:32 字數 1278 閱讀 8973

線段樹模板題,練的是懶惰標記。

懶惰標記,就是更新一段區間的時候,如果小區間被包含在了所需要更新的區間裡面,那麼直接對代表這個區間的陣列元素賦值,之後做乙個標記(表示這個區間的子區間都需要更新)但是不繼續遞迴(這樣可以節省很多的時候)。

11657115

2014-09-15 14:17:26

accepted

1698

796ms

2380k

1750 b

g++kinderriven

#include#include#include#include#include#include#include#include#include#includeusing namespace std;

typedef long long ll;

typedef unsigned long long ull;

#define esp 1e-10

const int maxn = 100000 + 10;

int n;

int tree[maxn << 2];

int mark[maxn << 2];

void buildtree(int l,int r,int pos)

int m = (l + r) >> 1;

buildtree(l,m,pos << 1);

buildtree(m + 1, r ,(pos << 1)|1);

tree[pos] = tree[pos << 1] + tree[(pos << 1)|1];

mark[pos] = 0;

return ;

}void update(int l,int r,int add,int l,int r,int pos)

int m = (r + l) >> 1;

int len = r - l + 1;

if(mark[pos])

if(l <= m)update(l,r,add,l,m,pos << 1);

if(r > m)update(l,r,add,m + 1,r,(pos << 1)|1);

tree[pos] = tree[pos << 1] + tree[(pos << 1)|1];

return ;

}int main()

printf("case %d: the total value of the hook is %d.\n",case ++,tree[1]);

}return 0;

}

線段樹 02 構建線段樹

public inte ce merger 不能再縮小的基本問題是 對treeindex指向的節點的情況進行討論 public class segmenttree 在treeindex的位置建立表示區間 l.r 的線段樹 private void buildsegmenttree int treei...

線段樹 01 線段樹基礎

物理上 public class segmenttree public int getsize public e get int index 返回完全二叉樹的陣列表示中,乙個索引所表示的元素的左孩子節點的索引 private int leftchild int index 返回完全二叉樹的陣列表示中...

線段樹和zkw線段樹

好啦,我們就開始說說線段樹吧 線段樹是個支援區間操作和查詢的東東,平時的話還是蠻實用的 下面以最基本的區間加以及查詢區間和為例 線段樹顧名思義就是棵樹嘛,葉子節點是每個基本點,它們所對應的父親就是它們的和,具體如下圖 但是對於這樣的線段樹來說,操作所需的時間是遠達不到我們的要求的 會被t 因為我們會...