UVA12532 线段树(单点更新,区间求乘积的正负)
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 ;
}
UVA12532 线段树(单点更新,区间求乘积的正负)的更多相关文章
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
		
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
 - HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
		
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
 - POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
		
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
 - hdu 1166线段树 单点更新 区间求和
		
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
 - hdu1166(线段树单点更新&区间求和模板)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...
 - hdu2795(线段树单点更新&区间最值)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...
 - hihoCoder #1586 : Minimum-结构体版线段树(单点更新+区间最值求区间两数最小乘积) (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
		
#1586 : Minimum Time Limit:1000ms Case Time Limit:1000ms Memory Limit:256MB Description You are give ...
 - HDU 3308 LCIS(线段树单点更新区间合并)
		
LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...
 - 【HDU】1754 I hate it ——线段树 单点更新 区间最值
		
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
 - hdu 1754 I Hate It 线段树 单点更新 区间最值
		
线段树功能:update:单点更新 query:区间最值 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define r ...
 
随机推荐
- VisualStudio一打开工程就崩溃-重打开output显示We were unable to automatically populate your Visual Studio Online accounts.
			
and this method exactly effected on me
 - LoadRunner下载文件脚本
			
LoadRunner下载文件脚本 在看普泽关于pezybase的测试报告的时候,发现里面有用到jmeter(http协议)并发测试下载文件,考虑到后面可能需要在公司pezybase的并发下载,把之前 ...
 - MySQL常用数据类型小结
			
在 MySQL 中,有三种主要的类型:字符串.数字和日期/时间类型. 目录 [隐藏] 1 字符串类型 2 数值类型 3 日期和时间类型 4 使用建议 5 艺搜参考 字符串类型 CHAR 0-255字 ...
 - CentOS升级Python 2.6到2.7
			
查看python的版本 python -V Python 2.6.6 下载Python Python-2.7.8.tar.xz 链接:http://pan.baidu.com/s/1i4 ...
 - (转)ACM next_permutation函数
			
转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记 (1) int 类型的next_permuta ...
 - AngularJS学习之模块
			
1.模块定义了一个应用程序:模块是应用程序中不同部分的容器:模块是应用控制器的容器:控制器通常属于一个模块 2.创建模块:你可以通过AngularJS的angular.module函数来创建模块: & ...
 - 我的c++学习(6)默认参数和内联函数
			
默认参数 一般情况下,函数调用时实参个数应与形参相同,但为了更方便地使用函数,C++也允许定义具有默认参数的函数,这种函数调用时实参个数可以与形参不相同.“默认参数”指在定义或声明函数时为形参指定默认 ...
 - 让Docker使用国内的镜像服务
			
使用 Docker 的时候,需要经常从官方获取镜像,但是由于显而易见的网络原因,拉取镜像的过程非常耗时,严重影响使用 Docker 的体验.因此 DaoCloud 推出 Docker 加速器解决这个难 ...
 - 并查集 + 线段树 LA 4730 Kingdom
			
题目传送门 题意:训练指南P248 分析:第一个操作可以用并查集实现,保存某集合的最小高度和最大高度以及城市个数.运用线段树成端更新来统计一个区间高度的个数,此时高度需要离散化.这题两种数据结构一起使 ...
 - BZOJ4607 : [PA2015 Final]Edycja
			
显然做完操作$2$后再做操作$1$. 建立一个$26$个点的有向图,每个点只有一条出边,$i$->$j$表示$i$最终变成了$j$,边权为一开始是$i$,最后不是$j$的位置个数,如果$i\ne ...