UVa Online Judge 12532 - Interval Product

UVA - 12532 Interval Product

http://acm.bnu.edu.cn/v3/problem_show.php?pid=27853

Interval Product

Time Limit: 2000ms

Memory Limit: 131072KB

This problem will be judged on UVA. Original ID: 12532

64-bit integer IO format: %lld Java class name:
Main

Prev

Submit
Status Statistics Discuss

Next

Type:

None

[PDF Link]

It‘s normal to feel worried and tense the day before a programming contest. To relax, you went out for a drink with some friends in a nearby pub. To keep your mind sharp for the next day, you decided to play 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 product
    XI x XI+1
    x
    ... x XJ-1 x 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 of beer. You are worried this could affect you negatively at the next day‘s contest, and you don‘t want to check if Ballmer‘s peak theory is correct. Fortunately,
your friends gave you the right to use your notebook. Since you trust more your coding skills than your math, you decided to write a program to help you in the game.

Input

Each test case is described using several lines. The first line contains two integers
N and K, indicating respectively the number of elements in the sequence and the number of rounds of the game (
1N,
K105). The second line contains
N integers Xi that represent the initial values of the sequence (
-100Xi100
for i = 1, 2,..., N). Each of the next
K lines describes a command and starts with an uppercase letter that is either `C‘ or `P‘. If the letter is `C‘, the line describes a
change command, and the letter is followed by two integers
I and V indicating that XI must receive the value
V ( 1IN
and -100V100).
If the letter is `P‘, the line describes a product command, and the letter is followed by two integers
I and J indicating that the product from
XI to XJ, inclusive must be calculated (
1IJN).
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 in the test case. The i-th character of the string represents the result of the
i-th product command. If the result of the command is positive the character must be `+‘ (plus); if the result is negative the character must 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

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3977

题目大意:C时更新下标I处的值为V,P时计算下标I到J的数的乘积,若乘积为正输出+,乘积为负输出-,乘积为0输出0。

解题思路:裸线段树,T了很多次,注意更新和读取值函数的返回条件。

代码如下:

#include <cstdio>
#include <cstring>
const int maxn=100005;
int a[maxn],tr[4*maxn];
void build(int l,int r,int root)
{
	if(l==r)
	{
		scanf("%d",&a[l]);
		if(a[l]==0)
			tr[root]=0;
		else if(a[l]<0)
			tr[root]=-1;
		else
			tr[root]=1;
		return ;
	}
	int mid=(l+r)/2;
	build(l,mid,root*2);
	build(mid+1,r,root*2+1);
	tr[root]=tr[root*2]*tr[root*2+1];
}
void change(int l,int r,int x,int v,int root)
{
	if(l==r)
	{
		if(l==x)
		{
			a[l]=v;
			if(a[l]==0)
				tr[root]=0;
			else if(a[l]<0)
				tr[root]=-1;
			else
				tr[root]=1;
		}
		return ;
	}
	int mid=(l+r)/2;
	if(x<=mid)
	change(l,mid,x,v,root*2);
	else
	change(mid+1,r,x,v,root*2+1);
	tr[root]=tr[root*2]*tr[root*2+1];
}
int product(int l,int r,int x,int y,int root)
{
	if(l>=x&&r<=y)
		return tr[root];
	int mid=(l+r)/2,tm=1;
	if(x<=mid)
		tm*=product(l,mid,x,y,root*2);
	if(y>mid)
		tm*=product(mid+1,r,x,y,root*2+1);
	return tm;
}
int main()
{
	int n,k,x,y;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		char c;
		int m=0;
		memset(a,0,sizeof(a));
		build(1,n,1);
		for(int i=0;i<k;i++)
		{
			getchar();
			scanf("%c%d%d",&c,&x,&y);
			if(c=='C')
			{
				if(a[x]==0&&y==0||a[x]<0&&y<0||a[x]>0&&y>0)
					continue;
				a[x]=y;
				change(1,n,x,y,1);
			}
			else
			{
				int tm=product(1,n,x,y,1);
				if(tm<0)
					printf("-");
				else if(tm==0)
					printf("0");
				else
					printf("+");
			}
		}
		printf("\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-22 10:07:18

UVa Online Judge 12532 - Interval Product的相关文章

UVA - 12532 Interval Product

http://acm.bnu.edu.cn/v3/problem_show.php?pid=27853 Interval Product Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1253264-bit integer IO format: %lld Java class name: Main Prev Submit Status Statistics Di

UVA 489-- Hangman Judge(暴力串处理)

 Hangman Judge  In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follo

[UVa Online Judge] Longest Common Subsequence

This is the classic LCS problem. Since it only requires you to print the maximum length, the code can be optimized to use only O(m) space (see here). My accepted code is as follows. 1 #include <iostream> 2 #include <string> 3 #include <vect

UVa 725 - Division(枚举简单题)

今日无事,写篇日记.这是我写的第一道uva题(uva是算法竞赛入门经典里使用例题的题目来源),正值暑假,前几天看了书上中文题目和解析,玩了几日san11,又重拾起acm,今天晚上写了一下还是花了点时间. 题目:给你一个数字n,用0~9,10个数字组成两个五位数,使得他们的商为n,按顺序输出所有结果. 1.这里的除法不是c中的除(/)而是整除,一开始以为是整除,但那样79546 / 01283 = 62是一组解,79546+1 / 01283 = 62也是一组解了. 2.复杂度:枚举分子然后分子乘

Online Judge for ACM-ICPC etc.

原文链接:http://blog.csdn.net/tigerisland45/article/details/52134189 Virtual Judge ACM-ICPC Live Archive - Home UVa Online Judge - Home Welcome To PKU JudgeOnline(POJ) Welcome to Hangzhou Dianzi University Online Judge(HDU) OpenJudge - 百练 - 首页(PKU) Codef

uva 10065 (凸包+求面积)

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=1006 Problem A Useless Tile Packers Input: standard input Output: standard output Yes, as you have apprehended the Useless Tile Pac

uva 11538 - Chess Queen(组合数)

UVa  Online Judge https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2533 [题意] 给定一个棋盘,在棋盘上放两个皇后(一白一黑),求使得两个皇后相互攻击(在一行.一列.对角线均可相互攻击),求方案数. 计数问题,分类: 1.在一行或一列:n*m(m-1),m*n*(n-1) 2.在对角线,假设n<m,则各对角线长度

UVA - 12001 UVa Panel Discussion

Description  UVa Panel Discussion  The UVa online judge team is arranging a panel discussion for the next ACM-ICPC World Finals event in Orlando, Florida. They want that three or four of the contestants take part in the panel and as they have about 3

UVA 796 Critical Links(无向图求桥)

题目来源: UVa Online Judgehttps://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 求一个连通图中必不可少的路径: #include<stdio.h> #include<algorithm> #include<vector> #include<string.h> #define