uva 12532 - Interval Product

一个简单的线段树;

被我看错题了,浪费一个半小时;

 1 #include<cstdio>
 2 #define maxn 500005
 3 using namespace std;
 4 
 5 struct tree
 6 {
 7     int l,r,value;
 8     tree *right,*left;
 9 }tr[maxn];
10 int nonocount;
11 int ans;
12 
13 void build(tree *rt,int l,int r)
14 {
15     rt->l=l;
16     rt->r=r;
17     if(l==r)
18     {
19         scanf("%d",&rt->value);
20         if(rt->value>0)rt->value=1;
21         else if(rt->value==0)rt->value=0;
22         else rt->value=-1;
23         return;
24     }
25     nonocount++;
26     rt->left=tr+nonocount;
27     nonocount++;
28     rt->right=tr+nonocount;
29     int mid=(l+r)>>1;
30     build(rt->left,l,mid);
31     build(rt->right,mid+1,r);
32     rt->value=(rt->left->value)*(rt->right->value);
33 }
34 
35 void insert(tree *rt,int p,int x)
36 {
37     if(rt->l==p&&rt->r==p)
38     {
39         rt->value=x;
40         if(rt->value>0)rt->value=1;
41         else if(rt->value==0)rt->value=0;
42         else rt->value=-1;
43         return;
44     }
45     int mid=(rt->r+rt->l)>>1;
46     if(p<=mid)insert(rt->left,p,x);
47     else insert(rt->right,p,x);
48     rt->value=(rt->left->value)*(rt->right->value);
49 }
50 
51 void query(tree *rt,int l,int r)
52 {
53     if(l==rt->l&&r==rt->r)
54     {
55         ans=ans*(rt->value);
56         return;
57     }
58     int mid=(rt->l + rt->r)/2;
59     if(r<=mid)query(rt->left,l,r);
60     else if(l>mid)query(rt->right,l,r);
61     else
62     {
63         query(rt->left,l,mid);
64         query(rt->right,mid+1,r);
65     }
66 }
67 
68 char s[10];
69 int main()
70 {
71 //    freopen("in.txt","r",stdin);
72     int n,k,a,b;
73     while(scanf("%d%d",&n,&k)!=EOF)
74     {
75         nonocount=0;
76         build(tr,1,n);
77         for(int i=0;i<k;i++)
78         {
79             scanf("%s",&s);
80             if(s[0]=='C')
81             {
82                 scanf("%d%d",&a,&b);
83                 insert(tr,a,b);
84             }
85             else if(s[0]=='P')
86             {
87                 ans=1;
88                 scanf("%d%d",&a,&b);
89                 query(tr,a,b);
90                 if(ans==0)printf("0");
91                 else if(ans>0)printf("+");
92                 else if(ans<0)printf("-");
93             }
94         }
95         puts("");
96     }
97     return 0;
98 }
View Code

 

posted @ 2013-10-26 17:53  Yours1103  阅读(154)  评论(0编辑  收藏  举报