zoukankan      html  css  js  c++  java
  • 树状数组BIT

    模板1

    #include<iostream>
    #include<cstdio>
    using namespace std;
    
    int n, m, c[500010];
    
    inline int read(){
        int s=0,w=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')   { if(ch=='-')w=-1; ch=getchar(); }
        while(ch>='0'&&ch<='9') { s=s*10+ch-'0';   ch=getchar(); }
        return s*w;
    }
    
    void update(int x, int y){ for(; x<=n; x+=x&-x)	c[x]+=y; }
    
    int sum(int x){
        int ans=0;
        for(; x; x-=x&-x)	ans+=c[x];
        return ans;
    }
    
    int main(){
        n=read(), m=read();
        for(int i=1, t; i<=n; i++)	t=read(), update(i, t);
        for(int i=1, k, x, y; i<=m; i++)
        {
            k=read(), x=read(), y=read();
            if(k==1)    update(x, y);
            else        printf("%d
    ", sum(y)-sum(x-1));
        }
        return 0;
    }
    

    模板2

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int N, M, C[500010], A[500010];
    inline int read()
    {
    	int s=0, w=1;
    	char ch=getchar();
    	while(ch<'0' || ch>'9'){ if(ch=='-') w=-1; ch=getchar();}
    	while(ch>='0'&&ch<='9'){ s=s*10+ch-'0';    ch=getchar();}
    	return s*w;
    }
    
    void update(int x, int y){ for(; x<=N; x+=x&-x)  C[x]+=y; }
    int sum(int x)
    {
    	int ans=0;
    	for(; x; x-=x&-x) ans+=C[x];
    	return ans;
    }
    
    int main(){
    	N=read(), M=read();
    	for(int i=1; i<=N; i++)
    	{
    		int t=read();
    		A[i]=t;
    		update(i, t-A[i-1]);
    	}
    	for(int i=1; i<=M; i++)
    	{
    		int t=read();
    		if(t==1)
    		{
    			int x, y, k;
    			x=read(), y=read(), k=read();
    			update(x, k);
    			update(y+1, -k);
    		}else
    		{
    			int x=read();
    			printf("%d
    ", sum(x));
    		}
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    男子头部胀痛案
    摩这些穴位可以对付突发的疾病
    跟师学习时的几个经验方
    黄褐斑良方--- 疏肺散斑汤
    食积咳嗽的特点
    牙痛用药口决(来源于网络暂未验证)
    20多年脚气起泡脱皮瘙痒外洗方治验案
    起死回生通关散
    腰突辩治心得(付济华)
    战痘良方--付济华
  • 原文地址:https://www.cnblogs.com/lfyzoi/p/10573136.html
Copyright © 2011-2022 走看看