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

None
Graph Theory
2-SAT
Articulation/Bridge/Biconnected Component
Cycles/Topological Sorting/Strongly Connected Component
Shortest Path
Bellman
Ford
Dijkstra/Floyd Warshall
Euler Trail/Circuit
Heavy-Light
Decomposition
Minimum Spanning Tree
Stable Marriage Problem
Trees
Directed Minimum Spanning
Tree
Flow/Matching
Graph Matching
Bipartite
Matching
Hopcroft–Karp Bipartite
Matching
Weighted Bipartite
Matching/Hungarian Algorithm
Flow
Max Flow/Min Cut
Min Cost Max Flow
DFS-like
Backtracking with Pruning/Branch
and Bound
Basic Recursion
IDA* Search
Parsing/Grammar
Breadth First Search/Depth First Search
Advanced Search Techniques
Binary
Search/Bisection
Ternary Search
Geometry
Basic Geometry
Computational Geometry
Convex
Hull
Pick‘s Theorem
Game
Theory
Green Hackenbush/Colon Principle/Fusion
Principle
Nim
Sprague-Grundy Number
Matrix
Gaussian Elimination
Matrix
Exponentiation
Data Structures
Basic Data Structures
Binary Indexed
Tree
Binary Search Tree
Hashing
Orthogonal Range
Search
Range Minimum Query/Lowest Common
Ancestor
Segment Tree/Interval Tree
Trie Tree
Sorting
Disjoint Set
String
Aho Corasick
Knuth-Morris-Pratt
Suffix
Array/Suffix Tree
Math
Basic Math
Big Integer
Arithmetic
Number Theory
Chinese Remainder Theorem
Extended Euclid
Inclusion/Exclusion
Modular
Arithmetic
Combinatorics
Group Theory/Burnside‘s lemma
Counting
Probability/Expected
Value
Others
Tricky
Hardest
Unusual
Brute Force
Implementation
Constructive
Algorithms
Two Pointer
Bitmask
Beginner
Discrete Logarithm/Shank‘s Baby-step Giant-step Algorithm
Greedy
Divide and
Conquer
Dynamic Programming

Tag it!

[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
#include<iostream>
#include<cstdio>
#define maxn 1000005
struct  node
{
    int left,right;
    int num;
}tree[3*maxn];
int out=1;
void build(int left,int right,int i)
{
    tree[i].left=left;
    tree[i].right=right;
    tree[i].num=0;
    if(tree[i].left==tree[i].right)
         return ;
    int mid=(left+right)/2;
    build(left,mid,2*i);
    build(mid+1,right,2*i+1);
}
void insert(int id,int i,int j)
{
      if(tree[id].left==i&&tree[id].right==i)
               {

                   tree[id].num=j;
                }
      if(tree[id].left==tree[id].right)
                 return ;
    if(i>tree[id].right)
        return;
    if(i<tree[id].left)
            return;
     int mid=(tree[id].left+tree[id].right)/2;
     if(i<=mid)
         insert(id*2,i,j);
     else
         insert(id*2+1,i,j);
    tree[id].num=tree[2*id].num*tree[2*id+1].num;

}
void sum(int id,int i,int j)
{
    int mid=(tree[id].left+tree[id].right)/2;
    if(tree[id].left==i&&tree[id].right==j)
     {

         out*=tree[id].num;
             return ;
     }
     if(j<=mid)
       sum(id*2,i,j);
     else if(i>mid)
         sum(id*2+1,i,j);
     else
     {
        sum(id*2,i,mid);
        sum(id*2+1,mid+1,j);
     }
}
int main()
{
    int i,x,y,n,m,u,num;

   while(~ scanf("%d%d",&n,&m))
   {
       char str[1000006];
       build(1,n,1);
     for(i=1;i<=n;i++)
    {
        scanf("%d",&u);
        if(u>0)
           u=1;
         else if(u==0)
               u=0;
         else
           u=-1;
      //  printf("u=%d\n",u);
        insert(1,i,u);

    }

    char p;
    int r=0;
    for(i=1;i<=m;i++)
    {
     // printf("AAA\n");
        getchar();
       scanf("%c",&p);
        //printf("p=%c\n",p);
          if(p==‘C‘)
             {
                scanf("%d%d",&x,&y);
             if(y>0)
               y=1;
            else if(y==0)
               y=0;
            else
               y=-1;
           // printf("y=%d\n",y);
                 //printf("aa\n");
                 insert(1,x,y);
              }
          if(p==‘P‘)
            {
                scanf("%d%d",&x,&y);
                sum(1,x,y);
              //  printf("out=%d\n",out);
                 if(out>0)
                  {
                     str[r++]=‘+‘;
                    //  printf("+\n");
                  }
                 else if(out<0)
                     {
                         str[r++]=‘-‘;
                         // printf("-\n");
                     }
                  else
                  {
                     str[r++]=‘0‘;
                     // printf("0\n");
                  }
                        out=1;
            }
        }
        str[r] = ‘\0‘;
        printf("%s\n",str);
   }
     return 0;
}
时间: 2024-10-29 19:10:27

UVA - 12532 Interval Product的相关文章

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 Pr

UVa 11059 Maximum Product

题意:给出n个数组成的序列,求乘积最大的连续子序列 看的紫书,因为n最大为18,每个数最大为10,所以10^18用long long 能够存下, 直接枚举起点和终点找最大值就可以了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include&l

UVA 11059 Maximum Product(枚举start+end)

题目大意: 就是说给你n个数字,然后求出连续序列的最大和.如果ans为负数就输出0,要么就输出这个ans. 解题思路: 其实我们只需要枚举起点和终点,然后考虑所有的情况就可以了,有点类似于求解最大连续和的问题. 唯一的不同就是每次都要初始化t = 1, t*=a[j].注意long long.因为最多有18个数字,且每个数字的 最大值为10,那么他们的乘积是不会超过10^18的. 代码: # include<cstdio> # include<iostream> # include

UVa 11059 Maximum Product(简单枚举7.1)使用longlong,输出格式%lld

这题数据最大18位,应该用Long long 粗心在几个地方(函数返回值,中间比较值max pro)用了Int,提交了好几次 #include<stdio.h> long long func(int *a,int head,int rear){//一开始用了int long long pro=a[head];//注意这里初值不能赋值为1 2\n 0 0这个通不过 if(head==rear) return a[head]; else for(int i=head+1;i<=rear;i+

UVA OJ-11095 Maximum Product(暴力求解法)

Given a sequence of integers S = {S1, S2, ..., Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you should consider 0 as the value of the maximum pro

UVA 10106 Product (大数相乘)

Product The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer. The Output For each input pair of lines the output line should c

UVA 10106 Product 高精度运算

J - Product Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Product  The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The

uva 993 Product of digits (分解因子)

uva 993 Product of digits 题目大意:给定一个数N,要求出一个数,使得这个数的每一位相乘会等于N,且这个数的值最小. 解题思路:简单的分解因子.要注意的地方有两点:1) 因子从9开始向2遍历,可以保证位数最小: 2)当N等于1是最好特殊处理. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std;

Product UVA 10106

题目: Product The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer. The Output For each input pair of lines the output line shou