Since you are at a pub, it was decided that the penalty for a wrong answer is to drink a pint of beer. You are worried this could affect you negatively at the next day‘s contest, and you don‘t want to check if Ballmer‘s peak theory is correct. Fortunately, your friends gave you the right to use your notebook. Since you trust more your coding skills than your math, you decided to write a program to help you in the game.
4 6 -2 6 0 -1 C 1 10 P 1 4 C 3 7 P 2 2 C 4 -5 P 1 4 5 9 1 5 -2 4 3 P 1 2 P 1 5 C 4 -5 P 1 5 P 4 5 C 3 0 P 1 5 C 4 -5 C 4 -5
0+- +-+-0
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3977
题目大意:C时更新下标I处的值为V,P时计算下标I到J的数的乘积,若乘积为正输出+,乘积为负输出-,乘积为0输出0。
解题思路:裸线段树,T了很多次,注意更新和读取值函数的返回条件。
代码如下:
#include <cstdio> #include <cstring> const int maxn=100005; int a[maxn],tr[4*maxn]; void build(int l,int r,int root) { if(l==r) { scanf("%d",&a[l]); if(a[l]==0) tr[root]=0; else if(a[l]<0) tr[root]=-1; else tr[root]=1; return ; } int mid=(l+r)/2; build(l,mid,root*2); build(mid+1,r,root*2+1); tr[root]=tr[root*2]*tr[root*2+1]; } void change(int l,int r,int x,int v,int root) { if(l==r) { if(l==x) { a[l]=v; if(a[l]==0) tr[root]=0; else if(a[l]<0) tr[root]=-1; else tr[root]=1; } return ; } int mid=(l+r)/2; if(x<=mid) change(l,mid,x,v,root*2); else change(mid+1,r,x,v,root*2+1); tr[root]=tr[root*2]*tr[root*2+1]; } int product(int l,int r,int x,int y,int root) { if(l>=x&&r<=y) return tr[root]; int mid=(l+r)/2,tm=1; if(x<=mid) tm*=product(l,mid,x,y,root*2); if(y>mid) tm*=product(mid+1,r,x,y,root*2+1); return tm; } int main() { int n,k,x,y; while(scanf("%d%d",&n,&k)!=EOF) { char c; int m=0; memset(a,0,sizeof(a)); build(1,n,1); for(int i=0;i<k;i++) { getchar(); scanf("%c%d%d",&c,&x,&y); if(c=='C') { if(a[x]==0&&y==0||a[x]<0&&y<0||a[x]>0&&y>0) continue; a[x]=y; change(1,n,x,y,1); } else { int tm=product(1,n,x,y,1); if(tm<0) printf("-"); else if(tm==0) printf("0"); else printf("+"); } } printf("\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
UVa Online Judge 12532 - Interval Product
标签:线段树
原文地址:http://blog.csdn.net/criminalcode/article/details/47460727