涓€缁?amp;&浜岀淮鏍戠姸鏁扮粍

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了涓€缁?amp;&浜岀淮鏍戠姸鏁扮粍相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%9f%a5%e8%af%a2' title='鏌ヨ'>鏌ヨ   BMI   rabbit   ase   pre   鍖洪棿淇敼   while   cout   lang   

楂樼骇鏍戠姸鏁扮粍璁茶В锛?a href="https://www.cnblogs.com/RabbitHu/p/BIT.html">https://www.cnblogs.com/RabbitHu/p/BIT.html

鏍戠姸鏁扮粍

鍗曠偣淇敼锛屽尯闂存煡璇?/h3>

https://loj.ac/problem/130

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define DOF 0x7f7f7f7f
#define endl 鈥榎n鈥?#define mem(a,b) memset(a,b,sizeof(a))
#define debug(case,x); cout<<case<<"  : "<<x<<endl;
#define open freopen("ii.txt","r",stdin)
#define close freopen("oo.txt","w",stdout)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
using namespace std;

#define int long long
int n;

const int maxn=1e6+10;
int d[maxn];
inline int lowbit(int x){return -x&x;}

void add(int x,int y){
    while(x<=n){
        d[x]+=y;x+=lowbit(x);
    }
}

int get_sum(int x){
    int res=0;
    while(x){
        res+=d[x];x-=lowbit(x);
    }
    return res;
}

signed main(){
    int q;cin>>n>>q;
    for(int i=1;i<=n;++i){
        int t;cin>>t;
        add(i,t);
    }
    while(q--){
        int p,x,y;cin>>p>>x>>y;
        if(p==1){
            add(x,y);
        }else{
            cout<<(get_sum(y)-get_sum(x-1))<<endl;
        }


    }
}

https://loj.ac/problem/131

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define DOF 0x7f7f7f7f
#define endl 鈥榎n鈥?#define mem(a,b) memset(a,b,sizeof(a))
#define debug(case,x); cout<<case<<"  : "<<x<<endl;
#define open freopen("ii.txt","r",stdin)
#define close freopen("oo.txt","w",stdout)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
using namespace std;

#define int long long
int n;
const int maxn=1e6+10;
int d[maxn],cha[maxn];
inline int lowbit(int x){return -x&x;}


void add(int x,int y){
    while(x<=n){
        d[x]+=y;x+=lowbit(x);
    }
}

int get_sum(int x){
    int res=0;
    while(x){
        res+=d[x];x-=lowbit(x);
    }
    return res;
}

signed main(){
    int q;cin>>n>>q;
    for(int i=1;i<=n;++i){
        cin>>cha[i];
        add(i,cha[i]-cha[i-1]);
    }
    while(q--){
        int op;cin>>op;
        if(op==1){
            int l,r,x;cin>>l>>r>>x;
            add(l,x);
            add(r+1,-x);
        }else{
            int i;cin>>i;
            cout<<get_sum(i)<<endl;

        }
    }

}

https://loj.ac/problem/132

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define DOF 0x7f7f7f7f
#define endl 鈥榎n鈥?#define mem(a,b) memset(a,b,sizeof(a))
#define debug(case,x); cout<<case<<"  : "<<x<<endl;
#define open freopen("ii.txt","r",stdin)
#define close freopen("oo.txt","w",stdout)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
using namespace std;

#define int long long
int n;
const int maxn=1e6+10;
int t[maxn];
inline int lowbit(int x){return -x&x;}

class Tree_arry{

public:
    int d[maxn];
    void add(int x,int y){
        while(x<=n){
            d[x]+=y;x+=lowbit(x);
        }
    }

    int query(int x){
        int res=0;
        while(x){
            res+=d[x];x-=lowbit(x);
        }
        return res;
    }
}b,bi;

int get_xum(int x){
    int res=0;
    res=(x+1)*b.query(x)-bi.query(x);
    return res;

}

signed main(){
    int q;cin>>n>>q;
    for(int i=1;i<=n;++i){
        cin>>t[i];
        b.add(i,t[i]-t[i-1]);
        bi.add(i,i*(t[i]-t[i-1]));
    }
    while(q--){
        int op;cin>>op;
        int l,r,x;cin>>l>>r;
        if(op==1){
            cin>>x;
            b.add(l,x);
            b.add(r+1,-x);
            bi.add(l,x*l);
            bi.add(r+1,-x*(r+1));
        }else{
            cout<<(get_xum(r)-get_xum(l-1))<<endl;

        }
    }

}

浜岀淮鏍戠姸鏁扮粍

鍗曠偣淇敼锛屽尯闂存煡璇?/h3>

https://loj.ac/submission/868327

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define DOF 0x7f7f7f7f
#define endl 鈥榎n鈥?#define mem(a,b) memset(a,b,sizeof(a))
#define debug(case,x); cout<<case<<"  : "<<x<<endl;
#define open freopen("ii.txt","r",stdin)
#define close freopen("oo.txt","w",stdout)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
using namespace std;

#define int long long
int n,m;
const int maxn=1e6+10;
const int MAXN=(1<<12)+10;

int d[MAXN][MAXN];
inline int lowbit(int x){return -x&x;}

