P3368 【模板】树状数组 2

https://www.luogu.org/problemnew/show/P3368

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;

const int MAXN=500005;
int n,m,l,k,r,p;
long long s,c[MAXN],x,y;

int look(int u){return u&(-u);}

void add(int x, long long num) {
	for(int i=x;i<=n;i+=look(i))
		c[i]+=num;
}

long long query(int x) {
    long long ans = 0;
	for(int i=x;i>0;i-=look(i)) ans+=c[i];
    return ans;
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){scanf("%lld",&y);add(i,y-x);x=y;}
	while(m--){
		scanf("%d",&k);
		if(k==1) {
			scanf("%d%d%lld",&l,&r,&s);
			add(l,s);
            add(r+1,-s);
		}
		if(k==2){scanf("%d",&p);printf("%lld\n",query(p));}
	}
	return 0;
}
上一篇:MbUnit Introduction


下一篇:Captivate 2017: First Look Captivate 2017入门教程 Lynda课程中文字幕