It’s normal to feel worried and tense the day before a programming contest. To relax, you went out fora drink with some friends in a nearby pub. To keep your mind sharp for the next day, you decided toplay the following game. To start, your friends will give you a sequence of N integers X1, X2, . . . , XN.Then, there will be K rounds; at each round, your friends will issue a command, which can be:• a change command, when your friends want to change one of the values in the sequence; or• a product command, when your friends give you two values I, J and ask you if the productXI × XI+1 × . . . × XJ−1 × XJ is positive, negative or zero.Since you are at a pub, it was decided that the penalty for a wrong answer is to drink a pint ofbeer. You are worried this could affect you negatively at the next day’s contest, and you don’t wantto check if Ballmer’s peak theory is correct. Fortunately, your friends gave you the right to use yournotebook. Since you trust more your coding skills than your math, you decided to write a program tohelp you in the game.
Input
Each test case is
described using several lines. The first line contains two integers N
and K, indicatingrespectively the number of elements in the sequence and
the number of rounds of the game (1 ≤N, K ≤ 105).
The second line contains N integers Xi that represent the initial
values of the sequence(−100 ≤ Xi ≤ 100 for i = 1, 2, . . . , N). Each of
the next K lines describes a command and starts withan uppercase letter
that is either ‘C’ or ‘P’. If the letter is ‘C’,
the line describes a change command, andthe letter is followed by two
integers I and V indicating that XI must receive the value V (1 ≤ I ≤
Nand −100 ≤ V ≤ 100). If the letter is ‘P’, the line describes a product
command, and the letteris followed by two integers
I and J indicating that the product from XI to XJ , inclusive must
becalculated (1 ≤ I ≤ J ≤ N). Within each test case there is at least
one product command.
Output
For each test case output
a line with a string representing the result of all the product
commands inthe test case. The i-th character of the string represents
the result of the i-th product command. If theresult
of the command is positive the character must be ‘+’ (plus); if the
result is negative the charactermust be ‘-’ (minus); if the result is
zero the character must be ‘0’ (zero).
Sample Input
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
Sample Output
0+-
+-+-0
题意:
给出一串数,有两种操作,C,I,V表示将I位置的数改为V;P,I,J表示求I到J位置的所有数的乘积的符号,+,-,0;
代码:
/*
吧pushup 的求和改为求乘积即可,由于数据较大可以吧正数用1代替,负数用-1,代替,注意线段树数组要多开
大4倍。该题输出是一个一个输出的。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX=;
int n,k;
int sum[MAX];
void pushup(int rt)
{
sum[rt]=sum[rt<<]*sum[rt<<|];
}
void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&sum[rt]);
if(sum[rt]>) sum[rt]=;
else if(sum[rt]<) sum[rt]=-;
else sum[rt]=;
return;
}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
}
void update(int id,int val,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=val;
return;
}
int mid=(l+r)>>;
if(id<=mid)
update(id,val,l,mid,rt<<);
else
update(id,val,mid+,r,rt<<|);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
return sum[rt];
int mid=(l+r)>>;
int s=;
if(L<=mid)
s*=query(L,R,l,mid,rt<<);
if(R>mid)
s*=query(L,R,mid+,r,rt<<|);
return s;
}
int main()
{
char ch[];
int I,J;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(sum,,sizeof(sum));
build(,n,);
while(k--)
{
scanf("%s%d%d",ch,&I,&J);
if(ch[]=='C')
{
if(J>)
update(I,,,n,);
else if(J<)
update(I,-,,n,);
else update(I,,,n,);
}
else if(ch[]=='P')
{
int num=query(I,J,,n,);
if(num>) printf("+");
else if(num<) printf("-");
else printf("");
}
}
printf("\n");
}
return ;
}