void add(int x,int y,int val){

    while(x<=n){
        int ty=y;
        while(ty<=m){
            d[x][ty]+=val;ty+=lowbit(ty);
        }
        x+=lowbit(x);
    }


}

int query(int x,int y){
    int res=0;
    while(x){
        int ty=y;
        while(ty){
            res+=d[x][ty];
            ty-=lowbit(ty);
        }
        x-=lowbit(x);
    }
    return res;

}

int get_xum(int a,int b,int c,int d){
    return query(c,d)-query(c,b-1)-query(a-1,d)+query(a-1,b-1);

}

signed main(){
    cin>>n>>m;
    int op,a,b,c,d,k;
    while(cin>>op){
        if(op==1){
            cin>>a>>b>>k;
            add(a,b,k);
        }else{
            cin>>a>>b>>c>>d;
            cout<<get_xum(a,b,c,d)<<endl;
        }
    }

}

鍖洪棿淇敼锛屽崟鐐规煡璇?/h3>

https://loj.ac/problem/134

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define DOF 0x7f7f7f7f
#define endl 鈥榎n鈥?#define mem(a,b) memset(a,b,sizeof(a))
#define debug(case,x); cout<<case<<"  : "<<x<<endl;
#define open freopen("ii.txt","r",stdin)
#define close freopen("oo.txt","w",stdout)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
using namespace std;

#define int long long
int n,m;
const int maxn=1e6+10;
const int MAXN=(1<<12)+10;

int d[MAXN][MAXN];
inline int lowbit(int x){return -x&x;}

void add(int x,int y,int val){

    while(x<=n){
        int ty=y;
        while(ty<=m){
            d[x][ty]+=val;ty+=lowbit(ty);
        }
        x+=lowbit(x);
    }


}

int query(int x,int y){
    int res=0;
    while(x){
        int ty=y;
        while(ty){
            res+=d[x][ty];
            ty-=lowbit(ty);
        }
        x-=lowbit(x);
    }
    return res;

}

void update_add(int a,int b,int c,int d,int k){
    add(a,b,k);
    add(a,d+1,-k);
    add(c+1,b,-k);
    add(c+1,d+1,k);
}

signed main(){
    cin>>n>>m;
    int op,a,b,c,d,k;
    while(cin>>op>>a>>b){

        if(op==1){
            cin>>c>>d>>k;
            update_add(a,b,c,d,k);
        }else{
            cout<<query(a,b)<<endl;
        }
    }

}

鍖洪棿淇敼锛屽尯闂存煡璇?/h3>

https://loj.ac/problem/135

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define DOF 0x7f7f7f7f
#define endl 鈥榎n鈥?#define mem(a,b) memset(a,b,sizeof(a))
#define debug(case,x); cout<<case<<"  : "<<x<<endl;
#define open freopen("ii.txt","r",stdin)
#define close freopen("oo.txt","w",stdout)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
using namespace std;

#define int long long
int n,m;
const int maxn=1e6+10;

inline int lowbit(int x){return -x&x;}

class Tree_arry{

public:
    int d[3000][3000];

    void add(int x,int y,int val){
        while(x<=n){
            int ty=y;
            while(ty<=m){
                d[x][ty]+=val;ty+=lowbit(ty);
            }
            x+=lowbit(x);
        }
    }

    int query(int x,int y){
        int res=0;
        while(x){
            int ty=y;
            while(ty){
                res+=d[x][ty];
                ty-=lowbit(ty);
            }
            x-=lowbit(x);
        }
    return res;
    }

}b,bi,bj,bij;



void add_all(int x,int y,int val){
    b.add(x,y,val);
    bi.add(x,y,val*x);
    bj.add(x,y,val*y);
    bij.add(x,y,val*x*y);
}

void update_add(int x1,int y1,int x2,int y2,int k){
    add_all(x1,y1,k);
    add_all(x2+1,y2+1,k);
    add_all(x2+1,y1,-k);
    add_all(x1,y2+1,-k);
}

int get_xum(int x,int y){
    return (x+1)*(y+1)*b.query(x,y)-(y+1)*bi.query(x,y)-(x+1)*bj.query(x,y)+bij.query(x,y);

}

int get_sum(int x1,int y1,int x2,int y2){
    return get_xum(x2,y2)-get_xum(x2,y1-1)-get_xum(x1-1,y2)+get_xum(x1-1,y1-1);
}
signed main(){
    cin>>n>>m;
    int op,x1,y1,x2,y2,d;
    while(cin>>op){
        cin>>x1>>y1>>x2>>y2;
        if(op==1){
            cin>>d;
            update_add(x1,y1,x2,y2,d);
        }else{
            cout<<get_sum(x1,y1,x2,y2)<<endl;

        }
    }

}

以上是关于涓€缁?amp;&浜岀淮鏍戠姸鏁扮粍的主要内容,如果未能解决你的问题,请参考以下文章

浜岀淮鏁扮粍瑕嗙洊

1.浜岀淮鏁扮粍涓殑鏌ユ壘

浠嬬粛涓€涓瓧绗︿覆杞崲鐨勫嚱鏁扮粍SCMS_CONV

leetcode-64-鏈€灏忚矾寰勫拰

USACO 2017 FEB Why Did the Cow Cross the Road III G

leetcode 4. 瀵绘壘涓や釜姝e簭鏁扮粍鐨勪腑浣嶆暟