POJ 3468 A Simple Problem with Integers (线段树区间更新模板)

POJ 3468

老是忘。。


#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
typedef long long ll;
struct node  {
	int l, r, m;
	ll val, mark;	//注意此处标记和值都要定义为long long 
};
const int MAXN = 100100;
node T[MAXN * 4 + 10];
ll a[MAXN];
void build(int root, int begin, int end) {
	T[root].mark = 0;
	T[root].l = begin;
	T[root].r = end;
	T[root].m = (begin + end) >> 1;
	if(begin == end) {
		T[root].val = a[begin];
	}
	else {
		build(root << 1, begin, T[root].m);
		build(root << 1 | 1, T[root].m + 1, end);
		T[root].val = T[root << 1].val + T[root << 1 | 1].val;
	}
}
ll query(int root, int l, int r) {
	if(T[root].l == l && T[root].r == r) {
		return T[root].val + T[root].mark * (r - l + 1);
	}
	else {
		if(T[root].mark != 0) {
			T[root << 1].mark += T[root].mark;
			T[root << 1 | 1].mark += T[root].mark;
			T[root].val += T[root].mark * (T[root].r - T[root].l + 1);
			T[root].mark = 0;
		}
		if(r <= T[root].m) return query(root << 1, l, r);
		else if(l > T[root].m) return query(root << 1 | 1, l, r);
		else return query(root << 1, l, T[root].m) + query(root << 1 | 1, T[root].m + 1, r);
	}
}
void updata(int root, int l, int r, int add) {
	if(T[root].l == l && T[root].r == r) {
		T[root].mark += add;
	}
	else {
		T[root].val += add * (r - l + 1);
		if(r <= T[root].m) updata(root << 1, l, r, add);
		else if(l > T[root].m) updata(root << 1 | 1, l, r, add);
		else {
			updata(root << 1, l, T[root].m, add);
			updata(root << 1 | 1, T[root].m + 1, r, add);
		}
	}
}
int main() {
	int n, q;
	while(~scanf("%d %d", &n, &q)) {
		int i;
		for(i = 1; i <= n; i++) {
			scanf("%I64d", a + i);
		}
		getchar();
		build(1, 1, n);
		char ch;
		int l, r, add;
		for(i = 0; i < q; i++) {
			scanf("%c %d %d", &ch, &l, &r);
			getchar();
			if(ch == 'Q') {
				printf("%I64d\n", query(1, l, r));
			}
			if(ch == 'C') {
				scanf("%d", &add);
				getchar();
				updata(1, l, r, add);
			}
		}
	}
	return 0;
}

另附一个线段树模板:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#define ll __int64
#define M 100007
using namespace std;
struct node
{
    ll l,r,mid,val,mark;
}tree[M<<2];
ll s[M];
void build(ll left,ll right,ll i)//建树
{
    tree[i].l=left;tree[i].r=right;
    tree[i].mid=(left+right)>>1;tree[i].mark=0;
    if(left==right){tree[i].val=s[left]; return;}
    build(left,tree[i].mid,i*2);
    build(tree[i].mid+1,right,i*2+1);
    tree[i].val=tree[i*2].val+tree[i*2+1].val;
}
void update(int pos,ll val,int i)//点更新
{
    tree[i].val+=val;
    if(tree[i].l==tree[i].r)return;
    if(pos<=tree[i].mid)update(pos,val,i*2);
    else update(pos,val,i*2+1);
}
//ll query(int left,int right,int i)//点查询
//{
//    if(tree[i].l==left&&tree[i].r==right)return tree[i].val;
//    if(right<=tree[i].mid)query(left,right,i*2);
//    else if(left>tree[i].mid)query(left,right,i*2+1);
//    else return query(left,tree[i].mid,i*2)+query(tree[i].mid+1,right,i*2+1);
//}
void update(int left,int right,ll val,int i)//区间更新
{
    if(tree[i].l==left&&tree[i].r==right){tree[i].mark+=val;return;}
    tree[i].val+=val*(right-left+1);
    if(tree[i].mid<left)update(left,right,val,2*i+1);
    else if(tree[i].mid>=right)update(left,right,val,2*i);
    else
    {
        update(left,tree[i].mid,val,2*i);
        update(tree[i].mid+1,right,val,2*i+1);
    }
}
ll query(int left,int right,int i)//区间查询
{
    if(tree[i].l==left&&tree[i].r==right)  return tree[i].val+tree[i].mark*(right-left+1);
    if(tree[i].mark!=0)
    {
        tree[i*2].mark+=tree[i].mark;tree[i*2+1].mark+=tree[i].mark;
        tree[i].val+=(tree[i].r-tree[i].l+1)*tree[i].mark;tree[i].mark=0;
    }
    if(tree[i].mid>=right){return query(left,right,i*2);}
    else if(tree[i].mid<left){return query(left,right,i*2+1);}
    else{return query(left,tree[i].mid,i*2)+query(tree[i].mid+1,right,i*2+1);}
}
int main()
{
    ll n,m,i,j,k;
    ll l,f,num;
    char str[5];
    while(scanf("%I64d%I64d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%I64d",&s[i]);
        build(0,n,1);

        for(i=0;i<m;i++)
        {
          //  printf("i=%I64d",i);
            scanf("%s",&str);
            if(str[0]=='Q')
            {
                scanf("%I64d%I64d",&l,&f);
                printf("%I64d\n",query(l,f,1));
            }
            if(str[0]=='C')
            {
                scanf("%I64d%I64d%I64d",&l,&f,&num);
                update(l,f,num,1);
            }
        }



    }
    return 0;
}


你可能感兴趣的:(POJ 3468 A Simple Problem with Integers (线段树区间更新模板))