http://acm.bnu.edu.cn/v3/problem_show.php?pid=27853
Interval producttime limit:2000msmemory limit:131072kbthis problem would be judged onUVA. Original id:12532
64-bit integer IO format: %lld Java class name:Main Prevsubmitstatus Statistics discuss Next
[PDF Link]
It's normal to feel worried and tense, the day before a programming contest. To relax, you went out for a drink with some friends in a nearby pub. To keep your-the-the-decided to play the following game. To start, your friends would give you a sequence of
N integers
x1,
x2,...,
XN. Then, there would be
K rounds; At each round, your friends would issue a command, which can be:
- A change command, when your friends want to change one of the values in the sequence;
- A product command, when your friends give you II valuesI, J and ask you if the Produ CTxI x xi+1 x ... x xJ-1 x xJ is positive, negative or zero.
Since you in a pub, it was decided that the penalty for a wrong answer be to drink a pint of beer. You is worried this could affect your 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 the right to use your notebook. Since you trust more your coding skills than your math, you decided to write a program to the game.
InputEach test case is described using several lines. The first line contains integers
Nand
K, indicating respectively the number of elements in the sequence and the number of rounds of the game (1
N,
KThe second line contains
NIntegers
XI that represent the initial values of the sequence (-100
XI100 for
I= 1, 2,...,
N). Each of the next
KLines describes a command and starts with the uppercase letter it is either 'C' or 'P‘. If the letter is 'C', the line describes aChange command, and the letter are followed by II integers
I and
V indicating that
X I must receive the value
V ( 1
I
N and -100
v100). If the letter was 'P', the line describes a product command, and the letter was followed by and the INTEGERS
I and
J indicating that the product from
xI to
xJ, inclusive m UST be calculated (1
I
J
N). Within each test case there was at least one product command.
Output< Span class= "math" > For Each test case output a line with a string representing the result of the the product commands in th E test Case. The
i -th character of the string represents the result of The
i - th product command. If The result of the command is positive the character must be ' + ' (plus); If the result is negative, the character must be ' - ' (minus); If the result is zero, the character must be ' 0 ' (zero).
Sample Input
4 6-2 6 0-1c 1 10P 1 4C 3 7P 2 2C 4-5p 1 5-2 4 3P 1 2P 1 5C 4-5p 1 5P 4 5C 3 0P 1 5C 4-5c 4-5
Sample Output
0+-+-+-0
Topic Link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=3977
The main idea: C when the value of the update subscript I at v,p when the number of the subscript I to J of the product, if the product is positive output +, the product is a negative output-the product is 0 output 0.
Problem solving ideas: Bare section tree, T many times, pay attention to update and read value function return condition.
The code is as follows:
#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;elsetr[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;elsetr[root]=1;} return;} int mid= (L+R)/2;if (x<=mid) Change (l,mid,x,v,root*2); Elsechange (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"); elseprintf ("+");}} printf ("\ n");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVa Online Judge 12532-interval Product