UVA 12532 Interval Product

线段树水题,忽略每个数的大小,只记住他们的正负即可,规规矩矩的代码。不过这是我第一次完全自己写的一次A的代码了。纪念一下。。。

#include <iostream>
#include <cstdio>
using namespace std;
#define N 100010 int tree[*N];
int x;
char ans[]; void build(int l,int r,int rt)
{
if(l == r)
{
cin>>x;
if(x>)
tree[rt] = ;
else if(x == )
tree[rt] = ;
else
tree[rt] = -;
return;
}
int mid = (l+r)/;
build(l,mid,*rt);
build(mid+,r,*rt+);
tree[rt] = tree[*rt]*tree[*rt+];
} void change(int l,int r,int pos,int val,int rt)
{
if(l == r)
{
if(val<)
tree[rt] = -;
else if(val == )
tree[rt] = ;
else
tree[rt] = ;
return;
}
int mid = (l+r)/;
if(pos<=mid)
change(l,mid,pos,val,*rt);
else
change(mid+,r,pos,val,*rt+);
tree[rt] = tree[*rt]*tree[*rt+];
} int query(int l,int r,int aa,int bb,int rt)
{
if(aa>r||bb<l)
return ;
if(aa<=l&&bb>=r)
return tree[rt];
int mid = (l+r)/;
if(bb<=mid)
return query(l,mid,aa,bb,*rt);
if(aa>mid)
return query(mid+,r,aa,bb,*rt+);
return query(l,mid,aa,bb,*rt)*query(mid+,r,aa,bb,*rt+);
}
int main()
{
int n,k,i,j;
int ka;
char ss[];
int aa,bb;
while(scanf("%d%d",&n,&k)!=EOF)
{
ka = ;
memset(ans,,sizeof(ans));
build(,n,);
for(i=;i<k;i++)
{
scanf("%s %d %d",ss,&aa,&bb);
if(ss[] == 'C')
{
change(,n,aa,bb,);
}
else
{
int res = query(,n,aa,bb,);
if(res > )
ans[ka++] = '+';
else if(res < )
ans[ka++] = '-';
else
ans[ka++] = '';
}
}
printf("%s\n",ans);
}
return ;
}
上一篇:How to install ZeroMQ on Ubuntu14.04


下一篇:在应用中嵌入Python:转