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 ...
随机推荐
- 如何hash一条有向边
之前这个问题还困扰了我好久,但是现在我才明白这个很蠢的问题 那就是(3,7)(4,9)(3,3)这种有向序点对(括号可能用的不对) 我们可以变成对"(3,7)"字符串的hash,当 ...
- html5 head头标签
桌面端开发中,meta标签通常用来为搜索引擎优化(SEO)及 robots定义页面主题,或者是定义用户浏览器上的cookie:它可以用于鉴别作者,设定页面格式,标注内容提要和关键字:还可以设置页面使其 ...
- Jmeter 检查点
Jmeter的检查点就是插入个断言,但用下来不好用,没LR好用,先放放.
- Android源码学习之模板方法模式应用
一.模板方法模式定义 模板方法模式定义: defines the skeleton of an algorithm in a method, deferring some steps to subcl ...
- LoadRunner检查点
web_reg_find("Text=ABC", "SaveCount=abc_count", LAST);51Testing软件测试网V?2Rs.J Gmdw ...
- JS Number对象
数字属性 MAX_VALUE MIN_VALUE NEGATIVE_INFINITY POSITIVE_INFINITY NaN prototype constructor 数字方法 toExpone ...
- 廖雪峰js教程笔记7 基本类型和包装类型
在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用typeof操作符获取对象的类型,它总是返回一个字符串: typeof 123; // ' ...
- jquery.easing.js的使用
jquery.easing.js是个好东西,各种动画效果扩展,加强和丰富了jquery自带的各种动画函数 box点击就会像移动出弹簧效果,$(obj).animate(目前状态,时间,效果,回调函数) ...
- 每天一个linux命令--locate
linux下,不知道自己安装的程序放在哪里了,可以使用locate命令进行查找. [hongye@dev107 ~]$ locate activemq.xml /home/hongye/hongyeC ...
- svn服务器端的客户端自动更新
先说这个方式的一个弊端,那就是服务器端这边代码不能与svn服务器出现冲突,一旦冲突就失效. 实现原理是,利用svn版本库,hooks目录的bat钩子开展工作. 共在hooks目录下建立3个文件如下:p ...