【模板】线段树

线段树 处理区间问题的高级数据结构
听说树状数组能做(树状数组我不会啊QAQ )的线段树都能做,就丢个线段树吧QAQ
快NOIP了还是好弱怎么办 急在线等
NOIP Loiers RP ++!!

void up(int p)//更新数据
{
    tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum;
    tree[p].max = max(tree[p<<1].max,tree[p<<1|1].max);
    tree[p].min = min(tree[p<<1].min,tree[p<<1|1].min);
}
void push(int p)//标记下放
{
    tree[p<<1].sum += tree[p].add * (tree[p<<1].r - tree[p<<1].l + 1);
    tree[p<<1].add += tree[p].add;
    tree[p<<1|1].sum += tree[p].add * (tree[p<<1|1].r - tree[p<<1|1].l + 1);
    tree[p<<1|1].add += tree[p].add;
    tree[p].add = 0;
} 
void build(int p,int l,int r)//建树
{
    tree[p].l = l;
    tree[p].r = r;
    if(l == r)
    {
        tree[p].min = tree[p].max = tree[p].sum = num[l];
        return ;
    }
    int mid = (l + r) >> 1;
    build(p<<1,l,mid);
    build(p<<1|1,mid+1,r);
    up(p);
}
void change_qujian(int p,int l,int r,int v)//区间修改
{
    if(l <= tree[p].l && tree[p].r <= r)
    {
        tree[p].sum += v * (tree[p].r - tree[p].l +1);
        tree[p].max += v;
        tree[p].min += v;
        tree[p].add += v;
        return;
    }
    push(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    if(l <= mid ) change(p<<1,l,r,v);
    if(mid < r) change(p<<1|1,l,r,v);
    up(p);
}
void change_dian(int p,int a,int x)//单点修改
{
    if(a==tree[p].l&&tree[p].r==a)
    {
        tree[p].sum+=x;
        tree[p].add+=x;
        return;
    }
    push(p);
    int mid = (tree[p].l+tree[p].r)/2;
    if(a <= mid) change(p*2,a,x);
    if(mid < a) change(p*2+1,a,x);
    up(p);
}
ll ask(int p,int l)//单点查询
{
    if(l == tree[p].l && tree[p].r == l)
    {
        return tree[p].sum;
    }
    push(p);
    int mid = (tree[p].l + tree[p].r)/2;
    ll ans = 0;
    if(l <= mid) ans += ask(p*2,l);
    if(mid < l) ans += ask(p*2+1,l);
    return ans;
}
ll ask_min(int p,int l,int r)//区间最小值
{
    if(l <= tree[p].l && tree[p].r <= r)
    {
        return tree[p].min;
    }
    push(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    ll ans = 21474836472333333;
    if(l <= mid) ans = min(ans,ask_min(p<<1,l,r));
    if(mid < r) ans = min(ans,ask_min(p<<1|1,l,r));
    return ans;

}
ll ask_max(int p,int l,int r)//区间最大值
{
    if(l <= tree[p].l && tree[p].r <= r)
    {
        return tree[p].max;
    }
    push(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    ll ans = 0;
    if(l <= mid) ans = max(ans,ask_max(p<<1,l,r));
    if(mid < r) ans = max(ans,ask_max(p<<1|1,l,r));
    return ans;
}
ll ask_sum(int p,int l,int r)//区间求和
{
    if(l <= tree[p].l && tree[p].r <= r)
    {
        return tree[p].sum;
    }
    push(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    ll ans = 0;
    if(l <= mid) ans += ask_sum(p<<1,l,r);
    if(mid < r) ans += ask_sum(p<<1|1,l,r));
    return ans;
}
以下是acwing模板线段树的示例代码: ```cpp const int N = 100010; int n, m; int a[N]; struct Node { int l, r; int v, lazy; } tree[N * 4]; void pushup(int x) { tree[x].v = tree[x * 2].v + tree[x * 2 + 1].v; } void pushdown(int x) { if (tree[x].lazy) { int l = tree[x].l, r = tree[x].r; int mid = (l + r) >> 1; tree[x * 2].v += tree[x].lazy * (mid - l + 1); tree[x * 2 + 1].v += tree[x].lazy * (r - mid); tree[x * 2].lazy += tree[x].lazy; tree[x * 2 + 1].lazy += tree[x].lazy; tree[x].lazy = 0; } } void build(int x, int l, int r) { tree[x].l = l, tree[x].r = r; if (l == r) { tree[x].v = a[l]; return; } int mid = (l + r) >> 1; build(x * 2 l, mid); build(x * 2 + 1, mid +1, r); pushup(x); } void modify(int x, int l, int r, int val) { if (tree[x].l >= l && tree[x].r <= r) { tree[x].v += val * (tree[x].r - tree[x].l + 1); tree[x].lazy += val; return; } pushdown(x); int mid = (tree[x].l + tree[x].r) >> 1; if (l <= mid) modify(x * 2, l, r, val); if (r > mid) modify(x * 2 + 1, l, r, val); pushup(x); } int query(int x, int l, int r) { if (tree[x].l >= l && tree[x].r <= r) { return tree[x].v; } pushdown(x); int mid = (tree[x].l + tree[x].r) >> 1; int sum = 0; if (l <= mid) sum += query(x * 2, l, r); if (r > mid) sum += query(x * 2 + 1, l, r); return sum; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } build(1, 1, n); while (m--) { int op, l, r, val; cin >> op; if (op == 1) { cin >> l >> r >> val; modify(1, l, r, val); } else if (op == 2) { cin >> l >> r; cout << query(1, l, r) << endl; } } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值