懶散線段樹

2021-06-29 05:20:56 字數 1564 閱讀 1832

時間限制: 3 s

空間限制: 128000 kb

題目等級 : 大師 master

題解

給你n個數,有兩種操作:

1:給區間[a,b]的所有數增加x

2:詢問區間[a,b]的數的和。

輸入描述 input description

第一行乙個正整數n,接下來n行n個整數,

再接下來乙個正整數q,每行表示操作的個數,

如果第乙個數是1,後接3個正整數,

表示在區間[a,b]內每個數增加x,如果是2,

表示操作2詢問區間[a,b]的和是多少。

輸出描述 output description

對於每個詢問輸出一行乙個答案

樣例輸入 sample input

3123

21 2 3 2

2 2 3

樣例輸出 sample output

9 資料範圍及提示 data size & hint

資料範圍

1<=n<=200000

1<=q<=200000

#include#include#include#include#include#include#include#include#include#include#define mst(ss,b) memset((ss),(b),sizeof(ss))

#define maxn 0x3f3f3f3f

using namespace std;

int n,m;

struct node s[800001];

int a[200001];

void build(int root,int l,int r)

int mid=(l+r)/2;

build(root*2,l,mid);

build(root*2+1,mid+1,r);

s[root].v=s[root*2].v+s[root*2+1].v;

}void putdown(int root)

void up(int root,int l,int r,int v)

if(s[root].flag)

putdown(root);

int mid=(s[root].l+s[root].r)/2;

if(r<=mid)

else if(l>mid)

else

s[root].v=s[root*2].v+s[root*2+1].v;

}long long query(int root,int l,int r)

if(s[root].flag)

putdown(root);

int mid=(s[root].l+s[root].r)/2;

if(r<=mid)

return query(root*2,l,r);

else if(l>mid)

return query(root*2+1,l,r);

else

}int main()

else

}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 因為我們會...