UVA12532 Interval Product(线段树)

来源:互联网 发布:淘宝otc药品货到付款 编辑:程序博客网 时间:2024/06/05 05:51

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

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


线段树典型题目,问的是乘积正负,所以更新为0, -1, 1即可。


AC代码:


#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"using namespace std;const int MAXN = 1e6 + 5;int tree[MAXN], n, k;char op[2];void build(int l, int r, int o){if(l == r) {int x;scanf("%d", &x);if(x < 0) tree[o] = -1;else if(x == 0) tree[o] = 0;else tree[o] = 1;return;}int mid = (l + r) >> 1;build(l, mid, 2 * o);build(mid + 1, r, 2 * o + 1);tree[o] = tree[2 * o] * tree[2 * o + 1];}void change(int l, int r, int pos, int val, int o){if(l == r) {if(val < 0) tree[o] = -1;else if(val == 0) tree[o] = 0;else tree[o] = 1;return;}int mid = (l + r) >> 1;if(pos <= mid) change(l, mid, pos, val, 2 * o);else change(mid + 1, r, pos, val, 2 * o + 1);tree[o] = tree[2 * o] * tree[2 * o + 1];}int query(int l, int r, int a, int b, int o){if(a > r || b < l) return 0;if(a <= l && b >= r) return tree[o];int mid = (l + r) >> 1;if(b <= mid) return query(l, mid, a, b, 2 * o);if(a > mid) return query(mid + 1, r, a, b, 2 * o + 1);return query(l, mid, a, b, 2 * o) * query(mid + 1, r, a, b, 2 * o + 1);}int main(int argc, char const *argv[]){while(scanf("%d%d", &n, &k) != EOF) {build(1, n, 1);while(k--) {int a, b;scanf("%s%d%d", op, &a, &b);if(op[0] == 'C') change(1, n, a, b, 1);else {int res = query(1, n, a, b, 1);if(res < 0) printf("-");else if(res == 0) printf("0");else printf("+");}}printf("\n");}return 0;}


1 0