題目:已知N各數字X1~Xn,執行兩種操作:1、改變Xi為A;2、求區間[L,R]上元素乘積的符號。
分析:DS,線段樹。利用線段樹處理即可,每個數字正的取1,負的取-1,否則取0。
說明:╮(╯▽╰)╭。
#include <cstdlib>
#include <cstring>
#include <cstdio>
int data[100001];
//segment_tree__begin
typedef struct tnode
{
tnode* Lchild;
tnode* Rchild;
int Lvalue;
int Rvalue;
int Value;
}tnode;
tnode Node[200002];
tnode* Root;
int Count;
tnode* buildtree(int a, int b)
{
tnode* np = &Node[Count ++];
np->Lvalue = a;
np->Rvalue = b;
np->Value = 0;
if (a < b) {
np->Lchild = buildtree(a, (a+b)/2);
np->Rchild = buildtree((a+b)/2+1, b);
}else {
np->Lchild = NULL;
np->Rchild = NULL;
}
return np;
}
void segment_tree(int a, int b)
{
Count = 0;
Root = buildtree(a, b);
}
void Insert(tnode*r, int a, int b, int v)
{
if (r->Lvalue == a && r->Rvalue == b) {
r->Value = v;
return;
}
if (b <= r->Lchild->Rvalue) {
Insert(r->Lchild, a, b, v);
}else if (a >= r->Rchild->Lvalue) {
Insert(r->Rchild, a, b, v);
}
r->Value = r->Lchild->Value * r->Rchild->Value;
}
int Query(tnode* r, int a, int b)
{
if (r->Lvalue == a && r->Rvalue == b) {
return r->Value;
}else if (b <= r->Lchild->Rvalue) {
return Query(r->Lchild, a, b);
}else if (a >= r->Rchild->Lvalue) {
return Query(r->Rchild, a, b);
}else {
return Query(r->Lchild, a, r->Lchild->Rvalue) * Query(r->Rchild, r->Rchild->Lvalue, b);
}
}
void Insert(int a, int b, int v)
{
Insert(Root, a, b ,v);
}
int Query(int a, int b)
{
return Query(Root, a, b);
}
//segment_tree__end
int main()
{
int N, K, value1, value2, ans;
char operators[2];
while (~scanf("%d%d",&N,&K)) {
for (int i = 1; i <= N; ++ i) {
scanf("%d",&data[i]);
}
segment_tree(1, N);
for (int i = 1; i <= N; ++ i) {
if (data[i] > 0) {
Insert(i, i, 1);
}else if (data[i] < 0) {
Insert(i, i, -1);
}else {
Insert(i, i, 0);
}
}
for (int i = 1; i <= K; ++ i) {
scanf("%s%d%d",operators,&value1,&value2);
if (operators[0] == 'C') {
if (value2 > 0) {
Insert(value1, value1, 1);
}else if (value2 < 0) {
Insert(value1, value1, -1);
}else {
Insert(value1, value1, 0);
}
}else {
ans = Query(value1, value2);
if (ans > 0) {
printf("+");
}else if (ans < 0) {
printf("-");
}else {
printf("0");
}
}
}
puts("");
}
return 0;
}