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 ...
随机推荐
- PC端重置
-PC 一,meta <!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta cha ...
- python 定义实例方法
定义实例方法 一个实例的私有属性就是以__开头的属性,无法被外部访问,那这些属性定义有什么用? 虽然私有属性无法从外部访问,但是,从类的内部是可以访问的.除了可以定义实例的属性外,还可以定义实例的方法 ...
- ASP.Net MVC开发基础学习笔记(5):区域、模板页与WebAPI初步
一.区域—麻雀虽小,五脏俱全的迷你MVC项目 1.1 Area的兴起 为了方便大规模网站中的管理大量文件,在ASP.NET MVC 2.0版本中引入了一个新概念—区域(Area). 在项目上右击创建新 ...
- js的一些小笔记,(不定期更新)
2个$的用法$本身并无特定意义,它表示什么意思要看是如何定义的,如果没有定义就便是两个$,可能是变量名的开始.一般是一个函数,用来代替document.getElementByIdfunction $ ...
- 记一次小团队Git实践(中)
对于初学者,从使用上先入手,往往学的最快,并从中汲取教训,再回头更深入的学习,效果尤佳. 安装git 安装git自不必说,mac已经内置了git,linux下一个命令就能搞定,windows下需要下载 ...
- 用div,ul,input模拟select下拉框
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- XSS 跨站脚本攻击之构造剖析(二)
1.利用字符编码 (1)字符编码在跨站脚本中经常运用到,透过这种技巧,不仅能让XSS代码绕过服务端的过滤,还能更好的隐藏ShellCode (2)使用一个XSS编码工具,以便对字符串进行十进制和十六进 ...
- 启动windows的服务--《用delphi开发共享软件》-15.2桌面提示器
在dos 下用命令启动一个服务:NET START "Windows Desktop Reminder" 一下为用delphi启动服务: Function RunProcess(s ...
- 【CLR Via C#】第5章 基元类型、引用类型、值类型
第二遍看这本书,决定记录一下加深印象. 1,基元类型 什么事基元类型?基元类型是直接映射到FrameWork类库(FCL)中存在的类型,编译器直接支持的数据类型.比如int直接映射到System.In ...
- 原生 js 写分页
欢迎留言或者加本人QQ172360937咨询 这段代码是用原生 js 写的一个分页的效果 <!doctype html> <html lang="en"> ...