zoukankan      html  css  js  c++  java
  • 【codevs1081】线段树练习2

    problem

    solution

    codes

    #include<iostream>
    using namespace std;
    const int maxn = 100010;
    #define lch p<<1
    #define rch p<<1|1
    struct node{
        int val, addmark;
    }sgt[maxn<<2];
    void pushdown(int p, int l, int r){
        if(!sgt[p].addmark)return;
        int t = sgt[p].addmark, m=l+r>>1;
        sgt[lch].addmark += t;
        sgt[rch].addmark += t;
        sgt[lch].val += t*(m-l+1);
        sgt[rch].val += t*(r-m);
        sgt[p].addmark = 0;
    }
    void update(int p, int l, int r, int ql, int qr, int v){
        if(l>qr || r<ql)return ;
        if(ql<=l && r<=qr){
            sgt[p].val += v*(r-l+1);
            sgt[p].addmark += v;
            return ;
        }
        int m = l+r>>1;
        if(ql<=m)update(lch,l,m,ql,qr,v);
        if(qr>m)update(rch,m+1,r,ql,qr,v);
        sgt[p].val = sgt[lch].val+sgt[rch].val;
    }
    int query(int p, int l, int r, int ql, int qr){
        if(ql<=l && r<=qr)return sgt[p].val;
        pushdown(p,l,r);
        int m = l+r>>1, ans = 0;
        if(ql<=m)ans += query(lch,l,m,ql,qr);
        if(qr>m)ans += query(rch,m+1,r,ql,qr);
        return ans;
    }
    int main(){
        int n, m;
        cin>>n;
        for(int i = 1; i <= n; i++){
            int x;  cin>>x;  update(1,1,n,i,i,x);
        }
        cin>>m;
        for(int i = 1; i <= m; i++){
            int op, x, y, k;
            cin>>op;
            if(op == 1){
                cin>>x>>y>>k;
                update(1,1,n,x,y,k);
            }else{
                cin>>x;
                cout<<query(1,1,n,x,x)<<"
    ";
            }
        }
        return 0;
    }
  • 相关阅读:
    快速幂,矩阵乘法,矩阵快速幂
    关于xor
    数位dp
    tarjan,割边,桥,割点
    RMQ,ST表,dp
    逆序对,树状数组,归并排序
    线段树
    dp,LCS
    清北 游
    青蛙的约会(扩展欧几里得)
  • 原文地址:https://www.cnblogs.com/gwj1314/p/9444730.html
Copyright © 2011-2022 走看看