由于很久很久以前做过线段树 所以现在只是挑几道复习一下,如果需要题目可以到http://blog.csdn.net/yrhsilence/article/details/5793699
由于本人被一个傻逼问题气到了=_=,而且刚好看到一个挺好的博客介绍线段树,So
http://blog.csdn.net/x314542916/article/details/7837276
(别打我
傻逼问题就是之前我的宏定义是#define max(a,b) (a>b?a:b)
这里会有一个问题就是max(a,max(b,c))的时候会变成
a>b>c?b:c?:a:b
可以自行运行一下(之前以为就算不加括号应该不会有什么影响)
#include<stdio.h>
#define max(a,b) a>b?a:b
int main()
{
printf("%d\n",max(1,max(3,max(4,5))));
}
/*
运行结果:
3
*/
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define sfor(i,a,h) for(i=h[a];i!=-1;i=e[i].next)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
#define cheak(i) printf("%d ",i)
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define inf 0x7fffffff
typedef long long LL;
#define maxn 50005
int s[maxn],tree[maxn*4];
char str[10];
void build(int l,int r,int index)
{
if(l==r)
{
tree[index]=s[l];
return ;
}
int mid=(l+r)/2;
build(l,mid,index*2);
build(mid+1,r,index*2+1);
tree[index]=tree[index*2]+tree[index*2+1];
}
void insert(int l,int r,int pos,int val,int index)
{
if(l==r&&l==pos)
{
tree[index]+=val;
return ;
}
int mid=(l+r)/2;
if(pos>mid) insert(mid+1,r,pos,val,index*2+1);
else insert(l,mid,pos,val,index*2);
tree[index]=tree[index*2]+tree[index*2+1];
}
int query(int l,int r,int left,int right,int index)
{
if(left<=l&&right>=r)
return tree[index];
int mid=(l+r)/2;
if(left>mid)
return query(mid+1,r,left,right,index*2+1);
else if(right<=mid)
return query(l,mid,left,right,index*2);
else
{
int sum=0;
sum=query(l,mid,left,right,index*2);
sum+=query(mid+1,r,left,right,index*2+1);
return sum;
}
}
int main()
{
int T,i,n,a,b,o=0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
rfor(i,1,n)
scanf("%d",&s[i]);
build(1,n,1);
printf("Case %d:\n",++o);
while(1)
{
scanf("%s",str);
if(!strcmp(str,"End")) break;
scanf("%d%d",&a,&b);
if(!strcmp(str,"Add"))
insert(1,n,a,b,1);
else if(!strcmp(str,"Sub"))
insert(1,n,a,-b,1);
else printf("%d\n",query(1,n,a,b,1));
}
}
return 0;
}