ACM模板库

ACM模板库


经典线段树

<pre><code class="language-cpp">#include &lt;iostream&gt; using namespace std; typedef long long ll; const ll maxn = 1e6+5; struct Node{ int l; int r; ll addlazy; ll sum; }; Node tree[maxn&lt;&lt;2]; void pushdown(int now){ tree[2*now].addlazy += tree[now].addlazy; tree[2*now+1].addlazy += tree[now].addlazy; tree[2*now].sum += tree[now].addlazy * (tree[2*now].r - tree[2*now].l + 1); tree[2*now+1].sum += tree[now].addlazy * (tree[2*now+1].r - tree[2*now+1].l + 1); tree[now].addlazy = 0; return; } void build(Node *tree, int now, int l, int r){ tree[now].l = l; tree[now].r = r; tree[now].addlazy = 0; if(l == r){ cin &gt;&gt; tree[now].sum; return; } int mid = (l + r) &gt;&gt; 1; build(tree, 2*now, l, mid); build(tree, 2*now+1, mid+1, r); tree[now].sum = tree[2*now].sum + tree[2*now+1].sum; return; } void updata(Node *tree, int now, int z, ll data){ if(tree[now].l == tree[now].r){ tree[now].sum = data; return; } if(tree[now].addlazy) pushdown(now); int mid = (tree[now].l + tree[now].r) &gt;&gt; 1; if(z &lt;= mid) updata(tree, 2*now, z, data); else updata(tree, 2*now+1, z, data); tree[now].sum = tree[2*now].sum + tree[2*now+1].sum; return; } ll ask(Node *tree, int now, int z){ if(tree[now].l == tree[now].r){ return tree[now].sum; } if(tree[now].addlazy) pushdown(now); int mid = (tree[now].l + tree[now].r) &gt;&gt; 1; if(z &lt;= mid) return ask(tree, 2*now, z); else return ask(tree, 2*now+1, z); } void upgrade(Node *tree, int now, int l, int r, ll data){ if(tree[now].l &gt;= l &amp;&amp; tree[now].r &lt;= r){ tree[now].sum += data * (tree[now].r - tree[now].l + 1); tree[now].addlazy += data; return; } if(tree[now].addlazy) pushdown(now); int mid = (tree[now].l + tree[now].r) &gt;&gt; 1; if(l &lt;= mid) upgrade(tree, 2*now, l, r, data); if(r &gt; mid) upgrade(tree, 2*now+1, l, r, data); tree[now].sum = tree[2*now].sum + tree[2*now+1].sum; return; } ll query(Node *tree, int now, int l, int r){ if(tree[now].l &gt;= l &amp;&amp; tree[now].r &lt;= r) return tree[now].sum; if(tree[now].addlazy) pushdown(now); int mid = (tree[now].l + tree[now].r) &gt;&gt; 1; return (l &lt;= mid? query(tree, 2*now, l, r): 0) + (r &gt; mid? query(tree, 2*now+1, l, r): 0); }</code></pre>

页面列表

ITEM_HTML