看人家的AC代码真的好羡慕啊 我的就是不AC啊 要哭啦
</pre><pre code_snippet_id="1649084" snippet_file_name="blog_20160415_1_1692645" name="code" class="cpp">#include <iostream>
#include<cstdio>
#include<algorithm>
const int N = 50000;
struct mtree
{
int l, r, sum;
}tree[N*4+1];
using namespace std;
void build(int node, int b, int e)
{
tree[node].l = b;
tree[node].r = e;
if(b == e)
{
return;
}
int mid = (b+e)>>1;
if(b<=mid)
build(node<<1, b, mid);
if(e>mid)
build(node<<1|1, mid+1, e);
}
void update(int node, int b, int e, int add)
{
if(tree[node].l >= b && tree[node].r <= e)
{
tree[node].sum += add;
return;
}
int mid = (tree[node].l + tree[node].r)>>1;
if(b <= mid)
update(node<<1, b, e, add);
if(e > mid)
update(node<<1|1, b, e, add);
tree[node].sum = tree[node<<1].sum + tree[node<<1|1].sum;
}
int question2(int node, int b, int e)
{
if(tree[node].l >= b && tree[node].r <=e)
{
return tree[node].sum;
}
int x = 0;
if(b<=tree[node<<1].r)
x += question2(node<<1, b, e);
if(e>=tree[node<<1|1].l)
x += question2(node<<1|1, b, e);
return x;
}
int main()
{
int T;
int w = 0;
scanf("%d", & T);
while(T--)
{
int m;
int k;
int l, r;
char c[100];
printf("Case ");
printf("%d", w+1);
printf(":\n");
scanf("%d", &m);
build(1,1,m);
for(int i=1; i<=m; i++)
{
scanf("%d", &k);
update(1, i, i, k);
}
while(scanf("%s", c))
{
if(c[0] == 'E')
break;
else
{
scanf("%d%d", &l, &r);
if(c[0] == 'Q')
printf("%d\n", question2(1, l, r));
else if(c[0] == 'A')
update(1, l, l, r);
else if(c[0] == 'S')
update(1, l, l, -r);
}
}
}
return 0;
}
/*
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
*/
经过一番奋斗之后 我的代码终于AC啦
#include <iostream>
#include<cstdio>
#include<cstring>
struct mtree
{
int l, r, v, sum;
}tree[200010];
using namespace std;
void build(int node, int b, int e)
{
tree[node].v = tree[node].sum = 0;
tree[node].l = b;
tree[node].r = e;
int mid = (b+e)>>1;
if(b == e)
{
return;
}
if(b<=mid)
build(node<<1, b, mid);
if(e>mid)
build(node<<1|1, mid+1, e);
}
void update(int node, int b, int e, int add)
{
if(tree[node].l >=b && tree[node].r <=e)
{
tree[node].sum += add;
tree[node].v += add;
return;
}
int mid = (tree[node].l +tree[node].r)>>1;
if(b<=mid)
update(node<<1, b, e, add);
if(e>mid)
update(node<<1|1, b, e, add);
tree[node].sum = tree[node<<1].sum + tree[node<<1|1].sum;
}
int query(int node, int b, int e)
{
if(tree[node].l >= b && tree[node].r <= e)
{
return tree[node].sum;
}
int x = 0;
if(b<=tree[node<<1].r)
x += query(node<<1, b, e);
if(e>=tree[node<<1|1].l)
x += query(node<<1|1, b, e);
return x;
}
int main()
{
int T;
int l, r;
int n;
int k;
int w = 0;
char c[20];
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
build(1,1,n);
for(int i=1; i<=n; i++)
{
scanf("%d", &k);
update(1,i,i,k);
}
printf("Case %d:\n", ++w);
while(scanf("%s", c) && strcmp(c,"End")!=0)
{
scanf("%d%d", &l, &r);
if(strcmp(c,"Query") == 0)
{
printf("%d\n",query(1,l,r));
}
else if(strcmp(c,"Add")==0)
update(1,l,l,r);
else if(strcmp(c, "Sub")==0)
update(1,l,l,-r);
}
}
return 0;
}
可以借鉴的代码~~~~
#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 50000
struct mtree
{
int b,e;//b-e为区间
int sum;
}tree[maxn*4];
void build(int node,int b,int e)
{
tree[node].b=b;
tree[node].e=e;
tree[node].sum=0;
if(b==e) return;
int mid=(b+e)/2;
//左儿子
if(b<=mid)
{
build(node*2,b,mid);
}
//右儿子
if(mid<e)
build(node*2+1,mid+1,e);
}
void update(int node,int n,int x)
{
if(tree[node].b==tree[node].e && tree[node].e ==n)
{
tree[node].sum+=x;
return;
}
int mid=(tree[node].b+tree[node].e)/2;
if(n<=mid) update(node*2,n,x);//往左查找
else update(node*2+1,n,x);
tree[node].sum=tree[node*2].sum+tree[node*2+1].sum;
}
int query(int node,int b,int e)
{
int ans1=0,ans2=0;
if(tree[node].b>=b&&tree[node].e<=e)//查询空间包括了树的区间才能取值
{
return tree[node].sum;
}
int mid=(tree[node].b+tree[node].e)/2;
if(b<=mid)
ans1=query(node*2,b,e);
if(mid<e)
ans2=query(node*2+1,b,e);
return ans1+ans2;
}
int main()
{
int t,i,j;
scanf("%d",&t);
for(j=1;j<=t;++j)
{
int n,a;
char s[10];
int x,y;
scanf("%d",&n);
build(1,1,n);//建树
for(i=1;i<=n;++i)
{
scanf("%d",&a);
update(1,i,a);
}
printf("Case %d:\n",j);
while(scanf("%s",s))
{
if(s[0]=='E') break;
if(s[0]=='A')
{
scanf("%d%d",&x,&y);
update(1,x,y);
}
if(s[0]=='S')
{
scanf("%d%d",&x,&y);
update(1,x,-y);
}
if(s[0]=='Q')
{
scanf("%d%d",&x,&y);
printf("%d\n",query(1,x,y));
}
}
}
return 0;
}